Class: Micronaut::Behaviour
Instance Attribute Summary collapse
Class Method Summary
collapse
Methods included from Matchers
#be, #be_close, #change, clear_generated_description, #eql, #equal, generated_description, #has, #have, #have_at_least, #have_at_most, #include, #match, #method_missing, #raise_error, #respond_to, #satisfy, #simple_matcher, #throw_symbol, #wrap_expectation
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Micronaut::Matchers
Instance Attribute Details
Returns the value of attribute reporter.
5
6
7
|
# File 'lib/micronaut/behaviour.rb', line 5
def reporter
@reporter
end
|
#running_example ⇒ Object
Returns the value of attribute running_example.
5
6
7
|
# File 'lib/micronaut/behaviour.rb', line 5
def running_example
@running_example
end
|
Class Method Details
.after(type = :each, &block) ⇒ Object
46
47
48
|
# File 'lib/micronaut/behaviour.rb', line 46
def self.after(type=:each, &block)
afters[type] << block
end
|
.after_alls ⇒ Object
42
43
44
|
# File 'lib/micronaut/behaviour.rb', line 42
def self.after_alls
afters[:all]
end
|
.after_ancestors ⇒ Object
155
156
157
|
# File 'lib/micronaut/behaviour.rb', line 155
def self.after_ancestors
@_after_ancestors ||= ancestors(true)
end
|
.after_eachs ⇒ Object
38
39
40
|
# File 'lib/micronaut/behaviour.rb', line 38
def self.after_eachs
afters[:each]
end
|
34
35
36
|
# File 'lib/micronaut/behaviour.rb', line 34
def self.afters
@_afters ||= { :all => [], :each => [] }
end
|
.alias_example_to(new_alias, extra_options = {}) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/micronaut/behaviour.rb', line 54
def self.alias_example_to(new_alias, ={})
new_alias = <<-END_RUBY
def self.#{new_alias}(desc=nil, options={}, &block)
updated_options = options.update(:caller => caller[0])
updated_options.update(#{.inspect})
block = nil if updated_options[:pending] == true || updated_options[:disabled] == true
examples << Micronaut::Example.new(self, desc, updated_options, block)
end
END_RUBY
module_eval(new_alias, __FILE__, __LINE__)
end
|
.ancestors(superclass_last = false) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/micronaut/behaviour.rb', line 139
def self.ancestors(superclass_last=false)
classes = []
current_class = self
while current_class < Micronaut::Behaviour
superclass_last ? classes << current_class : classes.unshift(current_class)
current_class = current_class.superclass
end
classes
end
|
.before(type = :each, &block) ⇒ Object
30
31
32
|
# File 'lib/micronaut/behaviour.rb', line 30
def self.before(type=:each, &block)
befores[type] << block
end
|
.before_all_ivars ⇒ Object
159
160
161
|
# File 'lib/micronaut/behaviour.rb', line 159
def self.before_all_ivars
@before_all_ivars ||= {}
end
|
.before_alls ⇒ Object
26
27
28
|
# File 'lib/micronaut/behaviour.rb', line 26
def self.before_alls
befores[:all]
end
|
.before_ancestors ⇒ Object
151
152
153
|
# File 'lib/micronaut/behaviour.rb', line 151
def self.before_ancestors
@_before_ancestors ||= ancestors
end
|
.before_eachs ⇒ Object
22
23
24
|
# File 'lib/micronaut/behaviour.rb', line 22
def self.before_eachs
befores[:each]
end
|
18
19
20
|
# File 'lib/micronaut/behaviour.rb', line 18
def self.befores
@_befores ||= { :all => [], :each => [] }
end
|
.describe(*args, &behaviour_block) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/micronaut/behaviour.rb', line 127
def self.describe(*args, &behaviour_block)
raise(ArgumentError, "No arguments given. You must a least supply a type or description") if args.empty?
raise(ArgumentError, "You must supply a block when calling describe") if behaviour_block.nil?
subclass('NestedLevel') do
args << {} unless args.last.is_a?(Hash)
args.last.update(:behaviour_block => behaviour_block)
set_it_up(*args)
module_eval(&behaviour_block)
end
end
|
.describes ⇒ Object
115
116
117
|
# File 'lib/micronaut/behaviour.rb', line 115
def self.describes
metadata[:behaviour][:describes]
end
|
.description ⇒ Object
119
120
121
|
# File 'lib/micronaut/behaviour.rb', line 119
def self.description
metadata[:behaviour][:description]
end
|
.eval_after_alls(running_behaviour) ⇒ Object
176
177
178
179
180
|
# File 'lib/micronaut/behaviour.rb', line 176
def self.eval_after_alls(running_behaviour)
after_alls.each { |blk| running_behaviour.instance_eval(&blk) }
Micronaut.configuration.find_before_or_after(:after, :all, self).each { |blk| running_behaviour.instance_eval(&blk) }
before_all_ivars.keys.each { |ivar| before_all_ivars[ivar] = running_behaviour.instance_variable_get(ivar) }
end
|
.eval_after_eachs(running_behaviour) ⇒ Object
182
183
184
185
|
# File 'lib/micronaut/behaviour.rb', line 182
def self.eval_after_eachs(running_behaviour)
after_ancestors.each { |ancestor| ancestor.after_eachs.each { |blk| running_behaviour.instance_eval(&blk) } }
Micronaut.configuration.find_before_or_after(:after, :each, self).each { |blk| running_behaviour.instance_eval(&blk) }
end
|
.eval_before_alls(running_behaviour) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/micronaut/behaviour.rb', line 163
def self.eval_before_alls(running_behaviour)
superclass.before_all_ivars.each { |ivar, val| running_behaviour.instance_variable_set(ivar, val) }
Micronaut.configuration.find_before_or_after(:before, :all, self).each { |blk| running_behaviour.instance_eval(&blk) }
before_alls.each { |blk| running_behaviour.instance_eval(&blk) }
running_behaviour.instance_variables.each { |ivar| before_all_ivars[ivar] = running_behaviour.instance_variable_get(ivar) }
end
|
.eval_before_eachs(running_behaviour) ⇒ Object
171
172
173
174
|
# File 'lib/micronaut/behaviour.rb', line 171
def self.eval_before_eachs(running_behaviour)
Micronaut.configuration.find_before_or_after(:before, :each, self).each { |blk| running_behaviour.instance_eval(&blk) }
before_ancestors.each { |ancestor| ancestor.before_eachs.each { |blk| running_behaviour.instance_eval(&blk) } }
end
|
.example(desc = nil, options = {}, &block) ⇒ Object
50
51
52
|
# File 'lib/micronaut/behaviour.rb', line 50
def self.example(desc=nil, options={}, &block)
examples << Micronaut::Example.new(self, desc, options.update(:caller => caller[0]), block)
end
|
71
72
73
|
# File 'lib/micronaut/behaviour.rb', line 71
def self.examples
@_examples ||= []
end
|
.examples_to_run ⇒ Object
75
76
77
|
# File 'lib/micronaut/behaviour.rb', line 75
def self.examples_to_run
@_examples_to_run ||= []
end
|
.extended_modules ⇒ Object
13
14
15
16
|
# File 'lib/micronaut/behaviour.rb', line 13
def self.extended_modules ancestors = class << self; ancestors end
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
end
|
.file_path ⇒ Object
123
124
125
|
# File 'lib/micronaut/behaviour.rb', line 123
def self.file_path
metadata[:behaviour][:file_path]
end
|
107
108
109
|
# File 'lib/micronaut/behaviour.rb', line 107
def self.metadata
@metadata ||= { :behaviour => {} }
end
|
.name(friendly = true) ⇒ Object
111
112
113
|
# File 'lib/micronaut/behaviour.rb', line 111
def self.name(friendly=true)
friendly ? metadata[:behaviour][:name] : super()
end
|
.run(reporter) ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/micronaut/behaviour.rb', line 187
def self.run(reporter)
behaviour_instance = new
reporter.add_behaviour(self)
eval_before_alls(behaviour_instance)
success = run_examples(behaviour_instance, reporter)
eval_after_alls(behaviour_instance)
success
end
|
.run_examples(behaviour_instance, reporter) ⇒ Object
Runs all examples, returning true only if all of them pass
198
199
200
|
# File 'lib/micronaut/behaviour.rb', line 198
def self.run_examples(behaviour_instance, reporter)
examples_to_run.map { |ex| ex.run(behaviour_instance) }.all?
end
|
.set_it_up(*args) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/micronaut/behaviour.rb', line 79
def self.set_it_up(*args)
@metadata = { }
= args.last.is_a?(Hash) ? args.pop : {}
.delete(:behaviour) @metadata.update(self.superclass.metadata)
@metadata[:behaviour] = {}
@metadata[:behaviour][:describes] = args.shift unless args.first.is_a?(String)
@metadata[:behaviour][:describes] ||= self.superclass.metadata && self.superclass.metadata[:behaviour][:describes]
@metadata[:behaviour][:description] = args.shift || ''
@metadata[:behaviour][:name] = "#{describes} #{description}".strip
@metadata[:behaviour][:block] = .delete(:behaviour_block)
@metadata[:behaviour][:caller] = .delete(:caller) || caller(1)
@metadata[:behaviour][:file_path] = .delete(:file_path) || @metadata[:behaviour][:caller][4].split(":")[0].strip
@metadata[:behaviour][:line_number] = .delete(:line_number) || @metadata[:behaviour][:caller][4].split(":")[1].to_i
@metadata.update()
Micronaut.configuration.find_modules(self).each do |include_or_extend, mod, opts|
if include_or_extend == :extend
Micronaut.configuration.trace { "Extending module #{mod} on #{self}" }
send(:extend, mod) unless extended_modules.include?(mod)
else
Micronaut.configuration.trace { "Including module #{mod} on #{self}" }
send(:include, mod) unless included_modules.include?(mod)
end
end
end
|
.subclass(base_name, &body) ⇒ Object
202
203
204
205
206
207
208
209
210
|
# File 'lib/micronaut/behaviour.rb', line 202
def self.subclass(base_name, &body) @_sub_class_count ||= 0
@_sub_class_count += 1
klass = Class.new(self)
class_name = "#{base_name}_#{@_sub_class_count}"
const_set(class_name, klass)
klass.instance_eval(&body)
klass
end
|
212
213
214
|
# File 'lib/micronaut/behaviour.rb', line 212
def self.to_s
self == Micronaut::Behaviour ? 'Micronaut::Behaviour' : name
end
|