From 26a752baeaeac57c387f942c30e7415c4c8cdfd0 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 4 Apr 2020 18:49:17 -0400 Subject: [PATCH] Added a simple javascript "shim" to invoke dustjs for later use with testing --- src/js/README.md | 9 +++++++ src/js/dustjs_shim.js | 29 +++++++++++++++++++++ src/js/test_cases/hello_world/input1.json | 1 + src/js/test_cases/hello_world/input2.json | 1 + src/js/test_cases/hello_world/template.dust | 1 + 5 files changed, 41 insertions(+) create mode 100644 src/js/README.md create mode 100644 src/js/dustjs_shim.js create mode 100644 src/js/test_cases/hello_world/input1.json create mode 100644 src/js/test_cases/hello_world/input2.json create mode 100644 src/js/test_cases/hello_world/template.dust diff --git a/src/js/README.md b/src/js/README.md new file mode 100644 index 0000000..70a0fdd --- /dev/null +++ b/src/js/README.md @@ -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 +``` diff --git a/src/js/dustjs_shim.js b/src/js/dustjs_shim.js new file mode 100644 index 0000000..83b1ba8 --- /dev/null +++ b/src/js/dustjs_shim.js @@ -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); + } +}); diff --git a/src/js/test_cases/hello_world/input1.json b/src/js/test_cases/hello_world/input1.json new file mode 100644 index 0000000..ad7288d --- /dev/null +++ b/src/js/test_cases/hello_world/input1.json @@ -0,0 +1 @@ +{"name": "Bob"} diff --git a/src/js/test_cases/hello_world/input2.json b/src/js/test_cases/hello_world/input2.json new file mode 100644 index 0000000..54011f2 --- /dev/null +++ b/src/js/test_cases/hello_world/input2.json @@ -0,0 +1 @@ +{"name": "Alice"} diff --git a/src/js/test_cases/hello_world/template.dust b/src/js/test_cases/hello_world/template.dust new file mode 100644 index 0000000..09f2e19 --- /dev/null +++ b/src/js/test_cases/hello_world/template.dust @@ -0,0 +1 @@ +Hello {name}!