malsyned / pfstest

Professional Firmware Services Unit Test Framework
Other
1 stars 0 forks source link

Value and matcher tests are too tightly coupled to assert_that's output format #44

Closed malsyned closed 6 years ago

malsyned commented 6 years ago

Every test for failure in test-values-and-matchers.c assumes that assert_that outputs text of the form

Failed assertion
Expected: the short -32768
Actual:   the short 32767

which means that the text "Failed assertion", "Expected: ", and "Actual: " appears in a string in about half of the tests in values-and-matchers.c. This tight coupling is against the spirit of testing separate units separately.

Since test-assert-that.c already verifies that assert_that works by invoking pfstest_matcher_matches(), pfstest_matcher_print(), and pfstest_value_print(), we can use these API calls directly in test-values-and-matchers.c to make these tests cleaner. For example:

test(the_short_should_print_itself)
{
    const pfstest_pg_ptr char *expected =
        pfstest_pg_str("the short 32767");

    pfstest_value_print(message_spy, the_short(32767));

    assert_that("shorts print themselves",
                the_string(captured_output), matches_the_pg_string(expected));
}

test(the_short_should_print_negative_shorts)
{
    const pfstest_pg_ptr char *expected =
        pfstest_pg_str("the short -32768");

    pfstest_value_print(message_spy, the_short(-32768));

    assert_that("negative shorts print themselves",
                the_string(captured_output), matches_the_pg_string(expected));
}

test(equal_shorts_should_pass)
{
    assert_true("is_the_short matches equal shorts",
                the_bool(pfstest_matcher_matches(is(the_short(32767)),
                                                 the_short(32767))));
}

test(unequal_shorts_should_fail)
{
    assert_false("is_the_short fails unequal shorts",
                 the_bool(pfstest_matcher_matches(is(the_short(32767)),
                                                  the_short(-32768))));
}
malsyned commented 6 years ago

Fixed. Phew.