-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathuser_callable_test.cpp
400 lines (358 loc) · 19.5 KB
/
user_callable_test.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "jinja2cpp/template.h"
#include "jinja2cpp/user_callable.h"
#include "jinja2cpp/generic_list_iterator.h"
#include "test_tools.h"
using namespace jinja2;
std::string UrlProcessorGlobal(const std::string& urlLink, const std::string& labelName, int limitWord, const std::string& targetStr)
{
std::string result = urlLink + "?";
for (int n = 0; n < limitWord; ++n)
result += labelName;
result += "#" + targetStr;
return result;
}
class UserCallableTest : public BasicTemplateRenderer
{
public:
std::string UrlProcessor(const std::string& urlLink, const std::string& labelName, int limitWord, const std::string& targetStr)
{
return UrlProcessorGlobal(urlLink, labelName, limitWord, targetStr);
}
std::string UrlProcessorConst(const std::string& urlLink, const std::string& labelName, int limitWord, const std::string& targetStr) const
{
return UrlProcessorGlobal(urlLink, labelName, limitWord, targetStr);
}
};
MULTISTR_TEST(UserCallableTest, SimpleUserCallable,
R"(
{{ test() }}
{{ test() }}
{{ test_wide() }}
{{ test_wide() }}
)",
//------------
R"(
Hello World!
Hello World!
Hello World!
Hello World!
)"
)
{
jinja2::UserCallable uc;
uc.callable = [](auto&)->jinja2::Value {return "Hello World!";};
jinja2::UserCallable ucWide;
ucWide.callable = [](auto&)->jinja2::Value {return std::wstring(L"Hello World!"); };
params["test"] = std::move(uc);
params["test_wide"] = std::move(ucWide);
}
MULTISTR_TEST(UserCallableTest, SimpleUserCallableWithParams1,
R"(
{{ test('Hello', 'World!') }}
{{ test(str2='World!', str1='Hello') }}
)",
//-------------
R"(
Hello World!
Hello World!
)"
)
{
jinja2::UserCallable uc;
uc.callable = [](auto& params)->jinja2::Value {
auto str1 = params["str1"];
auto str2 = params["str2"];
if (str1.isString())
return str1.asString() + " " + str2.asString();
return str1.asWString() + L" " + str2.asWString();
};
uc.argsInfo = {{"str1", true}, {"str2", true}};
params["test"] = std::move(uc);
}
MULTISTR_TEST(UserCallableTest, SimpleUserCallableWithParams2,
R"(
{{ test('Hello', 'World!') }}
{{ test(str2='World!', str1='Hello') }}
{{ test(str2='World!') }}
{{ test('Hello') }}
{{ test_w('Hello', 'World!') }}
{{ test_w(str2='World!', str1='Hello') }}
{{ test_w(str2='World!') }}
{{ test_w('Hello') }}
{{ test2(['H', 'e', 'l', 'l', 'o']) }}
{{ test3("https://google.com", "label1", 3, "someTarget") }}
{{ test4("https://google.com", "label1", 3, "someTarget") }}
{{ test5("https://google.com", "label1", 3, "someTarget") }}
{{ test6("https://google.com", "label1", 3, "someTarget") }}
)",
//-------------
R"(
Hello World!
Hello World!
World!
Hello default
Hello World!
Hello World!
World!
Hello default
Hello
https://google.com?label1label1label1#someTarget
https://google.com?label1label1label1#someTarget
https://google.com?label1label1label1#someTarget
https://google.com?label1label1label1#someTarget
)"
)
{
params["test"] = MakeCallable(
[](const std::string& str1, const std::string& str2) {
return str1 + " " + str2;
},
ArgInfo{"str1"}, ArgInfo{"str2", false, "default"}
);
params["test_w"] = MakeCallable(
[](const std::wstring& str1, const std::wstring& str2) {
return str1 + L" " + str2;
},
ArgInfo{ "str1" }, ArgInfo{ "str2", false, "default" }
);
params["test2"] = MakeCallable(
[](const GenericList& list) {
std::ostringstream os;
for(auto& v : list)
os << AsString(v);
return os.str();
},
ArgInfo{"list"}
);
params["test3"] =
MakeCallable([](auto& urlLink, auto& labelName, auto limitWord, auto& targetStr) { return UrlProcessorGlobal(urlLink, labelName, limitWord, targetStr); },
jinja2::ArgInfoT<const std::string&>{ "urlLink" },
jinja2::ArgInfoT<const std::string&>{ "labelName" },
jinja2::ArgInfoT<int>{ "limitWord", false, 0 },
jinja2::ArgInfoT<const std::string&>{ "targetStr", false, "" });
params["test4"] = MakeCallable(UrlProcessorGlobal,
jinja2::ArgInfo{ "urlLink" },
jinja2::ArgInfo{ "labelName" },
jinja2::ArgInfo{ "limitWord", false, 0 },
jinja2::ArgInfo{ "targetStr", false, "" });
params["test5"] = MakeCallable(&UserCallableTest::UrlProcessor,
const_cast<UserCallableTest*>(&test),
jinja2::ArgInfo{ "urlLink" },
jinja2::ArgInfo{ "labelName" },
jinja2::ArgInfo{ "limitWord", false, 0 },
jinja2::ArgInfo{ "targetStr", false, "" });
params["test6"] = MakeCallable(&UserCallableTest::UrlProcessorConst,
&test,
jinja2::ArgInfo{ "urlLink" },
jinja2::ArgInfo{ "labelName" },
jinja2::ArgInfo{ "limitWord", false, 0 },
jinja2::ArgInfo{ "targetStr", false, "" });
}
TEST(UserCallableTestSingle, ReflectedCallable)
{
std::string source = R"(
{% set callable = reflected.basicCallable %}{{ callable() }}
{{ reflected.basicCallable() }}
{% set inner = reflected.getInnerStruct() %}{{ inner.strValue }}
{% set innerValue = reflected.getInnerStructValue() %}{{ innerValue.strValue }}
{{ innerReflected.strValue }}
)";
Template tpl;
auto parseRes = tpl.Load(source);
EXPECT_TRUE(parseRes.has_value());
if (!parseRes)
{
std::cout << parseRes.error() << std::endl;
return;
}
TestStruct reflected;
auto innerReflected = std::make_shared<TestInnerStruct>();
innerReflected->strValue = "!!Hello World!!";
reflected.intValue = 100500;
reflected.innerStruct = std::make_shared<TestInnerStruct>();
reflected.innerStruct->strValue = "Hello World!";
{
jinja2::ValuesMap params;
params["reflected"] = jinja2::Reflect(reflected);
params["innerReflected"] = jinja2::Reflect(innerReflected);
std::string result = tpl.RenderAsString(params).value();
std::cout << result << std::endl;
std::string expectedResult = R"(
100500
100500
Hello World!
Hello World!
!!Hello World!!
)";
EXPECT_EQ(expectedResult, result);
}
EXPECT_EQ(1L, innerReflected.use_count());
EXPECT_EQ(1L, reflected.innerStruct.use_count());
}
struct UserCallableParamConvertTestTag;
using UserCallableParamConvertTest = InputOutputPairTest<UserCallableParamConvertTestTag>;
TEST_P(UserCallableParamConvertTest, Test)
{
auto& testParam = GetParam();
std::string source = "{{" + testParam.tpl + " | pprint }}";
jinja2::ValuesMap params = PrepareTestData();
params["BoolFn"] = MakeCallable([](bool val) {return val;}, ArgInfo{"val"});
params["IntFn"] = MakeCallable([](int val) {return val;}, ArgInfo{"val"});
params["Int64Fn"] = MakeCallable([](int64_t val) {return val;}, ArgInfo{"val"});
params["DoubleFn"] = MakeCallable([](double val) {return val;}, ArgInfo{"val"});
params["StringFn"] = MakeCallable([](const std::string& val) {return val;}, ArgInfo{"val"});
params["WStringFn"] = MakeCallable([](const std::wstring& val) {return val;}, ArgInfo{"val"});
params["StringViewFn"] = MakeCallable([](const nonstd::string_view& val) {return std::string(val.begin(), val.end()); }, ArgInfo{ "val" });
params["WStringViewFn"] = MakeCallable([](const nonstd::wstring_view& val) {return std::wstring(val.begin(), val.end()); }, ArgInfo{ "val" });
params["GListFn"] = MakeCallable([](const GenericList& val)
{
return val;
}, ArgInfo{"val"});
params["GMapFn"] = MakeCallable([](const GenericMap& val) {return val;}, ArgInfo{"val"});
params["VarArgsFn"] = MakeCallable([](const ValuesList& val) {
return val;
}, ArgInfo{"*args"});
params["VarKwArgsFn"] = MakeCallable([](const ValuesMap& val) {
return val;
}, ArgInfo{"**kwargs"});
params["ContextArgFn"] =
MakeCallable([](const GenericMap& val, const std::string& varName) { return val.GetValueByName(varName); }, ArgInfo{ "*context" }, ArgInfo{ "name" });
params["test_value"] = 100500;
PerformBothTests(source, testParam.result, params);
}
struct UserCallableFilterTestTag;
using UserCallableFilterTest = InputOutputPairTest<UserCallableFilterTestTag>;
TEST_P(UserCallableFilterTest, Test)
{
auto& testParam = GetParam();
std::string source = "{{ " + testParam.tpl + " }}";
jinja2::ValuesMap params = PrepareTestData();
params["surround"] = MakeCallable([](const std::string& val, const std::string& before, const std::string& after) { return before + val + after; },
ArgInfoT<const std::string&>{ "val" },
ArgInfoT<const std::string&>{ "before", false, ">>> " },
ArgInfoT<const std::string&>{ "after", false, " <<<" });
params["joiner"] = MakeCallable([](const std::string& delim, const ValuesList& items) {
std::ostringstream os;
bool isFirst = true;
for (auto& v : items)
{
if (isFirst)
isFirst = false;
else
os << delim;
os << AsString(v);
}
return os.str();
}, ArgInfo{"delim"}, ArgInfo{"*args"});
params["tester"] = MakeCallable([](const std::string& testValue, const std::string& pattern) {
return testValue == pattern;
}, ArgInfo{"testVal"}, ArgInfo{"pattern"});
PerformBothTests(source, testParam.result, params);
}
INSTANTIATE_TEST_SUITE_P(BoolParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"BoolFn()", "false"},
InputOutputPair{"BoolFn(true)", "true"}
));
INSTANTIATE_TEST_SUITE_P(IntParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"IntFn()", "0"},
InputOutputPair{"IntFn(10)", "10"},
InputOutputPair{"IntFn(10.123)", "10"}
));
INSTANTIATE_TEST_SUITE_P(Int64ParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"Int64Fn()", "0"},
InputOutputPair{"Int64Fn(10)", "10"},
InputOutputPair{"Int64Fn(10.123)", "10"}
));
INSTANTIATE_TEST_SUITE_P(DoubleParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"DoubleFn()", "0"},
InputOutputPair{"DoubleFn(10)", "10"},
InputOutputPair{"DoubleFn(10.123)", "10.123"}
));
INSTANTIATE_TEST_SUITE_P(VarArgsParamsConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"VarArgsFn()", "[]"},
InputOutputPair{"VarArgsFn(10, 'abc', false)", "[10, 'abc', false]"},
InputOutputPair{"VarArgsFn(10.123, (1, 2, 3), arg=1)", "[10.123, [1, 2, 3]]"}
));
INSTANTIATE_TEST_SUITE_P(VarKwArgsParamsConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"VarKwArgsFn()", "{}"},
InputOutputPair{"VarKwArgsFn(arg1=10, arg2='abc', arg3=false) | dictsort",
"['arg1': 10, 'arg2': 'abc', 'arg3': false]"},
InputOutputPair{"VarKwArgsFn(arg1=10.123, arg2=(1, 2, 3), 1) | dictsort",
"['arg1': 10.123, 'arg2': [1, 2, 3]]"}
));
INSTANTIATE_TEST_SUITE_P(GlobalContextAccess, UserCallableParamConvertTest, ::testing::Values(InputOutputPair{ "ContextArgFn(name='test_value')", "100500" }));
INSTANTIATE_TEST_SUITE_P(StringParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"StringFn()", "''"},
InputOutputPair{"StringFn('Hello World')", "'Hello World'"},
InputOutputPair{"StringFn(stringValue)", "'rain'"},
InputOutputPair{"StringFn(wstringValue)", "' hello world '"}
));
INSTANTIATE_TEST_SUITE_P(WStringParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"WStringFn()", "''"},
InputOutputPair{"WStringFn('Hello World')", "'Hello World'"},
InputOutputPair{"WStringFn(stringValue)", "'rain'"},
InputOutputPair{"WStringFn(wstringValue)", "' hello world '"}
));
INSTANTIATE_TEST_SUITE_P(StringViewParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"StringViewFn()", "''"},
InputOutputPair{"StringViewFn('Hello World')", "'Hello World'"},
InputOutputPair{"StringViewFn(stringValue)", "'rain'"},
InputOutputPair{"StringViewFn(wstringValue)", "' hello world '"}
));
INSTANTIATE_TEST_SUITE_P(WStringViewParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"WStringViewFn()", "''"},
InputOutputPair{"WStringViewFn('Hello World')", "'Hello World'"},
InputOutputPair{"WStringViewFn(stringValue)", "'rain'"},
InputOutputPair{"WStringViewFn(wstringValue)", "' hello world '"}
));
INSTANTIATE_TEST_SUITE_P(ListParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"GListFn()", "[]"},
InputOutputPair{"GListFn([1, 2, 3, 4])", "[1, 2, 3, 4]"},
InputOutputPair{"GListFn(intList)", "[9, 0, 8, 1, 7, 2, 6, 3, 5, 4]"},
InputOutputPair{"GListFn(reflectedIntVector)", "[9, 0, 8, 1, 7, 2, 6, 3, 5, 4]"},
InputOutputPair{"GListFn(range(10))", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"},
InputOutputPair{"GListFn([1, 2, 3, 4] | sort)","[1, 2, 3, 4]"},
InputOutputPair{"GListFn(intList | sort)", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"},
InputOutputPair{"GListFn(reflectedVal.tmpStructList)",
"[{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}]"},
InputOutputPair{"GListFn(reflectedIntVector | sort)", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"},
InputOutputPair{"GListFn(range(10) | sort)", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"},
InputOutputPair{"GListFn({'name'='itemName', 'val'='itemValue'} | list) | sort", "['name', 'val']"},
InputOutputPair{"GListFn({'name'='itemName', 'val'='itemValue'} | list | sort)", "['name', 'val']"}
));
INSTANTIATE_TEST_SUITE_P(MapParamConvert, UserCallableParamConvertTest, ::testing::Values(
InputOutputPair{"GMapFn()", "{}"},
InputOutputPair{"GMapFn({'key'=10})", "{'key': 10}"},
InputOutputPair{"GMapFn(simpleMapValue) | dictsort",
"['boolValue': true, 'dblVal': 100.5, 'intVal': 10, 'stringVal': 'string100.5']"},
InputOutputPair{"GMapFn(reflectedVal) | dictsort",
"['basicCallable': <callable>, 'boolValue': false, 'dblValue': 0, 'getInnerStruct': <callable>, 'getInnerStructValue': <callable>, 'innerStruct': {'strValue': 'Hello World!'}, "
"'innerStructList': [{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}], 'intEvenValue': 0, 'intValue': 0, "
"'strValue': 'test string 0', 'strViewValue': 'test string 0', 'tmpStructList': [{'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, "
"{'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}, {'strValue': 'Hello World!'}], "
"'wstrValue': 'test string 0', 'wstrViewValue': 'test string 0']" },
InputOutputPair{"GMapFn(reflectedVal.innerStruct) | dictsort", "['strValue': 'Hello World!']"}
));
INSTANTIATE_TEST_SUITE_P(UserDefinedFilter, UserCallableFilterTest, ::testing::Values(
InputOutputPair{"'Hello World' | surround", ">>> Hello World <<<"},
InputOutputPair{"'Hello World' | surround(before='### ')", "### Hello World <<<"},
InputOutputPair{"'Hello World' | surround(after=' ###', before='### ')", "### Hello World ###"},
InputOutputPair{"'Hello World' | surround('### ', ' ###')", "### Hello World ###"},
InputOutputPair{"'Hello World' | joiner(delim=', ', '1', '2', '3')", "Hello World, 1, 2, 3"},
InputOutputPair{"'Hello World' | joiner(delim=', ', '1', '2', '3')", "Hello World, 1, 2, 3"},
InputOutputPair{"('A', 'B', 'C') | map('surround', before='> ', after=' <') | pprint", "['> A <', '> B <', '> C <']"},
InputOutputPair{"'str1' if 'str1' is tester('str1') else 'str2'", "str1"},
InputOutputPair{"'str1' if 'str3' is tester(pattern='str1') else 'str2'", "str2"},
InputOutputPair{"('str1', 'A', 'str1', 'B', 'str1', 'C') | select('tester', 'str1') | map('surround', before='> ', after=' <') | pprint", "['> str1 <', '> str1 <', '> str1 <']"}
));