Module: Kintama::Assertions
- Included in:
- Test
- Defined in:
- lib/kintama/assertions.rb
Instance Method Summary collapse
- #assert(expression, message = "failed") ⇒ Object
- #assert_equal(expected, actual, message = "Expected #{expected.inspect} but got #{actual.inspect}") ⇒ Object
- #assert_kind_of(klass, thing, message = "should be a kind of #{klass}") ⇒ Object
- #assert_match(regexp, string, message = "expected #{string.inspect} to match #{regexp.inspect}") ⇒ Object
- #assert_nil(object, message = "#{object.inspect} was not nil") ⇒ Object
- #assert_no_match(regexp, string, message = "expected #{string.inspect} not to match #{regexp.inspect}") ⇒ Object
- #assert_not_equal(expected, actual, message = "Expected #{expected.inspect} to not be equal to #{actual.inspect}") ⇒ Object
- #assert_not_nil(object, message = "should not be nil") ⇒ Object
- #assert_not_output(not_expected, message = "Expected output not to match #{not_expected.inspect}", &block) ⇒ Object
- #assert_nothing_raised(message = "should not raise anything", &block) ⇒ Object
- #assert_output(expected, message = "Expected output to match #{expected.inspect}", &block) ⇒ Object
- #assert_raises(klass_or_message = Exception, message = "should raise an exception", &block) ⇒ Object
- #assert_same(expected, actual, message = "Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})") ⇒ Object
- #assert_same_elements(expected, object, message = "#{object.inspect} does not contain the same elements as #{expected.inspect}") ⇒ Object
- #flunk(message = "flunked.") ⇒ Object
Instance Method Details
#assert(expression, message = "failed") ⇒ Object
6 7 8 |
# File 'lib/kintama/assertions.rb', line 6 def assert(expression, ="failed") raise Kintama::TestFailure, 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, ="Expected #{expected.inspect} but got #{actual.inspect}") assert actual == expected, 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, ="should be a kind of #{klass}") assert thing.is_a?(klass), 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, ="expected #{string.inspect} to match #{regexp.inspect}") assert (string =~ regexp), 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, ="#{object.inspect} was not nil") assert_equal nil, object, 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, ="expected #{string.inspect} not to match #{regexp.inspect}") assert !(string =~ regexp), 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, ="Expected #{expected.inspect} to not be equal to #{actual.inspect}") assert actual != expected, 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, ="should not be nil") assert_not_equal nil, object, 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, ="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, else assert_not_equal not_expected, output, 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(="should not raise anything", &block) yield rescue Exception => e raise Kintama::TestFailure, + " (#{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, ="Expected output to match #{expected.inspect}", &block) output = capture_output(&block).read.strip if expected.is_a?(Regexp) assert_match expected, output, else assert_equal expected, output, 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(=Exception, ="should raise an exception", &block) if .respond_to?(:ancestors) klass = else = klass = Exception end yield raised = false rescue => e if e.class.ancestors.include?(klass) raised = true else raised = false end ensure raise Kintama::TestFailure, 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, ="Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})") assert actual.equal?(expected), 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, = "#{object.inspect} does not contain the same elements as #{expected.inspect}") assert Set.new(expected) == Set.new(object), end |
#flunk(message = "flunked.") ⇒ Object
10 11 12 |
# File 'lib/kintama/assertions.rb', line 10 def flunk(="flunked.") assert false, end |