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, &block) ⇒ Object



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

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

.after_allsObject



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

def self.after_alls
  afters[:all]
end

.after_ancestorsObject



155
156
157
# File 'lib/micronaut/behaviour.rb', line 155

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

.after_eachsObject



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

def self.after_eachs
  afters[:each]
end

.aftersObject



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, extra_options={})
  new_alias = <<-END_RUBY
                def self.#{new_alias}(desc=nil, options={}, &block)
                  updated_options = options.update(:caller => caller[0])
                  updated_options.update(#{extra_options.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_ivarsObject



159
160
161
# File 'lib/micronaut/behaviour.rb', line 159

def self.before_all_ivars
  @before_all_ivars ||= {}
end

.before_allsObject



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

def self.before_alls
  befores[:all]
end

.before_ancestorsObject



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

def self.before_ancestors
  @_before_ancestors ||= ancestors 
end

.before_eachsObject



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

def self.before_eachs
  befores[:each]
end

.beforesObject



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

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

.describe(*args, &behaviour_block) ⇒ Object

Raises:

  • (ArgumentError)


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

.describesObject



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

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

.descriptionObject



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

def self.description
  [: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

.examplesObject



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

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

.examples_to_runObject



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

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

.extended_modulesObject

:nodoc:



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

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

.file_pathObject



123
124
125
# File 'lib/micronaut/behaviour.rb', line 123

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

.inherited(klass) ⇒ Object



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

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

.metadataObject



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

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

.name(friendly = true) ⇒ Object



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

def self.name(friendly=true)
  friendly ? [: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) # Remove it when present to prevent it clobbering the one we setup
  @metadata.update(self.superclass.) 
  @metadata[:behaviour] = {}
  @metadata[:behaviour][:describes] = args.shift unless args.first.is_a?(String)
  @metadata[:behaviour][:describes] ||= self.superclass. && self.superclass.[: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

:nodoc:



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

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



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

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