Skip to content

Commit

Permalink
liquid::parse_file(...) method
Browse files Browse the repository at this point in the history
Implements basic expected functionality:
- Takes as arument a filepath (&str) and LiquidOptions.
- Returns Result<Template>
Includes basics tests (extends fixtures.rs).
Includes doc-string documentation.
fixes cobalt-org#57
  • Loading branch information
pop committed Aug 16, 2016
1 parent e006e59 commit 247ca16
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ use lexer::Element;
use tags::{assign_tag, cycle_tag, include_tag, break_tag, continue_tag, comment_block, raw_block,
for_block, if_block, unless_block, capture_block, case_block};
use std::default::Default;
use std::path::PathBuf;
use std::fs::File;
use std::io::prelude::Read;
use std::path::{PathBuf, Path};
use error::Result;

pub use value::Value;
Expand Down Expand Up @@ -214,3 +216,36 @@ pub fn parse(text: &str, options: LiquidOptions) -> Result<Template> {
let tokens = try!(lexer::tokenize(&text));
parser::parse(&tokens, &options).map(Template::new)
}

/// Parse a liquid template from a file, returning Result<Template, io::Error>
/// # Examples
///
/// ## Minimal Template
///
/// template.liquid:
/// ```
/// "Liquid! {{num | minus: 2}}"
/// ```
///
/// Your rust code:
/// ```
/// use liquid::{Renderable, LiquidOptions, Context};
///
/// let template = liquid::parse_file("/path/to/template.liquid",
/// LiquidOptions::default()).unwrap();
/// let mut data = Context::new();
/// data.set_val("num", Value::Num(4f32));
/// let output = template.render(&mut data);
/// assert_eq!(output.unwrap(), Some("Liquid! 2".to_string()));
/// ```
pub fn parse_file<P: AsRef<Path>>(fp: P, options: LiquidOptions) -> Result<Template> {
let mut options = options;
options.register_known_blocks();

let mut f = try!(File::open(fp));
let mut buf = String::new();
try!(f.read_to_string(&mut buf));

let tokens = try!(lexer::tokenize(&buf));
parser::parse(&tokens, &options).map(Template::new)
}
62 changes: 62 additions & 0 deletions tests/parse_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
extern crate difference;
extern crate liquid;

use std::fs::File;
use std::io::Read;
use liquid::*;

// README: The compare_by_file and following tests are almost line for line carbon-copies of the
// tests in `fixutres.rs`. This might be overkill but keep that in mind when making changes to
// fixtures that might necessitate changes to the parse_file method tested here.

fn compare_by_file(name: &str, context: &mut Context) {
let input_file = format!("tests/fixtures/input/{}.txt", name);
let output_file = format!("tests/fixtures/output/{}.txt", name);

let options: LiquidOptions = Default::default();
let template = parse_file(&input_file, options).unwrap();

let output = template.render(context).unwrap();

let mut comp = String::new();
File::open(output_file).unwrap().read_to_string(&mut comp).unwrap();

difference::assert_diff(&comp, &output.unwrap(), " ", 0);
}

#[test]
pub fn error_on_nonexistent_file() {
// Assert that parsing a bogus file with default options Results in an error.
let options = Default::default();
let template = parse_file("not-a-file.ext", options);
assert_eq!(template.is_ok(), false);
}

#[test]
pub fn chained_filters_by_file() {
let mut context = Context::new();
context.set_val("foo", Value::Str("foofoo".to_owned()));
compare_by_file("chained_filters", &mut context)
}

#[test]
pub fn example_by_file() {
let mut context = Context::new();
context.set_val("num", Value::Num(5f32));
context.set_val("numTwo", Value::Num(6f32));
compare_by_file("example", &mut context)
}

#[test]
pub fn include_by_file() {
let mut context = Context::new();
compare_by_file("include", &mut context);
}

#[test]
pub fn include_with_context_by_file() {
let mut context = Context::new();
context.set_val("content", Value::Str("hello, world!".to_owned()));

compare_by_file("include_with_context", &mut context);
}

0 comments on commit 247ca16

Please sign in to comment.