Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

These test codes for call func make strange error #29

Open
rocket049 opened this issue May 26, 2023 · 0 comments
Open

These test codes for call func make strange error #29

rocket049 opened this issue May 26, 2023 · 0 comments

Comments

@rocket049
Copy link

These test codes for call func make strange error. Function 'tmpl_test1' can not pass, But function 'tmpl_test2' can pass.
The different between them:
In tmpl_test1, the function is stored in a map;
In tmpl_test2, the function is used straight.

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use gtmpl::{gtmpl_fn, template, Value};
    use gtmpl_value::{self, FuncError, Function};

    gtmpl_fn!(
        fn shout(n: i64) -> Result<String, FuncError> {
            let mut v = Vec::new();
            for _ in 0..n {
                v.push("wang".to_string());
            }
            Ok(v.join(","))
        }
    );

    #[test]
    fn tmpl_test1() {
        let mut a: HashMap<String, Value> = HashMap::new();
        a.insert(
            "shout".to_string(),
            Value::Function(Function { f: shout }),
        );
        let equal = template(r#"{{ call .shout 3 }}"#, a);
        if equal.is_ok() {
            assert_eq!(&equal.unwrap(), "wang,wang,wang");
        } else {
            assert_eq!(&equal.unwrap_err().to_string(), "wang,wang,wang");
        }
    }
    #[test]
    fn tmpl_test2() {
        let equal: Result<String, gtmpl::TemplateError> = template(
            r#"{{ call . 3 }}"#,
            Value::Function(Function { f: shout }),
        );
        if equal.is_ok() {
            assert_eq!(&equal.unwrap(), "wang,wang,wang");
        } else {
            assert_eq!(&equal.unwrap_err().to_string(), "wang,wang,wang");
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant