
Matthias Schabel wrote:
Naturally, for compile-time units there are some cases where compile- time failure is the correct behavior - how should I structure tests so that this is recognized? Presumably I need to have a separate test source file for each expected compilation error, right?
Hi Matthias, I'm using the following little program: $ cat failcheck.cpp ... int main() { // Stuff that needs to compile // or declarations needed ... // Errors to be detected, one in each clause #ifdef ERROR_A01 // no matching function for call ... #elif ERROR_A02 // constructor is private ... #elif ERROR_A03 // <whatever, comment will be printed during test> ... #endif } $ Then the following little script: $ cat failcheck.sh echo echo "trying without error emulation" g++ -Wall -DDEBUG -o failcheck failcheck.cpp || { echo; echo "*** ERROR: compilation doesn't succeeded at all ***"; echo; kill $$; exit 1; } cat failcheck.cpp \ | grep -e '^#ifdef' -e '^#elif' \ | awk '$2~/ERROR/{$1=""; print}' \ | while read label comment do echo echo "trying $label: $comment" g++ -Wall -D${label} -DDEBUG -o failcheck failcheck.cpp && { echo; echo "*** ERROR: compilation succeeded for $label ***"; echo; kill $$; exit 1; } done echo echo "*** FAILCHECK OK ***" echo $ The script scans the sample programme for test cases and tries to compile it with one test activate at a time. If a compilation succeeds, the test is aborted with an error message. (Note the kill command! Piping into a while loop causes the loop body to be executed in a subshell, so just exiting wouldn't be enough, you need to kill the parent process.) You don't need a different source files for each error, just a different clause. Make sure, however, that there's only one error per clause and that each label occurs only once. (The script should probably check this, maybe in the next version :-) Andreas