Method: Test::Unit::Assertions#assert_type_has_instance_methods

Defined in:
lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb

#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



18
19
20
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
# File 'lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb', line 18

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