Forum: Ruby why no assert_not?

Posted by Martin DeMello (Guest)
on 2010-09-01 10:19
(Received via mailing list)
Test::Unit could use a built-in antonym of assert, that succeeds if
its argument is either false or nil.

martin
Posted by Brian Candler (candlerb)
on 2010-09-01 11:07
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
Posted by Ryan Davis (Guest)
on 2010-09-01 11:13
(Received via mailing list)
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:
Posted by Martin DeMello (Guest)
on 2010-09-01 15:49
(Received via mailing list)
On Wed, Sep 1, 2010 at 2:37 PM, Brian Candler <b.candler@pobox.com> 
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
Posted by Rick Denatale (rdenatale)
on 2010-09-01 17:23
(Received via mailing list)
On Wed, Sep 1, 2010 at 5:12 AM, Ryan Davis <ryand-ruby@zenspider.com> 
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
Posted by Phlip (Guest)
on 2010-09-01 20:35
(Received via mailing list)
On Sep 1, 8:23 am, Rick DeNatale <rick.denat...@gmail.com> 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 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.
Posted by Ryan Davis (Guest)
on 2010-09-01 21:04
(Received via mailing list)
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
Posted by Daniel Berger (djberg96)
on 2010-09-01 21:48
(Received via mailing list)
On Sep 1, 2:19 am, Martin DeMello <martindeme...@gmail.com> 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
Posted by Ryan Davis (Guest)
on 2010-09-01 22:13
(Received via mailing list)
On Sep 1, 2010, at 12:47 , Daniel Berger wrote:

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

uhhh... you're fired?
Posted by Daniel Berger (djberg96)
on 2010-09-01 22:43
(Received via mailing list)
On Sep 1, 2:12 pm, Ryan Davis <ryand-r...@zenspider.com> wrote:
> On Sep 1, 2010, at 12:47 , Daniel Berger 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
Posted by David A. Black (Guest)
on 2010-09-02 12:23
(Received via mailing list)
Hi --

On Thu, 2 Sep 2010, Daniel Berger 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.