Module: Test::Unit::Assertions

Defined in:
lib/xqsr3/doc_.rb,
lib/xqsr3/extensions/test/unit/assert_eql.rb,
lib/xqsr3/extensions/test/unit/assert_not.rb,
lib/xqsr3/extensions/test/unit/assert_true.rb,
lib/xqsr3/extensions/test/unit/assert_false.rb,
lib/xqsr3/extensions/test/unit/assert_not_eql.rb,
lib/xqsr3/extensions/test/unit/assert_subclass_of.rb,
lib/xqsr3/extensions/test/unit/assert_superclass_of.rb,
lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb,
lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb

Overview

Standard module

Components of interest

  • ::Test::Unit::Assertions#assert_eql

  • ::Test::Unit::Assertions#assert_false

  • ::Test::Unit::Assertions#assert_not

  • ::Test::Unit::Assertions#assert_not_eql

  • ::Test::Unit::Assertions#assert_raise_with_message

  • ::Test::Unit::Assertions#assert_subclass_of

  • ::Test::Unit::Assertions#assert_superclass_of

  • ::Test::Unit::Assertions#assert_true

  • ::Test::Unit::Assertions#assert_type_has_instance_methods

Instance Method Summary collapse

Instance Method Details

#assert_eql(expected, actual, failure_message = '') ⇒ Object

Assert that expected and actual have the same hash key, as evaluated by the instance method eq?



14
15
16
17
# File 'lib/xqsr3/extensions/test/unit/assert_eql.rb', line 14

def assert_eql(expected, actual, failure_message = '')

  assert expected.eql?(actual), failure_message
end

#assert_false(expression, failure_message = '') ⇒ Object

Assert that expression is false (and not merely falsey)



13
14
15
16
# File 'lib/xqsr3/extensions/test/unit/assert_false.rb', line 13

def assert_false(expression, failure_message = '')

  assert ::FalseClass === (expression), failure_message
end

#assert_not(expression, failure_message = '') ⇒ Object

Assert that expression is falsey



13
14
15
16
# File 'lib/xqsr3/extensions/test/unit/assert_not.rb', line 13

def assert_not(expression, failure_message = '')

  assert !(expression), failure_message
end

#assert_not_eql(expected, actual, failure_message = '') ⇒ Object

Assert that expected and actual have different hash keys, as evaluated by the instance method eq?



14
15
16
17
# File 'lib/xqsr3/extensions/test/unit/assert_not_eql.rb', line 14

def assert_not_eql(expected, actual, failure_message = '')

  assert !(expected.eql?(actual)), failure_message
end

#assert_raise_with_message(type_spec, message_spec, failure_message = nil, &block) ⇒ Object

Asserts that the attached block raises an exception of one of the exceptions defined by type_spec and/or has a message matching message_spec

Signature

  • Parameters:

    • type_spec (String, Regexp, [String], [Regexp], nil) Specification of type expectation(s)

    • message_spec (String, Regexp, [String], [Regexp], nil) Specification of message expectation(s)

    • failure_message (String, nil) Optional message to be used if the matching fails

  • Block

A required block containing code that is expected to raise an exception



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb', line 46

def assert_raise_with_message(type_spec, message_spec, failure_message = nil, &block)

  unless block_given?

    msg = "WARNING: no block_given to assert_raise_with_message() called from: #{caller[0]}"

    warn "\n#{msg}"

    assert false, msg
  end

  case type_spec
  when ::Array, nil

    ;
  else

    type_spec = [ type_spec ]
  end

  case message_spec
  when ::Array, nil

    ;
  else

    message_spec = [ message_spec ]
  end


  begin

    yield

    assert false, 'the block did not throw an exception as was expected'
  rescue ::Xqsr3::Internal_::X_assert_raise_with_message_::AssertionFailedError_

    raise
  rescue Exception => x

    if type_spec

      assert false, "exception (#{x.class}) - message: '#{x.message}' - not of any of required types (#{type_spec.join(', ')}); #{x.class} given" unless type_spec.any? { |c| x.is_a? c}
    end

    if message_spec

      assert false, "exception message not of any of required messages; '#{x.message}' given" unless message_spec.any? do |m|

        case m
        when ::Regexp

          x.message =~ m
        when ::String

          x.message == m
        else

          warn "\nunsupported message_spec entry '#{m}' (#{m.class})"
        end
      end
    end

    assert true
  end
end

#assert_subclass_of(parent_class, tested_class, failure_message = nil) ⇒ Object

Assert that tested_class is a sub-class of parent_class



13
14
15
16
17
18
# File 'lib/xqsr3/extensions/test/unit/assert_subclass_of.rb', line 13

def assert_subclass_of(parent_class, tested_class, failure_message = nil)

  failure_message ||= "#{tested_class} is not a subclass of #{parent_class}"

  assert(tested_class < parent_class, failure_message)
end

#assert_superclass_of(child_class, tested_class, failure_message = nil) ⇒ Object

Assert that tested_class is a super-class of child_class



13
14
15
16
17
18
# File 'lib/xqsr3/extensions/test/unit/assert_superclass_of.rb', line 13

def assert_superclass_of(child_class, tested_class, failure_message = nil)

  failure_message ||= "#{tested_class} is not a superclass of #{child_class}"

  assert(child_class < tested_class, failure_message)
end

#assert_true(expression, failure_message = '') ⇒ Object

Assert that expression is true (and not merely truey)



13
14
15
16
# File 'lib/xqsr3/extensions/test/unit/assert_true.rb', line 13

def assert_true(expression, failure_message = '')

  assert ::TrueClass === (expression), failure_message
end

#assert_type_has_instance_methods(type, message_spec, failure_message = nil) ⇒ Object

Fails unless the given type responds to all of the messages given by message_spec

Signature

  • Parameters:

    • type (::Class) The type

    • message_spec (::Symbol, ::Array, ::Hash) A specification of message(s) received by the instances of type. If a ::Symbol, then instances must respond to this single message. If an ::Array (all elements of which must be ::Symbol), then instances must respond to all messages. If a ::Hash, then instances must respond to all messages represented by the keys; the values are available for specifying a custom failure message (or value is nil for stock message)

  • failure_message (::String) If specified, is used when instances of type do not respond to a message and no custom failure message is provided for it



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb', line 21

def assert_type_has_instance_methods(type, message_spec, failure_message = nil)

  warn "type parameter - '#{type} (#{type.class})' - should be a Class" unless type.is_a?(::Class)

  case message_spec
  when ::Hash

    warn "every key in a Hash message_spec should be of type Symbol" unless message_spec.keys.all? { |k| ::Symbol === k }
  when ::Array

    warn "every key in an Array message_spec should be of type Symbol" unless message_spec.all? { |k| ::Symbol === k }

    message_spec = Hash[message_spec.map { |s| [ s, nil ] }]
  when ::Symbol

    message_spec[message_spec] = nil
  else

    msg = "message_spec - '#{message_spec} (#{message_spec.class})' - should be a Symbol, an Array of Symbols, or a Hash of Symbol => message"

    warn msg

    return assert false, msg
  end

  ims = type.instance_methods

  message_spec.each do |sym, message|

    unless ims.include? sym

      message ||= failure_message
      message ||= "type #{type} does not contain the instance method #{sym}"

      return assert false, message
    end
  end

  assert true
end