Class: Micronaut::Behaviour

Inherits:
Object show all
Includes:
Matchers
Defined in:
lib/micronaut/behaviour.rb

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

#running_exampleObject

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, options = {}, &block) ⇒ Object



45
46
47
# File 'lib/micronaut/behaviour.rb', line 45

def self.after(type=:each, options={}, &block)
  afters[type] << [options, block]
end

.after_allsObject



41
42
43
# File 'lib/micronaut/behaviour.rb', line 41

def self.after_alls
  afters[:all]
end

.after_ancestorsObject



151
152
153
# File 'lib/micronaut/behaviour.rb', line 151

def self.after_ancestors
  @_after_ancestors ||= ancestors(true)
end

.after_eachsObject



37
38
39
# File 'lib/micronaut/behaviour.rb', line 37

def self.after_eachs
  afters[:each]
end

.aftersObject



33
34
35
# File 'lib/micronaut/behaviour.rb', line 33

def self.afters
  @_afters ||= { :all => [], :each => [] }
end

.alias_example_to(new_alias, extra_options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/micronaut/behaviour.rb', line 53

def self.alias_example_to(new_alias, extra_options={})
  new_alias = "                def self.\#{new_alias}(desc=nil, options={}, &block)\n                  updated_options = options.update(:caller => caller[0])\n                  updated_options.update(\#{extra_options.inspect})\n                  block = nil if updated_options[:pending] == true || updated_options[:disabled] == true\n                  examples << Micronaut::Example.new(self, desc, updated_options, block)\n                end\n              END_RUBY\n  module_eval(new_alias, __FILE__, __LINE__)\nend\n"

.ancestors(superclass_last = false) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/micronaut/behaviour.rb', line 135

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, options = {}, &block) ⇒ Object



29
30
31
# File 'lib/micronaut/behaviour.rb', line 29

def self.before(type=:each, options={}, &block)
  befores[type] << [options, block]
end

.before_allsObject



25
26
27
# File 'lib/micronaut/behaviour.rb', line 25

def self.before_alls
  befores[:all]
end

.before_ancestorsObject



147
148
149
# File 'lib/micronaut/behaviour.rb', line 147

def self.before_ancestors
  @_before_ancestors ||= ancestors 
end

.before_eachsObject



21
22
23
# File 'lib/micronaut/behaviour.rb', line 21

def self.before_eachs
  befores[:each]
end

.beforesObject



17
18
19
# File 'lib/micronaut/behaviour.rb', line 17

def self.befores
  @_befores ||= { :all => [], :each => [] }
end

.describe(*args, &behaviour_block) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/micronaut/behaviour.rb', line 123

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

.describesObject



111
112
113
# File 'lib/micronaut/behaviour.rb', line 111

def self.describes
  [:behaviour][:describes]
end

.descriptionObject



115
116
117
# File 'lib/micronaut/behaviour.rb', line 115

def self.description
  [:behaviour][:description]
end

.eval_after_alls(example) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/micronaut/behaviour.rb', line 171

def self.eval_after_alls(example)
  Micronaut.configuration.find_before_or_after(:after, :all, self).each { |blk| example.instance_eval(&blk) }
        
  after_ancestors.each do |ancestor| 
    ancestor.after_alls.each { |opts, blk| example.instance_eval(&blk) }
  end
end

.eval_after_eachs(example) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/micronaut/behaviour.rb', line 179

def self.eval_after_eachs(example)
  Micronaut.configuration.find_before_or_after(:after, :each, self).each { |blk| example.instance_eval(&blk) }
  
  after_ancestors.each do |ancestor|
    ancestor.after_eachs.each { |opts, blk| example.instance_eval(&blk) }
  end
rescue Exception => e # TODO not sure what to do about this case?
  nil
end

.eval_before_alls(example) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/micronaut/behaviour.rb', line 155

def self.eval_before_alls(example)
  Micronaut.configuration.find_before_or_after(:before, :all, self).each { |blk| example.instance_eval(&blk) }
  
  before_ancestors.each do |ancestor| 
    ancestor.before_alls.each { |opts, blk| example.instance_eval(&blk) }
  end
end

.eval_before_eachs(example) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/micronaut/behaviour.rb', line 163

def self.eval_before_eachs(example)
  Micronaut.configuration.find_before_or_after(:before, :each, self).each { |blk| example.instance_eval(&blk) }
  
  before_ancestors.each do |ancestor| 
    ancestor.before_eachs.each { |opts, blk| example.instance_eval(&blk) }
  end
end

.example(desc = nil, options = {}, &block) ⇒ Object



49
50
51
# File 'lib/micronaut/behaviour.rb', line 49

def self.example(desc=nil, options={}, &block)
  examples << Micronaut::Example.new(self, desc, options.update(:caller => caller[0]), block)
end

.examplesObject



70
71
72
# File 'lib/micronaut/behaviour.rb', line 70

def self.examples
  @_examples ||= []
end

.examples_to_runObject



74
75
76
# File 'lib/micronaut/behaviour.rb', line 74

def self.examples_to_run
  @_examples_to_run ||= []
end

.extended_modulesObject

:nodoc:



12
13
14
15
# File 'lib/micronaut/behaviour.rb', line 12

def self.extended_modules #:nodoc:
  ancestors = class << self; ancestors end
  ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
end

.file_pathObject



119
120
121
# File 'lib/micronaut/behaviour.rb', line 119

def self.file_path
  [:behaviour][:file_path]
end

.inherited(klass) ⇒ Object



7
8
9
10
# File 'lib/micronaut/behaviour.rb', line 7

def self.inherited(klass)
  super
  Micronaut.world.behaviours << klass
end

.metadataObject



103
104
105
# File 'lib/micronaut/behaviour.rb', line 103

def self.
  
end

.nameObject



107
108
109
# File 'lib/micronaut/behaviour.rb', line 107

def self.name
  [:behaviour][:name]
end

.run(reporter) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/micronaut/behaviour.rb', line 189

def self.run(reporter)
  return true if examples.empty?

  reporter.add_behaviour(self)
  
  group = new
  eval_before_alls(group)
  success = true

  examples_to_run.each do |ex|
    group.running_example = ex
    reporter.example_started(ex)

    execution_error = nil
    begin
      group._setup_mocks
      eval_before_eachs(group)

      if ex.example_block
        group.instance_eval(&ex.example_block)
        group._verify_mocks
        reporter.example_passed(ex)
      else
        reporter.example_pending(ex, 'Not yet implemented')
      end
    rescue Exception => e
      reporter.example_failed(ex, e)
      execution_error ||= e
    ensure
      eval_after_eachs(group)
      group._teardown_mocks
    end

    success &= execution_error.nil?
  end
  eval_after_alls(group)
  group.running_example = nil
  success
end

.set_it_up(*args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/micronaut/behaviour.rb', line 78

def self.set_it_up(*args)
   = { :behaviour => {} }
   = args.last.is_a?(Hash) ? args.pop : {}
  .delete(:behaviour) # Remove it when present to prevent it clobbering the one we setup
  
  [:behaviour][:describes] = args.shift unless args.first.is_a?(String)
  [:behaviour][:describes] ||= self.superclass. && self.superclass.[:behaviour][:describes]
  [:behaviour][:description] = args.shift || ''
  [:behaviour][:name] = "#{describes} #{description}".strip
  [:behaviour][:block] = .delete(:behaviour_block)
  [:behaviour][:caller] = eval("caller(0)[0]", [:behaviour][:block].binding)
  [:behaviour][:file_path] = [:behaviour][:caller].split(":")[0].strip
  [:behaviour][:line_number] = [:behaviour][:caller].split(":")[1].to_i
  
  .update()
  
  Micronaut.configuration.find_modules(self).each do |include_or_extend, mod, opts|                                                                                                                                                                               
    if include_or_extend == :extend                                                                                                                                                                                                                               
      send(:extend, mod) unless extended_modules.include?(mod)                                                                                                                                                                                                    
    else                                                                                                                                                                                                                                                          
      send(:include, mod) unless included_modules.include?(mod)                                                                                                                                                                                                   
    end                                                                                                                                                                                                                                                           
  end
end

.subclass(base_name, &body) ⇒ Object

:nodoc:



229
230
231
232
233
234
235
236
237
# File 'lib/micronaut/behaviour.rb', line 229

def self.subclass(base_name, &body) # :nodoc:
  @_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

.to_sObject



239
240
241
# File 'lib/micronaut/behaviour.rb', line 239

def self.to_s
  self == Micronaut::Behaviour ? 'Micronaut::Behaviour' : [:behaviour][:name]
end