Class: Spec::DSL::Behaviour

Inherits:
Object show all
Extended by:
BehaviourCallbacks
Defined in:
lib/spec/dsl/behaviour.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BehaviourCallbacks

add, after_all_parts, after_each_parts, append_after, append_before, before_all_parts, before_each_parts, clear_before_and_after!, prepend_after, prepend_before, remove_after, scope_and_options, setup, teardown

Constructor Details

#initialize(*args, &behaviour_block) ⇒ Behaviour

Returns a new instance of Behaviour.



27
28
29
30
31
32
# File 'lib/spec/dsl/behaviour.rb', line 27

def initialize(*args, &behaviour_block)
  init_description(*args)
  init_eval_module
  before_eval
  eval_behaviour(&behaviour_block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (protected)

Messages that this class does not understand are passed directly to the @eval_module.



143
144
145
# File 'lib/spec/dsl/behaviour.rb', line 143

def method_missing(sym, *args, &block)
  @eval_module.send(sym, *args, &block)
end

Class Method Details

.add_shared_behaviour(behaviour) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/spec/dsl/behaviour.rb', line 8

def add_shared_behaviour(behaviour)
  return if behaviour.equal?(found_behaviour = find_shared_behaviour(behaviour.description))
  return if found_behaviour and File.expand_path(behaviour.description[:spec_path]) == File.expand_path(found_behaviour.description[:spec_path])
  raise ArgumentError.new("Shared Behaviour '#{behaviour.description}' already exists") if found_behaviour
  shared_behaviours << behaviour
end

.find_shared_behaviour(behaviour_description) ⇒ Object



15
16
17
# File 'lib/spec/dsl/behaviour.rb', line 15

def find_shared_behaviour(behaviour_description)
  shared_behaviours.find { |b| b.description == behaviour_description }
end

.shared_behavioursObject



19
20
21
22
23
24
# File 'lib/spec/dsl/behaviour.rb', line 19

def shared_behaviours
  # TODO - this needs to be global, or at least accessible from
  # from subclasses of Behaviour in a centralized place. I'm not loving
  # this as a solution, but it works for now.
  $shared_behaviours ||= []
end

Instance Method Details

#behaviour_typeObject

:nodoc:



125
126
127
# File 'lib/spec/dsl/behaviour.rb', line 125

def behaviour_type #:nodoc:
  @description[:behaviour_type]
end

#include(*args) ⇒ Object

Includes modules in the Behaviour (the describe block).



121
122
123
# File 'lib/spec/dsl/behaviour.rb', line 121

def include(*args)
  @eval_module.include(*args)
end

#matches?(specified_examples) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
# File 'lib/spec/dsl/behaviour.rb', line 93

def matches?(specified_examples)
  matcher ||= ExampleMatcher.new(description)

  examples.each do |example|
    return true if example.matches?(matcher, specified_examples)
  end
  return false
end

#methodsObject



114
115
116
117
118
# File 'lib/spec/dsl/behaviour.rb', line 114

def methods
  my_methods = super
  my_methods |= @eval_module.methods
  my_methods
end

#number_of_examplesObject



89
90
91
# File 'lib/spec/dsl/behaviour.rb', line 89

def number_of_examples
  examples.length
end

#retain_examples_matching!(specified_examples) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/spec/dsl/behaviour.rb', line 106

def retain_examples_matching!(specified_examples)
  return if specified_examples.index(description)
  matcher = ExampleMatcher.new(description)
  examples.reject! do |example|
    !example.matches?(matcher, specified_examples)
  end
end

#run(reporter, dry_run = false, reverse = false, timeout = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spec/dsl/behaviour.rb', line 64

def run(reporter, dry_run=false, reverse=false, timeout=nil)
  raise "shared behaviours should never run" if shared?
  # TODO - change add_behaviour to add_description ??????
  reporter.add_behaviour(@description)
  prepare_execution_context_class
  before_all_errors = run_before_all(reporter, dry_run)

  exs = reverse ? examples.reverse : examples
  example_execution_context = nil
   
  if before_all_errors.empty?
    exs.each do |example|
      example_execution_context = execution_context(example)
      example_execution_context.copy_instance_variables_from(@before_and_after_all_context_instance) unless before_all_proc(behaviour_type).nil?
      
      befores = before_each_proc(behaviour_type) {|e| raise e}
      afters = after_each_proc(behaviour_type)
      example.run(reporter, befores, afters, dry_run, example_execution_context, timeout)
    end
  end
  
  @before_and_after_all_context_instance.copy_instance_variables_from(example_execution_context) unless after_all_proc(behaviour_type).nil?
  run_after_all(reporter, dry_run)
end

#set_sequence_numbers(number, reverse) ⇒ Object

Sets the #number on each Example and returns the next number



130
131
132
133
134
135
136
137
# File 'lib/spec/dsl/behaviour.rb', line 130

def set_sequence_numbers(number, reverse) #:nodoc:
  exs = reverse ? examples.reverse : examples
  exs.each do |example|
    example.number = number
    number += 1
  end
  number
end

#shared?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/spec/dsl/behaviour.rb', line 102

def shared?
  @description[:shared]
end