From 407fe7535eba6f6ef5f0af09b592f8f60b678569 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 18 Nov 2022 17:13:30 -0800 Subject: [PATCH] Add test for 'State::current_call()' --- minijinja/tests/test_templates.rs | 101 ++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/minijinja/tests/test_templates.rs b/minijinja/tests/test_templates.rs index 10c4e720..75233f95 100644 --- a/minijinja/tests/test_templates.rs +++ b/minijinja/tests/test_templates.rs @@ -146,3 +146,104 @@ fn test_loop_changed() { ); assert_eq!(rv, "12345"); } + +#[test] +fn test_current_call_state() { + use minijinja::value::{Object, Value}; + use std::fmt; + + #[derive(Debug)] + struct MethodAndFunc; + + impl fmt::Display for MethodAndFunc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } + } + + impl Object for MethodAndFunc { + fn call_method(&self, state: &State, name: &str, args: &[Value]) -> Result { + assert_eq!(name, state.current_call().unwrap()); + let args = args + .iter() + .map(|v| v.to_string()) + .collect::>() + .join(", "); + + Ok(format!("{}({args})", state.current_call().unwrap()).into()) + } + + fn call(&self, state: &State, args: &[Value]) -> Result { + let args = args + .iter() + .map(|v| v.to_string()) + .collect::>() + .join(", "); + + Ok(format!("{}({args})", state.current_call().unwrap()).into()) + } + } + + fn current_call(state: &State, value: Option<&str>) -> String { + format!("{}({})", state.current_call().unwrap(), value.unwrap_or("")) + } + + fn check_test(state: &State, value: &str) -> bool { + state.current_call() == Some(value) + } + + let mut env = Environment::new(); + env.add_function("fn_call_a", current_call); + env.add_function("fn_call_b", current_call); + env.add_filter("filter_call", current_call); + env.add_test("my_test", check_test); + env.add_test("another_test", check_test); + env.add_global("object", Value::from_object(MethodAndFunc)); + + env.add_template( + "test", + r#" + {{ fn_call_a() }} + {{ "foo" | filter_call }} + {{ fn_call_a() | filter_call }} + {{ fn_call_b() | filter_call }} + {{ fn_call_a(fn_call_b()) }} + {{ fn_call_a(fn_call_b()) | filter_call }} + + {{ "my_test" is my_test }} + {{ "another_test" is my_test }} + {{ "another_test" is another_test }} + + {{ object.foo() }} + {{ object.bar() }} + {{ object.foo(object.bar(object.baz())) }} + {{ object(object.bar()) }} + {{ object.baz(object()) }} + "#, + ) + .unwrap(); + + let tmpl = env.get_template("test").unwrap(); + let rv = tmpl.render(context!()).unwrap(); + assert_eq!( + rv, + r#" + fn_call_a() + filter_call(foo) + filter_call(fn_call_a()) + filter_call(fn_call_b()) + fn_call_a(fn_call_b()) + filter_call(fn_call_a(fn_call_b())) + + true + false + true + + foo() + bar() + foo(bar(baz())) + object(bar()) + baz(object()) + "# + ); +}