Module: Kintama::Assertions

Included in:
Test
Defined in:
lib/kintama/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert(expression, message = "failed") ⇒ Object


6
7
8
# File 'lib/kintama/assertions.rb', line 6

def assert(expression, message="failed")
  raise Kintama::TestFailure, message unless expression
end

#assert_equal(expected, actual, message = "Expected #{expected.inspect} but got #{actual.inspect}") ⇒ Object


14
15
16
# File 'lib/kintama/assertions.rb', line 14

def assert_equal(expected, actual, message="Expected #{expected.inspect} but got #{actual.inspect}")
  assert actual == expected, message
end

#assert_kind_of(klass, thing, message = "should be a kind of #{klass}") ⇒ Object


38
39
40
# File 'lib/kintama/assertions.rb', line 38

def assert_kind_of(klass, thing, message="should be a kind of #{klass}")
  assert thing.is_a?(klass), message
end

#assert_match(regexp, string, message = "expected #{string.inspect} to match #{regexp.inspect}") ⇒ Object


30
31
32
# File 'lib/kintama/assertions.rb', line 30

def assert_match(regexp, string, message="expected #{string.inspect} to match #{regexp.inspect}")
  assert (string =~ regexp), message
end

#assert_nil(object, message = "#{object.inspect} was not nil") ⇒ Object


22
23
24
# File 'lib/kintama/assertions.rb', line 22

def assert_nil(object, message="#{object.inspect} was not nil")
  assert_equal nil, object, message
end

#assert_no_match(regexp, string, message = "expected #{string.inspect} not to match #{regexp.inspect}") ⇒ Object


34
35
36
# File 'lib/kintama/assertions.rb', line 34

def assert_no_match(regexp, string, message="expected #{string.inspect} not to match #{regexp.inspect}")
  assert !(string =~ regexp), message
end

#assert_not_equal(expected, actual, message = "Expected #{expected.inspect} to not be equal to #{actual.inspect}") ⇒ Object


18
19
20
# File 'lib/kintama/assertions.rb', line 18

def assert_not_equal(expected, actual, message="Expected #{expected.inspect} to not be equal to #{actual.inspect}")
  assert actual != expected, message
end

#assert_not_nil(object, message = "should not be nil") ⇒ Object


26
27
28
# File 'lib/kintama/assertions.rb', line 26

def assert_not_nil(object, message="should not be nil")
  assert_not_equal nil, object, message
end

#assert_not_output(not_expected, message = "Expected output not to match #{not_expected.inspect}", &block) ⇒ Object


84
85
86
87
88
89
90
91
# File 'lib/kintama/assertions.rb', line 84

def assert_not_output(not_expected, message="Expected output not to match #{not_expected.inspect}", &block)
  output = capture_output(&block).read.strip
  if not_expected.is_a?(Regexp)
    assert_no_match not_expected, output, message
  else
    assert_not_equal not_expected, output, message
  end
end

#assert_nothing_raised(message = "should not raise anything", &block) ⇒ Object


50
51
52
53
54
# File 'lib/kintama/assertions.rb', line 50

def assert_nothing_raised(message="should not raise anything", &block)
  yield
rescue Exception => e
  raise Kintama::TestFailure, message + " (#{e} was raised)"
end

#assert_output(expected, message = "Expected output to match #{expected.inspect}", &block) ⇒ Object


75
76
77
78
79
80
81
82
# File 'lib/kintama/assertions.rb', line 75

def assert_output(expected, message="Expected output to match #{expected.inspect}", &block)
  output = capture_output(&block).read.strip
  if expected.is_a?(Regexp)
    assert_match expected, output, message
  else
    assert_equal expected, output, message
  end
end

#assert_raises(klass_or_message = Exception, message = "should raise an exception", &block) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kintama/assertions.rb', line 56

def assert_raises(klass_or_message=Exception, message="should raise an exception", &block)
  if klass_or_message.respond_to?(:ancestors)
    klass = klass_or_message
  else
    message = klass_or_message
    klass = Exception
  end
  yield
  raised = false
rescue => e
  if e.class.ancestors.include?(klass)
    raised = true
  else
    raised = false
  end
ensure
  raise Kintama::TestFailure, message unless raised
end

#assert_same(expected, actual, message = "Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})") ⇒ Object


42
43
44
# File 'lib/kintama/assertions.rb', line 42

def assert_same(expected, actual, message="Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})")
  assert actual.equal?(expected), message
end

#assert_same_elements(expected, object, message = "#{object.inspect} does not contain the same elements as #{expected.inspect}") ⇒ Object


46
47
48
# File 'lib/kintama/assertions.rb', line 46

def assert_same_elements(expected, object, message = "#{object.inspect} does not contain the same elements as #{expected.inspect}")
  assert Set.new(expected) == Set.new(object), message
end

#flunk(message = "flunked.") ⇒ Object


10
11
12
# File 'lib/kintama/assertions.rb', line 10

def flunk(message="flunked.")
  assert false, message
end