Class: Thoughtbot::Shoulda::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/context.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, &blk) ⇒ Context

Returns a new instance of Context.



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/shoulda/context.rb', line 186

def initialize(name, parent, &blk)
  Shoulda.add_context(self)
  self.name               = name
  self.parent             = parent
  self.setup_blocks       = []
  self.teardown_blocks    = []
  self.shoulds            = []
  self.should_eventuallys = []
  self.subcontexts        = []

  merge_block(&blk)
  Shoulda.remove_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



300
301
302
# File 'lib/shoulda/context.rb', line 300

def method_missing(method, *args, &blk)
  test_unit_class.send(method, *args, &blk)
end

Instance Attribute Details

#nameObject

my name



178
179
180
# File 'lib/shoulda/context.rb', line 178

def name
  @name
end

#parentObject

may be another context, or the original test::unit class.



179
180
181
# File 'lib/shoulda/context.rb', line 179

def parent
  @parent
end

#setup_blocksObject

blocks given via setup methods



181
182
183
# File 'lib/shoulda/context.rb', line 181

def setup_blocks
  @setup_blocks
end

#should_eventuallysObject

array of hashes representing the should eventually statements



184
185
186
# File 'lib/shoulda/context.rb', line 184

def should_eventuallys
  @should_eventuallys
end

#shouldsObject

array of hashes representing the should statements



183
184
185
# File 'lib/shoulda/context.rb', line 183

def shoulds
  @shoulds
end

#subcontextsObject

array of contexts nested under myself



180
181
182
# File 'lib/shoulda/context.rb', line 180

def subcontexts
  @subcontexts
end

#teardown_blocksObject

blocks given via teardown methods



182
183
184
# File 'lib/shoulda/context.rb', line 182

def teardown_blocks
  @teardown_blocks
end

Instance Method Details

#am_subcontext?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/shoulda/context.rb', line 233

def am_subcontext?
  parent.is_a?(self.class) # my parent is the same class as myself.
end

#buildObject



290
291
292
293
294
295
296
297
298
# File 'lib/shoulda/context.rb', line 290

def build
  shoulds.each do |should|
    create_test_from_should_hash(should)
  end

  subcontexts.each { |context| context.build }

  print_should_eventuallys
end

#context(name, &blk) ⇒ Object



204
205
206
# File 'lib/shoulda/context.rb', line 204

def context(name, &blk)
  self.subcontexts << Context.new(name, self, &blk)
end

#create_test_from_should_hash(should) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/shoulda/context.rb', line 241

def create_test_from_should_hash(should)
  test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym

  if test_unit_class.instance_methods.include?(test_name.to_s)
    warn "  * WARNING: '#{test_name}' is already defined"
  end

  context = self
  test_unit_class.send(:define_method, test_name) do
    begin
      context.run_parent_setup_blocks(self)
      should[:before].bind(self).call if should[:before]
      context.run_current_setup_blocks(self)
      should[:block].bind(self).call
    ensure
      context.run_all_teardown_blocks(self)
    end
  end
end

#full_nameObject



228
229
230
231
# File 'lib/shoulda/context.rb', line 228

def full_name
  parent_name = parent.full_name if am_subcontext?
  return [parent_name, name].join(" ").strip
end

#merge_block(&blk) ⇒ Object



200
201
202
# File 'lib/shoulda/context.rb', line 200

def merge_block(&blk)
  blk.bind(self).call
end


283
284
285
286
287
288
# File 'lib/shoulda/context.rb', line 283

def print_should_eventuallys
  should_eventuallys.each do |should|
    test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
    puts "  * DEFERRED: " + test_name
  end
end

#run_all_setup_blocks(binding) ⇒ Object



261
262
263
264
# File 'lib/shoulda/context.rb', line 261

def run_all_setup_blocks(binding)
  run_parent_setup_blocks(binding)
  run_current_setup_blocks(binding)
end

#run_all_teardown_blocks(binding) ⇒ Object



276
277
278
279
280
281
# File 'lib/shoulda/context.rb', line 276

def run_all_teardown_blocks(binding)
  teardown_blocks.reverse.each do |teardown_block|
    teardown_block.bind(binding).call
  end
  self.parent.run_all_teardown_blocks(binding) if am_subcontext?
end

#run_current_setup_blocks(binding) ⇒ Object



270
271
272
273
274
# File 'lib/shoulda/context.rb', line 270

def run_current_setup_blocks(binding)
  setup_blocks.each do |setup_block|
    setup_block.bind(binding).call
  end
end

#run_parent_setup_blocks(binding) ⇒ Object



266
267
268
# File 'lib/shoulda/context.rb', line 266

def run_parent_setup_blocks(binding)
  self.parent.run_all_setup_blocks(binding) if am_subcontext?
end

#setup(&blk) ⇒ Object



208
209
210
# File 'lib/shoulda/context.rb', line 208

def setup(&blk)
  self.setup_blocks << blk
end

#should(name, options = {}, &blk) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/shoulda/context.rb', line 216

def should(name, options = {}, &blk)
  if block_given?
    self.shoulds << { :name => name, :before => options[:before], :block => blk }
  else
   self.should_eventuallys << { :name => name }
 end
end

#should_eventually(name, &blk) ⇒ Object



224
225
226
# File 'lib/shoulda/context.rb', line 224

def should_eventually(name, &blk)
  self.should_eventuallys << { :name => name, :block => blk }
end

#teardown(&blk) ⇒ Object



212
213
214
# File 'lib/shoulda/context.rb', line 212

def teardown(&blk)
  self.teardown_blocks << blk
end

#test_unit_classObject



237
238
239
# File 'lib/shoulda/context.rb', line 237

def test_unit_class
  am_subcontext? ? parent.test_unit_class : parent
end