Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 65 additions & 7 deletions src/renderer/html_handlebars/helpers/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ pub fn next(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), R
mod tests {
use super::*;

static TEMPLATE: &'static str =
"{{#previous}}{{title}}: {{link}}{{/previous}}|{{#next}}{{title}}: {{link}}{{/next}}";

#[test]
fn test_next_previous() {
let template = "
{{#previous}}{{title}}: {{link}}{{/previous}}
{{#next}}{{title}}: {{link}}{{/next}}";

let data = json!({
"name": "two",
"path": "two.path",
Expand All @@ -169,8 +168,67 @@ mod tests {
h.register_helper("previous", Box::new(previous));
h.register_helper("next", Box::new(next));

assert_eq!(h.template_render(template, &data).unwrap(), "
one: one.html
three: three.html");
assert_eq!(
h.template_render(TEMPLATE, &data).unwrap(),
"one: one.html|three: three.html");
}

#[test]
fn test_first() {
let data = json!({
"name": "one",
"path": "one.path",
"chapters": [
{
"name": "one",
"path": "one.path"
},
{
"name": "two",
"path": "two.path",
},
{
"name": "three",
"path": "three.path"
}
]
});

let mut h = Handlebars::new();
h.register_helper("previous", Box::new(previous));
h.register_helper("next", Box::new(next));

assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually break this out into a couple lines. So at the top of the test I'll assign what I expect to a should_be variable, then assign what we actually get to got. Then at the bottom you can write assert_eq!(got, should_be); (e.g. config.rs).

It's definitely a personal taste thing, but it's nice when you can look at the top and figure out what the expected output should be at a glance without needing to look for all assert_eq!() calls.

h.template_render(TEMPLATE, &data).unwrap(),
"|two: two.html");
}
#[test]
fn test_last() {
let data = json!({
"name": "three",
"path": "three.path",
"chapters": [
{
"name": "one",
"path": "one.path"
},
{
"name": "two",
"path": "two.path",
},
{
"name": "three",
"path": "three.path"
}
]
});

let mut h = Handlebars::new();
h.register_helper("previous", Box::new(previous));
h.register_helper("next", Box::new(next));

assert_eq!(
h.template_render(TEMPLATE, &data).unwrap(),
"two: two.html|");
}
}