implement string equality comparison & test
This commit is contained in:
parent
d8e8ffac55
commit
5f89f143d1
19
src/lib.rs
19
src/lib.rs
@ -1,3 +1,4 @@
|
||||
#[macro_use]
|
||||
extern crate handlebars;
|
||||
use handlebars::{Context, Output, Handlebars, Helper, RenderContext, RenderError};
|
||||
|
||||
@ -10,8 +11,11 @@ fn json_format_helper(helper: &Helper, _: &Handlebars, _: &Context, _: &mut Rend
|
||||
Ok(())
|
||||
}
|
||||
|
||||
handlebars_helper!(eqs: |x: str, y: str| x == y);
|
||||
|
||||
pub fn register(handle: &mut Handlebars) {
|
||||
handle.register_helper("JSONstringify", Box::new(json_format_helper));
|
||||
handle.register_helper("eqs", Box::new(eqs));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -24,11 +28,24 @@ mod tests {
|
||||
let mut handlebars = Handlebars::new();
|
||||
register(&mut handlebars);
|
||||
|
||||
println!("{:?}", handlebars.render_template("FOO: {{JSONstringify foo}}", &json!({
|
||||
println!("{:?}", handlebars.render_template("{{JSONstringify foo}}", &json!({
|
||||
"foo": {
|
||||
"bar": 1,
|
||||
"baz": 2
|
||||
}
|
||||
})).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eqs () {
|
||||
let mut handlebars = Handlebars::new();
|
||||
register(&mut handlebars);
|
||||
|
||||
assert_eq!(
|
||||
handlebars.render_template("{{#if (eqs page \"login\")}}login{{/if}}", &json!({
|
||||
"page": "login"
|
||||
})).unwrap(),
|
||||
"login"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user