Added a simple javascript "shim" to invoke dustjs for later use with testing

This commit is contained in:
Tom Alexander 2020-04-04 18:49:17 -04:00
parent d82224ecd1
commit 26a752baea
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 41 additions and 0 deletions

9
src/js/README.md Normal file
View File

@ -0,0 +1,9 @@
* Compliance Tests
These tests run my implementation of LinkedIn's Dust and the official implementation of [LinkedIn's DustJS](https://www.dustjs.com) as compares the output to ensure they match.
** Setup
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
```

29
src/js/dustjs_shim.js Normal file
View File

@ -0,0 +1,29 @@
var dust = require('dustjs-linkedin');
var fs = require('fs');
var argv = process.argv.slice(2);
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'));
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) {
if(err) {
console.error(err);
process.exit(1);
} else {
console.log(out);
process.exit(0);
}
});

View File

@ -0,0 +1 @@
{"name": "Bob"}

View File

@ -0,0 +1 @@
{"name": "Alice"}

View File

@ -0,0 +1 @@
Hello {name}!