diff options
author | Paul Duncan <pabs@pablotron.org> | 2018-09-09 03:50:48 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2018-09-09 03:50:48 -0400 |
commit | 2cff700e3e59adc2bfcd4b2c536675dfdeb96312 (patch) | |
tree | dfe4eb712a521e3a4bd5d4ae3113ab6f7053684f /js | |
parent | fc93b32955b3a30ad884e4a9b14ebe052b86f120 (diff) | |
download | luigi-template-2cff700e3e59adc2bfcd4b2c536675dfdeb96312.tar.bz2 luigi-template-2cff700e3e59adc2bfcd4b2c536675dfdeb96312.zip |
js: rm test.js, add test/{test.html,*.js} based on mocha/chai
Diffstat (limited to 'js')
-rw-r--r-- | js/test.js | 58 | ||||
-rw-r--r-- | js/test/cache.js | 32 | ||||
-rw-r--r-- | js/test/default-filters.js | 73 | ||||
-rw-r--r-- | js/test/errors.js | 34 | ||||
-rw-r--r-- | js/test/filters.js | 59 | ||||
-rw-r--r-- | js/test/template.js | 33 | ||||
-rw-r--r-- | js/test/test.html | 28 |
7 files changed, 259 insertions, 58 deletions
diff --git a/js/test.js b/js/test.js deleted file mode 100644 index 0fedfcd..0000000 --- a/js/test.js +++ /dev/null @@ -1,58 +0,0 @@ - -load('luigi-template.js'); - -// define custom template filter -function custom_filter(v) { - return "foo" + v + "bar"; -} - -function custom_filter_with_args(v, args) { - var i, l, r = [v]; - - for (i = 0, l = args.length; i < l; i++) - r.push(args[i]); - - return r.join(' and '); -} - -// add custom template filters -LuigiTemplate.FILTERS.custom = custom_filter; -LuigiTemplate.FILTERS.custom_args = custom_filter_with_args; - -// build template string -var template_str = [ - // test basic templates - "%{greet}, %{name}!", - - // test filters and filters with parameters - "Your name uppercase is: %{name|uc}", - - // test custom filter - "Your custom filtered name is: %{name|custom}", - - // test custom filter with arguments - "Your custom_args name is: %{name|custom_args foo bar baz}", - - // test whitespace in filters - "random test: %{name | lc }", - - // test pluralize filter - 'pluralize test (0): %{count_0} item%{count_0 | s}', - 'pluralize test (1): %{count_1} item%{count_1 | s}', - 'pluralize test (10): %{count_10} item%{count_10 | s}', - - // terminating newline - '' -].join("\n"); - -// build template -var t = new LuigiTemplate(template_str); - -// print results -print(t.run({ - greet: 'hello', - name: 'paul', - count_0: 0, - count_1: 1, - count_10: 10 -})); diff --git a/js/test/cache.js b/js/test/cache.js new file mode 100644 index 0000000..39dd46d --- /dev/null +++ b/js/test/cache.js @@ -0,0 +1,32 @@ +(function() { + 'use strict'; + var assert = chai.assert; + + it('cache', function() { + var cache = new LuigiTemplate.Cache({ + foo: ['foo%{bar}'], + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foofoo'); + }); + + it('cache with custom filters', function() { + var cache = new LuigiTemplate.Cache({ + foo: ['foo%{bar | barify}'], + }, { + barify: function(s) { + return 'bar-' + s + '-bar'; + }, + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foobar-foo-bar'); + }); +})(); diff --git a/js/test/default-filters.js b/js/test/default-filters.js new file mode 100644 index 0000000..f897485 --- /dev/null +++ b/js/test/default-filters.js @@ -0,0 +1,73 @@ +(function() { + 'use strict'; + var assert = chai.assert; + + [{ + filter: 'uc', + template: 'foo%{bar|uc}', + args: { + bar: 'foo', + }, + expect: 'fooFOO', + }, { + filter: 'lc', + template: 'foo%{bar|lc}', + args: { + bar: 'FOO' + }, + expect: 'foofoo', + }, { + filter: 'h', + template: '%{bar|h}', + args: { + bar: '<>&"\'', + }, + expect: '<>&"'', + }, { + filter: 'u', + template: '%{bar|u}', + args: { + bar: 'asdf<>&"\' \u000f', + }, + expect: 'asdf%3C%3E%26%22%27+%0F' + }, { + filter: 'json', + template: '%{bar|json}', + args: { + bar: { + true: true, + false: false, + null: null, + number: 5, + string: 'foo', + hash: { 'foo': 'bar' }, + array: [0, 1], + }, + }, + expect: '{"true":true,"false":false,"null":null,"number":5,"string":"foo","hash":{"foo":"bar"},"array":[0,1]}', + }, { + filter: 'trim', + template: '%{bar|trim}', + args: { bar: ' \t\v\r\nfoo \t\v\r\n' }, + expect: 'foo' + }, { + filter: 's', + template: 'one foo%{foo|s}, two bar%{bar|s}', + args: { + foo: 1, + bar: 2, + }, + expect: 'one foo, two bars' + }, { + filter: 'length', + template: 'length of bar: %{bar|length}', + args: { + bar: [0, 1, 2], + }, + expect: 'length of bar: 3' + }].forEach(function(row) { + it('default filter: ' + row.filter, function() { + assert.equal(LuigiTemplate.run(row.template, row.args), row.expect); + }); + }); +})(); diff --git a/js/test/errors.js b/js/test/errors.js new file mode 100644 index 0000000..3ce96d5 --- /dev/null +++ b/js/test/errors.js @@ -0,0 +1,34 @@ +(function() { + 'use strict'; + var assert = chai.assert; + + it('unknown key error', function() { + assert.throws(function() { + var r = LuigiTemplate.run('foo%{unknown-key}', { + bar: 'foo', + }); + }, Error, /^unknown key/); + }); + + it('unknown filter error', function() { + assert.throws(function() { + var r = LuigiTemplate.run('foo%{bar | unknown-filter}', { + bar: 'foo', + }); + }, Error, /^unknown filter/); + }); + + it('unknown template error', function() { + assert.throws(function() { + var cache = new LuigiTemplate.Cache({ + foo: [ + 'foo%{bar}', + ], + }); + + var r = cache.run('unknown-template', { + bar: 'foo', + }); + }, Error, /^unknown template/); + }); +})(); diff --git a/js/test/filters.js b/js/test/filters.js new file mode 100644 index 0000000..c9ba21a --- /dev/null +++ b/js/test/filters.js @@ -0,0 +1,59 @@ +(function() { + 'use strict'; + var assert = chai.assert; + + it('filter', function() { + var t = new LuigiTemplate('foo%{bar|h}'), + r = t.run({ bar: '<' }); + + assert.equal(r, 'foo<'); + }); + + it('filter chain', function() { + var r = LuigiTemplate.run('foo%{bar|uc|lc}', { + bar: 'foo' + }); + + assert.equal(r, 'foofoo'); + }); + + it('custom global filter', function() { + LuigiTemplate.FILTERS.barify = function(s) { + return 'bar-' + s + '-bar'; + }; + + var r = LuigiTemplate.run('foo%{bar | barify}', { + bar: 'foo' + }); + + assert.equal(r, 'foobar-foo-bar'); + }); + + it('custom template filter', function() { + var r = LuigiTemplate.run('foo%{bar | barify}', { + bar: 'foo' + }, { + barify: function(s) { + return 'bar-' + s + '-bar'; + }, + }); + + assert.equal(r, 'foobar-foo-bar'); + }); + + it('filter args', function() { + var r = LuigiTemplate.run('foo%{bar | wrap bar}', { + bar: 'foo' + }, { + wrap: function(s, args) { + if (args.length == 1) { + return [args[0], s, args[0]].join('-'); + } else { + return s; + } + }, + }); + + assert.equal(r, 'foobar-foo-bar'); + }); +})(); diff --git a/js/test/template.js b/js/test/template.js new file mode 100644 index 0000000..9a0018f --- /dev/null +++ b/js/test/template.js @@ -0,0 +1,33 @@ +(function() { + 'use strict'; + var assert = chai.assert; + + it('run', function() { + var t = new LuigiTemplate('foo%{bar}'), + r = t.run({ bar: 'foo' }); + + assert.equal(r, 'foofoo'); + }); + + it('run singleton', function() { + var r = LuigiTemplate.run('foo%{bar}', { bar: 'foo' }); + + assert.equal(r, 'foofoo'); + }); + + it('run with multiple keys', function() { + var r = LuigiTemplate.run('foo%{bar}%{baz}', { + bar: 'foo', + baz: 'foo', + }); + + assert.equal(r, 'foofoofoo'); + }); + + it('run with whitespace around key', function() { + var s = "%{ \t\v\r\nbar}%{ \t\v\r\nbar \t\v\r\n}%{bar \t\v\r\n}", + r = LuigiTemplate.run(s, { bar: 'foo' }); + + assert.equal(r, 'foofoofoo'); + }); +})(); diff --git a/js/test/test.html b/js/test/test.html new file mode 100644 index 0000000..6448e10 --- /dev/null +++ b/js/test/test.html @@ -0,0 +1,28 @@ +<!DOCTYPE html> +<html lang='en-US'> + <head> + <meta charset='utf-8'/> + <title>Luigi Template Mocha Tests</title> + <link href='https://unpkg.com/mocha@5.2.0/mocha.css' rel='stylesheet' /> + </head> + + <body> + <div id='mocha'></div> + + <script src='../luigi-template.js'></script> + <script src='https://unpkg.com/chai/chai.js'></script> + <script src='https://unpkg.com/mocha@5.2.0/mocha.js'></script> + + <script>mocha.setup('bdd')</script> + <script src='template.js'></script> + <script src='filters.js'></script> + <script src='cache.js'></script> + <script src='errors.js'></script> + <script src='default-filters.js'></script> + <script> + mocha.checkLeaks(); + mocha.run(); + </script> + </body> +</html> + |