Comparing strings and integers in Test::Unit

I have a test that looks like this:

require ‘test/unit’

class My_Test < Test::Unit::TestCase
def test_1
assert_equal(‘10’, ‘10’) # okay
assert_equal(10, ‘10’) # failure
end
end

The result looks like this:

Loaded suite test2
Started
F
Finished in 0.00906 seconds.

1) Failure!!!

test_1(My_Test) [test2.rb:6]:
<10> expected but was
<10>

1 tests, 2 assertions, 1 failures, 0 errors

It’s not immediately obvious why <10> is not equal to <10>.

  1. Is there anything I can do to get a more useful assertion failure
    message?
  2. When I want to differentiate like this in my own code, I use #inspect
    instead of #to_s. I’m not sure if that’s appropriate here, since
    #inspect works well for integers and strings and regexes, but might
    not work well with user-defined types. Could Test::Unit use #inspect
    instead of #to_s, and would there be any unwanted side-effects?
  3. Runit has the same problem; can I work around this in Runit?

Also, I noticed that Test::Unit 0.1.8 has different output from 0.1.6.

  1. What was the reason for the change?
  2. Why is there a carriage return after the word “was”?
  3. Is it possible to get Test::Unit to print the names of the tests as
    they are run, like it used to?

Paul

I have a test that looks like this:

require ‘test/unit’

class My_Test < Test::Unit::TestCase
def test_1
assert_equal(‘10’, ‘10’) # okay
assert_equal(10, ‘10’) # failure
end
end

1) Failure!!!
test_1(My_Test) [test2.rb:6]:
<10> expected but was
<10>

1 tests, 2 assertions, 1 failures, 0 errors

It’s not immediately obvious why <10> is not equal to <10>.

  1. Is there anything I can do to get a more useful assertion failure
    message?

If this is a problem with just one test, you could always do something like:

assert(a==‘10’, “<10> expected but was\n<#{a}><#{a.class}>”)

  1. When I want to differentiate like this in my own code, I use #inspect
    instead of #to_s. I’m not sure if that’s appropriate here, since
    #inspect works well for integers and strings and regexes, but might
    not work well with user-defined types. Could Test::Unit use #inspect
    instead of #to_s, and would there be any unwanted side-effects?

I don’t know either, but it seems that it would be a good idea for
assert_equal to check for expected.to_s == actual.to_s, and if so, provide a
more detailled error message [probably using ‘inspect’]

There are other cases where that would be useful - structs which are not
equal, for example.

Regards,

Brian.

···

On Tue, Feb 25, 2003 at 01:16:58AM +0900, Paul Brannan wrote:

One time when this bit me, I did the following (assuming variables
and ):

redefine expected, actual
assert_equal(expected, actual)

def redefine(*args)
args.each do |obj|
def obj.to_s
inspect # or some user-defined output
end
end
end

In other words, I redefined the #to_s method for the expected and
actual objects so that #inspect was called instead.

I like Brian’s idea that assert_equal should produce a more
intelligent error message if the two objects are not equal but produce
equal strings.

Gavin

···

On Tuesday, February 25, 2003, 3:16:58 AM, Paul wrote:

It’s not immediately obvious why <10> is not equal to <10>.

  1. Is there anything I can do to get a more useful assertion failure
    message?