Class: Bacon::Context

Inherits:
Object show all
Includes:
ContextAssertions
Defined in:
lib/bacon.rb,
lib/bacon/ext/mocha.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContextAssertions

#create_context, #execute_spec, #run, #run_requirement

Constructor Details

#initialize(name, toplevel = false, &block) ⇒ Context

Returns a new instance of Context.



175
176
177
178
179
180
# File 'lib/bacon.rb', line 175

def initialize(name, toplevel = false, &block)
  @name = name
  @before, @after = [], []
  @block = block
  @toplevel = toplevel
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



171
172
173
# File 'lib/bacon.rb', line 171

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



171
172
173
# File 'lib/bacon.rb', line 171

def name
  @name
end

#toplevelObject (readonly)

Returns the value of attribute toplevel.



171
172
173
# File 'lib/bacon.rb', line 171

def toplevel
  @toplevel
end

Instance Method Details

#after(&block) ⇒ Object



183
# File 'lib/bacon.rb', line 183

def after(&block);  @after << block; end

#before(&block) ⇒ Object



182
# File 'lib/bacon.rb', line 182

def before(&block); @before << block; end

#behaves_like(*names) ⇒ Object



185
186
187
# File 'lib/bacon.rb', line 185

def behaves_like(*names)
  names.each { |name| instance_eval(&Shared[name]) }
end

#change?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


230
# File 'lib/bacon.rb', line 230

def change?(*args, &block); block.change?(*args); end

#describe(*args, &block) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/bacon.rb', line 197

def describe(*args, &block)
  context = create_context(args.join(' '), &block)
  
  # define any user methods in the new context so
  # that helpers defined in main describe can be used in nested contexts
  (parent_context = self).methods(false).each {|e|
    class<<context; self end.send(:define_method, e) {|*args| parent_context.send(e, *args)}
  }
  @before.each { |b| context.before(&b) }
  @after.each { |b| context.after(&b) }
  context.run
end

#focus(spec_str) ⇒ Object



210
211
212
213
# File 'lib/bacon.rb', line 210

def focus(spec_str)
  raise "focused test forbidden" unless Bacon::allow_focused_run?
  Bacon::focus_name_regexp = %r{#{spec_str}}
end

#focus_context(context_str) ⇒ Object



215
216
217
218
# File 'lib/bacon.rb', line 215

def focus_context(context_str)
  raise "focused test forbidden" unless Bacon::allow_focused_run?
  Bacon::focus_context_regexp = %r{#{context_str}}
end

#freeze_time(*args, &block) ⇒ Object



23
24
25
# File 'lib/bacon/ext/mocha.rb', line 23

def freeze_time(*args, &block)
  Bacon.freeze_time(*args, &block)
end

#it(description, &block) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/bacon.rb', line 220

def it(description, &block)
  return unless name =~ Bacon::focus_context_regexp
  return  unless description =~ Bacon::focus_name_regexp
  block ||= lambda { should.flunk "not implemented" }
  Counter[:specifications] += 1
  run_requirement description, block
end

#raise?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


228
# File 'lib/bacon.rb', line 228

def raise?(*args, &block); block.raise?(*args); end

#should(label, *args, &block) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/bacon.rb', line 189

def should(label, *args, &block)
  if Counter[:depth]==0
    it("should #{label}",&block)
  else
    super(label, *args,&block)
  end
end

#throw?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


229
# File 'lib/bacon.rb', line 229

def throw?(*args, &block); block.throw?(*args); end