In Files

  • test/unit/assertions.rb

Class/Module Index [+]

Quicksearch

Test::Unit::Assertions

Public Instance Methods

assert_equal(exp, act, msg = nil) click to toggle source
 
               # File test/unit/assertions.rb, line 55
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)
        exp_comment = " (nsec=#{exp.nsec})"
        act_comment = " (nsec=#{act.nsec})"
      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
            
assert_no_match(regexp, string, msg=nil) click to toggle source
 
               # File test/unit/assertions.rb, line 99
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
            
assert_not_equal(exp, act, msg=nil) click to toggle source
 
               # File test/unit/assertions.rb, line 94
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
            
assert_not_nil(exp, msg=nil) click to toggle source
 
               # File test/unit/assertions.rb, line 89
def assert_not_nil(exp, msg=nil)
  msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
  assert(!exp.nil?, msg)
end
            
assert_not_same(expected, actual, message="") click to toggle source
 
               # File test/unit/assertions.rb, line 106
      def assert_not_same(expected, actual, message="")
        msg = message(msg) { build_message(message, "<?>
with id <?> expected to not be equal\\? to
<?>
with id <?>.
", expected, expected.__id__, actual, actual.__id__) }
        assert(!actual.equal?(expected), msg)
      end
            
assert_nothing_raised(*args) click to toggle source
 
               # File test/unit/assertions.rb, line 17
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! {|line| ans =~ line}
    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
            
assert_nothing_thrown(msg=nil) click to toggle source
 
               # File test/unit/assertions.rb, line 44
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
            
assert_raise(*args, &b) click to toggle source
 
               # File test/unit/assertions.rb, line 13
def assert_raise(*args, &b)
  assert_raises(*args, &b)
end
            
build_message(head, template=nil, *arguments) click to toggle source
 
               # File test/unit/assertions.rb, line 116
def build_message(head, template=nil, *arguments)
  template &&= template.chomp
  template.gsub(/\?/) { mu_pp(arguments.shift) }
end
            
mu_pp(obj) click to toggle source
 
               # File test/unit/assertions.rb, line 9
def mu_pp(obj)
  obj.pretty_inspect.chomp
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus