minja: add str.endswith

This commit is contained in:
ochafik 2024-09-26 03:21:23 +01:00
parent 76d2938ef8
commit c124ab48ea
2 changed files with 6 additions and 0 deletions

View file

@ -1234,6 +1234,11 @@ public:
if (method->get_name() == "strip") {
args.expectArgs("strip method", {0, 0}, {0, 0});
return Value(strip(obj.get<std::string>()));
} else if (method->get_name() == "endswith") {
args.expectArgs("endswith method", {1, 1}, {0, 0});
auto str = obj.get<std::string>();
auto suffix = args.args[0]->evaluate(context).get<std::string>();
return suffix.length() <= str.length() && std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
}
throw std::runtime_error("Unknown method: " + method->get_name());

View file

@ -149,6 +149,7 @@ static void test_error_contains(const std::string & template_str, const json & b
}
static void test_template_features() {
test_render(R"({{ 'abc'.endswith('bc') }},{{ ''.endswith('a') }})", {}, {}, "True,False");
test_render(R"({{ 'a' in {"a": 1} }},{{ 'a' in {} }})", {}, {}, "True,False");
test_render(R"({{ 'a' in ["a"] }},{{ 'a' in [] }})", {}, {}, "True,False");
test_render(R"({{ [{"a": 1}, {"a": 2}, {}] | selectattr("a", "equalto", 1) }})", {}, {}, R"([{'a': 1}])");