Skip to content

Commit 1bc27f7

Browse files
TestsPrinters.cpp: add FAIL() assertion to error tests.
Add FAIL() assertion to generated tests in no verbose mode. The line with FAIL() assertion is unreachable in some tests but it gives additional information to the user to make the tests easier to read. I add a unit test to check that the code of generated tests (error suit) has the line with FAIL() assertion. I decided not to use statuses from the tests execution because the line with FAIL() assertion can be unreachable. Refs: #472
1 parent 2de5b52 commit 1bc27f7

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

server/src/printers/TestsPrinter.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,10 @@ void TestsPrinter::genVerboseTestCase(const Tests::MethodDescription &methodDesc
238238
TestsPrinter::verboseFunctionCall(methodDescription, testCase);
239239
markTestedFunctionCallIfNeed(methodDescription.name, testCase);
240240

241-
ss << NL;
242241
if (testCase.isError()) {
243-
ss << LINE_INDENT()
244-
<< "FAIL() << \"Unreachable point. "
245-
"Function was supposed to fail, but actually completed successfully.\""
246-
<< SCNL;
242+
printFailAssertion();
247243
} else {
244+
ss << NL;
248245
TestsPrinter::verboseAsserts(methodDescription, testCase, predicateInfo);
249246
}
250247
ss << RB() << NL;
@@ -667,6 +664,8 @@ void TestsPrinter::parametrizedAsserts(const Tests::MethodDescription &methodDes
667664
globalParamsAsserts(methodDescription, testCase);
668665
classAsserts(methodDescription, testCase);
669666
changeableParamsAsserts(methodDescription, testCase);
667+
} else {
668+
printFailAssertion();
670669
}
671670
}
672671

@@ -763,6 +762,15 @@ void printer::TestsPrinter::parametrizedInitializeSymbolicStubs(const Tests::Met
763762
}
764763
}
765764

765+
void TestsPrinter::printFailAssertion() {
766+
ss << NL;
767+
ss << LINE_INDENT()
768+
<< "FAIL() << \"Unreachable point or the function was supposed to fail, but \"\n"
769+
<< LINE_INDENT() << LINE_INDENT()
770+
<< "\"actually completed successfully. See the SARIF report for details.\"";
771+
ss << SCNL;
772+
}
773+
766774
std::string printer::MultiLinePrinter::print(TestsPrinter *printer,
767775
const tests::StructValueView *view) {
768776
auto subViews = view->getSubViews();

server/src/printers/TestsPrinter.h

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ namespace printer {
158158
int &testNum);
159159

160160
std::uint32_t printSuiteAndReturnMethodsCount(const std::string &suiteName, const Tests::MethodsMap &methods);
161+
162+
void printFailAssertion();
161163
};
162164
}
163165
#endif // UNITTESTBOT_TESTSPRINTER_H

server/test/framework/Syntax_Tests.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#include "KleeGenerator.h"
55
#include "Server.h"
66
#include "TestUtils.h"
7-
#include "streams/coverage/ServerCoverageAndResultsWriter.h"
7+
#include "Tests.h"
88
#include "coverage/CoverageAndResultsGenerator.h"
9-
10-
#include "utils/path/FileSystemPath.h"
11-
#include "utils/StringUtils.h"
9+
#include "gmock/gmock.h"
10+
#include "streams/coverage/ServerCoverageAndResultsWriter.h"
1211
#include "utils/SizeUtils.h"
13-
#include "Tests.h"
12+
#include "utils/StringUtils.h"
13+
#include "utils/path/FileSystemPath.h"
14+
1415
#include <functional>
1516

1617
namespace {
@@ -1337,6 +1338,18 @@ namespace {
13371338
ASSERT_TRUE(status.ok()) << status.error_message();
13381339
}
13391340

1341+
TEST_F(Syntax_Test, Check_Error_Tests_Have_Fail_Assertion) {
1342+
auto [testGen, status] = createTestForFunction(structs_with_pointers_c, 111);
1343+
1344+
ASSERT_TRUE(status.ok()) << status.error_message();
1345+
1346+
for (const auto &[source_file_path, tests] : testGen.tests) {
1347+
EXPECT_THAT(tests.code,
1348+
::testing::HasSubstr(
1349+
"FAIL() << \"Unreachable point or the function was supposed to fail"));
1350+
}
1351+
}
1352+
13401353
TEST_F(Syntax_Test, Pass_Pointer_To_Const_Struct_With_Pointer) {
13411354
auto [testGen, status] = createTestForFunction(structs_with_pointers_c, 115);
13421355

0 commit comments

Comments
 (0)