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

#reporterObject

Returns the value of attribute reporter.



5
6
7
# File 'lib/micronaut/behaviour.rb', line 5

def reporter
  @reporter
end

#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



153
154
155
# File 'lib/micronaut/behaviour.rb', line 153

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



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

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



149
150
151
# File 'lib/micronaut/behaviour.rb', line 149

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)


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

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



113
114
115
# File 'lib/micronaut/behaviour.rb', line 113

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

.descriptionObject



117
118
119
# File 'lib/micronaut/behaviour.rb', line 117

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

.eval_after_alls(example) ⇒ Object



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

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



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

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



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

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

.eval_before_eachs(example) ⇒ Object



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

def self.eval_before_eachs(example)
  before_ancestors.each do |ancestor| 
    ancestor.before_eachs.each { |opts, blk| example.instance_eval(&blk) }
  end
  
  Micronaut.configuration.find_before_or_after(:before, :each, self).each { |blk| example.instance_eval(&blk) }
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



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

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



105
106
107
# File 'lib/micronaut/behaviour.rb', line 105

def self.
   ||= { :behaviour => {} }
end

.nameObject



109
110
111
# File 'lib/micronaut/behaviour.rb', line 109

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

.run(reporter) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/micronaut/behaviour.rb', line 191

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

  behaviour_instance = new
  reporter.add_behaviour(self)
  eval_before_alls(behaviour_instance)
  success = examples_to_run.all? { |ex| ex.run(behaviour_instance, reporter) }
  eval_after_alls(behaviour_instance)
  
  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
102
103
# 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] = caller(1)
  [:behaviour][:file_path] = [:behaviour][:caller][4].split(":")[0].strip
  [:behaviour][:line_number] = [:behaviour][:caller][4].split(":")[1].to_i
  
  .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

:nodoc:



203
204
205
206
207
208
209
210
211
# File 'lib/micronaut/behaviour.rb', line 203

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



213
214
215
# File 'lib/micronaut/behaviour.rb', line 213

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