aboutsummaryrefslogtreecommitdiff
path: root/js/test
diff options
context:
space:
mode:
Diffstat (limited to 'js/test')
-rw-r--r--js/test/cache.js8
-rw-r--r--js/test/compat/cache.js58
-rw-r--r--js/test/compat/default-filters.js73
-rw-r--r--js/test/compat/errors.js34
-rw-r--r--js/test/compat/filters.js59
-rw-r--r--js/test/compat/template.js33
-rw-r--r--js/test/compat/test.html29
-rw-r--r--js/test/default-filters.js2
-rw-r--r--js/test/errors.js6
-rw-r--r--js/test/filters.js12
-rw-r--r--js/test/template.js8
11 files changed, 304 insertions, 18 deletions
diff --git a/js/test/cache.js b/js/test/cache.js
index 22490c1..9395223 100644
--- a/js/test/cache.js
+++ b/js/test/cache.js
@@ -3,7 +3,7 @@
var assert = chai.assert;
it('cache', function() {
- var cache = new LuigiTemplate.Cache({
+ var cache = new Luigi.Cache({
foo: 'foo%{bar}',
});
@@ -15,7 +15,7 @@
});
it('cache with array', function() {
- var cache = new LuigiTemplate.Cache({
+ var cache = new Luigi.Cache({
foo: ['foo%{bar}'],
});
@@ -27,7 +27,7 @@
});
it('cache singleton', function() {
- var cache = LuigiTemplate.cache({
+ var cache = Luigi.cache({
foo: 'foo%{bar}',
});
@@ -40,7 +40,7 @@
it('cache with custom filters', function() {
- var cache = LuigiTemplate.cache({
+ var cache = Luigi.cache({
foo: ['foo%{bar | cache-barify}'],
}, {
'cache-barify': function(s) {
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: '&lt;&gt;&amp;&quot;&apos;',
+ }, {
+ 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&lt;');
+ });
+
+ 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>
+
diff --git a/js/test/default-filters.js b/js/test/default-filters.js
index f897485..26b4a44 100644
--- a/js/test/default-filters.js
+++ b/js/test/default-filters.js
@@ -67,7 +67,7 @@
expect: 'length of bar: 3'
}].forEach(function(row) {
it('default filter: ' + row.filter, function() {
- assert.equal(LuigiTemplate.run(row.template, row.args), row.expect);
+ assert.equal(Luigi.run(row.template, row.args), row.expect);
});
});
})();
diff --git a/js/test/errors.js b/js/test/errors.js
index 3ce96d5..467a6c3 100644
--- a/js/test/errors.js
+++ b/js/test/errors.js
@@ -4,7 +4,7 @@
it('unknown key error', function() {
assert.throws(function() {
- var r = LuigiTemplate.run('foo%{unknown-key}', {
+ var r = Luigi.run('foo%{unknown-key}', {
bar: 'foo',
});
}, Error, /^unknown key/);
@@ -12,7 +12,7 @@
it('unknown filter error', function() {
assert.throws(function() {
- var r = LuigiTemplate.run('foo%{bar | unknown-filter}', {
+ var r = Luigi.run('foo%{bar | unknown-filter}', {
bar: 'foo',
});
}, Error, /^unknown filter/);
@@ -20,7 +20,7 @@
it('unknown template error', function() {
assert.throws(function() {
- var cache = new LuigiTemplate.Cache({
+ var cache = new Luigi.Cache({
foo: [
'foo%{bar}',
],
diff --git a/js/test/filters.js b/js/test/filters.js
index c9ba21a..9bf1a1c 100644
--- a/js/test/filters.js
+++ b/js/test/filters.js
@@ -3,14 +3,14 @@
var assert = chai.assert;
it('filter', function() {
- var t = new LuigiTemplate('foo%{bar|h}'),
+ var t = new Luigi.Template('foo%{bar|h}'),
r = t.run({ bar: '<' });
assert.equal(r, 'foo&lt;');
});
it('filter chain', function() {
- var r = LuigiTemplate.run('foo%{bar|uc|lc}', {
+ var r = Luigi.run('foo%{bar|uc|lc}', {
bar: 'foo'
});
@@ -18,11 +18,11 @@
});
it('custom global filter', function() {
- LuigiTemplate.FILTERS.barify = function(s) {
+ Luigi.FILTERS.barify = function(s) {
return 'bar-' + s + '-bar';
};
- var r = LuigiTemplate.run('foo%{bar | barify}', {
+ var r = Luigi.run('foo%{bar | barify}', {
bar: 'foo'
});
@@ -30,7 +30,7 @@
});
it('custom template filter', function() {
- var r = LuigiTemplate.run('foo%{bar | barify}', {
+ var r = Luigi.run('foo%{bar | barify}', {
bar: 'foo'
}, {
barify: function(s) {
@@ -42,7 +42,7 @@
});
it('filter args', function() {
- var r = LuigiTemplate.run('foo%{bar | wrap bar}', {
+ var r = Luigi.run('foo%{bar | wrap bar}', {
bar: 'foo'
}, {
wrap: function(s, args) {
diff --git a/js/test/template.js b/js/test/template.js
index 9a0018f..a364c61 100644
--- a/js/test/template.js
+++ b/js/test/template.js
@@ -3,20 +3,20 @@
var assert = chai.assert;
it('run', function() {
- var t = new LuigiTemplate('foo%{bar}'),
+ var t = new Luigi.Template('foo%{bar}'),
r = t.run({ bar: 'foo' });
assert.equal(r, 'foofoo');
});
it('run singleton', function() {
- var r = LuigiTemplate.run('foo%{bar}', { bar: 'foo' });
+ var r = Luigi.run('foo%{bar}', { bar: 'foo' });
assert.equal(r, 'foofoo');
});
it('run with multiple keys', function() {
- var r = LuigiTemplate.run('foo%{bar}%{baz}', {
+ var r = Luigi.run('foo%{bar}%{baz}', {
bar: 'foo',
baz: 'foo',
});
@@ -26,7 +26,7 @@
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' });
+ r = Luigi.run(s, { bar: 'foo' });
assert.equal(r, 'foofoofoo');
});