Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
94 lines (76 loc) · 3.03 KB

File metadata and controls

94 lines (76 loc) · 3.03 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <gtest/gtest.h>
#include <jsonata/Jsonata.h>
#include <nlohmann/json.hpp>
#include <jsonata/JException.h>
#include <vector>
#include <memory>
namespace jsonata {
class NullSafetyTest : public ::testing::Test {
protected:
void SetUp() override {}
void TearDown() override {}
static nlohmann::ordered_json makeArray(const std::vector<nlohmann::ordered_json>& values) {
nlohmann::ordered_json arr = nlohmann::ordered_json::array();
for (const auto& v : values) arr.push_back(v);
return arr;
}
};
TEST_F(NullSafetyTest, testNullSafety) {
auto r = Jsonata("$sift(undefined, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$each(undefined, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$keys(null)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$map(null, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$filter(null, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$single(null, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$reduce(null, $uppercase)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$lookup(null, 'anykey')").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
r = Jsonata("$spread(null)").evaluate(nullptr);
EXPECT_TRUE(r.is_null());
}
TEST_F(NullSafetyTest, testFilterNull) {
auto arrayData = makeArray({1, nullptr});
Jsonata expr("$filter($, function($v, $i, $a){$v})");
auto result = expr.evaluate(arrayData);
ASSERT_TRUE(result.is_number());
EXPECT_EQ(result.get<int>(), 1);
}
TEST_F(NullSafetyTest, testNotNull) {
auto result = Jsonata("$not($)").evaluate(nullptr);
EXPECT_TRUE(result.is_null());
}
TEST_F(NullSafetyTest, testSingleNull) {
auto arrayData = makeArray({nullptr, 1});
Jsonata expr("$single($, function($v, $i, $a){ $v })");
auto result = expr.evaluate(arrayData);
ASSERT_TRUE(result.is_number());
EXPECT_EQ(result.get<int>(), 1);
}
TEST_F(NullSafetyTest, testArrayIndexPreservesNull) {
// Indexing into an array element that is JSON null must yield null,
// not be filtered out as if it were undefined.
auto data = nlohmann::ordered_json::parse(
R"({"data": [[1, null, 3], [2, null, 4], [3, null, 5]]})");
auto result = Jsonata("[$map(data, function($row) { $row[1] })]").evaluate(data);
auto expected = nlohmann::ordered_json::parse("[null, null, null]");
EXPECT_EQ(result, expected);
}
TEST_F(NullSafetyTest, testFilterNullLookup) {
nlohmann::ordered_json arrayData = nlohmann::ordered_json::array({
nlohmann::ordered_json::object({{"content", "some"}}),
nlohmann::ordered_json::object()
});
Jsonata expr("$filter($, function($v, $i, $a){$lookup($v, 'content')})");
auto result = expr.evaluate(arrayData);
ASSERT_TRUE(result.is_object());
ASSERT_TRUE(result.contains("content"));
EXPECT_EQ(result["content"].get<std::string>(), "some");
}
} // namespace jsonata
Morty Proxy This is a proxified and sanitized view of the page, visit original site.