Files
zen/core/template/tests/template.rs
T
stefan-gorules daecf901e6 feat: custom node (#138)
* feat: custom node

* add zen template, expose $nodes and $root

* add custom handler to go and nodejs

* add support for python, improve bindings, add error to zen template

* fix: correct binding exports for nodejs and python

* fix benchmark

* improve rust api, trim template in zen templates

* update cargo action

* update expression version

* compile action

* fix action format
2024-04-03 16:09:51 +02:00

101 lines
2.6 KiB
Rust

use serde_json::{json, Value};
use zen_template::render;
#[test]
fn test_values_types() {
struct TestCase {
template: &'static str,
context: Value,
expected: Value,
}
let test_cases = vec![
TestCase {
template: "{{ null }}",
context: json!(null),
expected: json!(null),
},
TestCase {
template: "{{ 1 + 1 }}",
context: json!(null),
expected: json!(2),
},
TestCase {
template: "{{ 'hello' }}",
context: json!(null),
expected: json!("hello"),
},
TestCase {
template: "{{ true or false }}",
context: json!(null),
expected: json!(true),
},
TestCase {
template: "{{ [1, 2, 3] }}",
context: json!(null),
expected: json!([1, 2, 3]),
},
TestCase {
template: "{{ customer }}",
context: json!({ "customer": { "firstName": "John", "lastName": "Doe" } }),
expected: json!({ "firstName": "John", "lastName": "Doe" }),
},
];
for test_case in test_cases {
assert_eq!(
render(test_case.template, &test_case.context).unwrap(),
test_case.expected
);
}
}
#[test]
fn test_interpolation() {
struct TestCase {
template: &'static str,
context: Value,
expected: Value,
}
let test_cases = vec![
TestCase {
template: "{{ null }}s",
context: json!(null),
expected: json!("nulls"),
},
TestCase {
template: "{{ 1 + 1 }} hello",
context: json!(null),
expected: json!("2 hello"),
},
TestCase {
template: "{{ 'hello' }} world",
context: json!(null),
expected: json!("hello world"),
},
TestCase {
template: "{{ true or false }} test",
context: json!(null),
expected: json!("true test"),
},
TestCase {
template: "{{ [1, 2, 3] }} array",
context: json!(null),
expected: json!("[1,2,3] array"),
},
TestCase {
template: "Customer: {{ customer }}",
context: json!({ "customer": { "firstName": "John", "lastName": "Doe" } }),
expected: json!(r#"Customer: {"firstName":"John","lastName":"Doe"}"#),
},
];
for test_case in test_cases {
assert_eq!(
render(test_case.template, &test_case.context).unwrap(),
test_case.expected
);
}
}