Add support for supplying multiple templates to the dustjs shim to support testing partials.

This commit is contained in:
Tom Alexander
2020-04-10 19:07:02 -04:00
parent 608c55575e
commit 7e0a617ba6
5 changed files with 40 additions and 14 deletions

View File

@@ -1,9 +1,24 @@
extern crate nom;
use parser::template;
use std::io::{self, Read};
mod parser;
fn main() {
let parsed_template = template("{#foo.bar}hello {name}{/foo.bar}!");
println!("{:?}", parsed_template);
let context = read_context_from_stdin();
println!("{:?}", context);
}
fn read_context_from_stdin() -> serde_json::map::Map<String, serde_json::Value> {
let mut buffer = String::new();
io::stdin()
.read_to_string(&mut buffer)
.expect("Failed to read stdin");
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
match parsed {
serde_json::Value::Object(obj) => obj,
_ => panic!("Expected context to be an object"),
}
}

View File

@@ -5,5 +5,7 @@ Install LinkedIn's DustJS in order to rust the compliance tests
** Running
The dustjs_shim expects a path to a template file as the first parameter, and it expects a json context over stdin. For example, to invoke it on the command line you could run:
```sh
cat test_cases/hello_world/input1.json | node dustjs_shim.js test_cases/hello_world/template.dust
cat test_cases/hello_world/input1.json | node dustjs_shim.js test_cases/hello_world/main.dust
```
Each template will be compiled with the same name as the file without the file extension (so main.dust will be compiled as "main"). THe shim renders the first template passed into it (all the others can be accessed as partials).

View File

@@ -1,24 +1,31 @@
var dust = require('dustjs-linkedin');
var fs = require('fs');
const path = require('path');
var argv = process.argv.slice(2);
if (argv.length != 1) {
if (argv.length < 1) {
console.error("Expecting only 1 argument (a path to a template)");
process.exit(1);
}
var context = JSON.parse(fs.readFileSync(0, 'utf-8'));
var main_template = path.parse(argv[0])["name"];
for (var i = 0, len = argv.length; i < len; ++i) {
var filename = path.parse(argv[i])["name"];
try {
var template_source = fs.readFileSync(argv[i], 'utf-8');
} catch (err) {
console.error(err);
process.exit(1);
}
var compiled_template = dust.compile(template_source, filename);
dust.loadSource(compiled_template);
try {
var template_source = fs.readFileSync(argv[0], 'utf-8');
} catch (err) {
console.error(err);
process.exit(1);
}
var compiled_template = dust.compile(template_source, "tmpl");
dust.loadSource(compiled_template);
dust.render("tmpl", context, function(err, out) {
dust.render(main_template, context, function(err, out) {
if(err) {
console.error(err);
process.exit(1);