Module: Test::Unit::Assertions

Defined in:
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

Instance Method Summary collapse

Instance Method Details

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



9
10
11
12
# File 'lib/xqsr3/extensions/test/unit/assert_eql.rb', line 9

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

	assert expected.eql?(actual), failure_message
end

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



9
10
11
12
# File 'lib/xqsr3/extensions/test/unit/assert_false.rb', line 9

def assert_false(expression, failure_message = '')

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

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



9
10
11
12
# File 'lib/xqsr3/extensions/test/unit/assert_not.rb', line 9

def assert_not(test, failure_message = '')

	assert !(test), failure_message
end

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



9
10
11
12
# File 'lib/xqsr3/extensions/test/unit/assert_not_eql.rb', line 9

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) ⇒ Object



9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb', line 9

def assert_raise_with_message(type_spec, message_spec, failure_message = nil)

	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, "expected did not throw an exception"
	rescue Exception => x

		if type_spec

			assert false, "exception 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



9
10
11
12
13
14
# File 'lib/xqsr3/extensions/test/unit/assert_subclass_of.rb', line 9

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



9
10
11
12
13
14
# File 'lib/xqsr3/extensions/test/unit/assert_superclass_of.rb', line 9

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



9
10
11
12
# File 'lib/xqsr3/extensions/test/unit/assert_true.rb', line 9

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
    


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
61
62
63
64
65
66
# File 'lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb', line 27

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, msg|

		unless ims.include? sym

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

			return assert false, msg
		end
	end

	assert true
end