Why Ruby?

Nick Sabalausky a at a.a
Mon Dec 13 10:41:07 PST 2010


"Nick Sabalausky" <a at a.a> wrote in message 
news:ie5n5t$2vuk$1 at digitalmars.com...
> "Ary Borenszweig" <ary at esperanto.org.ar> wrote in message 
> news:ie5ek1$2bn5$1 at digitalmars.com...
>> Well, about a year ago I started programming in dynamic languages like 
>> python and
>> Ruby. I did very little of python and a lot in Ruby. At first I thought 
>> "Hm, it
>> must be really hard to program without autocompletion, static type 
>> verification,
>> etc.". Soon I found out I was wrong.
>>
>> First, in Ruby it's very easy to test the code. And you can do crazy 
>> things like
>> what I mention in here (
>> http://weblogs.manas.com.ar/ary/2010/04/15/quick-way-to-test-many-values-in-ruby/ )
>>
>> [[1, 1], [4, 2], [9, 3]].each do |input, output|
>>  test "sqrt #{input}"
>>    assert_equal output, sqrt(input)
>>  end
>> end
>>
>> and the good thing is that if it fails for a specific input/output pair 
>> it is
>> treated as a different test, so you can clearly see which input/output 
>> pair
>> failed. I can see you can do the same in D with string mixins, probably 
>> putting
>> all the code between do/end in a string... :-(
>>
>
> That's why I refuse to use assert in unittests, and use a non-fatal 
> alternative instead. There isn't a chance in hell I'm replacing:
>
> ------------------------------------------
> unittest
> {
>    /*
>    A few lines
>    of setup
>    for foo
>    */
>
>    assert(foo(/*arg set 1*/) == /*answer 1*/);
>    assert(foo(/*arg set 2*/) == /*answer 2*/);
>    assert(foo(/*arg set 3*/) == /*answer 3*/);
>    assert(foo(/*arg set 4*/) == /*answer 4*/);
>    assert(foo(/*arg set 5*/) == /*answer 5*/);
>
>    /*
>    A few lines
>    of setup
>    for bar
>    */
>
>    assert(bar(/*arg set 1*/) == /*answer 1*/);
>    assert(bar(/*arg set 2*/) == /*answer 2*/);
>    assert(bar(/*arg set 3*/) == /*answer 3*/);
>    assert(bar(/*arg set 4*/) == /*answer 4*/);
>    assert(bar(/*arg set 5*/) == /*answer 5*/);
> }
> ------------------------------------------
>
> ...With this god-awful mess, or any equally-bad mixin-based alternative, 
> just to get the damn thing to run all my tests:
> ------------------------------------------
> /*
> The declarations needed by
> foo's unittests here
> */
> void setupUnittestFoo()
> {
>    /*
>    A few lines
>    of setup
>    for foo
>    */
> }
>
> unittest { setupUnittestFoo() }
> unittest { assert(foo(/*arg set 1*/) == /*answer 1*/); }
> unittest { assert(foo(/*arg set 2*/) == /*answer 2*/); }
> unittest { assert(foo(/*arg set 3*/) == /*answer 3*/); }
> unittest { assert(foo(/*arg set 4*/) == /*answer 4*/); }
> unittest { assert(foo(/*arg set 5*/) == /*answer 5*/); }
>
> /*
> The declarations needed by
> bar's unittests here
> */
> void setupUnittestBar()
> {
>    /*
>    A few lines
>    of setup
>    for bar
>    */
> }
>
> unittest { setupUnittestBar() }
> unittest { assert(bar(/*arg set 1*/) == /*answer 1*/); }
> unittest { assert(bar(/*arg set 2*/) == /*answer 2*/); }
> unittest { assert(bar(/*arg set 3*/) == /*answer 3*/); }
> unittest { assert(bar(/*arg set 4*/) == /*answer 4*/); }
> unittest { assert(bar(/*arg set 5*/) == /*answer 5*/); }
>
> ------------------------------------------
>

The reason I mention that, of course, is that with a non-fatal alternative 
to assert you can do like you said even without being able to put a 
"unittest{...}" inside a foreach:

unittest
{
    foreach(pair; [[1, 1], [4, 2], [9, 3]])
        nonFatalAssertEqual(pair[0], sqrt(pair[1]));
}




More information about the Digitalmars-d mailing list