martinmoene / lest

A modern, C++11-native, single-file header-only, tiny framework for unit-tests, TDD and BDD (includes C++98 variant)
Boost Software License 1.0
390 stars 45 forks source link

Play nice with clang-format? #54

Open DrChr opened 6 years ago

DrChr commented 6 years ago

Hi,

I tried running clang-format on the test case example at the main page. Unfortunately the result wasn't so nice (see below). I tried a few different settings for clang-format, but I couldn't get it better. Any thoughts on how to improve this?

Note: I guess the problem is due to clang-format not understanding what the CASE macro really represent.

Note: I know I can wrap the test case section with // clang-format off, but then I'm back to manually formatting the white space...

Here's the result after clang-format:

const lest::test specification[] = {
    CASE("Empty string has length zero (succeed)"){
        EXPECT(0 == string().length());
EXPECT(0 == string("").length());
}
,

    CASE("Specific expected exception is reported missing (fail)") {
    EXPECT_THROWS_AS(true, std::runtime_error);
}
,
}
;

This is the original formatting:

const lest::test specification[] =
{
    CASE( "Empty string has length zero (succeed)" )
    {
        EXPECT( 0 == string(  ).length() );
        EXPECT( 0 == string("").length() );
    },

    CASE( "Text compares lexically (fail)" )
    {
        EXPECT( string("hello") > string("world") );
    },
};

Note: I can get the formatting to behave with clang-format by not using the macro CASE, as below. I can also of course replace (lest::env & lestenv) with a macro of my own like lest_TC_ARGS:

const lest::test specification[] = {
    {
        "A test case title, within extra braces",
        [](lest::env &lest_env) {
            EXPECT(0 == string().length());
            EXPECT(0 == string().length());
        },
    },
    "Another test case, now without extra braces and macro for test args",
    [] lest_TC_ARGS {
        EXPECT(0 == string().length());
        EXPECT(0 == string().length());
    },
    {
        "Again within extra braces, and using macro for test args",
        [] lest_TC_ARGS {
            EXPECT(0 == string().length());
            EXPECT(0 == string().length());
        },
    },
};
martinmoene commented 6 years ago

Interesting subject, which I haven't given any thought yet in relation to lest.

Your last example starts looking a bit like bandit ;)