std.unittests for (final?) review [Update]

Nick Sabalausky a at a.a
Mon Jan 10 11:27:21 PST 2011


"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message 
news:mailman.535.1294662576.4748.digitalmars-d at puremagic.com...
>Okay, here's the new code and its documentation: http://is.gd/ktURa
>
>I followed Andrei's suggestion and merged most of the functions into a 
>highly
>flexible assertPred. I also renamed the functions as suggested and 
>attempted to
>fully document everything with fully functional examples instead of 
>examples
>using types or functions which don't actually exist.
>
>So, now there's just assertThrown, assertNotThrown, collectExceptionMsg, 
>and
>assertPred (though there are eight different overloads of assertPred). So, 
>review
>away.
>
>From the sounds of it, if this code gets voted in, it'll be going into
>std.exception.

I like it. (I still think a way to make them collect an array of exceptions 
instead of throwing should make it's way in there at some point though.)

Couple small things though:

- The error message from the conditional version of assertPred should 
probably omit the two words "is not". That much is already clear from the 
fact that assertPred threw and says "failed:", and getting rid of it makes 
it much more readable as an expression: "failed: [hello] == [goodbye]" or 
"failed: [5] < [2]"

- The docs should have a section that gives an overview of assertPred by 
using a few simple examples of each of the assertPred uses. The way it is 
now, it's hard to see the overall logic behind the design of the 
assertPred's params since it's spread out all over. For instance:

---------------------------------------------
//Equivalent to assert(4 <= 5);
assertPred!"<="(4, 5);

//Equivalent to assert(1 * 2 == 2);
assertPred!"=="(1 * 2, 2);

//Equivalent to assert(7 + 5 == 12);
assertPred!"+"(7, 5, 12);

/*
   Equivalent to
   void func(T, U)(T lhs, U rhs)
   {
       auto result = lhs = rhs;

       assert(lhs == rhs);
       assert(result == rhs);
   }

   func(5, 7);
 */
assertPred!"opAssign"(5, 7);

/*
   Equivalent to
   void func(T, U, E)(T lhs, U rhs, E expected)
   {
       auto result = lhs += rhs;

       assert(lhs == expected);
       assert(result == expected);
   }

   func(5, 7, 12);
*/
assertPred!"-="(7, 5, 2);

assertPred!"*="(7, 5, 35);

//Equivalent to assert("hello " ~ "world" == "hello world");
assertPred!"~"("hello ", "world", "hello world");

//Equivalent to assert(1 == 1);
assertPred!"a == 1"(1);

//Equivalent to assert(2 * 3.0 == 6);
assertPred!"a * 3.0 == 6.0"(2);

//Equivalent to assert(42 == 42);
assertPred!"a == b"(42, 42);

//Equivalent to assert("hello " ~ "world" == "hello world");
assertPred!`a ~ b == "hello world"`("hello ", "world");

//Equivalent to assert((int[] range, int i){return canFind(range, i);}([1, 
5, 7, 2], 7));
assertPred!((int[] range, int i){return canFind(range, i);})([1, 5, 7, 2], 
7);

//Equivalent to assert((int a, int b, int c){return a == b && b == c;}(5, 5, 
5));
assertPred!((int a, int b, int c){return a == b && b == c;})(5, 5, 5);

struct IntWrapper
{
    int value;

    int opCmp(const ref IntWrapper rhs) const
    {
        if(value < rhs.value)
            return -1;
        else if(value > rhs.value)
            return 1;

        return 0;
    }

    string toString()
    {
        return to!string(value);
    }
}

// Equivalent to assert(IntWrapper(0).opCmp(IntWrapper(6)) < 0);
assertPred!("opCmp", "<")(IntWrapper(0), IntWrapper(6));

// Equivalent to assert(IntWrapper(42).opCmp(IntWrapper(42)) == 0);
assertPred!("opCmp", "==")(IntWrapper(42), IntWrapper(42));
---------------------------------------------

Also notice that I cleaned up a few things, like eliminating the unnecessary 
struct constructor and making a few of the assert equivalents more clear.




More information about the Digitalmars-d mailing list