mumuki / cspec

Small BDD framework for C/C++
GNU General Public License v3.0
106 stars 18 forks source link

Idea: Syntax change #39

Closed RaniAgus closed 2 months ago

RaniAgus commented 6 months ago

Most C/C++ code formatters (like clang-format) are not able to understand the {} end syntax, so they end up formatting CSpec code like this:

#include <cspecs/cspec.h>
#include <stdbool.h>
#include <stdio.h>

context(example) {

    describe("Hello world"){

        it("true should be true"){should_bool(true) be equal to(true);
}
end

        it("true shouldn't be false") {
    should_bool(true) not be equal to(false);
}
end

        it("this test will fail because 10 is not equal to 11") {
    should_int(10) be equal to(11);
}
end

        skip("this test will fail because \"Hello\" is not \"Bye\"") {
    should_string("Hello") be equal to("Bye");
}
end
}
end
}

In a custom branch I've tried getting rid of end macro and moving the curly braces inside parenthesis:

#include <cspecs/cspec.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

context(example) {

    describe("Hello world", {
        char *hello;

        before({
            hello = strdup("Hello");
        });

        after({
            free(hello);
        });

        it("true should be true", {
            bool the_truth = true;
            should_bool(the_truth) be equal to(true);
        });

        it("true shouldn't be false", {
            bool the_truth = true;
            should_bool(the_truth) not be equal to(false);
        });

        it("this test will fail because 10 is not equal to 11", {
            int ten = 10;
            should_int(ten) be equal to(11);
        });

        skip("this test will fail because \"Hello\" is not \"Bye\"", {
            should_string(hello) be equal to("Bye");
        });
    });
}

And it indents almost as expected (it doesn't work for single-line blocks, even though I've set every AllowShort*OnASingleLine option to false/Never/None):

#include <cspecs/cspec.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

context(example) {

    describe("Hello world", {
        char *hello;

        before({ hello = strdup("Hello"); });

        after({ free(hello); });

        it("true should be true", {
            bool the_truth = true;
            should_bool(the_truth) be equal to(true);
        });

        it("true shouldn't be false", {
            bool the_truth = true;
            should_bool(the_truth) not be equal to(false);
        });

        it("this test will fail because 10 is not equal to 11", {
            int ten = 10;
            should_int(ten) be equal to(11);
        });

        skip("this test will fail because \"Hello\" is not \"Bye\"", { should_string(hello) be equal to("Bye"); });
    });
}

But, I find this syntax much more easier to understand and use, and also more beginner friendly. What do you think?

RaniAgus commented 2 months ago

Nevermind, I've configured clang-format like this:

ForEachMacros: [describe, it, before, after, skip]
Macros: ["end=/*end*/"]

And it works:

context(example) {

    describe ("Hello world") {

        it ("true should be true") {
            should_bool(true) be equal to(true);
        } end

        it ("true shouldn't be false") {
            should_bool(true) not be equal to(false);
        } end

        it ("this test will fail because 10 is not equal to 11") {
            should_int(10) be equal to(11);
        } end

        skip ("this test will fail because \"Hello\" is not \"Bye\"") {
            should_string("Hello") be equal to("Bye");
        } end
    } end
}