I am doing unit testing in C++ using a some simple macros that I’ve written myself. I would like create a new grammar which specializes the default C++ grammar to add scopes for my test macros. The reason is that I want to provide different syntax highlighting for the test macros which emphasizes the content of the assertion rather than the “ASSERT_EQUALS” text (for example I might dull the color of the “ASSERT_EQUALS” text or mark it with a special color).
The test files follow a simple shape:
// Example.test.cpp
// Other C++ code
TEST(MyTestFunction) {
// Other C++ code
ASSERT_EQUALS(1,2); // will fail
}
Where the macro “TEST” expands to function definition (among other things), and the macro ASSERT_EQUALS is self explanatory.
The issue I’m having is that I can’t extend the C/C++ grammar definition for a function block to override the meaning of ASSERT_EQUALS in the context of TEST methods but still support all the normal C++ syntax highlighting for everything else in the test method. Is there a way to do this?
Here is what I have tried so far (which hopefully illustrates what I’m trying to do). It works for TEST and ASSERT_EQUALS and for normal C++ syntax outside of TEST functions, but does not work for normal C++ syntax inside TEST functions.
{
"name": "SimpleTesting test file",
"scopeName": "source.c++.tests",
"fileTypes": "tests.cpp"],
"uuid": "bbfa5657-536d-4c19-83cd-2f0213dfe703",
"patterns":
{
"name": "meta.block.c++.test",
"begin": "TEST\\s*\\(\\s*[A-Za-z_][A-Za-z0-9_]*\\s*\\)\\s*\\{",
"beginCaptures": {
"0": { "name": "meta.function.test.head.c++" }
},
"end": "\\}",
"patterns":
{
"match": "\\bASSERT[A-Za-z0-9_]*\\s*\\(.*?\\)",
"name": "meta.function-call.assert.c++"
},
{
"comment": "I'm trying to import all the normal c stuff here, but it doesn't work",
"include": "meta.function.c"
},
{
"comment": "I'm trying to import all the normal c stuff here, but it doesn't work",
"include": "meta.block.c"
}
]
},
{
"include": "source.c++"
}
]
}