jinja: correct stats for tojson and string filters (#19785)
This commit is contained in:
@@ -32,6 +32,7 @@ static void test_string_methods(testing & t);
|
||||
static void test_array_methods(testing & t);
|
||||
static void test_object_methods(testing & t);
|
||||
static void test_hasher(testing & t);
|
||||
static void test_stats(testing & t);
|
||||
static void test_fuzzing(testing & t);
|
||||
|
||||
static bool g_python_mode = false;
|
||||
@@ -70,6 +71,7 @@ int main(int argc, char *argv[]) {
|
||||
t.test("object methods", test_object_methods);
|
||||
if (!g_python_mode) {
|
||||
t.test("hasher", test_hasher);
|
||||
t.test("stats", test_stats);
|
||||
t.test("fuzzing", test_fuzzing);
|
||||
}
|
||||
|
||||
@@ -1795,6 +1797,63 @@ static void test_hasher(testing & t) {
|
||||
});
|
||||
}
|
||||
|
||||
static void test_stats(testing & t) {
|
||||
static auto get_stats = [](const std::string & tmpl, const json & vars) -> jinja::value {
|
||||
jinja::lexer lexer;
|
||||
auto lexer_res = lexer.tokenize(tmpl);
|
||||
|
||||
jinja::program prog = jinja::parse_from_tokens(lexer_res);
|
||||
|
||||
jinja::context ctx(tmpl);
|
||||
jinja::global_from_json(ctx, json{{ "val", vars }}, true);
|
||||
ctx.is_get_stats = true;
|
||||
|
||||
jinja::runtime runtime(ctx);
|
||||
runtime.execute(prog);
|
||||
|
||||
return ctx.get_val("val");
|
||||
};
|
||||
|
||||
t.test("stats", [](testing & t) {
|
||||
jinja::value val = get_stats(
|
||||
"{{val.num}} "
|
||||
"{{val.str}} "
|
||||
"{{val.arr[0]}} "
|
||||
"{{val.obj.key1}} "
|
||||
"{{val.nested | tojson}}",
|
||||
// Note: the json below will be wrapped inside "val" in the context
|
||||
json{
|
||||
{"num", 1},
|
||||
{"str", "abc"},
|
||||
{"arr", json::array({1, 2, 3})},
|
||||
{"obj", json::object({{"key1", 1}, {"key2", 2}, {"key3", 3}})},
|
||||
{"nested", json::object({
|
||||
{"inner_key1", json::array({1, 2})},
|
||||
{"inner_key2", json::object({{"a", "x"}, {"b", "y"}})}
|
||||
})},
|
||||
{"mixed", json::object({
|
||||
{"used", 1},
|
||||
{"unused", 2},
|
||||
})},
|
||||
}
|
||||
);
|
||||
|
||||
t.assert_true("num is used", val->at("num")->stats.used);
|
||||
t.assert_true("str is used", val->at("str")->stats.used);
|
||||
|
||||
t.assert_true("arr is used", val->at("arr")->stats.used);
|
||||
t.assert_true("arr[0] is used", val->at("arr")->at(0)->stats.used);
|
||||
t.assert_true("arr[1] is not used", !val->at("arr")->at(1)->stats.used);
|
||||
|
||||
t.assert_true("obj is used", val->at("obj")->stats.used);
|
||||
t.assert_true("obj.key1 is used", val->at("obj")->at("key1")->stats.used);
|
||||
t.assert_true("obj.key2 is not used", !val->at("obj")->at("key2")->stats.used);
|
||||
|
||||
t.assert_true("inner_key1[0] is used", val->at("nested")->at("inner_key1")->at(0)->stats.used);
|
||||
t.assert_true("inner_key2.a is used", val->at("nested")->at("inner_key2")->at("a")->stats.used);
|
||||
});
|
||||
}
|
||||
|
||||
static void test_template_cpp(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) {
|
||||
t.test(name, [&tmpl, &vars, &expect](testing & t) {
|
||||
jinja::lexer lexer;
|
||||
|
||||
Reference in New Issue
Block a user