From 516b91cc8e679dbf19a736b22cdc137d6c657dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigbj=C3=B8rn=20Skj=C3=A6ret?= Date: Mon, 28 Sep 2026 14:20:46 +0200 Subject: [PATCH] add tests --- tests/test-jinja.cpp | 56 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp index 891b785c4f..7649e36246 100644 --- a/tests/test-jinja.cpp +++ b/tests/test-jinja.cpp @@ -452,6 +452,18 @@ static void test_expressions(testing & t) { "c" ); + test_template(t, "array bool access", + "{{ items[true] }}", + {{"items", json::array({"a", "b", "c"})}}, + "b" + ); + + test_template(t, "array non-index access", + "{{ items[1.0] is undefined }}", + {{"items", json::array({"a", "b", "c"})}}, + "True" + ); + test_template(t, "array slice", "{{ items[1:-1]|string }}", {{"items", json::array({"a", "b", "c"})}}, @@ -735,6 +747,16 @@ static void test_filters(testing & t) { "xyz" ); + test_template(t, "sort with numeric-like attribute", + "{{ items|sort(attribute='01')|join(attribute=1) }}", + {{"items", json::array({ + json::array({1, "z"}), + json::array({2, "x"}), + json::array({3, "y"}), + })}}, + "xyz" + ); + test_template(t, "join", "{{ items|join(', ') }}", {{"items", json::array({"a", "b", "c"})}}, @@ -1590,6 +1612,16 @@ static void test_array_methods(testing & t) { "b c " ); + test_template(t, "array|selectattr numeric-like with operator", + "{% for item in items|selectattr('0', 'gt', 1) %}{{ item.1 }} {% endfor %}", + {{"items", json::array({ + json::array({3, "z"}), + json::array({1, "x"}), + json::array({2, "y"}), + })}}, + "z y " + ); + test_template(t, "array|tojson", "{{ arr|tojson }}", {{"arr", json::array({1, 2, 3})}}, @@ -1654,6 +1686,12 @@ static void test_array_methods(testing & t) { "123" ); + test_template(t, "array|join numeric-like attribute", + "{{ arr|join(attribute='0') }}", + {{"arr", json::array({json::array({1}), json::array({2}), json::array({3})})}}, + "123" + ); + test_template(t, "array.pop() last", "{{ arr.pop() }}-{{ arr|join(',') }}", {{"arr", json::array({"a", "b", "c"})}}, @@ -1712,6 +1750,16 @@ static void test_array_methods(testing & t) { "10 20 30 " ); + test_template(t, "array|map with numeric-like attribute", + "{% for v in arr|map(attribute='1') %}{{ v }} {% endfor %}", + {{"arr", json::array({ + json::array({10, "x"}), + json::array({20, "y"}), + json::array({30, "z"}), + })}}, + "x y z " + ); + test_template(t, "array|map with negative attribute", "{% for v in arr|map(attribute=-1) %}{{ v }} {% endfor %}", {{"arr", json::array({ @@ -1741,21 +1789,21 @@ static void test_array_methods(testing & t) { ); test_template(t, "array|min attribute", - "{{ items|min(attribute='x') }}", + "{{ items|min(attribute='x')|tojson }}", {{"items", json::array({ json({{"x", 2}}), json({{"x", 1}}), })}}, - "{'x': 1}" + "{\"x\": 1}" ); test_template(t, "array|max attribute", - "{{ items|max(attribute='x') }}", + "{{ items|max(attribute='x')|tojson }}", {{"items", json::array({ json({{"x", 2}}), json({{"x", 1}}), })}}, - "{'x': 2}" + "{\"x\": 2}" ); // not used by any chat templates