Class: Section9::UnitTest::TestCase

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/section9/unittest.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.describe(obj, &block) ⇒ Object

‘describe’ and ‘it’



28
29
30
31
32
33
# File 'lib/section9/unittest.rb', line 28

def self.describe(obj, &block)
  (@__describes ||= []) << obj
  yield
ensure
  @__describes.pop
end

.it(desc, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/section9/unittest.rb', line 35

def self.it(desc, &block)
  @__count ||= 0
  @__count += 1
  arr = @__describes
  desc = "[#{arr.join(' > ')}] #{desc}" if arr && ! arr.empty?
  method_name = "test_%03d: %s" % [@__count, desc]
  define_method(method_name, block)
end

Instance Method Details

#__clear_registeredObject

:nodoc:



62
63
64
# File 'lib/section9/unittest.rb', line 62

def __clear_registered                  #:nodoc:
  @__not_yet.clear if @__not_yet
end

#__register(vo) ⇒ Object

:nodoc:



54
55
56
# File 'lib/section9/unittest.rb', line 54

def __register(vo)                      #:nodoc:
  (@__not_yet ||= {})[vo.__id__] = vo
end

#__unregister(vo) ⇒ Object

:nodoc:



58
59
60
# File 'lib/section9/unittest.rb', line 58

def __unregister(vo)                    #:nodoc:
  (@__not_yet ||= {}).delete(vo.__id__)
end

#assert_block(*msgs) ⇒ Object

assertions



84
85
86
# File 'lib/section9/unittest.rb', line 84

def assert_block(*msgs)        # re-define for Ruby <= 1.9.1
  assert yield, *msgs
end

#assert_not_in_delta(expected, actual, delta, msg = "") ⇒ Object



100
101
102
103
# File 'lib/section9/unittest.rb', line 100

def assert_not_in_delta(expected, actual, delta, msg="")
  errmsg = "<#{expected.inspect}> and\n<#{actual.inspect}> expected to be without\n<0.0001> of each other."
  assert_block(errmsg) { (expected - actual).abs > delta }
end

#assert_not_instance_of(klass, obj, msg = "") ⇒ Object



94
95
96
97
98
# File 'lib/section9/unittest.rb', line 94

def assert_not_instance_of(klass, obj, msg="")
  #errmsg = build_message(msg, "<?>\nexpected not to be instance_of\\?\n<?> but was.", obj, klass)
  errmsg = "<#{obj.inspect}>\nexpected not to be instance_of?\n<#{klass}> but was."
  assert_block(errmsg) { ! obj.instance_of?(klass) }
end

#assert_not_kind_of(klass, obj, msg = "") ⇒ Object



88
89
90
91
92
# File 'lib/section9/unittest.rb', line 88

def assert_not_kind_of(klass, obj, msg="")
  #errmsg = build_message("<?>\nexpected not to be kind_of\\?\n<?> but was\n<?>.", obj, klass, obj.class)
  errmsg = "<#{obj.inspect}>\nexpected not to be kind_of?\n<#{klass}> but was\n<#{obj.class}>."
  assert_block(errmsg) { ! obj.kind_of?(klass) }
end

#assert_not_respond_to(actual, method, msg = "") ⇒ Object



105
106
107
108
109
# File 'lib/section9/unittest.rb', line 105

def assert_not_respond_to(actual, method, msg="")
  #errmsg = build_message(msg, "Expected ? not to respond to ?.", actual, method.to_s.intern)
  errmsg = "Expected #{actual.inspect} not to respond to #{method.inspect}."
  assert_block(errmsg) { ! actual.respond_to?(method) }
end

#assert_nothing_thrown(msg = "") ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/section9/unittest.rb', line 111

def assert_nothing_thrown(msg="")
  errcls = RUBY_VERSION >= "1.9" ? ArgumentError : NameError
  yield
  assert_block("Expected nothing to be thrown") { true }
rescue errcls => ex
  #errmsg = build_message(msg, "nothing should be thrown but #{ex.message}")
  errmsg = "nothing should be thrown but #{ex.message}"
  assert_block(errmsg) { false }
end

#default_testObject

ignore unnecessary warning



122
123
124
# File 'lib/section9/unittest.rb', line 122

def default_test
  super if self.class.name != 'Section9::UnitTest::TestCase'
end

#teardownObject



76
77
78
79
# File 'lib/section9/unittest.rb', line 76

def teardown
  super
  __report_registered()
end

#verify_(actual = nil, &block) ⇒ Object

shortcut for assertion methods



47
48
49
50
51
52
# File 'lib/section9/unittest.rb', line 47

def verify_(actual=nil, &block)
  location = caller(1).first
  vo = VerifyObject.new(self, actual, location, &block)
  __register(vo)
  vo
end