Skip to content

Add REQUIRE macro for stopping tests early (UNITTEST_NO_EXCEPTIONS support) #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 24, 2016
Merged

Add REQUIRE macro for stopping tests early (UNITTEST_NO_EXCEPTIONS support) #97

merged 15 commits into from
Feb 24, 2016

Conversation

ohz10
Copy link
Contributor

@ohz10 ohz10 commented Feb 9, 2016

This is a add on to Pull Request #95, which adds a REQUIRE macro for stopping tests early. The code in Pull Request #95 does not support stopping tests early when exceptions are disabled.

This pull requests adds a few commits to add support for stopping tests early when exceptions are disabled via std::longjmp.

ohz10 and others added 15 commits February 6, 2016 09:28
test when an assert fails.

The following macro has been added:

	REQUIRE

An example of when these type of checks are useful:

    std::vector<int> v = foo();

    REQUIRE(CHECK_EQUAL(3, v.size())); // test stops here on a fail
                                       // so we don't segfault looking at
                                       // v[0] below.
    CHECK_EQUAL(1, v[0]);
    CHECK_EQUAL(2, v[1]);
    CHECK_EQUAL(3, v[2]);

Multiple checks are supported as follows:

    REQUIRE({
        CHECK_EQUAL(1, 2);
        CHECK(true);
    };

In the multiple check scenario, all the checks in the REQUIRE block will
be run. After which, if any failures were reported, the TEST case will
be stopped.

When UNITTEST_NO_EXCEPTIONS is defined, REQUIRE is a noop.
I changed the definition of the REQUIRE macro to use for loops and
some comma operator shenanigans to allow things like:

    REQUIRE
    {
      CHECK(...);
      CHECK_EQUAL(..., ...);
    }

or

    REQUIRE CHECK(...);

I updated the tests and they all passed on my machine. My only
concern is that some compilers might complain about the unreachable
code in the (throw UnitTest::AssertException(), true) expression.
(1) unreachable code in for loop shenanigans is eliminated.
(2) code after a failing REQUIRE check no longer executes. Used a
    decorating TestReporter to achive this.
Rather than re-using AssertException, it felt more correct to
create a new special-purpose exception.
Was able to remove ThrowingTestReporter::SetDecorated and
::GetDecorated by changing RequiredCheckTestReporter to accept
its TestResults by reference. This simple change removed
several if-checks and some functions.
Visual Studio 2015 complained about calling UT_THROW with zero arguments.
Visual Studio 6 complained about calling UT_CATCH with an empty second
argument. For these cases, I added UT_RETHROW(ExceptionName).

I also added a catch of RequiredCheckException to ExecuteTest to avoid
two error messages on each failed REQUIRE check.
…ceptionsOn.cpp, testing REQUIRE with exceptions turned off will require a significantly different approach.
…s defined, the definition for REQUIRE macro is empty.

This commit adds functionality for stopping unit tests early when UNITTEST_NO_EXCEPTIONS is defined via std::longjmp.
@pjohnmeyer pjohnmeyer merged commit de38509 into unittest-cpp:master Feb 24, 2016
@pjohnmeyer pjohnmeyer added this to the 1.6.0 milestone Feb 24, 2016
@pjohnmeyer pjohnmeyer self-assigned this Feb 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants