Skip to content

Commit 84ea263

Browse files
Samat Gaynutdinovantipeon
authored andcommitted
feat: divide tests into error/regression regions
1 parent fc4839d commit 84ea263

File tree

6 files changed

+101
-21
lines changed

6 files changed

+101
-21
lines changed

server/src/Tests.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ using namespace types;
1515
static const string INT64_MIN_STRING =
1616
std::to_string(std::numeric_limits<int64_t>::min());
1717

18-
const string Tests::DEFAULT_SCOPE_NAME = "regression";
19-
const string Tests::ERROR_SCOPE_NAME = "error";
18+
const string Tests::DEFAULT_SUITE_NAME = "regression";
19+
const string Tests::ERROR_SUITE_NAME = "error";
2020

2121
const Tests::MethodParam &tests::Tests::getStdinMethodParam() {
2222
static const Tests::MethodParam stdinMethodParam =
2323
MethodParam(types::Type::CStringType(), types::Type::getStdinParamName(), std::nullopt);
2424
return stdinMethodParam;
2525
}
2626

27+
Tests::MethodDescription::MethodDescription() : suiteTestCases{
28+
{Tests::DEFAULT_SUITE_NAME, std::vector<MethodTestCase>()},
29+
{Tests::ERROR_SUITE_NAME, std::vector<MethodTestCase>()}
30+
}, codeText{
31+
{Tests::DEFAULT_SUITE_NAME, std::string()},
32+
{Tests::ERROR_SUITE_NAME, std::string()}
33+
} {}
34+
2735
static string makeDecimalConstant(string value, const string &typeName) {
2836
if (typeName == "long") {
2937
if (value == INT64_MIN_STRING) {
@@ -516,13 +524,13 @@ void KTestObjectParser::parseKTest(const MethodKtests &batch,
516524
}
517525
}
518526

519-
static string getScopeName(const UTBotKTest::Status &status,
527+
static string getSuiteName(const UTBotKTest::Status &status,
520528
const shared_ptr<LineInfo> lineInfo) {
521529
bool forAssert = lineInfo != nullptr && lineInfo->forAssert;
522530
if (status == UTBotKTest::Status::FAILED || forAssert) {
523-
return Tests::ERROR_SCOPE_NAME;
531+
return Tests::ERROR_SUITE_NAME;
524532
}
525-
return Tests::DEFAULT_SCOPE_NAME;
533+
return Tests::DEFAULT_SUITE_NAME;
526534
}
527535

528536
int KTestObjectParser::findFieldIndex(const StructInfo &structInfo, unsigned int offset) {
@@ -671,8 +679,8 @@ void KTestObjectParser::parseTestCases(const UTBotKTestList &cases,
671679
for (const auto &case_ : cases) {
672680
std::stringstream traceStream;
673681
traceStream << "Test case #" << (++caseCounter) << ":\n";
674-
string scopeName = getScopeName(case_.status, lineInfo);
675-
Tests::MethodTestCase testCase{ scopeName };
682+
string suiteName = getSuiteName(case_.status, lineInfo);
683+
Tests::MethodTestCase testCase{ suiteName };
676684
vector<Tests::TestCaseParamValue> paramValues;
677685

678686
Tests::TestCaseDescription testCaseDescription;
@@ -734,6 +742,7 @@ void KTestObjectParser::parseTestCases(const UTBotKTestList &cases,
734742
assignTypeStubVar(testCase, methodDescription);
735743

736744
methodDescription.testCases.push_back(testCase);
745+
methodDescription.suiteTestCases[testCase.suiteName].push_back(testCase);
737746
}
738747
}
739748

@@ -1124,6 +1133,6 @@ bool isUnnamed(char *name) {
11241133
}
11251134

11261135
bool Tests::MethodTestCase::isError() const {
1127-
return scopeName == ERROR_SCOPE_NAME;
1136+
return suiteName == ERROR_SUITE_NAME;
11281137
}
11291138
}

server/src/Tests.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ namespace tests {
338338
};
339339

340340
struct TestCaseDescription {
341-
string scopeName;
341+
string suiteName;
342342

343343
vector<TestCaseParamValue> globalPreValues;
344344
vector<TestCaseParamValue> globalPostValues;
@@ -361,7 +361,7 @@ namespace tests {
361361
};
362362

363363
struct MethodTestCase {
364-
string scopeName;
364+
string suiteName;
365365

366366
vector<TestCaseParamValue> globalPreValues;
367367
vector<TestCaseParamValue> globalPostValues;
@@ -396,7 +396,8 @@ namespace tests {
396396
struct MethodDescription {
397397
std::optional<MethodParam> classObj;
398398
std::string name;
399-
std::string code;
399+
typedef std::unordered_map<string, string> SuiteNameToCodeTextMap;
400+
SuiteNameToCodeTextMap codeText;
400401
std::string paramsString;
401402

402403
types::Type returnType;
@@ -411,9 +412,13 @@ namespace tests {
411412
typedef std::unordered_map<string, std::shared_ptr<types::FunctionInfo>> FPointerMap;
412413
FPointerMap functionPointers;
413414
vector<MethodTestCase> testCases;
415+
typedef std::unordered_map<string, vector<MethodTestCase>> SuiteNameToTestCasesMap;
416+
SuiteNameToTestCasesMap suiteTestCases;
414417

415418
bool operator==(const MethodDescription &other) const;
416419

420+
MethodDescription();
421+
417422
[[nodiscard]] vector<types::Type> getParamTypes() const {
418423
return CollectionUtils::transform(params, [](auto const& param) {
419424
return param.type;
@@ -484,8 +489,8 @@ namespace tests {
484489

485490
using MethodsMap = tsl::ordered_map<std::string, MethodDescription>;
486491

487-
static const string DEFAULT_SCOPE_NAME;
488-
static const string ERROR_SCOPE_NAME;
492+
static const string DEFAULT_SUITE_NAME;
493+
static const string ERROR_SUITE_NAME;
489494
static const MethodParam &getStdinMethodParam();
490495

491496
fs::path sourceFilePath;

server/src/printers/TestsPrinter.cpp

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,26 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
5555
}
5656
writeStubsForStructureFields(tests);
5757
ss << NL;
58-
for (const auto &[methodName, methodStub] : tests.methods) {
59-
if (methodStub.code.empty()) {
58+
printSuite(Tests::DEFAULT_SUITE_NAME, tests.methods);
59+
printSuite(Tests::ERROR_SUITE_NAME, tests.methods);
60+
ss << RB();
61+
tests.code = ss.str();
62+
}
63+
64+
void TestsPrinter::printSuite(const string &suiteName, const Tests::MethodsMap &methods) {
65+
if (std::all_of(methods.begin(), methods.end(), [&suiteName](const auto& method) {
66+
return method.second.codeText.at(suiteName).empty();
67+
})) {
68+
return;
69+
}
70+
ss << "#pragma region " << suiteName << NL;
71+
for (const auto &[methodName, methodStub] : methods) {
72+
if (methodStub.codeText.at(suiteName).empty()) {
6073
continue;
6174
}
62-
ss << methodStub.code;
75+
ss << methodStub.codeText.at(suiteName);
6376
}
64-
ss << RB();
65-
tests.code = ss.str();
77+
ss << "#pragma endregion" << NL;
6678
}
6779

6880
void TestsPrinter::genCode(Tests::MethodDescription &methodDescription,
@@ -78,17 +90,42 @@ void TestsPrinter::genCode(Tests::MethodDescription &methodDescription,
7890

7991
writeStubsForFunctionParams(typesHandler, methodDescription, false);
8092
writeExternForSymbolicStubs(methodDescription);
81-
for (auto &testCase : methodDescription.testCases) {
93+
94+
genCodeBySuiteName(Tests::DEFAULT_SUITE_NAME,
95+
methodDescription,
96+
predicateInfo,
97+
verbose,
98+
testNum);
99+
resetStream();
100+
genCodeBySuiteName(Tests::ERROR_SUITE_NAME,
101+
methodDescription,
102+
predicateInfo,
103+
verbose,
104+
testNum);
105+
resetStream();
106+
}
107+
108+
void TestsPrinter::genCodeBySuiteName(const string &targetSuiteName,
109+
Tests::MethodDescription &methodDescription,
110+
const std::optional<LineInfo::PredicateInfo>& predicateInfo,
111+
bool verbose,
112+
int& testNum) {
113+
const auto& testCases = methodDescription.suiteTestCases[targetSuiteName];
114+
if (testCases.empty()) {
115+
return;
116+
}
117+
for (auto &testCase : testCases) {
82118
testNum++;
83-
testHeader(testCase.scopeName, methodDescription, testNum);
119+
testHeader(testCase.suiteName, methodDescription, testNum);
84120
redirectStdin(methodDescription, testCase, verbose);
85121
if (verbose) {
86122
genVerboseTestCase(methodDescription, testCase, predicateInfo);
87123
} else {
88124
genParametrizedTestCase(methodDescription, testCase, predicateInfo);
89125
}
90126
}
91-
methodDescription.code = ss.str();
127+
128+
methodDescription.codeText[targetSuiteName] = ss.str();
92129
}
93130

94131
void TestsPrinter::genVerboseTestCase(const Tests::MethodDescription &methodDescription,

server/src/printers/TestsPrinter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ namespace printer {
146146
const Tests::MethodTestCase &testCase);
147147

148148
static Tests::MethodParam getValueParam(const Tests::MethodParam &param);
149+
150+
void genCodeBySuiteName(const string &targetSuiteName,
151+
Tests::MethodDescription &methodDescription,
152+
const std::optional<LineInfo::PredicateInfo>& predicateInfo,
153+
bool verbose,
154+
int& testNum);
155+
156+
void printSuite(const string &suiteName, const Tests::MethodsMap &methods);
149157
};
150158
}
151159
#endif // UNITTESTBOT_TESTSPRINTER_H

server/test/framework/Server_Tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,16 @@ namespace {
828828
checkFloatingPointPlain_C(testGen);
829829
}
830830

831+
TEST_F(Server_Test, Correct_CodeText_For_Regression) {
832+
auto [testGen, status] = performFeatureFileTestsRequest(floating_point_plain_c);
833+
const string code = testGen.tests.begin()->second.code;
834+
const string beginRegressionRegion = "#pragma region " + Tests::DEFAULT_SUITE_NAME + NL;
835+
const string endRegion = std::string("#pragma endregion") + NL;
836+
const string beginErrorRegion = "#pragma region " + Tests::ERROR_SUITE_NAME + NL;
837+
ASSERT_TRUE(code.find(beginRegressionRegion) != std::string::npos) << "No regression begin region";
838+
ASSERT_TRUE(code.find(endRegion) != std::string::npos) << "No regression end region";
839+
}
840+
831841
TEST_F(Server_Test, Linkage) {
832842
auto [testGen, status] = performFeatureFileTestsRequest(linkage_c);
833843
ASSERT_TRUE(status.ok()) << status.error_message();

server/test/framework/Syntax_Tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,17 @@ namespace {
12951295
);
12961296
}
12971297

1298+
TEST_F(Syntax_Test, Correct_CodeText_For_Regression_And_Error) {
1299+
auto [testGen, status] = createTestForFunction(linked_list_c, 3);
1300+
const string code = testGen.tests.begin()->second.code;
1301+
const string beginRegressionRegion = "#pragma region " + Tests::DEFAULT_SUITE_NAME + NL;
1302+
const string endRegion = std::string("#pragma endregion") + NL;
1303+
const string beginErrorRegion = "#pragma region " + Tests::ERROR_SUITE_NAME + NL;
1304+
ASSERT_TRUE(code.find(beginRegressionRegion) != std::string::npos) << "No regression begin region";
1305+
ASSERT_TRUE(code.find(endRegion) != std::string::npos) << "No regression end region";
1306+
ASSERT_TRUE(code.find(beginErrorRegion) != std::string::npos) << "No error begin region";
1307+
}
1308+
12981309
TEST_F(Syntax_Test, Function_Pointers_StructFieldParam) {
12991310
auto [testGen, status] = createTestForFunction(functions_as_params_c, 52);
13001311

0 commit comments

Comments
 (0)