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 = "                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



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_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



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

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

.eval_after_eachs(running_behaviour) ⇒ Object



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

def self.eval_after_eachs(running_behaviour)
  after_ancestors.each do |ancestor|
    ancestor.after_eachs.each { |blk| running_behaviour.instance_eval(&blk) }
  end
  
  Micronaut.configuration.find_before_or_after(:after, :each, self).each { |blk| running_behaviour.instance_eval(&blk) }
end

.eval_before_alls(running_behaviour) ⇒ Object



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

def self.eval_before_alls(running_behaviour)
  Micronaut.configuration.find_before_or_after(:before, :all, self).each { |blk| running_behaviour.instance_eval(&blk) }

  before_ancestors.each do |ancestor| 
    ancestor.before_alls.each { |blk| running_behaviour.instance_eval(&blk) }
  end
end

.eval_before_eachs(running_behaviour) ⇒ Object



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

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 do |ancestor| 
    ancestor.before_eachs.each { |blk| running_behaviour.instance_eval(&blk) }
  end
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.
   ||= { :behaviour => {} }
end

.nameObject



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

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 = 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



204
205
206
# File 'lib/micronaut/behaviour.rb', line 204

def self.run_examples(behaviour_instance, reporter)
  examples_to_run.map { |ex| ex.run(behaviour_instance, reporter) }.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)
   = { }
   = args.last.is_a?(Hash) ? args.pop : {}
  .delete(:behaviour) # Remove it when present to prevent it clobbering the one we setup
  .update(self.superclass.) 
  [:behaviour] = {}
  [: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:



208
209
210
211
212
213
214
215
216
# File 'lib/micronaut/behaviour.rb', line 208

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



218
219
220
# File 'lib/micronaut/behaviour.rb', line 218

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