Skip to content

Commit d904680

Browse files
committed
fix(test_display_expression): clearer test cases
1 parent 438f391 commit d904680

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

tests/test_display_expression.cpp

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,58 @@
22
#include <iostream>
33
#include <metricq/json.hpp>
44

5+
struct TestDisplayExpression
6+
{
7+
nlohmann::json input;
8+
std::string expected;
9+
};
10+
511
int main()
612
{
713
try
814
{
9-
// Input test data
10-
nlohmann::json data = {
11-
{ "testCases",
12-
{ { { "expression",
13-
{ { "operation", "*" },
14-
{ "left", 5 },
15-
{ "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } },
16-
{ { "expression",
17-
{ { "operation", "*" },
18-
{ "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } },
19-
{ "right",
20-
{ { "operation", "-" },
21-
{ "left", 10 },
22-
{ "right", "dummy.source" } } } } } },
23-
{ { "expression",
24-
{ { "operation", "-" },
25-
{ "left",
26-
{ { "operation", "+" },
27-
{ "left", 15.3 },
28-
{ "right",
29-
{ { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } },
30-
{ "right",
31-
{ { "operation", "throttle" },
32-
{ "cooldown_period", "42" },
33-
{ "input", 8 } } } } } } } }
34-
};
15+
std::vector<TestDisplayExpression> testCases;
16+
17+
testCases.emplace_back(TestDisplayExpression{
18+
{ { "expression",
19+
{ { "operation", "*" },
20+
{ "left", 5 },
21+
{ "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } },
22+
"(5 * (45 - 3))" });
23+
24+
testCases.emplace_back(TestDisplayExpression{
25+
{ { "expression",
26+
{ { "operation", "*" },
27+
{ "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } },
28+
{ "right",
29+
{ { "operation", "-" }, { "left", 10 }, { "right", "dummy.source" } } } } } },
30+
"((1 + 2) * (10 - dummy.source))" });
3531

36-
// Expected outputs
37-
std::vector<std::string> expected = { "(5 * (45 - 3))", "((1 + 2) * (10 - dummy.source))",
38-
"((15.300000 + min[42, 24, 8, 12]) - 8)" };
32+
testCases.emplace_back(TestDisplayExpression{
33+
{ { "expression",
34+
{ { "operation", "-" },
35+
{ "left",
36+
{ { "operation", "+" },
37+
{ "left", 15.3 },
38+
{ "right", { { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } },
39+
{ "right",
40+
{ { "operation", "throttle" },
41+
{ "cooldown_period", "42" },
42+
{ "input", 8 } } } } } },
43+
"((15.300000 + min[42, 24, 8, 12]) - 8)" });
3944

4045
// Test the cases
41-
const auto& testCases = data["testCases"];
42-
for (size_t i = 0; i < testCases.size(); ++i)
46+
for (const auto& testCase : testCases)
4347
{
44-
const auto& testCase = testCases[i];
45-
const auto& expression = testCase["expression"];
4648

47-
// Call the function to display the expression
49+
auto expression = testCase.input["expression"];
4850
std::string result = Combinator::displayExpression(expression);
4951

5052
// Compare with expected result
51-
if (result != expected[i])
53+
if (result != testCase.expected) // comparing with the expected output
5254
{
53-
std::cerr << "Test case " << i + 1 << " failed:\n";
54-
std::cerr << "Expected: " << expected[i] << "\n";
55+
std::cerr << "Test case " << testCase.input << " failed:\n";
56+
std::cerr << "Expected: " << testCase.expected << "\n";
5557
std::cerr << "Got: " << result << "\n";
5658
return 1;
5759
}

0 commit comments

Comments
 (0)