Skip to content

Commit

Permalink
TestsPrinters.cpp: add FAIL() assertion to error tests.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elenaboldyreva committed Nov 3, 2022
1 parent 2de5b52 commit 1bc27f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
18 changes: 13 additions & 5 deletions server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,10 @@ void TestsPrinter::genVerboseTestCase(const Tests::MethodDescription &methodDesc
TestsPrinter::verboseFunctionCall(methodDescription, testCase);
markTestedFunctionCallIfNeed(methodDescription.name, testCase);

ss << NL;
if (testCase.isError()) {
ss << LINE_INDENT()
<< "FAIL() << \"Unreachable point. "
"Function was supposed to fail, but actually completed successfully.\""
<< SCNL;
printFailAssertion();
} else {
ss << NL;
TestsPrinter::verboseAsserts(methodDescription, testCase, predicateInfo);
}
ss << RB() << NL;
Expand Down Expand Up @@ -667,6 +664,8 @@ void TestsPrinter::parametrizedAsserts(const Tests::MethodDescription &methodDes
globalParamsAsserts(methodDescription, testCase);
classAsserts(methodDescription, testCase);
changeableParamsAsserts(methodDescription, testCase);
} else {
printFailAssertion();
}
}

Expand Down Expand Up @@ -763,6 +762,15 @@ void printer::TestsPrinter::parametrizedInitializeSymbolicStubs(const Tests::Met
}
}

void TestsPrinter::printFailAssertion() {
ss << NL;
ss << LINE_INDENT()
<< "FAIL() << \"Unreachable point or the function was supposed to fail, but \"\n"
<< LINE_INDENT() << LINE_INDENT()
<< "\"actually completed successfully. See the SARIF report for details.\"";
ss << SCNL;
}

std::string printer::MultiLinePrinter::print(TestsPrinter *printer,
const tests::StructValueView *view) {
auto subViews = view->getSubViews();
Expand Down
2 changes: 2 additions & 0 deletions server/src/printers/TestsPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ namespace printer {
int &testNum);

std::uint32_t printSuiteAndReturnMethodsCount(const std::string &suiteName, const Tests::MethodsMap &methods);

void printFailAssertion();
};
}
#endif // UNITTESTBOT_TESTSPRINTER_H
23 changes: 18 additions & 5 deletions server/test/framework/Syntax_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include "KleeGenerator.h"
#include "Server.h"
#include "TestUtils.h"
#include "streams/coverage/ServerCoverageAndResultsWriter.h"
#include "Tests.h"
#include "coverage/CoverageAndResultsGenerator.h"

#include "utils/path/FileSystemPath.h"
#include "utils/StringUtils.h"
#include "gmock/gmock.h"
#include "streams/coverage/ServerCoverageAndResultsWriter.h"
#include "utils/SizeUtils.h"
#include "Tests.h"
#include "utils/StringUtils.h"
#include "utils/path/FileSystemPath.h"

#include <functional>

namespace {
Expand Down Expand Up @@ -1337,6 +1338,18 @@ namespace {
ASSERT_TRUE(status.ok()) << status.error_message();
}

TEST_F(Syntax_Test, Check_Error_Tests_Have_Fail_Assertion) {
auto [testGen, status] = createTestForFunction(structs_with_pointers_c, 111);

ASSERT_TRUE(status.ok()) << status.error_message();

for (const auto &[source_file_path, tests] : testGen.tests) {
EXPECT_THAT(tests.code,
::testing::HasSubstr(
"FAIL() << \"Unreachable point or the function was supposed to fail"));
}
}

TEST_F(Syntax_Test, Pass_Pointer_To_Const_Struct_With_Pointer) {
auto [testGen, status] = createTestForFunction(structs_with_pointers_c, 115);

Expand Down

0 comments on commit 1bc27f7

Please sign in to comment.