Skip to content

Commit

Permalink
liquid::parse_from_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 #57
  • Loading branch information
pop committed Aug 14, 2016
1 parent e006e59 commit 3ed3ed3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ 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::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use error::Result;

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(fp: &str, 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)
}
49 changes: 49 additions & 0 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ fn compare(name: &str, context: &mut Context) {
difference::assert_diff(&comp, &output.unwrap(), " ", 0);
}

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 chained_filters() {
let mut context = Context::new();
Expand Down Expand Up @@ -51,3 +66,37 @@ pub fn include_with_context() {

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

#[test]
pub fn graceful_fail_when_nonexistent_file() {
// TODO: Write this test
}

#[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 3ed3ed3

Please sign in to comment.