Skip to content

Commit 336cb2d

Browse files
elenaboldyrevaLana243
authored andcommitted
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 8635b51 commit 336cb2d

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

server/src/printers/TestsPrinter.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,10 @@ void TestsPrinter::genVerboseTestCase(const Tests::MethodDescription &methodDesc
248248
TestsPrinter::verboseFunctionCall(methodDescription, testCase);
249249
markTestedFunctionCallIfNeed(methodDescription.name, testCase);
250250

251-
ss << NL;
252251
if (testCase.isError()) {
253-
ss << LINE_INDENT()
254-
<< "FAIL() << \"Unreachable point. "
255-
"Function was supposed to fail, but actually completed successfully.\""
256-
<< SCNL;
252+
printFailAssertion();
257253
} else {
254+
ss << NL;
258255
TestsPrinter::verboseAsserts(methodDescription, testCase, predicateInfo);
259256
}
260257
ss << RB() << NL;
@@ -677,6 +674,8 @@ void TestsPrinter::parametrizedAsserts(const Tests::MethodDescription &methodDes
677674
globalParamsAsserts(methodDescription, testCase);
678675
classAsserts(methodDescription, testCase);
679676
changeableParamsAsserts(methodDescription, testCase);
677+
} else {
678+
printFailAssertion();
680679
}
681680
}
682681

@@ -773,6 +772,15 @@ void printer::TestsPrinter::parametrizedInitializeSymbolicStubs(const Tests::Met
773772
}
774773
}
775774

775+
void TestsPrinter::printFailAssertion() {
776+
ss << NL;
777+
ss << LINE_INDENT()
778+
<< "FAIL() << \"Unreachable point or the function was supposed to fail, but \"\n"
779+
<< LINE_INDENT() << LINE_INDENT()
780+
<< "\"actually completed successfully. See the SARIF report for details.\"";
781+
ss << SCNL;
782+
}
783+
776784
std::string printer::MultiLinePrinter::print(TestsPrinter *printer,
777785
const tests::StructValueView *view) {
778786
auto subViews = view->getSubViews();

server/src/printers/TestsPrinter.h

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 5 deletions
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)