-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTest.cpp
More file actions
147 lines (130 loc) · 5.64 KB
/
SignatureTest.cpp
File metadata and controls
147 lines (130 loc) · 5.64 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <gtest/gtest.h>
#include <jsonata/Jsonata.h>
#include <nlohmann/json.hpp>
#include <jsonata/JException.h>
#include <memory>
#include <string>
#include <vector>
#include <functional>
namespace jsonata {
class SignatureTest : public ::testing::Test {
protected:
void SetUp() override {
// Setup code if needed
}
void TearDown() override {
// Cleanup code if needed
}
};
TEST_F(SignatureTest, testParametersAreConvertedToArrays) {
Jsonata expr("$greet(1,null,3)");
JFunction greetFn;
greetFn.signature = std::make_shared<utils::Signature>("<a?a?a?a?>", "greet");
greetFn.implementation = [](const Utils::JList& args, const std::any&, std::shared_ptr<Frame>) -> std::any {
auto formatOne = [](const std::any& a) -> std::string {
if (a.type() == typeid(Utils::JList)) {
const auto& arr = std::any_cast<Utils::JList>(a);
if (arr.empty()) return "[null]";
const auto& el = arr[0];
if (!el.has_value()) return "[null]";
if (el.type() == typeid(double)) return "[" + std::to_string(static_cast<int>(std::any_cast<double>(el))) + "]";
if (el.type() == typeid(long long)) return "[" + std::to_string(std::any_cast<long long>(el)) + "]";
if (el.type() == typeid(long)) return "[" + std::to_string(std::any_cast<long>(el)) + "]";
if (el.type() == typeid(int)) return "[" + std::to_string(std::any_cast<int>(el)) + "]";
if (el.type() == typeid(std::nullptr_t)) return "[null]";
}
return "[]";
};
std::string out = "[";
for (size_t i = 0; i < 4; ++i) {
if (i > 0) out += ", ";
out += (i < args.size()) ? formatOne(args[i]) : std::string("[null]");
}
out += "]";
return out;
};
expr.registerFunction("greet", greetFn);
auto result = expr.evaluate(nullptr);
ASSERT_TRUE(result.is_string());
EXPECT_EQ(result.get<std::string>(), "[[1], [null], [3], [null]]");
}
TEST_F(SignatureTest, testError) {
Jsonata expr("$foo()");
JFunction fooFn;
fooFn.signature = std::make_shared<utils::Signature>("(sao)", "foo");
fooFn.implementation = [](const Utils::JList&, const std::any&, std::shared_ptr<Frame>) -> std::any {
return std::any{};
};
expr.registerFunction("foo", fooFn);
EXPECT_THROW(expr.evaluate(nullptr), jsonata::JException);
EXPECT_THROW(expr.evaluate(nlohmann::ordered_json(true)), jsonata::JException);
}
TEST_F(SignatureTest, testVarArg) {
Jsonata expression("$sumvar(1,2,3)");
JFunction sumFn;
sumFn.signature = std::make_shared<utils::Signature>("<n+:n>", "sumvar");
sumFn.implementation = [](const Utils::JList& args, const std::any&, std::shared_ptr<Frame>) -> std::any {
long long sum = 0;
for (const auto& a : args) {
if (!a.has_value()) continue;
if (a.type() == typeid(double)) sum += static_cast<long long>(std::any_cast<double>(a));
else if (a.type() == typeid(long long)) sum += std::any_cast<long long>(a);
else if (a.type() == typeid(long)) sum += std::any_cast<long>(a);
else if (a.type() == typeid(int)) sum += std::any_cast<int>(a);
}
return sum;
};
expression.registerFunction("sumvar", sumFn);
auto result = expression.evaluate(nullptr);
ASSERT_TRUE(result.is_number_integer());
EXPECT_EQ(result.get<long long>(), 6);
}
TEST_F(SignatureTest, testVarArgMany) {
Jsonata expr("$customArgs('test',[1,2,3,4],3)");
JFunction customArgsFn;
customArgsFn.signature = std::make_shared<utils::Signature>("<sa<n>n:s>", "customArgs");
customArgsFn.implementation = [](const Utils::JList& args, const std::any&, std::shared_ptr<Frame>) -> std::any {
std::string out = "[";
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) out += ", ";
const auto& a = args[i];
if (!a.has_value()) {
out += "null";
} else if (a.type() == typeid(std::string)) {
out += std::any_cast<std::string>(a);
} else if (a.type() == typeid(double)) {
auto v = std::any_cast<double>(a);
if (v == static_cast<long long>(v))
out += std::to_string(static_cast<long long>(v));
else
out += std::to_string(v);
} else if (a.type() == typeid(long long)) {
out += std::to_string(std::any_cast<long long>(a));
} else if (a.type() == typeid(Utils::JList)) {
const auto& arr = std::any_cast<Utils::JList>(a);
out += "[";
for (size_t j = 0; j < arr.size(); ++j) {
if (j > 0) out += ", ";
const auto& el = arr[j];
if (el.type() == typeid(double)) {
auto v = std::any_cast<double>(el);
if (v == static_cast<long long>(v))
out += std::to_string(static_cast<long long>(v));
else
out += std::to_string(v);
} else if (el.type() == typeid(long long)) {
out += std::to_string(std::any_cast<long long>(el));
}
}
out += "]";
}
}
out += "]";
return out;
};
expr.registerFunction("customArgs", customArgsFn);
auto result = expr.evaluate(nullptr);
ASSERT_TRUE(result.is_string());
EXPECT_EQ(result.get<std::string>(), "[test, [1, 2, 3, 4], 3]");
}
} // namespace jsonata