Skip to content

Commit

Permalink
fix(Template): allow rendering boolean values as leafs
Browse files Browse the repository at this point in the history
Fixes #60.
  • Loading branch information
Kevin Butler committed Aug 12, 2018
1 parent d60f451 commit 7723f7b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ impl<'a> RenderContext<'a> {
try!(self.render(wr, stack, &tokens));
}

Data::Bool(val) => {
let s = if val { "true" } else { "false" };
try!(self.write_tracking_newlines(wr, s));
}

ref value => {
bug!("render_utag: unexpected value {:?}", value);
}
Expand Down
45 changes: 45 additions & 0 deletions tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,51 @@ mod context_search {
assert_eq!(rendered, expected);
}

#[test]
fn renders_bool() {
let template = compile_str(
"{{bool}}
#map{{#outer}}
{{#bool}}
#bool
#vec{{#inner}}{{{.}}}{{/inner}}/vec
{{/bool}}\n\
{{^not_ok}}
^not_ok
#vec{{#inner}}{{{.}}}{{/inner}}/vec
{{/not_ok}}\n\
{{/outer}}
/map
{{ok}}");
let ctx = MapBuilder::new()
.insert_bool("bool", false)
.insert_bool("not_ok", false)
.insert_map("outer", |map| {
map.insert_bool("bool", true)
.insert_vec("inner", |vec| {
vec.push_bool(false)
.push_bool(true)
.push_bool(false)
})
})
.insert_bool("ok", true)
.build();

let expected = "false
#map
#bool
#vecfalsetruefalse/vec
^not_ok
#vecfalsetruefalse/vec
/map
true";
let rendered = render_data(&template, &ctx);
println!("{}\n----\n{}", rendered, expected);
assert_eq!(rendered, expected);
}

#[test]
fn from_base() {
let template = "\
Expand Down

0 comments on commit 7723f7b

Please sign in to comment.