Assertion reflection

[X-posted from the RSpec mailing list]

We discuss RSpec’s assertions, which use ‘should’ for ‘assert’.

Micah M. wrote:

x.should_equal 2
a.should_not_be nil

When they fail they yield messages like:

1 should equal 2
nil should not be nil

In my humble opinion, these messages should include their context and read
like this:

x should equal 2 but was 1
a should not be nil but was nil

This is why C++ is superior to Ruby. An assertion can reflect its
expression
into the diagnostic report like that.

Oh, and I mis-Googled this subject recently, and inadvertently learned
that
Python assertions now supports that reflection.

If I tell the maintainers that C++ can do something, they’l ignore me.
But
if I say Python can, they will doubtless see red and go crazy about it.

Here’s a snip of research that might still work:

def trace_(&b)
file, line_nr, funkshun = caller()[0].split(’:’)

sorry, folks - split’s the only stringey function I know :wink:

    funkshun = funkshun.split(/in `/)[1]
    funkshun = funkshun.split(/\'/)[0]
    line_nr=line_nr.to_i
    linecnt=1
    expr=nil

    File.open(file).each_line do |line|
            if linecnt==line_nr
                    if line =~ /(^|\W)trace_\s*\{\s*(.*?)\s*\}/
                            expr = $2
                    end
                    break
            end
            linecnt+=1
    end

    if expr
            print file

puts “(#{line_nr})#{funkshun} #{expr}: #{eval(expr, b).inspect}”
end #{file}
end

The intent was to let trace_{foo} reflect both foo and its value.

RSpec could get by with fewer fragile regular expressions, and could
simply
reflect its entire line, then tack the variables on the end:

x.should_equal 2 (1, 2)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs