Does assert() exist?

I saw a bunch of Ruby examples on the web that involve the assert()
method. However, I can’t seem to use it. Does this method exist / what
do I have to do to be able to access it?

irb(main):001:0> assert(true)
NameError: undefined method `assert’ for #Object:0x401ddce0
from (irb):1

The Test::Unit library defines a number of assert methods, but they’re
only used in test suites, not in regular code.

However, it’s not hard to write one. This one is a no-op unless the
$DEBUG flag is set, which is set by the “-d” command line switch to
ruby.

----------- assert.rb -----------
class AssertionFailure < StandardError
end

class Object
def assert(bool, message = ‘assertion failure’)
if $DEBUG
raise AssertionFailure.new(message) unless bool
end
end
end
--------- assertTest.rb -----------
require ‘assert.rb’
assert(true, “testing true”)
assert(false, “testing false”)

···

On Sunday 02 June 2002 06:54 pm, Philip Mak wrote:

I saw a bunch of Ruby examples on the web that involve the assert()
method. However, I can’t seem to use it. Does this method exist /
what do I have to do to be able to access it?

irb(main):001:0> assert(true)
NameError: undefined method `assert’ for #Object:0x401ddce0
from (irb):1


$ ruby assertTest.rb
$ ruby -d assertTest.rb
Exception AssertionFailure' at ./assert.rb:7 - testing false ./assert.rb:7:in assert’: testing false (AssertionFailure)
from assertTest.rb:4


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Ned Konz ned@bike-nomad.com wrote in message >

----------- assert.rb -----------
class AssertionFailure < StandardError
end

class Object
def assert(bool, message = ‘assertion failure’)
if $DEBUG
raise AssertionFailure.new(message) unless bool
end
end
end
--------- assertTest.rb -----------
require ‘assert.rb’
assert(true, “testing true”)
assert(false, “testing false”)

This is very nice, but I wonder if Ruby can simulate a stringizing
assert macro from C, which I find more elegant:

#define assert(x)(x || fprintf(stderr, “Assertion failed: %s\n”, #x))

Thus, in C, the assertion

assert(x != 1);

if violated would print out

Assertion failed: x != 1

The assertion expression gets evaluated, as well as converted into a
string for printing to standard error. Is it possible to do this in
Ruby?

Damon

Ned Konz ned@bike-nomad.com writes:

I saw a bunch of Ruby examples on the web that involve the assert()
method. However, I can’t seem to use it. Does this method exist /
what do I have to do to be able to access it?

irb(main):001:0> assert(true)
NameError: undefined method `assert’ for #Object:0x401ddce0
from (irb):1

The Test::Unit library defines a number of assert methods, but they’re
only used in test suites, not in regular code.

In the beginning there was Ruby::Unit. Then comes a lot other
unit-testing packages. Ruby::Unit is still used by some people (like
me). The main problem for this mailing list’s reader with ruby unit is
most of its documentation is in Japanese. However an excellent
tutorial for Ruby::Unit is at
http://www.eng.dmu.ac.uk/~hgs/ruby/ruby-unit.html

But, now it seems Test::Unit is the favoured unit-testing packages. It
is going to be included in standard ruby distribution. For now, you
can access it at http://testunit.talbott.ws. Full documentation is at
that site. Test::Unit provides all that Ruby::Unit provides and some
more.

YS.

···

On Sunday 02 June 2002 06:54 pm, Philip Mak wrote:

Damon wrote:

This is very nice, but I wonder if Ruby can simulate a stringizing
assert macro from C, which I find more elegant:

#define assert(x)(x || fprintf(stderr, “Assertion failed: %s\n”, #x))

Thus, in C, the assertion

assert(x != 1);

if violated would print out

Assertion failed: x != 1

The assertion expression gets evaluated, as well as converted into a
string for printing to standard error. Is it possible to do this in
Ruby?

You could probably to some evil eval tricks, but I’m not sure how you
would get the binding of the caller in an elegant way.

This is ugly and slow, but atleast it is a startingpoint:

def assert( &block )
code = block.call
test = eval code, block
print "Assertion ‘#{code}’ "
print (if test then ‘ok’ else ‘failed’ end)
puts “.”
end

x = 9
assert {‘5==2+3’} #=> Assertion ‘5==2+3’ ok.
assert {‘x==33’} #=> Assertion 'x==33’ ok.
assert {‘x == 1’} #=> Assertion ‘x == 1’ failed.

Only one I could think of where I didn’t pass in the binding explicitly
:slight_smile:

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

I sometimes do something like this:

$initial_directory = Dir.getwd

def caller_file_pathname(filename)
File.expand_path(filename, $initial_directory)
end

def parse_caller(caller_str)
file, lineno, method = caller_str.split(‘:’)
if method =~ /in `(.*)'/ then
method = $1.intern()
end
return file, lineno.to_i(), method
end

def caller_line(level=0)
dir = File.dirname(caller_file_pathname($0))
file, lineno, method = parse_caller(caller[level.succ])
File.open(File.expand_path(file, dir)) do |input|
(lineno.to_i - 1).times { input.gets }
return input.gets
end
end

if DEBUG then
def assert(x, msg=nil)
if not x then
failed_msg = msg.nil?
? caller_line().sub(/^\s*/, ‘’).chomp
: msg
puts “Assertion failed: #{failed_msg}”
end
end
else
def assert(x, msg=nil)
end
end

but usually using one of the test libraries (Test::Unit, Rubyunit,
Lapidary) is sufficient; assert_equal prints a MUCH more useful message
than the above code.

Paul

···

On Mon, Jun 03, 2002 at 08:10:59PM +0900, Damon wrote:

This is very nice, but I wonder if Ruby can simulate a stringizing
assert macro from C, which I find more elegant:

#define assert(x)(x || fprintf(stderr, “Assertion failed: %s\n”, #x))

It might be nice if Test::Unit could expose its list of assert calls as
as a mixin.

···


Alan Chen
Digikata LLC

def foo; bar; end
def bar; caller; end

– Nikodemus

···

On Mon, 3 Jun 2002, Kent Dahl wrote:

would get the binding of the caller in an elegant way.

It might be nice if Test::Unit could expose its list of
assert calls as as a mixin.

Actually, it does…

irb(main):001:0> require ‘test/unit/assertions’
true
irb(main):002:0> include Test::Unit::Assertions
Object
irb(main):003:0> assert(true, “This should be true”)
nil
irb(main):004:0> assert(false, “This should be false”)
C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:30:in
assert_block': This should be false (Test::Unit::AssertionFailedError) from C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:29:in _wrap_assertion’
from
C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:29:in
assert_block' from C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:39:in assert’
from
C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:38:in
_wrap_assertion' from C:/ruby/lib/ruby/site_ruby/1.7/test/unit/assertions.rb:38:in assert’
from (irb):4:in irb_binding' from C:/ruby/lib/ruby/1.7/irb/workspace.rb:51:in irb_binding’
from C:/ruby/lib/ruby/1.7/irb/workspace.rb:51

Nathaniel

<:((><

···

Alan Chen [mailto:alan@digikata.com] wrote:

RoleModel Software, Inc.
EQUIP VI

Nikodemus Siivola wrote:

would get the binding of the caller in an elegant way.
def foo; bar; end
def bar; caller; end

Unless this has changed in 1.7.* or something, it still doesn’t give me
a binding object I can pass to eval and such.

irb(main):001:0> def foo; bar; end nil irb(main):002:0> def bar; caller; end nil irb(main):003:0> a = foo ["(irb):1:in `foo'", "(irb):3:in `irb_binding'", "/usr/lib/ruby/1.6/irb/workspace.rb:51:in `irb_binding'", "/usr/lib/ruby/1.6/irb/workspace.rb:51"] irb(main):004:0> a.type Array

This is all nice enough for logging, but not for evaluating code in the
context of the ‘caller-parent’, which is necessary for the assert to
work.

···

On Mon, 3 Jun 2002, Kent Dahl wrote:


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)