use platelet::{
render,
renderer::{RenderError, RenderErrorKind},
};
use serde_json::{json, Map};
#[test]
fn templateless_text_node() {
let vars = json!({ "hello": "world" });
let result = render(&vars, "
nothing here
".into());
assert_eq!(result.unwrap(), "
nothing here
");
}
#[test]
fn templateless_html_doc() {
let vars = json!({ "hello": "world" });
let result = render(
&vars,
"a".into(),
);
assert_eq!(
result.unwrap(),
"a"
);
}
#[test]
fn templateess_table_fragment() {
let vars = Map::new().into();
let result = render(
&vars,
"
Data 0
Value 0
\
Data 1
Value 1
"
.into(),
);
assert_eq!(
result.unwrap(),
"
Data 0
Value 0
\
Data 1
Value 1
"
);
}
#[test]
fn templated_text_node() {
let vars = json!({ "hello": "world" });
let result = render(&vars, "
{{hello}}
".into());
assert_eq!(result.unwrap(), "
world
");
}
#[test]
fn complex_text_node() {
let vars = json!({ "user": {"firstname": "Yuri", "lastname" : "Gagarin" } });
let result = render(
&vars,
"
Dear {{user.firstname}} {{user.lastname}},
".into(),
);
assert_eq!(result.unwrap(), "
Dear Yuri Gagarin,
");
}
#[test]
fn text_node_with_expressions() {
let vars = json!({ "countries": [ "portugal" ] });
let result = render(&vars, "
{{countries[0]}} {{ 1 + 2 }}
".into());
assert_eq!(result.unwrap(), "
portugal 3
");
}
#[test]
fn pl_if() {
let vars = Map::new().into();
let result = render(
&vars,
"
this
\
not this
\
also this
"
.into(),
);
assert_eq!(result.unwrap(), "
this
also this
");
}
#[test]
fn pl_else_if_true() {
let vars = Map::new().into();
let result = render(
&vars,
"
this
\
not this
\
also this
"
.into(),
);
assert_eq!(result.unwrap(), "
this
also this
");
}
#[test]
fn pl_else_if_false() {
let vars = Map::new().into();
let result = render(
&vars,
"
this
\
not this
\
also this
"
.into(),
);
assert_eq!(result.unwrap(), "
this
");
}
#[test]
fn pl_is() {
let vars = json!({ "header": true });
let result = render(&vars, "
this
".into());
assert_eq!(result.unwrap(), "
this
");
}
#[test]
fn pl_html() {
let vars = json!({ "content": "
hello world
" });
let result = render(
&vars,
"something that used to be here".into(),
);
assert_eq!(result.unwrap(), "
hello world
");
}
#[test]
fn pl_html_with_vars_are_not_rendered() {
let vars = json!({ "content": "
hello {{mistake}} world
" });
let result = render(
&vars,
"something that used to be here".into(),
);
assert_eq!(
result.unwrap(),
"
hello {{mistake}} world
"
);
}
#[test]
fn pl_html_with_template() {
let vars = json!({ "content": "
hello world
" });
let result = render(
&vars,
"something that used to be here".into(),
);
assert_eq!(result.unwrap(), "
hello world
");
}
#[test]
fn template_preserved() {
let vars = Map::new().into();
let result = render(&vars, "
hello
".into());
assert_eq!(result.unwrap(), "
hello
");
}
#[test]
fn pl_for() {
let vars = Map::new().into();
let result = render(
&vars,
"
{{x}}
".into(),
);
assert_eq!(result.unwrap(), "
1
2
3
");
}
#[test]
fn pl_for_template() {
let vars = Map::new().into();
let result = render(
&vars,
"
{{x}}
".into(),
);
assert_eq!(result.unwrap(), "
1
2
3
");
}
#[test]
fn pl_if_template() {
let vars = Map::new().into();
let result = render(
&vars,
"
hello
world
".into(),
);
assert_eq!(result.unwrap(), "
hello
world
");
}
#[test]
fn loop_with_if_else() {
let vars = Map::new().into();
let result = render(
&vars,
"
A
\
B
\
C
\
Not A/B/C
"
.into(),
);
assert_eq!(result.unwrap(), "
B
B
");
}
#[test]
fn pl_else_true() {
let vars = Map::new().into();
let result = render(&vars, r#"
A
B
"#.into());
assert_eq!(result.unwrap(), "
A
");
}
#[test]
fn pl_else_false() {
let vars = Map::new().into();
let result = render(&vars, r#"
A
B
"#.into());
assert_eq!(result.unwrap(), "
B
");
}
#[test]
fn caret_attr_eval() {
let vars = Map::new().into();
let result = render(&vars, r#""#.into());
assert_eq!(result.unwrap(), "");
}
#[test]
fn correct_escaping() {
let vars = json!({"x": "<TAG>"});
let result = render(&vars, r#""#.into());
assert_eq!(result.unwrap(), "<TAG>");
}
#[test]
fn caret_attr_false() {
let vars = Map::new().into();
let result = render(&vars, r#""#.into());
assert_eq!(result.unwrap(), "");
}
#[test]
fn caret_attr_array() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(result.unwrap(), "");
}
#[test]
fn caret_attr_object() {
let vars = json!({ "classes": { "should-have": true, "should-not-have": null, "should-also-have": 1 } });
let result = render(&vars, r#""#.into());
assert_eq!(
result.unwrap(),
""
);
}
#[test]
fn comments_uneffected() {
let vars = Map::new().into();
let result = render(&vars, r#""#.into());
assert_eq!(result.unwrap(), "");
}
#[test]
fn order_unchanged() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(result.unwrap(), "");
}
#[test]
fn for_loop_array_index() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(
result.unwrap(),
""
);
}
#[test]
fn for_loop_kv() {
let vars = json!({"fields": {"first-name": "First Name", "last-name": "Last Name"}});
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(
result.unwrap(),
"\
"
);
}
#[test]
fn for_loop_kvi() {
let vars = json!({"fields": {"first-name": "First Name", "last-name": "Last Name"}});
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(
result.unwrap(),
"\
"
);
}
#[test]
fn for_loop_if_else_if() {
let vars = Map::new().into();
let result = render(
&vars,
"
A
\
B
\
C
"
.into(),
);
assert_eq!(
result.unwrap(),
"
B
B
B
C
"
);
}
#[test]
fn bad_pl_is_name() {
let vars = Map::new().into();
let result = render(&vars, "".into());
assert_eq!(
result.unwrap_err(),
RenderError {
filename: "input".to_owned(),
kind: RenderErrorKind::BadPlIsName("".to_string())
}
);
}
#[test]
fn pl_for_and_pl_is() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(result.unwrap(), "");
}
#[test]
fn only_include_styles_once() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(result.unwrap(), "");
}
#[test]
fn include_styles_or_scripts_twice_different_args() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(
result.unwrap(),
""
);
}
#[test]
fn no_script_injection() {
let vars = json!({"username": "'); alert('you have been hacked'); let x = ('"});
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(
result.unwrap(),
""
);
}
#[test]
fn no_html_text_injection() {
let vars =
json!({ "username": "username'" });
let result = render(&vars, r#"
{{username}}
"#.into());
assert_eq!(
result.unwrap(),
"
username</div><script>alert('you have been hacked');</script>'
"
);
}
#[test]
fn pl_else_if_template() {
let vars = Map::new().into();
let result = render(
&vars,
r#"
hello
this"#.into(),
);
assert_eq!(result.unwrap(), "this");
}
#[test]
fn pl_else_template() {
let vars = Map::new().into();
let result = render(
&vars,
r#"
hello
this"#.into(),
);
assert_eq!(result.unwrap(), "this");
}
#[test]
fn pl_is_and_pl_html() {
let vars = Map::new().into();
let result = render(
&vars,
r#""#.into(),
);
assert_eq!(result.unwrap(), "");
}
#[test]
fn pl_template_and_pl_if() {
let vars = Map::new().into();
let result = render(&vars, r#"{{ 99 }}"#.into());
assert_eq!(result.unwrap(), "99");
}