Simple Unit Tests

version 1/130803 by Dannii Willis

  • Home page
  • Beginning
  • Previous
  • Next



  • Chapter - The assert phrase

    The test assertion count is a number variable.
    The total assertion count is a number variable.
    The assertion failures count is a number variable.

    [ Assert that two values are the same ]
    To assert that/-- (A - a value) is (B - a value):
        stop capturing text;
        increment the test assertion count;
        increment the total assertion count;
        unless A is B:
            increment the assertion failures count;
            say "Failure for test: [the current unit test name], assertion: [the test assertion count]. Expected: [B], Got: [A][line break]";
        start capturing text;

    [ Assert that any condition is true, but with less information on failure ]
    To assert that/-- (C - a condition):
        (- Assert_Condition({C}); -).

    Include (-
    [ Assert_Condition C;
        EndCapture();
        (+ the test assertion count +)++;
        (+ the total assertion count +)++;
        if (~~(C))
        {
            (+ the assertion failures count +)++;
            print "Failure for test: ";
            print (INDEXED_TEXT_TY_Say) (+ the current unit test name +);
            print ", assertion: ", (+ the test assertion count +), ". (Asserted condition is false)^";
        }
        StartCapture();
    ];
    -).