why no assert_not?

M

Martin DeMello

Test::Unit could use a built-in antonym of assert, that succeeds if
its argument is either false or nil.

martin
 
B

Brian Candler

Martin said:
Test::Unit could use a built-in antonym of assert, that succeeds if
its argument is either false or nil.

Hmm, I would have thought "assert not ..." would work, but there is
something odd in the parsing of 'not' as an argument:

$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
$ irb --simple-prompttrue
=> nilSyntaxError: compile error
(irb):4: syntax error, unexpected kNOT, expecting ')'
puts(not 3 < 4)
^
(irb):4: syntax error, unexpected ')', expecting $end
from (irb):4
from :0false

In the past I've just done assert !(...)

But I think the issue is moot, because writing custom asserts is a
perfectly standard thing to do. That is, just write

def assert_not(bool, *rest)
assert(!bool, *rest)
end
 
R

Ryan Davis

Test::Unit could use a built-in antonym of assert, that succeeds if
its argument is either false or nil.

this is why minitest has refute, which means it is available in ruby =
1.9:
./ruby19 -Ilib -rtest/unit -e 'class X < Test::Unit::TestCase; def = test_x; refute true; end;end'
Test run options: --seed 53548
=20
Loaded suite -e
Started
F
Finished in 0.000635 seconds.
=20
1) Failure:
test_x(X) [-e:1]:
Failed refutation, no message given
=20
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
=20
Test run options: --seed 53548
 
M

Martin DeMello

But I think the issue is moot, because writing custom asserts is a
perfectly standard thing to do. That is, just write

=A0def assert_not(bool, *rest)
=A0 =A0assert(!bool, *rest)
=A0end

Yeah, but this one seems universal enough that it ought to be there by defa=
ult.

martin
 
R

Rick DeNatale

this is why minitest has refute, which means it is available in ruby 1.9:

FWIW

Refute isn't a very good antonym for assert.

Refute means to disprove, assert means to claim that something is
true, but doesn't imply truth of that assertion.

Deny or Rebut would be better antonyms.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
P

Phlip

Deny or Rebut would be better antonyms.

deny{} all the way.

Including many of the assert_not_yack_yack() out there should upgrade
to deny_yack_yack(). Less self-contradictory.

Also, Alex Chaffee's Wrong project has an assert{ yack yack } and
deny{ yack yack } where you write _anything_ for the yack yack, and
when it fails (or incorrectly succeeds!) it prints out all the details
of yack yack, including all intermediate variables names and their
values.

I wonder where he got the idea.
 
R

Ryan Davis

Refute isn't a very good antonym for assert.
=20
Refute means to disprove, assert means to claim that something is
true, but doesn't imply truth of that assertion.
=20
Deny or Rebut would be better antonyms.

blah blah blah... I've been through all of this before and in public no =
less. You had your chance. It is good enough and: "refute".size =3D=3D =
"assert".size
 
D

Daniel Berger

Test::Unit could use a built-in antonym of assert, that succeeds if
its argument is either false or nil.

test-unit 2.x has assert_false and assert_nil if that's any help.
Actually, test-unit 1.x has assert_nil, too, I believe. You can always
add your own.

class Test::Unit::TestCase
def assert_nil_or_false(condition)
assert_nil(condition) && assert_false(condition)
end
end

Regards,

Dan
 
D

Daniel Berger

uhhh... you're fired?

Hah, that's what I get for not actually trying my own code out first.

module Test::Unit::Assertions
def assert_nil_or_false(object, message =3D nil)
message =3D build_message(message, '<?> is not false or nil.',
object)
assert_block(message) do
object.nil? || object =3D=3D false
end
end
end

Regards,

Dan
 
D

David A. Black

Hi --

assert_block(message) do
object.nil? || object == false

!object would be equivalent there, and faster (about 1/3 the time for a
benchmark of mixed cases).


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top