Why no assert_not?

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

martin

Martin DeMello wrote:

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-prompt

not 1
=> false

not 3 < 4
=> false

puts(3 < 4)
true
=> nil

puts(not 3 < 4)
SyntaxError: compile error
(irb):4: syntax error, unexpected kNOT, expecting ‘)’
puts(not 3 < 4)
^
(irb):4: syntax error, unexpected ‘)’, expecting $end
from (irb):4
from :0

puts((not 3 < 4))
false

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

On Sep 1, 2010, at 01:19 , Martin DeMello wrote:

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:

On Wed, Sep 1, 2010 at 2:37 PM, Brian C. [email protected]
wrote:

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

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

martin

On Wed, Sep 1, 2010 at 5:12 AM, Ryan D. [email protected]
wrote:

On Sep 1, 2010, at 01:19 , Martin DeMello wrote:

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:

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

On Sep 1, 2010, at 08:23 , Rick DeNatale wrote:

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.

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 ==
“assert”.size

On Sep 1, 2:19 am, Martin DeMello [email protected] wrote:

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

On Sep 1, 8:23 am, Rick DeNatale [email protected] wrote:

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 C.'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.

On Sep 1, 2010, at 12:47 , Daniel B. wrote:

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

uhhh… you’re fired?

On Sep 1, 2:12 pm, Ryan D. [email protected] wrote:

On Sep 1, 2010, at 12:47 , Daniel B. wrote:

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

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 = nil)
message = build_message(message, ‘<?> is not false or nil.’,
object)
assert_block(message) do
object.nil? || object == false
end
end
end

Regards,

Dan

Hi –

On Thu, 2 Sep 2010, Daniel B. wrote:

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

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