Module: Test::Unit::Assertions

Includes:
MiniTest::Assertions
Defined in:
lib/test/unit/assertions.rb

Instance Method Summary (collapse)

Methods included from MiniTest::Assertions

#_assertions, #_assertions=, #assert_empty, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_raises, #assert_same, #assert_send, #assert_throws, #capture_io, #exception_details, #flunk, #message, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_respond_to, #refute_same, #skip

Instance Method Details

- (Object) assert(test, msg = (nomsg = true; nil))



13
14
15
16
17
18
19
20
# File 'lib/test/unit/assertions.rb', line 13

def assert(test, msg = (nomsg = true; nil))
  unless nomsg or msg.instance_of?(String) or msg.instance_of?(Proc) or
      (bt = caller).first.rindex(MiniTest::MINI_DIR, 0)
    bt.delete_if {|s| s.rindex(MiniTest::MINI_DIR, 0)}
    raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
  end
  super
end

- (Object) assert_block(*msgs)



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

def assert_block(*msgs)
  assert yield, *msgs
end

- (Object) assert_equal(exp, act, msg = nil)



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
112
# File 'lib/test/unit/assertions.rb', line 68

def assert_equal(exp, act, msg = nil)
  msg = message(msg) {
    exp_str = mu_pp(exp)
    act_str = mu_pp(act)
    exp_comment = ''
    act_comment = ''
    if exp_str == act_str
      if (exp.is_a?(String) && act.is_a?(String)) ||
         (exp.is_a?(Regexp) && act.is_a?(Regexp))
        exp_comment = " (#{exp.encoding})"
        act_comment = " (#{act.encoding})"
      elsif exp.is_a?(Float) && act.is_a?(Float)
        exp_str = "%\#.#{Float::DIG+2}g" % exp
        act_str = "%\#.#{Float::DIG+2}g" % act
      elsif exp.is_a?(Time) && act.is_a?(Time)
        if exp.subsec * 1000_000_000 == exp.nsec
          exp_comment = " (#{exp.nsec}[ns])"
        else
          exp_comment = " (subsec=#{exp.subsec})"
        end
        if act.subsec * 1000_000_000 == act.nsec
          act_comment = " (#{act.nsec}[ns])"
        else
          act_comment = " (subsec=#{act.subsec})"
        end
      elsif exp.class != act.class
        # a subclass of Range, for example.
        exp_comment = " (#{exp.class})"
        act_comment = " (#{act.class})"
      end
    elsif !Encoding.compatible?(exp_str, act_str)
      if exp.is_a?(String) && act.is_a?(String)
        exp_str = exp.dump
        act_str = act.dump
        exp_comment = " (#{exp.encoding})"
        act_comment = " (#{act.encoding})"
      else
        exp_str = exp_str.dump
        act_str = act_str.dump
      end
    end
    "<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
  }
  assert(exp == act, msg)
end

- (Object) assert_no_match(regexp, string, msg = nil)



124
125
126
127
128
129
# File 'lib/test/unit/assertions.rb', line 124

def assert_no_match(regexp, string, msg=nil)
  assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
  self._assertions -= 1
  msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
  assert(regexp !~ string, msg)
end

- (Object) assert_not_equal(exp, act, msg = nil)



119
120
121
122
# File 'lib/test/unit/assertions.rb', line 119

def assert_not_equal(exp, act, msg=nil)
  msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
  assert(exp != act, msg)
end

- (Object) assert_not_nil(exp, msg = nil)



114
115
116
117
# File 'lib/test/unit/assertions.rb', line 114

def assert_not_nil(exp, msg=nil)
  msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
  assert(!exp.nil?, msg)
end

- (Object) assert_not_same(expected, actual, message = "")



131
132
133
134
135
136
137
138
139
# File 'lib/test/unit/assertions.rb', line 131

def assert_not_same(expected, actual, message="")
  msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
<?>
with id <?> expected to not be equal\\? to
<?>
with id <?>.
EOT
  assert(!actual.equal?(expected), msg)
end

- (Object) assert_nothing_raised(*args)



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
# File 'lib/test/unit/assertions.rb', line 30

def assert_nothing_raised(*args)
  self._assertions += 1
  if Module === args.last
    msg = nil
  else
    msg = args.pop
  end
  begin
    line = __LINE__; yield
  rescue Exception => e
    bt = e.backtrace
    as = e.instance_of?(MiniTest::Assertion)
    if as
      ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
      bt.reject! {|ln| ans =~ ln}
    end
    if ((args.empty? && !as) ||
        args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
      msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
      raise MiniTest::Assertion, msg.call, bt
    else
      raise
    end
  end
  nil
end

- (Object) assert_nothing_thrown(msg = nil)



57
58
59
60
61
62
63
64
65
66
# File 'lib/test/unit/assertions.rb', line 57

def assert_nothing_thrown(msg=nil)
  begin
    yield
  rescue ArgumentError => error
    raise error if /\Auncaught throw (.+)\z/m !~ error.message
    msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
    flunk(msg)
  end
  assert(true, "Expected nothing to be thrown")
end

- (Object) assert_raise(*args, &b)



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

def assert_raise(*args, &b)
  assert_raises(*args, &b)
end

- (Object) assert_respond_to(obj, meth, msg = nil)

get rid of overcounting



142
143
144
# File 'lib/test/unit/assertions.rb', line 142

def assert_respond_to obj, meth, msg = nil
  super if !caller[0].rindex(MiniTest::MINI_DIR, 0) || !obj.respond_to?(meth)
end

- (Object) build_message(head, template = nil, *arguments)



152
153
154
155
# File 'lib/test/unit/assertions.rb', line 152

def build_message(head, template=nil, *arguments)
  template &&= template.chomp
  template.gsub(/\?/) { mu_pp(arguments.shift) }
end

- (Object) mu_pp(obj)



9
10
11
# File 'lib/test/unit/assertions.rb', line 9

def mu_pp(obj)
  obj.pretty_inspect.chomp
end