From 23e768403e90a637753e0fdeedbbca0429a1b1fa Mon Sep 17 00:00:00 2001 From: pantor Date: Tue, 4 Aug 2020 23:27:46 +0200 Subject: [PATCH] add test for nested functions --- include/inja/parser.hpp | 7 +++++-- single_include/inja/inja.hpp | 7 +++++-- test/test-functions.cpp | 8 ++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index b22e04d..f75c5bd 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -288,10 +288,13 @@ class Parser { func->callback = function_data.callback; } - function_stack.pop(); - + if (operator_stack.empty()) { + throw_parser_error("internal error at function " + func->name); + } + current_expression_list->rpn_output.emplace_back(operator_stack.top()); operator_stack.pop(); + function_stack.pop(); } } default: diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 45595d0..9919003 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -3000,10 +3000,13 @@ class Parser { func->callback = function_data.callback; } - function_stack.pop(); - + if (operator_stack.empty()) { + throw_parser_error("internal error at function " + func->name); + } + current_expression_list->rpn_output.emplace_back(operator_stack.top()); operator_stack.pop(); + function_stack.pop(); } } default: diff --git a/test/test-functions.cpp b/test/test-functions.cpp index d339e4c..d233789 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -223,6 +223,11 @@ TEST_CASE("callbacks") { return number1 * number2 * number3; }); + env.add_callback("length", 1, [](inja::Arguments args) { + auto number1 = args.at(0)->get(); + return number1.length(); + }); + env.add_callback("multiply", 0, [](inja::Arguments args) { return 1.0; }); CHECK(env.render("{{ double(age) }}", data) == "56"); @@ -230,6 +235,8 @@ TEST_CASE("callbacks") { CHECK(env.render("{{ double-greetings }}", data) == "Hello Hello!"); CHECK(env.render("{{ double-greetings() }}", data) == "Hello Hello!"); CHECK(env.render("{{ multiply(4, 5) }}", data) == "20.0"); + CHECK(env.render("{{ multiply(length(\"tester\"), 5) }}", data) == "30.0"); + CHECK(env.render("{{ multiply(5, length(\"t\")) }}", data) == "5.0"); CHECK(env.render("{{ multiply(3, 4, 5) }}", data) == "60.0"); CHECK(env.render("{{ multiply }}", data) == "1.0"); @@ -268,6 +275,7 @@ TEST_CASE("combinations") { CHECK(env.render("{{ last(list_of_objects).d * 2}}", data) == "10"); CHECK(env.render("{{ last(range(5)) * 2 }}", data) == "8"); CHECK(env.render("{{ last(range(5 * 2)) }}", data) == "9"); + CHECK(env.render("{{ last(range(5 * 2)) }}", data) == "9"); CHECK(env.render("{{ not true }}", data) == "false"); CHECK(env.render("{{ not (true) }}", data) == "false"); CHECK(env.render("{{ true or (true or true) }}", data) == "true");