jinja : implement sameas test (#29448)

* implement sameas test

* add tests
This commit is contained in:
Sigbjørn Skjæret
2026-09-26 10:14:58 +02:00
committed by GitHub
parent 86a24a182b
commit 81bc6b83f8
2 changed files with 47 additions and 3 deletions
+22 -2
View File
@@ -515,8 +515,28 @@ const func_builtins & global_builtins() {
}},
{"test_is_sameas", [](const func_args & args) -> value {
// Check if an object points to the same memory address as another object
(void)args;
throw not_implemented_exception("sameas test not implemented");
args.ensure_count(2);
auto a = args.get_pos(0);
auto b = args.get_pos(1);
bool res = false;
if (!is_val<value_undefined>(a) && !is_val<value_undefined>(b)) {
if (is_val<value_none>(a) && is_val<value_none>(b)) {
res = true;
} else if (is_val<value_bool>(a) && is_val<value_bool>(b)) {
if (a->as_bool() == b->as_bool()) {
res = true;
}
} else if (is_val<value_int>(a) && is_val<value_int>(b)) {
const int64_t x = a->as_int();
// Allow comparison within small-int cache range
if (x >= -5 && x <= 256 && x == b->as_int()) {
res = true;
}
} else if (a == b) {
res = true;
}
}
return mk_val<value_bool>(res);
}},
{"test_is_escaped", [](const func_args & args) -> value {
(void)args;
+25 -1
View File
@@ -1212,12 +1212,36 @@ static void test_tests(testing & t) {
"yes"
);
test_template(t, "is sameas",
test_template(t, "is sameas boolean",
"{{ 'yes' if x is sameas(false) }}",
{{"x", false}},
"yes"
);
test_template(t, "is sameas integer",
"{{ 'yes' if x is sameas(1) }}",
{{"x", 1}},
"yes"
);
test_template(t, "is sameas object",
"{{ 'yes' if x is sameas(x) }}",
{{"x", {{"y", false}}}},
"yes"
);
test_template(t, "is sameas ref object",
"{% set y = x.y %}{{ 'yes' if x.y is sameas(y) and x.y is not sameas(x.z) }}",
{{"x", {{"y", {{"z", 1}}}, {"z", {{"z", 1}}}}}},
"yes"
);
test_template(t, "is sameas undefined",
"{{ 'yes' if x is sameas(x) else 'no' }}",
json::object(),
"no"
);
test_template(t, "is boolean",
"{{ 'yes' if x is boolean }}",
{{"x", true}},