#[macro_use]
mod r#macro;
use crate::process::stack::StackFrame;
use crate::{Environment, Node, Stuart};
use std::path::PathBuf;
define_testcases![
for_loop_markdown,
for_loop_json_file,
for_loop_json_object,
for_loop_nested,
for_loop_skip_limit,
dateformat,
excerpt,
ifdefined,
conditionals,
markdown_functions,
escape
];
pub struct Testcase {
context: Node,
input: Node,
output: Node,
}
impl Testcase {
pub fn new(name: &str) -> Self {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("src/tests/testcases")
.join(name);
let mut context = load_base();
let specific_context = Node::create_from_dir(&path, true, None).unwrap();
context.merge(specific_context).unwrap();
let input = Node::create_from_file(path.join("in.html"), true, None).unwrap();
let output = Node::create_from_file(path.join("out"), false, None).unwrap();
match context {
Node::Directory {
ref mut children, ..
} => children.push(input.clone()),
_ => panic!("Base node is not a directory"),
}
Self {
context,
input,
output,
}
}
pub fn run(&self) {
let mut stuart = Stuart::new_from_node(self.context.clone());
stuart.base = Some(StackFrame::new("base"));
let env = Environment {
vars: &[],
root: self
.context
.get_at_path(&PathBuf::from("root.html"))
.unwrap()
.parsed_contents()
.tokens(),
md: self
.context
.get_at_path(&PathBuf::from("md.html"))
.unwrap()
.parsed_contents()
.tokens(),
};
let out = self.input.process(&stuart, env).unwrap();
match (&out, &self.output) {
(
Node::File { contents, .. },
Node::File {
contents: expected_contents,
..
},
) => {
assert_eq!(
std::str::from_utf8(contents)
.unwrap()
.replace(['\n', '\r'], ""),
std::str::from_utf8(expected_contents)
.unwrap()
.replace(['\n', '\r'], "")
);
}
_ => panic!("Not both files"),
}
}
}
fn load_base() -> Node {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/tests/testcases/_base");
Node::create_from_dir(path, true, None).unwrap()
}