Added a simple javascript "shim" to invoke dustjs for later use with testing
This commit is contained in:
parent
d82224ecd1
commit
26a752baea
9
src/js/README.md
Normal file
9
src/js/README.md
Normal 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
29
src/js/dustjs_shim.js
Normal 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);
|
||||
}
|
||||
});
|
1
src/js/test_cases/hello_world/input1.json
Normal file
1
src/js/test_cases/hello_world/input1.json
Normal file
@ -0,0 +1 @@
|
||||
{"name": "Bob"}
|
1
src/js/test_cases/hello_world/input2.json
Normal file
1
src/js/test_cases/hello_world/input2.json
Normal file
@ -0,0 +1 @@
|
||||
{"name": "Alice"}
|
1
src/js/test_cases/hello_world/template.dust
Normal file
1
src/js/test_cases/hello_world/template.dust
Normal file
@ -0,0 +1 @@
|
||||
Hello {name}!
|
Loading…
Reference in New Issue
Block a user