Skip to content

Commit 2150da0

Browse files
committed
add(tests): test cases for displayExpression
1 parent beffc34 commit 2150da0

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

tests/CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
add_executable(metricq-combinator.test_single_derivation test_single_derivation.cpp)
2-
add_test(metricq-combinator.test_single_derivation metricq-combinator.test_single_derivation)
1+
macro(add_test_case test_name)
2+
add_executable(${test_name} ${test_name}.cpp)
3+
add_test(${test_name} ${test_name})
4+
target_link_libraries(${test_name} PRIVATE metricq-combinator-lib)
5+
endmacro()
36

4-
target_link_libraries(
5-
metricq-combinator.test_single_derivation
6-
PRIVATE
7-
metricq-combinator-lib
8-
)
7+
add_test_case(test_single_derivation)
8+
add_test_case(test_display_expression)

tests/test_display_expression.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "../src/combinator.hpp"
2+
#include <iostream>
3+
#include <metricq/json.hpp>
4+
5+
int main()
6+
{
7+
try
8+
{
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+
};
35+
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)" };
39+
40+
// Test the cases
41+
const auto& testCases = data["testCases"];
42+
for (size_t i = 0; i < testCases.size(); ++i)
43+
{
44+
const auto& testCase = testCases[i];
45+
const auto& expression = testCase["expression"];
46+
47+
// Call the function to display the expression
48+
std::string result = Combinator::displayExpression(expression);
49+
50+
// Compare with expected result
51+
if (result != expected[i])
52+
{
53+
std::cerr << "Test case " << i + 1 << " failed:\n";
54+
std::cerr << "Expected: " << expected[i] << "\n";
55+
std::cerr << "Got: " << result << "\n";
56+
return 1;
57+
}
58+
}
59+
60+
std::cout << "All test cases passed successfully!" << std::endl;
61+
}
62+
catch (const std::exception& e)
63+
{
64+
std::cerr << "Error: " << e.what() << std::endl;
65+
return 1;
66+
}
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)