diff options
Diffstat (limited to 'js/test/compat')
-rw-r--r-- | js/test/compat/cache.js | 58 | ||||
-rw-r--r-- | js/test/compat/default-filters.js | 73 | ||||
-rw-r--r-- | js/test/compat/errors.js | 34 | ||||
-rw-r--r-- | js/test/compat/filters.js | 59 | ||||
-rw-r--r-- | js/test/compat/template.js | 33 | ||||
-rw-r--r-- | js/test/compat/test.html | 29 |
6 files changed, 286 insertions, 0 deletions
diff --git a/js/test/compat/cache.js b/js/test/compat/cache.js new file mode 100644 index 0000000..22490c1 --- /dev/null +++ b/js/test/compat/cache.js @@ -0,0 +1,58 @@ +(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 array', function() { + var cache = new LuigiTemplate.Cache({ + foo: ['foo%{bar}'], + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foofoo'); + }); + + it('cache singleton', function() { + var cache = LuigiTemplate.cache({ + foo: 'foo%{bar}', + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foofoo'); + }); + + + it('cache with custom filters', function() { + var cache = LuigiTemplate.cache({ + foo: ['foo%{bar | cache-barify}'], + }, { + 'cache-barify': function(s) { + return 'bar-' + s + '-bar'; + }, + }); + + // run template from cache, get result + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foobar-foo-bar'); + }); +})(); diff --git a/js/test/compat/default-filters.js b/js/test/compat/default-filters.js new file mode 100644 index 0000000..f897485 --- /dev/null +++ b/js/test/compat/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/compat/errors.js b/js/test/compat/errors.js new file mode 100644 index 0000000..3ce96d5 --- /dev/null +++ b/js/test/compat/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/compat/filters.js b/js/test/compat/filters.js new file mode 100644 index 0000000..c9ba21a --- /dev/null +++ b/js/test/compat/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/compat/template.js b/js/test/compat/template.js new file mode 100644 index 0000000..9a0018f --- /dev/null +++ b/js/test/compat/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/compat/test.html b/js/test/compat/test.html new file mode 100644 index 0000000..6254e4c --- /dev/null +++ b/js/test/compat/test.html @@ -0,0 +1,29 @@ +<!DOCTYPE html> +<html lang='en-US'> + <head> + <meta charset='utf-8'/> + <title>Luigi Template Compatibility 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='../../luigi-compat.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> + |