| Class | Spec::DSL::Behaviour |
| In: |
lib/spec/dsl/behaviour.rb
|
| Parent: | Object |
# File lib/spec/dsl/behaviour.rb, line 8
8: def add_shared_behaviour(behaviour)
9: return if behaviour.equal?(found_behaviour = find_shared_behaviour(behaviour.description))
10: return if found_behaviour and File.expand_path(behaviour.description[:spec_path]) == File.expand_path(found_behaviour.description[:spec_path])
11: raise ArgumentError.new("Shared Behaviour '#{behaviour.description}' already exists") if found_behaviour
12: shared_behaviours << behaviour
13: end
# File lib/spec/dsl/behaviour.rb, line 15
15: def find_shared_behaviour(behaviour_description)
16: shared_behaviours.find { |b| b.description == behaviour_description }
17: end
# File lib/spec/dsl/behaviour.rb, line 27
27: def initialize(*args, &behaviour_block)
28: init_description(*args)
29: init_eval_module
30: before_eval
31: eval_behaviour(&behaviour_block)
32: end
# File lib/spec/dsl/behaviour.rb, line 19
19: def shared_behaviours
20: # TODO - this needs to be global, or at least accessible from
21: # from subclasses of Behaviour in a centralized place. I'm not loving
22: # this as a solution, but it works for now.
23: $shared_behaviours ||= []
24: end
# File lib/spec/dsl/behaviour.rb, line 93
93: def matches?(specified_examples)
94: matcher ||= ExampleMatcher.new(description)
95:
96: examples.each do |example|
97: return true if example.matches?(matcher, specified_examples)
98: end
99: return false
100: end
# File lib/spec/dsl/behaviour.rb, line 114
114: def methods
115: my_methods = super
116: my_methods |= @eval_module.methods
117: my_methods
118: end
# File lib/spec/dsl/behaviour.rb, line 106
106: def retain_examples_matching!(specified_examples)
107: return if specified_examples.index(description)
108: matcher = ExampleMatcher.new(description)
109: examples.reject! do |example|
110: !example.matches?(matcher, specified_examples)
111: end
112: end
# File lib/spec/dsl/behaviour.rb, line 64
64: def run(reporter, dry_run=false, reverse=false, timeout=nil)
65: raise "shared behaviours should never run" if shared?
66: # TODO - change add_behaviour to add_description ??????
67: reporter.add_behaviour(@description)
68: prepare_execution_context_class
69: before_all_errors = run_before_all(reporter, dry_run)
70:
71: exs = reverse ? examples.reverse : examples
72: example_execution_context = nil
73:
74: if before_all_errors.empty?
75: exs.each do |example|
76: example_execution_context = execution_context(example)
77: example_execution_context.copy_instance_variables_from(@before_and_after_all_context_instance) unless before_all_proc(behaviour_type).nil?
78:
79: befores = before_each_proc(behaviour_type) {|e| raise e}
80: afters = after_each_proc(behaviour_type)
81: example.run(reporter, befores, afters, dry_run, example_execution_context, timeout)
82: end
83: end
84:
85: @before_and_after_all_context_instance.copy_instance_variables_from(example_execution_context) unless after_all_proc(behaviour_type).nil?
86: run_after_all(reporter, dry_run)
87: end
# File lib/spec/dsl/behaviour.rb, line 214
214: def described_type
215: @description.described_type
216: end
# File lib/spec/dsl/behaviour.rb, line 167
167: def execution_context(example)
168: execution_context_class.new(example)
169: end
Messages that this class does not understand are passed directly to the @eval_module.
# File lib/spec/dsl/behaviour.rb, line 143
143: def method_missing(sym, *args, &block)
144: @eval_module.send(sym, *args, &block)
145: end
# File lib/spec/dsl/behaviour.rb, line 200
200: def plugin_mock_framework
201: case mock_framework = Spec::Runner.configuration.mock_framework
202: when Module
203: include mock_framework
204: else
205: require Spec::Runner.configuration.mock_framework
206: include Spec::Plugins::MockFramework
207: end
208: end
# File lib/spec/dsl/behaviour.rb, line 147
147: def prepare_execution_context_class
148: plugin_mock_framework
149: weave_in_included_modules
150: define_predicate_matchers #this is in behaviour_eval
151: execution_context_class
152: end
# File lib/spec/dsl/behaviour.rb, line 188
188: def run_after_all(reporter, dry_run)
189: unless dry_run
190: begin
191: @before_and_after_all_context_instance ||= execution_context(nil)
192: @before_and_after_all_context_instance.instance_eval(&after_all_proc(behaviour_type))
193: rescue Exception => e
194: location = "after(:all)"
195: reporter.example_finished(Example.new(location), e, location) if reporter
196: end
197: end
198: end
# File lib/spec/dsl/behaviour.rb, line 171
171: def run_before_all(reporter, dry_run)
172: errors = []
173: unless dry_run
174: begin
175: @before_and_after_all_context_instance = execution_context(nil)
176: @before_and_after_all_context_instance.instance_eval(&before_all_proc(behaviour_type))
177: rescue Exception => e
178: errors << e
179: location = "before(:all)"
180: # The easiest is to report this as an example failure. We don't have an Example
181: # at this point, so we'll just create a placeholder.
182: reporter.example_finished(Example.new(location), e, location) if reporter
183: end
184: end
185: errors
186: end
# File lib/spec/dsl/behaviour.rb, line 154
154: def weave_in_included_modules
155: mods = [@eval_module]
156: mods << included_modules.dup
157: mods << Spec::Runner.configuration.modules_for(behaviour_type)
158: execution_context_class.class_eval do
159: # WARNING - the following can be executed in the context of any
160: # class, and should never pass more than one module to include
161: # even though we redefine include in this class. This is NOT
162: # tested anywhere, hence this comment.
163: mods.flatten.each {|mod| include mod}
164: end
165: end