Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • test/unit/assertions.rb

Class/Module Index [+]

Quicksearch

Test::Unit::Assertions

Public Instance Methods

assert(test, msg = (nomsg = true; nil)) click to toggle source
 
               # File 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
            
assert_block(*msgs) click to toggle source
 
               # File test/unit/assertions.rb, line 22
def assert_block(*msgs)
  assert yield, *msgs
end
            
assert_equal(exp, act, msg = nil) click to toggle source
 
               # File 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
            
assert_no_match(regexp, string, msg=nil) click to toggle source
 
               # File 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
            
assert_not_equal(exp, act, msg=nil) click to toggle source
 
               # File 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
            
assert_not_nil(exp, msg=nil) click to toggle source
 
               # File 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
            
assert_not_same(expected, actual, message="") click to toggle source
 
               # File test/unit/assertions.rb, line 131
      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 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
            
assert_nothing_thrown(msg=nil) click to toggle source
 
               # File 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
            
assert_raise(*args, &b) click to toggle source
 
               # File test/unit/assertions.rb, line 26
def assert_raise(*args, &b)
  assert_raises(*args, &b)
end
            
assert_respond_to(obj, meth, msg = nil) click to toggle source

get rid of overcounting

 
               # File 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
            
build_message(head, template=nil, *arguments) click to toggle source
 
               # File test/unit/assertions.rb, line 152
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