Skip to content

Commit 31cfd89

Browse files
Gabriel KimGabriel Kim
Gabriel Kim
authored and
Gabriel Kim
committed
Add tests for AddOperation and RmOperation
1 parent 8fbf99f commit 31cfd89

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/json_logic.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ namespace json_logic
9292
values = json::array({values});
9393
}
9494

95+
/**
96+
* In JsonLogic, operation names are treated as keywords and will result in one of the known operations being
97+
* invoked with the provided logic and data objects. Logic objects with unrecognized operations are treated as
98+
* "useless statements" that just returns its own values, such as:
99+
*
100+
* myLogic; // Returns myLogic.
101+
*
102+
* This is an important behaviour for the operation_array_merge, which can receive arbitrary JSON objects in its
103+
* parameters and is expected to just return them as elements of a flattened array.
104+
*/
95105
const auto operation_it = operations_.find(op);
96106

97107
if (operation_it != operations_.end())

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TEST_TARGET ${PROJECT_NAME}_test)
55
add_executable(
66
${TEST_TARGET}
77

8+
add_rm_operation.cpp
89
operations/array/all.cpp
910
operations/array/filter.cpp
1011
operations/array/in.cpp

test/add_rm_operation.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
3+
#include "gtest/gtest.h"
4+
#include "json.hpp"
5+
6+
#include "fixture.h"
7+
8+
using namespace nlohmann;
9+
10+
class AddRmOperation : public testing::Test
11+
{
12+
protected:
13+
JsonLogic* json_logic_;
14+
15+
void SetUp() override
16+
{
17+
json_logic_ = JsonLogic::GetInstance();
18+
19+
json_logic_->AddOperation("myOperation", [this](const json& values, const json& data) {
20+
const double a = json_logic_->Apply(values.at(0), data);
21+
const double b = json_logic_->Apply(values.at(1), data);
22+
23+
return a + b;
24+
});
25+
}
26+
};
27+
28+
TEST_F(AddRmOperation, AddOperationPrimitive) {
29+
const auto logic = R"(
30+
{
31+
"myOperation": [1, 2]
32+
}
33+
)"_json;
34+
35+
const auto result = json_logic_->Apply(logic);
36+
37+
EXPECT_EQ(result, 3);
38+
}
39+
40+
TEST_F(AddRmOperation, AddOperationData)
41+
{
42+
const auto logic = R"(
43+
{
44+
"myOperation": [
45+
{ "var": ["foo.bar"] },
46+
{ "var": ["foo.baz"] }
47+
]
48+
}
49+
)"_json;
50+
51+
const auto data = R"(
52+
{
53+
"foo": {
54+
"bar": 1,
55+
"baz": 2
56+
}
57+
}
58+
)"_json;
59+
60+
const auto result = json_logic_->Apply(logic, data);
61+
62+
EXPECT_EQ(result, 3);
63+
}
64+
65+
TEST_F(AddRmOperation, RmOperation)
66+
{
67+
json_logic_->RmOperation("myOperation");
68+
69+
const auto logic = R"(
70+
{
71+
"myOperation": [1, 2]
72+
}
73+
)"_json;
74+
75+
const auto result = json_logic_->Apply(logic);
76+
77+
EXPECT_EQ(result, logic);
78+
}

0 commit comments

Comments
 (0)