Class: Shoulda::FastContext

Inherits:
Context
  • Object
show all
Defined in:
lib/fast_context.rb

Instance Method Summary collapse

Instance Method Details

#buildObject



66
67
68
69
70
71
72
73
74
# File 'lib/fast_context.rb', line 66

def build
  create_test_from_should_hash
  subcontexts.each {|context| context.build }

  @fast_subcontexts ||= []
  @fast_subcontexts.each {|f| f.build }

  print_should_eventuallys
end

#create_test_from_should_hashObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fast_context.rb', line 36

def create_test_from_should_hash
  test_name = test_method_name

  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
    @shoulda_context = context
    @current_should = nil
    begin
      context.run_parent_setup_blocks(self)
      context.shoulds.each do |s| 
        @current_should = s
        s[:before].bind(self).call if s[:before] 
      end
      context.run_current_setup_blocks(self)

      context.shoulds.each {|should| should[:block].bind(self).call }
    rescue Test::Unit::AssertionFailedError => e
      error = Test::Unit::AssertionFailedError.new(["FAILED:", context.full_name, "should", "#{@current_should[:name]}:", e.message].flatten.join(' '))
      error.set_backtrace e.backtrace
      raise error
    ensure
      context.run_all_teardown_blocks(self)
    end
  end
end

#test_method_nameObject



29
30
31
32
33
34
# File 'lib/fast_context.rb', line 29

def test_method_name
  joined_should_name = shoulds.collect{ |should_hash| should_hash[:name] }.join(' and ')
  test_name = ["test", full_name, "should", joined_should_name].flatten.join('_')
  test_name = test_name.gsub(' ', '_').gsub(/[^a-zA-Z0-9_?!]/, '').gsub(/__+/, '_').to_sym
  return test_name
end