From 520c5d7d5f09c0b205e6e1c471483730380c5e33 Mon Sep 17 00:00:00 2001 From: Nathan Lapel Date: Sun, 15 Mar 2020 11:46:31 +0100 Subject: Support function calls --- testing/tests/simple.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'testing/tests') diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 3544599..3e156b4 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -262,6 +262,43 @@ fn test_slice_literal() { assert_eq!(t.render().unwrap(), "a"); } +#[derive(Template)] +#[template(source = "Hello, {{ world(\"123\", 4) }}!", ext = "txt")] +struct FunctionRefTemplate { + world: fn(s: &str, v: u8) -> String, +} + +#[test] +fn test_func_ref_call() { + let t = FunctionRefTemplate { + world: |s, r| format!("world({}, {})", s, r), + }; + assert_eq!(t.render().unwrap(), "Hello, world(123, 4)!"); +} + +fn world2(s: &str, v: u8) -> String { + format!("world{}{}", v, s) +} + +#[derive(Template)] +#[template(source = "Hello, {{ self::world2(\"123\", 4) }}!", ext = "txt")] +struct PathFunctionTemplate; + +#[test] +fn test_path_func_call() { + assert_eq!(PathFunctionTemplate.render().unwrap(), "Hello, world4123!"); +} + +#[derive(Template)] +#[template(source = "Hello, {{ Self::world3(self, \"123\", 4) }}!", ext = "txt")] +struct FunctionTemplate; + +impl FunctionTemplate { + fn world3(&self, s: &str, v: u8) -> String { + format!("world{}{}", s, v) + } +} + #[derive(Template)] #[template(source = " {# foo -#} ", ext = "txt")] struct CommentTemplate {} -- cgit