From 81bc6b83f827df746eb129235488d325c49cae52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigbj=C3=B8rn=20Skj=C3=A6ret?= Date: Sat, 26 Sep 2026 10:14:58 +0200 Subject: [PATCH] jinja : implement sameas test (#29448) * implement sameas test * add tests --- common/jinja/value.cpp | 24 ++++++++++++++++++++++-- tests/test-jinja.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp index 6999ef7d67..ce4c66f879 100644 --- a/common/jinja/value.cpp +++ b/common/jinja/value.cpp @@ -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(a) && !is_val(b)) { + if (is_val(a) && is_val(b)) { + res = true; + } else if (is_val(a) && is_val(b)) { + if (a->as_bool() == b->as_bool()) { + res = true; + } + } else if (is_val(a) && is_val(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(res); }}, {"test_is_escaped", [](const func_args & args) -> value { (void)args; diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp index 064f5927ba..50a79af3f3 100644 --- a/tests/test-jinja.cpp +++ b/tests/test-jinja.cpp @@ -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}},