From de4a7a4da2bdc03532f1f3a302326ad1e1ce481d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 2 Dec 2024 16:24:04 +0100 Subject: [PATCH] Convert tests to esm --- test/array.js | 92 ++++++++--- test/code.js | 115 ++++++++------ test/collection.js | 64 ++++---- test/comparison.js | 282 +++++++++++++++++++--------------- test/examples/index.js | 120 +++++++++------ test/helpers.js | 34 ++-- test/html.js | 139 +++++++++++------ test/i18n.js | 42 +++-- test/inflection.js | 28 ++-- test/integration/templates.js | 87 +++++++---- test/match.js | 56 ++++--- test/math.js | 38 ++--- test/misc.js | 46 +++--- test/number.js | 68 ++++---- test/object.js | 123 ++++++++------- test/path.js | 80 ++++++---- test/string.js | 257 +++++++++++++++++-------------- test/subexpressions.js | 14 +- test/support/index.js | 24 ++- test/url.js | 54 ++++--- test/utils.js | 37 +++-- test/uuid.js | 11 +- 22 files changed, 1051 insertions(+), 760 deletions(-) diff --git a/test/array.js b/test/array.js index a24d7642..a58511d8 100644 --- a/test/array.js +++ b/test/array.js @@ -9,7 +9,10 @@ import { array as _array, string, object } from '..'; _array({ handlebars: hbs }); string({ handlebars: hbs }); -var context = { array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], duplicate: ['a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g'] }; +var context = { + array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], + duplicate: ['a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g'] +}; describe('array', function() { describe('after', function() { @@ -31,7 +34,10 @@ describe('array', function() { describe('arrayify', function() { it('should arrayify a value', function() { equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')('foo'), 'foo'); - equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']), 'foo'); + equal( + hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']), + 'foo' + ); }); }); @@ -53,9 +59,14 @@ describe('array', function() { }); describe('eachIndex', function() { - it('should render the block using the array and each item\'s index', function() { - var fn = hbs.compile('{{#eachIndex array}} {{item}} is {{index}} {{/eachIndex}}'); - equal(fn(context), ' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 '); + it("should render the block using the array and each item's index", function() { + var fn = hbs.compile( + '{{#eachIndex array}} {{item}} is {{index}} {{/eachIndex}}' + ); + equal( + fn(context), + ' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 ' + ); }); }); @@ -97,7 +108,6 @@ describe('array', function() { }); it('should render a block for each object that has a "first" property with the value "d"', function() { - var ctx = { collection: [ { first: 'aaa', last: 'bbb' }, @@ -111,7 +121,8 @@ describe('array', function() { ] }; - var source = '{{#filter collection "d" property="first"}}{{this.first}}{{else}}ZZZ{{/filter}}'; + var source = + '{{#filter collection "d" property="first"}}{{this.first}}{{else}}ZZZ{{/filter}}'; var fn = hbs.compile(source); equal(fn(ctx), 'd'); }); @@ -161,7 +172,9 @@ describe('array', function() { }); it('should render the inverse block when a value does not exist', function() { - var fn = hbs.compile('{{#inArray array "foo"}}AAA{{else}}BBB{{/inArray}}'); + var fn = hbs.compile( + '{{#inArray array "foo"}}AAA{{else}}BBB{{/inArray}}' + ); equal(fn(context), 'BBB'); }); }); @@ -169,7 +182,7 @@ describe('array', function() { describe('isArray', function() { it('should return true if the value is an array', function() { equal(hbs.compile('{{isArray "foo"}}')(), 'false'); - equal(hbs.compile('{{isArray \'["foo"]\'}}')(), 'false'); + equal(hbs.compile("{{isArray '[\"foo\"]'}}")(), 'false'); equal(hbs.compile('{{isArray foo}}')({ foo: ['foo'] }), 'true'); equal(hbs.compile('{{isArray (arrayify "foo")}}')(), 'true'); equal(hbs.compile('{{isArray (arrayify ["foo"])}}')(), 'true'); @@ -180,16 +193,23 @@ describe('array', function() { var ctx = { array: ['foo', 'bar', 'baz'] }; it('should return a null value for undefined array', function() { - equal(hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')(), 'notfound'); + equal( + hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')(), + 'notfound' + ); }); it('should return a null value for empty array', function() { - var fn = hbs.compile('{{#if (itemAt array)}}exists{{else}}notfound{{/if}}'); + var fn = hbs.compile( + '{{#if (itemAt array)}}exists{{else}}notfound{{/if}}' + ); equal(fn({ array: [] }), 'notfound'); }); it('should return a null value for exceed bound', function() { - var fn = hbs.compile('{{#if (itemAt array 999)}}exists{{else}}notfound{{/if}}'); + var fn = hbs.compile( + '{{#if (itemAt array 999)}}exists{{else}}notfound{{/if}}' + ); equal(fn(ctx), 'notfound'); }); @@ -250,12 +270,16 @@ describe('array', function() { describe('lengthEqual', function() { it('should render the first block if length is the given number', function() { - var fn = hbs.compile('{{#lengthEqual array 8}}AAA{{else}}BBB{{/lengthEqual}}'); + var fn = hbs.compile( + '{{#lengthEqual array 8}}AAA{{else}}BBB{{/lengthEqual}}' + ); equal(fn(context), 'AAA'); }); it('should render the inverse block if length is not the given number', function() { - var fn = hbs.compile('{{#lengthEqual array 3}}AAA{{else}}BBB{{/lengthEqual}}'); + var fn = hbs.compile( + '{{#lengthEqual array 3}}AAA{{else}}BBB{{/lengthEqual}}' + ); equal(fn(context), 'BBB'); }); }); @@ -412,23 +436,38 @@ describe('array', function() { describe('withFirst', function() { it('should use the first item in an array', function() { - var fn = hbs.compile('{{#withFirst array}}{{this}} is smart.{{/withFirst}}'); + var fn = hbs.compile( + '{{#withFirst array}}{{this}} is smart.{{/withFirst}}' + ); equal(fn(context), 'a is smart.'); }); it('should return an empty string when no array is passed:', function() { equal(hbs.compile('{{#withFirst}}{{/withFirst}}')(), ''); }); it('should use the first two items in an array', function() { - var fn = hbs.compile('{{#withFirst array 2}}{{this}} is smart.{{/withFirst}}'); + var fn = hbs.compile( + '{{#withFirst array 2}}{{this}} is smart.{{/withFirst}}' + ); equal(fn(context), 'a is smart.b is smart.'); }); }); describe('withGroup', function() { it('should iterate over an array grouping elements by a given number', function() { - var fn = hbs.compile('{{#withGroup collection 4}}{{#each this}}{{name}}{{/each}}
{{/withGroup}}'); + var fn = hbs.compile( + '{{#withGroup collection 4}}{{#each this}}{{name}}{{/each}}
{{/withGroup}}' + ); var res = fn({ - collection: [{ name: 'a' }, { name: 'b' }, { name: 'c' }, { name: 'd' }, { name: 'e' }, { name: 'f' }, { name: 'g' }, { name: 'h' }] + collection: [ + { name: 'a' }, + { name: 'b' }, + { name: 'c' }, + { name: 'd' }, + { name: 'e' }, + { name: 'f' }, + { name: 'g' }, + { name: 'h' } + ] }); equal(res, 'abcd
efgh
'); }); @@ -443,7 +482,9 @@ describe('array', function() { equal(fn(context), 'h is dumb.'); }); it('should use the last two items in an array', function() { - var fn = hbs.compile('{{#withLast array 2}}{{this}} is dumb.{{/withLast}}'); + var fn = hbs.compile( + '{{#withLast array 2}}{{this}} is dumb.{{/withLast}}' + ); equal(fn(context), 'g is dumb.h is dumb.'); }); }); @@ -460,12 +501,16 @@ describe('array', function() { }); it('should sort the array in reverse order', function() { - var fn = hbs.compile('{{#withSort array reverse="true"}}{{this}}{{/withSort}}'); + var fn = hbs.compile( + '{{#withSort array reverse="true"}}{{this}}{{/withSort}}' + ); equal(fn(context), 'hgfedcba'); }); it('should sort the array by deliveries', function() { - var fn = hbs.compile('{{#withSort collection "deliveries"}}{{name}}: {{deliveries}}
{{/withSort}}'); + var fn = hbs.compile( + '{{#withSort collection "deliveries"}}{{name}}: {{deliveries}}
{{/withSort}}' + ); var res = fn({ collection: [ { name: 'f', deliveries: 8021 }, @@ -477,7 +522,9 @@ describe('array', function() { }); it('should sort the array by deliveries in reverse order', function() { - var fn = hbs.compile('{{#withSort collection "deliveries" reverse="true"}}{{name}}: {{deliveries}}
{{/withSort}}'); + var fn = hbs.compile( + '{{#withSort collection "deliveries" reverse="true"}}{{name}}: {{deliveries}}
{{/withSort}}' + ); var res = fn({ collection: [ { name: 'f', deliveries: 8021 }, @@ -499,5 +546,4 @@ describe('array', function() { equal(fn(context).toString(), 'a,b,c,d,f,g'); }); }); - }); diff --git a/test/code.js b/test/code.js index e32bfbee..41a83eb7 100644 --- a/test/code.js +++ b/test/code.js @@ -1,68 +1,79 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal, throws } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.code({handlebars: hbs}); +import { code } from '..'; +code({ handlebars: hbs }); describe('code', function() { describe('embed', function() { it('should embed markdown:', function() { - assert.equal(hbs.compile('{{{embed "test/fixtures/simple.md"}}}')(), [ - '```markdown', - '## Some Markdown\n', - ' - one', - ' - two', - ' - three\n', - '[Click here](http://github.com)\n', - '```\n' - ].join('\n')); + equal( + hbs.compile('{{{embed "test/fixtures/simple.md"}}}')(), + [ + '```markdown', + '## Some Markdown\n', + ' - one', + ' - two', + ' - three\n', + '[Click here](http://github.com)\n', + '```\n' + ].join('\n') + ); }); it('should determine the language from the file extension', function() { - assert.equal(hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')(), [ - '```markdown', - '## Markdown', - '', - 'Code example', - '', - '```js', - 'var urlresolve = function(base, href) {', - ' return url.resolve(base, href);', - '};', - '```', - '', - '[Click here](http://assemble.io) for more documentation.', - '', - '```\n' - ].join('\n')); + equal( + hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')(), + [ + '```markdown', + '## Markdown', + '', + 'Code example', + '', + '```js', + 'var urlresolve = function(base, href) {', + ' return url.resolve(base, href);', + '};', + '```', + '', + '[Click here](http://assemble.io) for more documentation.', + '', + '```\n' + ].join('\n') + ); }); it('should use the language defined in the last argument', function() { - var template = hbs.compile('{{{embed "test/fixtures/index.html" "hbs"}}}'); - assert.equal(template(), [ - '```hbs', - '', - ' ', - ' ', - ' ', - ' {{title}}', - ' ', - ' ', - ' {{> foo }}', - ' ', - '', - '', - '```\n' - ].join('\n')); + var template = hbs.compile( + '{{{embed "test/fixtures/index.html" "hbs"}}}' + ); + equal( + template(), + [ + '```hbs', + '', + ' ', + ' ', + ' ', + ' {{title}}', + ' ', + ' ', + ' {{> foo }}', + ' ', + '', + '', + '```\n' + ].join('\n') + ); }); }); describe('gist', function() { it('should return a gist script tag', function() { var fn = hbs.compile('{{{gist "abcdefg"}}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); }); @@ -70,11 +81,14 @@ describe('code', function() { it('should return a jsfiddle embed link, with default tabs assigned', function() { var source = '{{{jsfiddle id="UXbas"}}}'; var fn = hbs.compile(source); - assert.equal(fn(), ''); + equal( + fn(), + '' + ); }); it('should throw an error if id is missing', function() { - assert.throws(function() { + throws(function() { hbs.compile('{{jsfiddle}}')(); }); }); @@ -82,7 +96,10 @@ describe('code', function() { it('should return a jsfiddle embed link, with custom tabs assigned', function() { var source = '{{{jsfiddle id="UXbas" tabs="html,css"}}}'; var fn = hbs.compile(source); - assert.equal(fn(), ''); + equal( + fn(), + '' + ); }); }); }); diff --git a/test/collection.js b/test/collection.js index ca3f8355..86931ef1 100644 --- a/test/collection.js +++ b/test/collection.js @@ -1,105 +1,105 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.array({handlebars: hbs}); -helpers.collection({handlebars: hbs}); -helpers.string({handlebars: hbs}); +import { array as _array, collection, string } from '..'; +_array({ handlebars: hbs }); +collection({ handlebars: hbs }); +string({ handlebars: hbs }); -var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']}; +var context = { array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] }; describe('collection', function() { describe('isEmpty block helper', function() { it('should render the first block when an array is empty', function() { var fn = hbs.compile('{{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}'); - assert.equal(fn({array: []}), 'AAA'); + equal(fn({ array: [] }), 'AAA'); }); it('should render the first block when the value is null', function() { var fn = hbs.compile('{{#isEmpty}}AAA{{else}}BBB{{/isEmpty}}'); - assert.equal(fn({array: []}), 'AAA'); + equal(fn({ array: [] }), 'AAA'); }); it('should render the second block when an array is not empty', function() { var fn = hbs.compile('{{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}'); - assert.equal(fn(context), 'BBB'); + equal(fn(context), 'BBB'); }); it('should render the second block when an object is not empty', function() { var fn = hbs.compile('{{#isEmpty object}}AAA{{else}}BBB{{/isEmpty}}'); - assert.equal(fn({object: {foo: 'bar'}}), 'BBB'); + equal(fn({ object: { foo: 'bar' } }), 'BBB'); }); it('should render the first block when an object is empty', function() { var fn = hbs.compile('{{#isEmpty object}}AAA{{else}}BBB{{/isEmpty}}'); - assert.equal(fn({object: {}}), 'AAA'); + equal(fn({ object: {} }), 'AAA'); }); }); describe('isEmpty inline helper', function() { it('should render the first block when an array is empty', function() { var fn = hbs.compile('{{isEmpty array}}'); - assert.equal(fn({array: []}), 'true'); + equal(fn({ array: [] }), 'true'); }); it('should render the first block when the value is null', function() { var fn = hbs.compile('{{isEmpty}}'); - assert.equal(fn({array: []}), 'true'); + equal(fn({ array: [] }), 'true'); }); it('should render the second block when an array is not empty', function() { var fn = hbs.compile('{{isEmpty array}}'); - assert.equal(fn(context), 'false'); + equal(fn(context), 'false'); }); it('should render the second block when an object is not empty', function() { var fn = hbs.compile('{{isEmpty object}}'); - assert.equal(fn({object: {foo: 'bar'}}), 'false'); + equal(fn({ object: { foo: 'bar' } }), 'false'); }); it('should render the first block when an object is empty', function() { var fn = hbs.compile('{{isEmpty object}}'); - assert.equal(fn({object: {}}), 'true'); + equal(fn({ object: {} }), 'true'); }); }); describe('iterate', function() { describe('object', function() { it('should iterate over a plain object:', function() { - var obj = {a: 'aaa', b: 'bbb', c: 'ccc'}; + var obj = { a: 'aaa', b: 'bbb', c: 'ccc' }; var fn = hbs.compile('{{#iterate obj}}{{.}}{{/iterate}}'); - assert.equal(fn({obj: obj}), 'aaabbbccc'); + equal(fn({ obj: obj }), 'aaabbbccc'); }); it('should expose `@key`:', function() { - var obj = {a: 'aaa', b: 'bbb', c: 'ccc'}; + var obj = { a: 'aaa', b: 'bbb', c: 'ccc' }; var fn = hbs.compile('{{#iterate obj}}{{@key}}{{/iterate}}'); - assert.equal(fn({obj: obj}), 'abc'); + equal(fn({ obj: obj }), 'abc'); }); it('should render the inverse block when falsey:', function() { var fn = hbs.compile('{{#iterate obj}}A{{else}}B{{/iterate}}'); - assert.equal(fn(), 'B'); + equal(fn(), 'B'); }); }); describe('array', function() { it('should iterate over an array:', function() { - var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; + var arr = [{ name: 'a' }, { name: 'b' }, { name: 'c' }]; var fn = hbs.compile('{{#iterate arr}}{{name}}{{/iterate}}'); - assert.equal(fn({arr: arr}), 'abc'); + equal(fn({ arr: arr }), 'abc'); }); it('should expose `@index`:', function() { - var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; + var arr = [{ name: 'a' }, { name: 'b' }, { name: 'c' }]; var fn = hbs.compile('{{#iterate arr}}{{@index}}{{/iterate}}'); - assert.equal(fn({arr: arr}), '012'); + equal(fn({ arr: arr }), '012'); }); }); }); @@ -107,31 +107,31 @@ describe('collection', function() { describe('length', function() { it('should return the length of the array', function() { var fn = hbs.compile('{{length array}}'); - assert.equal(fn(context), '8'); + equal(fn(context), '8'); }); it('should return zero when undefined', function() { - assert.equal(hbs.compile('{{length}}')(), '0'); + equal(hbs.compile('{{length}}')(), '0'); }); it('should return the length of a string', function() { var fn = hbs.compile('{{length "foo"}}'); - assert.equal(fn(context), '3'); + equal(fn(context), '3'); }); it('should work with arrays passed via subexpression', function() { var fn = hbs.compile('{{length (split "b,c,a")}}'); - assert.equal(fn(context), '3'); + equal(fn(context), '3'); }); it('should return 0 when the array is invalid:', function() { var fn = hbs.compile('{{length foo}}'); - assert.equal(fn(context), '0'); + equal(fn(context), '0'); }); it('should return 0 when the value is not an array:', function() { var fn = hbs.compile('{{length foo}}'); - assert.equal(fn({foo: {}}), '0'); + equal(fn({ foo: {} }), '0'); }); }); }); diff --git a/test/comparison.js b/test/comparison.js index a9d62b45..8fa5b2bd 100644 --- a/test/comparison.js +++ b/test/comparison.js @@ -1,39 +1,39 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import assert, { equal, throws } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.comparison({handlebars: hbs}); +import { comparison } from '..'; +comparison({ handlebars: hbs }); describe('comparison', function() { describe('and', function() { describe('block', function() { it('should render a block if both values are truthy.', function() { var fn = hbs.compile('{{#and great magnificent}}A{{else}}B{{/and}}'); - assert.equal(fn({great: true, magnificent: true}), 'A'); + equal(fn({ great: true, magnificent: true }), 'A'); }); it('should render the inverse block if both values are not truthy.', function() { var fn = hbs.compile('{{#and great magnificent}}A{{else}}B{{/and}}'); - assert.equal(fn({great: true, magnificent: false}), 'B'); + equal(fn({ great: true, magnificent: false }), 'B'); }); }); describe('inline or subexpression', function() { it('should render a block if both values are truthy.', function() { var fn = hbs.compile('{{and great magnificent}}'); - assert.equal(fn({great: true, magnificent: true}), 'true'); + equal(fn({ great: true, magnificent: true }), 'true'); }); it('should render the inverse block if both values are not truthy.', function() { var fn = hbs.compile('{{and great magnificent}}'); - assert.equal(fn({great: true, magnificent: false}), 'false'); + equal(fn({ great: true, magnificent: false }), 'false'); }); it('should work as subexpressions', function() { var fn = hbs.compile('{{and (and a b) (and great magnificent)}}'); - assert.equal(fn({great: true, magnificent: false}), 'false'); + equal(fn({ great: true, magnificent: false }), 'false'); }); }); }); @@ -41,16 +41,16 @@ describe('comparison', function() { describe('compare', function() { describe('errors', function() { it('should throw an error when args are invalid', function() { - assert.throws(function() { + throws(function() { hbs.compile('{{#compare}}{{/compare}}')(); }, /expects 4 arguments/); - assert.throws(function() { + throws(function() { hbs.compile('{{#compare a b}}{{/compare}}')(); }, /expects 4 arguments/); }); it('should throw an error when the operator is invalid', function() { - assert.throws(function() { + throws(function() { hbs.compile('{{#compare a "~" b}}{{/compare}}')(); }); }); @@ -61,10 +61,10 @@ describe('comparison', function() { var fn = hbs.compile('{{#compare a "==" b}}A{{else}}B{{/compare}}'); it('should render the first block if `a` equals `b`', function() { - assert(fn({a: '0', b: 0}), 'A'); + assert(fn({ a: '0', b: 0 }), 'A'); }); it('should render the second block if false', function() { - assert(fn({a: 'foo', b: 0}), 'B'); + assert(fn({ a: 'foo', b: 0 }), 'B'); }); }); @@ -72,10 +72,10 @@ describe('comparison', function() { var fn = hbs.compile('{{#compare a "===" b}}A{{else}}B{{/compare}}'); it('should render the first block if `a` strictly equals `b`', function() { - assert(fn({a: '1', b: '1'}), 'A'); + assert(fn({ a: '1', b: '1' }), 'A'); }); it('should render the second block if false', function() { - assert(fn({a: '1', b: 1}), 'B'); + assert(fn({ a: '1', b: 1 }), 'B'); }); }); @@ -83,10 +83,10 @@ describe('comparison', function() { var fn = hbs.compile('{{#compare a "!=" b}}A{{else}}B{{/compare}}'); it('should render the first block if `a` does not equal `b`', function() { - assert(fn({a: 10, b: '11'}), 'A'); + assert(fn({ a: 10, b: '11' }), 'A'); }); it('should render the second block if false', function() { - assert(fn({a: 10, b: '10'}), 'B'); + assert(fn({ a: 10, b: '10' }), 'B'); }); }); @@ -94,71 +94,81 @@ describe('comparison', function() { var fn = hbs.compile('{{#compare a "!==" b}}A{{else}}B{{/compare}}'); it('should render the first block if `a` does not strictly equal `b`', function() { - assert(fn({a: 10, b: 11}), 'A'); + assert(fn({ a: 10, b: 11 }), 'A'); }); it('should render the second block if false', function() { - assert(fn({a: 10, b: 10}), 'B'); + assert(fn({ a: 10, b: 10 }), 'B'); }); }); describe('>', function() { - var fn = hbs.compile('{{#compare a ">" b}}greater than or equal to 15{{else}}less than 15{{/compare}}'); + var fn = hbs.compile( + '{{#compare a ">" b}}greater than or equal to 15{{else}}less than 15{{/compare}}' + ); it('should render the first block if true.', function() { - assert(fn({a: 20, b: 15}), 'greater than or equal to 15'); + assert(fn({ a: 20, b: 15 }), 'greater than or equal to 15'); }); it('should render the second block if false.', function() { - assert(fn({a: 14, b: 15}), 'less than 15'); + assert(fn({ a: 14, b: 15 }), 'less than 15'); }); }); describe('<', function() { - var fn = hbs.compile('I knew it, {{#compare unicorns "<" ponies}}unicorns are just low-quality ponies!{{else}}unicorns are special!{{/compare}}'); + var fn = hbs.compile( + 'I knew it, {{#compare unicorns "<" ponies}}unicorns are just low-quality ponies!{{else}}unicorns are special!{{/compare}}' + ); it('should render the first block if true.', function() { - var res = fn({unicorns: 5, ponies: 6}); + var res = fn({ unicorns: 5, ponies: 6 }); assert(res, 'I knew it, unicorns are just low-quality ponies!'); }); it('should render the second block if false.', function() { - var res = fn({unicorns: 7, ponies: 6}); + var res = fn({ unicorns: 7, ponies: 6 }); assert(res, 'I knew it, unicorns are special!'); }); }); describe('>=', function() { - var fn = hbs.compile('{{#compare a ">=" b}}greater than or equal to 15{{else}}less than 15{{/compare}}'); + var fn = hbs.compile( + '{{#compare a ">=" b}}greater than or equal to 15{{else}}less than 15{{/compare}}' + ); it('should render the first block if true.', function() { - assert(fn({a: 20, b: 15}), 'greater than or equal to 15'); + assert(fn({ a: 20, b: 15 }), 'greater than or equal to 15'); }); it('should render the first block if equal.', function() { - assert(fn({a: 15, b: 15}), 'greater than or equal to 15'); + assert(fn({ a: 15, b: 15 }), 'greater than or equal to 15'); }); it('should render the second block if false.', function() { - assert(fn({a: 14, b: 15}), 'less than 15'); + assert(fn({ a: 14, b: 15 }), 'less than 15'); }); }); describe('<=', function() { - var fn = hbs.compile('{{#compare a "<=" b}}less than or equal to 10{{else}}greater than 10{{/compare}}'); + var fn = hbs.compile( + '{{#compare a "<=" b}}less than or equal to 10{{else}}greater than 10{{/compare}}' + ); it('should render the first block if true.', function() { - assert(fn({a: 10, b: 15}), 'less than or equal to 10'); + assert(fn({ a: 10, b: 15 }), 'less than or equal to 10'); }); it('should render the second block if false.', function() { - assert(fn({a: 20, b: 15}), 'greater than 10'); + assert(fn({ a: 20, b: 15 }), 'greater than 10'); }); }); describe('typeof', function() { it('should render the first block if true', function() { - var fn = hbs.compile('{{#compare obj "typeof" "object"}}A{{else}}B{{/compare}}'); - assert(fn({obj: {}}), 'A'); + var fn = hbs.compile( + '{{#compare obj "typeof" "object"}}A{{else}}B{{/compare}}' + ); + assert(fn({ obj: {} }), 'A'); }); }); }); @@ -167,47 +177,51 @@ describe('comparison', function() { describe('contains', function() { it('should render a block if the condition is true.', function() { var fn = hbs.compile('{{#contains context "C"}}A{{else}}B{{/contains}}'); - assert.equal(fn({context: 'CCC'}), 'A'); + equal(fn({ context: 'CCC' }), 'A'); }); it('should render the inverse block if false.', function() { - var fn = hbs.compile('{{#contains context "zzz"}}A{{else}}B{{/contains}}'); - assert.equal(fn({context: 'CCC'}), 'B'); + var fn = hbs.compile( + '{{#contains context "zzz"}}A{{else}}B{{/contains}}' + ); + equal(fn({ context: 'CCC' }), 'B'); }); it('should work with arrays', function() { var fn = hbs.compile('{{#contains array "a"}}A{{else}}B{{/contains}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), 'A'); + equal(fn({ array: ['a', 'b', 'c'] }), 'A'); }); it('should render the block when an index is passed:', function() { var fn = hbs.compile('{{#contains array "a" 0}}A{{else}}B{{/contains}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), 'A'); + equal(fn({ array: ['a', 'b', 'c'] }), 'A'); }); it('should render the inverse block when false with index:', function() { var fn = hbs.compile('{{#contains array "a" 1}}A{{else}}B{{/contains}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), 'B'); + equal(fn({ array: ['a', 'b', 'c'] }), 'B'); }); it('should not render the block when an undefined argument is passed:', function() { var fn = hbs.compile('{{#contains array nothing}}A{{/contains}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), ''); + equal(fn({ array: ['a', 'b', 'c'] }), ''); }); it('should render the inverse block when an undefined argument is passed:', function() { - var fn = hbs.compile('{{#contains array nothing}}A{{else}}B{{/contains}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), 'B'); + var fn = hbs.compile( + '{{#contains array nothing}}A{{else}}B{{/contains}}' + ); + equal(fn({ array: ['a', 'b', 'c'] }), 'B'); }); }); describe('default', function() { it('should use the given value:', function() { - assert.equal(hbs.compile('{{default title "A"}}')({title: 'B'}), 'B'); + equal(hbs.compile('{{default title "A"}}')({ title: 'B' }), 'B'); }); it('should fallback to the default value when no value exists', function() { - assert.equal(hbs.compile('{{default title "A"}}')({title: null}), 'A'); - assert.equal(hbs.compile('{{default title "A"}}')(), 'A'); + equal(hbs.compile('{{default title "A"}}')({ title: null }), 'A'); + equal(hbs.compile('{{default title "A"}}')(), 'A'); }); }); @@ -216,28 +230,28 @@ describe('comparison', function() { describe('second arg', function() { it('should render the first block if true.', function() { - assert(fn({a: 20, b: 15}), 'A'); + assert(fn({ a: 20, b: 15 }), 'A'); }); it('should render the second block if equal.', function() { - assert(fn({a: 15, b: 15}), 'B'); + assert(fn({ a: 15, b: 15 }), 'B'); }); it('should render the second block if false.', function() { - assert(fn({a: 14, b: 15}), 'B'); + assert(fn({ a: 14, b: 15 }), 'B'); }); }); describe('compare hash', function() { it('should not render a block if the value is not equal to a given number.', function() { var fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}'); - assert.equal(fn({number: 5}), ''); + equal(fn({ number: 5 }), ''); }); it('should render a block if the value is greater than a given number.', function() { var fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}'); - assert.equal(fn({number: 10}), 'A'); + equal(fn({ number: 10 }), 'A'); }); it('should not render a block if the value is less than a given number.', function() { var fn = hbs.compile('{{#gt number compare=8}}A{{/gt}}'); - assert.equal(fn({number: 5}), ''); + equal(fn({ number: 5 }), ''); }); }); }); @@ -247,28 +261,28 @@ describe('comparison', function() { var fn = hbs.compile('{{#gte a b}}A{{else}}B{{/gte}}'); it('should render the first block if true.', function() { - assert.equal(fn({a: 20, b: 15}), 'A'); + equal(fn({ a: 20, b: 15 }), 'A'); }); it('should render the first block if equal.', function() { - assert.equal(fn({a: 15, b: 15}), 'A'); + equal(fn({ a: 15, b: 15 }), 'A'); }); it('should render the second block if false.', function() { - assert.equal(fn({a: 14, b: 15}), 'B'); + equal(fn({ a: 14, b: 15 }), 'B'); }); }); describe('hash compare', function() { it('should render a block if the value is greater than a given number.', function() { var fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}'); - assert.equal(fn({number: 12}), 'A'); + equal(fn({ number: 12 }), 'A'); }); it('should render a block if the value is equal to a given number.', function() { var fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}'); - assert.equal(fn({number: 8}), 'A'); + equal(fn({ number: 8 }), 'A'); }); it('should not render a block if the value is less than a given number.', function() { var fn = hbs.compile('{{#gte number compare=8}}A{{/gte}}'); - assert.equal(fn({number: 5}), ''); + equal(fn({ number: 5 }), ''); }); }); }); @@ -277,49 +291,49 @@ describe('comparison', function() { describe('inline', function() { it('should return true when the property exists', function() { var fn = hbs.compile('{{has "foo"}}'); - assert.equal(fn({foo: 'bar'}), 'true'); + equal(fn({ foo: 'bar' }), 'true'); }); }); describe('block', function() { it('should render a block if the condition is true.', function() { var fn = hbs.compile('{{#has context "C"}}A{{else}}B{{/has}}'); - assert.equal(fn({context: 'CCC'}), 'A'); + equal(fn({ context: 'CCC' }), 'A'); }); it('should render the inverse block if false.', function() { var fn = hbs.compile('{{#has context "zzz"}}A{{else}}B{{/has}}'); - assert.equal(fn({context: 'CCC'}), 'B'); + equal(fn({ context: 'CCC' }), 'B'); }); it('should render the inverse block if value is undefined.', function() { var fn = hbs.compile('{{#has context}}A{{else}}B{{/has}}'); - assert.equal(fn({context: 'CCC'}), 'B'); + equal(fn({ context: 'CCC' }), 'B'); }); it('should render the inverse block if context is undefined.', function() { var fn = hbs.compile('{{#has}}A{{else}}B{{/has}}'); - assert.equal(fn({context: 'CCC'}), 'B'); + equal(fn({ context: 'CCC' }), 'B'); }); it('should work with arrays', function() { var fn = hbs.compile('{{#has array "a"}}A{{else}}B{{/has}}'); - assert.equal(fn({array: ['a', 'b', 'c']}), 'A'); + equal(fn({ array: ['a', 'b', 'c'] }), 'A'); }); it('should work with two strings', function() { var fn = hbs.compile('{{#has "abc" "a"}}A{{else}}B{{/has}}'); - assert.equal(fn(), 'A'); + equal(fn(), 'A'); }); it('should return the inverse when the second string is not found', function() { var fn = hbs.compile('{{#has "abc" "z"}}A{{else}}B{{/has}}'); - assert.equal(fn(), 'B'); + equal(fn(), 'B'); }); it('should work with object keys', function() { var fn = hbs.compile('{{#has object "a"}}A{{else}}B{{/has}}'); - assert.equal(fn({object: {a: 'b'}}), 'A'); + equal(fn({ object: { a: 'b' } }), 'A'); }); }); }); @@ -327,59 +341,60 @@ describe('comparison', function() { describe('isFalsey', function() { it('should render block if given value is falsey.', function() { var fn = hbs.compile('{{#if (isFalsey value)}}A{{else}}B{{/if}}'); - assert.equal(fn({value: 'nope'}), 'A'); + equal(fn({ value: 'nope' }), 'A'); }); it('should render inverse if given value is truthy', function() { var fn = hbs.compile('{{#if (isFalsey value)}}A{{else}}B{{/if}}'); - assert.equal(fn({value: 'CCC'}), 'B'); + equal(fn({ value: 'CCC' }), 'B'); }); }); describe('isTruthy', function() { it('should render block if given value is truthy.', function() { var fn = hbs.compile('{{#if (isTruthy value)}}A{{else}}B{{/if}}'); - assert.equal(fn({value: 'CCC'}), 'A'); + equal(fn({ value: 'CCC' }), 'A'); }); it('should render inverse if given value is not truthy', function() { var fn = hbs.compile('{{#if (isTruthy value)}}A{{else}}B{{/if}}'); - assert.equal(fn({value: 'nope'}), 'B'); + equal(fn({ value: 'nope' }), 'B'); }); }); describe('eq', function() { it('should render a block if the value is equal to a given number.', function() { var fn = hbs.compile('{{#eq number compare=8}}A{{/eq}}'); - assert.equal(fn({number: 8}), 'A'); + equal(fn({ number: 8 }), 'A'); }); it('should render the inverse block if falsey.', function() { var fn = hbs.compile('{{#eq number compare=8}}A{{else}}B{{/eq}}'); - assert.equal(fn({number: 9}), 'B'); + equal(fn({ number: 9 }), 'B'); }); it('should compare first and second args', function() { var fn = hbs.compile('{{#eq number 8}}A{{else}}B{{/eq}}'); - assert.equal(fn({number: 9}), 'B'); + equal(fn({ number: 9 }), 'B'); }); }); describe('ifEven', function() { it('should render the block if the given value is an even number', function() { var fn = hbs.compile('{{#ifEven number}}A{{else}}B{{/ifEven}}'); - assert(fn({number: 8}), 'A'); + assert(fn({ number: 8 }), 'A'); }); it('should render the inverse block if the number is odd', function() { var fn = hbs.compile('{{#ifEven number}}A{{else}}B{{/ifEven}}'); - assert.equal(fn({number: 9}), 'B'); + equal(fn({ number: 9 }), 'B'); }); }); describe('ifNth', function() { it('should render a custom class on even rows', function() { - var source = '{{#each items}}{{name}}{{/each}}'; + var source = + '{{#each items}}{{name}}{{/each}}'; var fn = hbs.compile(source); var context = { items: [ @@ -390,59 +405,62 @@ describe('comparison', function() { { name: 'Hermes Conrad' } ] }; - assert(fn(context), [ - '
Philip J. Fry
', - '
Turanga Leela
', - '
Bender Bending Rodriguez
', - '
Amy Wong
', - '
Hermes Conrad
' - ].join('')); + assert( + fn(context), + [ + '
Philip J. Fry
', + '
Turanga Leela
', + '
Bender Bending Rodriguez
', + '
Amy Wong
', + '
Hermes Conrad
' + ].join('') + ); }); }); describe('ifOdd', function() { it('should render the block if the given value is an even number', function() { var fn = hbs.compile('{{#ifOdd number}}A{{else}}B{{/ifOdd}}'); - assert.equal(fn({number: 9}), 'A'); + equal(fn({ number: 9 }), 'A'); }); it('should render the inverse block if the number is odd', function() { var fn = hbs.compile('{{#ifOdd number}}A{{else}}B{{/ifOdd}}'); - assert.equal(fn({number: 8}), 'B'); + equal(fn({ number: 8 }), 'B'); }); }); describe('is', function() { it('should render a block if the condition is true.', function() { var fn = hbs.compile('{{#is value "CCC"}}A{{else}}B{{/is}}'); - assert.equal(fn({value: 'CCC'}), 'A'); + equal(fn({ value: 'CCC' }), 'A'); }); it('should use the `compare` arg on the options hash', function() { var fn = hbs.compile('{{#is value compare="CCC"}}A{{else}}B{{/is}}'); - assert.equal(fn({value: 'CCC'}), 'A'); + equal(fn({ value: 'CCC' }), 'A'); }); it('should render the inverse if the condition is false', function() { var fn = hbs.compile('{{#is value "FOO"}}A{{else}}B{{/is}}'); - assert.equal(fn({value: 'CCC'}), 'B'); + equal(fn({ value: 'CCC' }), 'B'); }); }); describe('isnt', function() { it('should render a block if the condition is not true.', function() { var fn = hbs.compile('{{#isnt number 2}}A{{else}}B{{/isnt}}'); - assert.equal(fn({number: 3}), 'A'); + equal(fn({ number: 3 }), 'A'); }); it('should use the `compare` arg on the options hash', function() { var fn = hbs.compile('{{#isnt value compare="CCC"}}A{{else}}B{{/isnt}}'); - assert.equal(fn({value: 'CCC'}), 'B'); + equal(fn({ value: 'CCC' }), 'B'); }); it('should render the inverse if the condition is false', function() { var fn = hbs.compile('{{#isnt value "FOO"}}A{{else}}B{{/isnt}}'); - assert.equal(fn({value: 'CCC'}), 'A'); + equal(fn({ value: 'CCC' }), 'A'); }); }); @@ -451,24 +469,24 @@ describe('comparison', function() { var fn = hbs.compile('{{#lt a b}}A{{else}}B{{/lt}}'); it('should render the first block if true.', function() { - assert.equal(fn({a: 14, b: 15}), 'A'); + equal(fn({ a: 14, b: 15 }), 'A'); }); it('should render the second block if equal.', function() { - assert.equal(fn({a: 15, b: 15}), 'B'); + equal(fn({ a: 15, b: 15 }), 'B'); }); it('should render the second block if false.', function() { - assert.equal(fn({a: 20, b: 15}), 'B'); + equal(fn({ a: 20, b: 15 }), 'B'); }); }); describe('compare hash', function() { it('should render a block if the value is less than a given number.', function() { var fn = hbs.compile('{{#lt number compare=8}}A{{/lt}}'); - assert.equal(fn({number: 5}), 'A'); + equal(fn({ number: 5 }), 'A'); }); it('should not render a block if the value is greater than a given number.', function() { var fn = hbs.compile('{{#lt number compare=8}}A{{/lt}}'); - assert.equal(fn({number: 42}), ''); + equal(fn({ number: 42 }), ''); }); }); }); @@ -478,45 +496,49 @@ describe('comparison', function() { describe('second arg', function() { it('should render the first block if true.', function() { - assert.equal(fn({a: 14, b: 15}), 'A'); + equal(fn({ a: 14, b: 15 }), 'A'); }); it('should render the first block if equal.', function() { - assert.equal(fn({a: 15, b: 15}), 'A'); + equal(fn({ a: 15, b: 15 }), 'A'); }); it('should render the second block if false.', function() { - assert.equal(fn({a: 20, b: 15}), 'B'); + equal(fn({ a: 20, b: 15 }), 'B'); }); }); describe('compare hash', function() { it('should render a block if the value is less than a given number.', function() { var fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}'); - assert.equal(fn({number: 1}), 'A'); + equal(fn({ number: 1 }), 'A'); }); it('should render a block if the value is equal to a given number.', function() { var fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}'); - assert.equal(fn({number: 8}), 'A'); + equal(fn({ number: 8 }), 'A'); }); it('should not render a block if the value is greater than a given number.', function() { var fn = hbs.compile('{{#lte number compare=8}}A{{/lte}}'); - assert.equal(fn({number: 27}), ''); + equal(fn({ number: 27 }), ''); }); }); }); describe('neither', function() { it('should render a block if one of the values is truthy.', function() { - var fn = hbs.compile('{{#neither great magnificent}}A{{else}}B{{/neither}}'); - assert.equal(fn({great: false, magnificent: false}), 'A'); + var fn = hbs.compile( + '{{#neither great magnificent}}A{{else}}B{{/neither}}' + ); + equal(fn({ great: false, magnificent: false }), 'A'); }); it('should render the inverse block if neither are true.', function() { - var fn = hbs.compile('{{#neither great magnificent}}A{{else}}B{{/neither}}'); - assert.equal(fn({great: true, magnificent: false}), 'B'); + var fn = hbs.compile( + '{{#neither great magnificent}}A{{else}}B{{/neither}}' + ); + equal(fn({ great: true, magnificent: false }), 'B'); }); }); @@ -524,31 +546,35 @@ describe('comparison', function() { describe('block', function() { it('should render a block if one of the values is truthy.', function() { var fn = hbs.compile('{{#or great magnificent}}A{{else}}B{{/or}}'); - assert.equal(fn({great: false, magnificent: true}), 'A'); + equal(fn({ great: false, magnificent: true }), 'A'); }); it('should render a block if any of the values are truthy.', function() { - var fn = hbs.compile('{{#or great magnificent fantastic}}A{{else}}B{{/or}}'); - assert.equal(fn({great: false, magnificent: false, fantastic: true}), 'A'); + var fn = hbs.compile( + '{{#or great magnificent fantastic}}A{{else}}B{{/or}}' + ); + equal(fn({ great: false, magnificent: false, fantastic: true }), 'A'); }); it('should render the inverse block if neither are true.', function() { var fn = hbs.compile('{{#or great magnificent}}A{{else}}B{{/or}}'); - assert.equal(fn({great: false, magnificent: false}), 'B'); + equal(fn({ great: false, magnificent: false }), 'B'); }); it('should render the inverse block if none are true.', function() { - var fn = hbs.compile('{{#or great magnificent fantastic}}A{{else}}B{{/or}}'); - assert.equal(fn({great: false, magnificent: false, fantastic: false}), 'B'); + var fn = hbs.compile( + '{{#or great magnificent fantastic}}A{{else}}B{{/or}}' + ); + equal(fn({ great: false, magnificent: false, fantastic: false }), 'B'); }); }); describe('inline', function() { it('should return false none of the values is truthy.', function() { var fn = hbs.compile('{{or great magnificent}}'); - assert.equal(fn({great: false, magnificent: false}), 'false'); + equal(fn({ great: false, magnificent: false }), 'false'); }); it('should return true if one of the values is truthy.', function() { var fn = hbs.compile('{{or great magnificent}}'); - assert.equal(fn({great: false, magnificent: true}), 'true'); + equal(fn({ great: false, magnificent: true }), 'true'); }); }); }); @@ -556,63 +582,63 @@ describe('comparison', function() { describe('unlessEq', function() { it('should render a block unless the value is equal to a given number.', function() { var fn = hbs.compile('{{#unlessEq number compare=8}}A{{/unlessEq}}'); - assert.equal(fn({number: 10}), 'A'); + equal(fn({ number: 10 }), 'A'); }); it('should render a block unless the value is equal to a given number.', function() { var fn = hbs.compile('{{#unlessEq number compare=8}}A{{/unlessEq}}'); - assert.equal(fn({number: 8}), ''); + equal(fn({ number: 8 }), ''); }); }); describe('unlessGt', function() { it('should render a block unless the value is greater than a given number.', function() { var fn = hbs.compile('{{#unlessGt number compare=8}}A{{/unlessGt}}'); - assert.equal(fn({number: 5}), 'A'); + equal(fn({ number: 5 }), 'A'); }); it('should render a block unless the value is greater than a given number.', function() { var fn = hbs.compile('{{#unlessGt number compare=8}}A{{/unlessGt}}'); - assert.equal(fn({number: 10}), ''); + equal(fn({ number: 10 }), ''); }); }); describe('unlessLt', function() { it('should render a block unless the value is less than a given number.', function() { var fn = hbs.compile('{{#unlessLt number compare=8}}A{{/unlessLt}}'); - assert.equal(fn({number: 10}), 'A'); + equal(fn({ number: 10 }), 'A'); }); it('should render a block unless the value is less than a given number.', function() { var fn = hbs.compile('{{#unlessLt number compare=8}}A{{/unlessLt}}'); - assert.equal(fn({number: 5}), ''); + equal(fn({ number: 5 }), ''); }); }); describe('unlessGteq', function() { it('should render a block unless the value is greater than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}'); - assert.equal(fn({number: 4}), 'A'); + equal(fn({ number: 4 }), 'A'); }); it('should render a block unless the value is greater than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}'); - assert.equal(fn({number: 8}), ''); + equal(fn({ number: 8 }), ''); }); it('should not render a block unless the value is greater than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessGteq number compare=8}}A{{/unlessGteq}}'); - assert.equal(fn({number: 34}), ''); + equal(fn({ number: 34 }), ''); }); }); describe('unlessLteq', function() { it('should render a block unless the value is less than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}'); - assert.equal(fn({number: 10}), 'A'); + equal(fn({ number: 10 }), 'A'); }); it('should render a block unless the value is less than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}'); - assert.equal(fn({number: 8}), ''); + equal(fn({ number: 8 }), ''); }); it('should not render a block unless the value is less than or equal to a given number.', function() { var fn = hbs.compile('{{#unlessLteq number compare=8}}A{{/unlessLteq}}'); - assert.equal(fn({number: 4}), ''); + equal(fn({ number: 4 }), ''); }); }); }); diff --git a/test/examples/index.js b/test/examples/index.js index bb4bec66..25890180 100644 --- a/test/examples/index.js +++ b/test/examples/index.js @@ -1,22 +1,23 @@ - 'use strict'; -require('mocha'); +import 'mocha'; -const sinon = require('sinon'); -sinon.stub(require('../../lib/uuid'), 'uuid').returns('f34ebc66-93bd-4f7c-b79b-92b5569138bc'); +import { stub } from 'sinon'; +stub(require('../../lib/uuid'), 'uuid').returns( + 'f34ebc66-93bd-4f7c-b79b-92b5569138bc' +); -sinon.stub(require('../../lib/math'), 'random').returns(10); +stub(require('../../lib/math'), 'random').returns(10); -var assert = require('assert'); -var lib = require('../../lib/'); +import { equal } from 'assert'; +import lib from '../../lib/'; -const fs = require('fs'); -const doctrine = require('doctrine'); -const path = require('path'); +import { readFileSync } from 'fs'; +import { parse } from 'doctrine'; +import { join } from 'path'; var handlebars = require('handlebars').create(); -var helpers = require('../..'); +import helpers from '../..'; function lookForward(lines, funcLines, idx) { const funcLen = funcLines.length; @@ -55,14 +56,14 @@ function getCommentInfo(file, func) { if (comment == null) { return { description: '' }; } - const docs = doctrine.parse(comment, { unwrap: true }); + const docs = parse(comment, { unwrap: true }); // some hacky fixes docs.description = docs.description.replace(/\n/g, ' '); docs.description = docs.description.replace(/[ ]{2,}/g, ' '); docs.description = docs.description.replace(/is is/g, 'is'); const examples = docs.tags - .filter(el => el.title === 'example') - .map(el => el.description); + .filter((el) => el.title === 'example') + .map((el) => el.description); const blocks = docs.description.split('```'); if (examples.length > 0) { docs.example = examples.join(' '); @@ -85,47 +86,66 @@ describe('examples', function() { const group = lib[key]; - const fileContent = fs.readFileSync(require.resolve(path.join('../../lib/', key)), 'utf8'); + const fileContent = readFileSync( + require.resolve(join('../../lib/', key)), + 'utf8' + ); describe(key, function() { for (const func in group) { - const { example } = getCommentInfo(fileContent, lib[key][func].toString()); - - example && it(func, function() { - let [hbs, expectedResult] = example.split('->').map(x => x.trim()); - - const context = { - double: i => i * 2, - isString: (x) => typeof(x) === 'string' - }; - - const arrays = hbs.match(/\[[^/\]]+\]/); - arrays && arrays.forEach((arrayString, i) => { - hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`); - context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"')); - }); + const { example } = getCommentInfo( + fileContent, + lib[key][func].toString() + ); + + example && + it(func, function() { + let [hbs, expectedResult] = example + .split('->') + .map((x) => x.trim()); + + const context = { + double: (i) => i * 2, + isString: (x) => typeof x === 'string' + }; + + const arrays = hbs.match(/\[[^/\]]+\]/); + arrays && + arrays.forEach((arrayString, i) => { + hbs = hbs.replace( + new RegExp(escapeRegExp(arrayString)), + `array${i}` + ); + context[`array${i}`] = JSON.parse( + arrayString.replace(/\'/g, '"') + ); + }); + + if (expectedResult === undefined) { + // The function has no return value + return; + } - if (expectedResult === undefined) { - // The function has no return value - return; - } - - let result = handlebars.compile(hbs)(context); - // Trim 's - expectedResult = expectedResult.replace(/^\'|\'$/g, ''); - try { - let parsedExpected; - if ( - Array.isArray((parsedExpected = JSON.parse(expectedResult.replace(/\'/g, '"')))) - ) { - expectedResult = parsedExpected.join(','); + let result = handlebars.compile(hbs)(context); + // Trim 's + expectedResult = expectedResult.replace(/^\'|\'$/g, ''); + try { + let parsedExpected; + if ( + Array.isArray( + (parsedExpected = JSON.parse( + expectedResult.replace(/\'/g, '"') + )) + ) + ) { + expectedResult = parsedExpected.join(','); + } + } catch (e) { + // Nothing to parse } - } catch (e) { - // Nothing to parse - } - result = result.replace(/&nbsp;/g, ' '); - assert.equal(result, expectedResult); - }); + result = result.replace(/&nbsp;/g, ' '); + equal(result, expectedResult); + }); } }); } diff --git a/test/helpers.js b/test/helpers.js index fb9fee7c..c3c609ab 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,9 +1,9 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); -var hbs = require('handlebars'); -var helpers = require('..'); +import 'mocha'; +import assert from 'assert'; +import hbs, { helpers as _helpers, create, registerHelper } from 'handlebars'; +import helpers, { math } from '..'; describe('helpers', function() { it('should should return all helpers:', function() { @@ -15,13 +15,13 @@ describe('helpers', function() { }); it('should register helpers with handlebars:', function() { - helpers({handlebars: hbs}); - assert(hbs.helpers.hasOwnProperty('contains')); - assert(hbs.helpers.hasOwnProperty('default')); + helpers({ handlebars: hbs }); + assert(_helpers.hasOwnProperty('contains')); + assert(_helpers.hasOwnProperty('default')); }); it('should get the specified collections', function() { - var res = helpers(['string', 'array'], {handlebars: hbs.create()}); + var res = helpers(['string', 'array'], { handlebars: create() }); assert(res.hasOwnProperty('replace')); assert(res.hasOwnProperty('reverse')); assert(res.hasOwnProperty('some')); @@ -31,7 +31,7 @@ describe('helpers', function() { }); it('should get only the specified collection', function() { - var res = helpers('string', {handlebars: hbs.create()}); + var res = helpers('string', { handlebars: create() }); assert(res.hasOwnProperty('replace')); assert(res.hasOwnProperty('reverse')); @@ -42,22 +42,22 @@ describe('helpers', function() { }); it('should support passing an instance of handlebars:', function() { - helpers({handlebars: hbs}); - hbs.registerHelper('foo', function() {}); - assert(hbs.helpers.hasOwnProperty('foo')); + helpers({ handlebars: hbs }); + registerHelper('foo', function() {}); + assert(_helpers.hasOwnProperty('foo')); }); it('should return a single collection:', function() { - var res = helpers.math(); + var res = math(); assert(res.hasOwnProperty('add')); assert(res.hasOwnProperty('subtract')); assert(res.hasOwnProperty('divide')); }); it('should register collection helpers with handlebars:', function() { - helpers.math(); - assert(hbs.helpers.hasOwnProperty('add')); - assert(hbs.helpers.hasOwnProperty('subtract')); - assert(hbs.helpers.hasOwnProperty('divide')); + math(); + assert(_helpers.hasOwnProperty('add')); + assert(_helpers.hasOwnProperty('subtract')); + assert(_helpers.hasOwnProperty('divide')); }); }); diff --git a/test/html.js b/test/html.js index b8df111d..0bdc4ac4 100644 --- a/test/html.js +++ b/test/html.js @@ -1,121 +1,164 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.html({handlebars: hbs}); +import { html } from '..'; +html({ handlebars: hbs }); -var locals = {data: [{aaa: 'AAA', bbb: 'BBB'}, {aaa: 'CCC', bbb: 'DDD'}]}; +var locals = { + data: [ + { aaa: 'AAA', bbb: 'BBB' }, + { aaa: 'CCC', bbb: 'DDD' } + ] +}; var actual; describe('html', function() { describe('attr', function() { it('should strip html from a string.', function() { - var actual = hbs.compile('')({foo: 'btn'}); - assert.equal(actual, '
'); - assert.equal(hbs.compile('{{attr}}')(), ''); + var actual = hbs.compile('')({ + foo: 'btn' + }); + equal(actual, '
'); + equal(hbs.compile('{{attr}}')(), ''); }); }); describe('css', function() { it('should return an empty string when no context is passed:', function() { - assert.equal(hbs.compile('{{{css}}}')(), ''); + equal(hbs.compile('{{{css}}}')(), ''); }); it('should use a path passed as a string', function() { var actual = hbs.compile('{{{css "abc.css"}}}')(); - assert.equal(actual, ''); + equal(actual, ''); }); it('should use options.assets', function() { - var actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: 'foo'}}); - assert.equal(actual, ''); + var actual = hbs.compile('{{{css "abc.css"}}}')({ + options: { assets: 'foo' } + }); + equal( + actual, + '' + ); }); it('should ensure that options.assets is a string', function() { - var actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: null}}); - assert.equal(actual, ''); + var actual = hbs.compile('{{{css "abc.css"}}}')({ + options: { assets: null } + }); + equal(actual, ''); }); it('should not use options.assets when passing in an absolute url', function() { - var actual = hbs.compile('{{{css "https://abc.com/bar.css"}}}')({options: {assets: 'foo'}}); - assert.equal(actual, ''); + var actual = hbs.compile('{{{css "https://abc.com/bar.css"}}}')({ + options: { assets: 'foo' } + }); + equal( + actual, + '' + ); }); it('should use the `href` attribute on the hash', function() { actual = hbs.compile('{{{css href=""}}}')(); - assert.equal(actual, ''); + equal(actual, ''); actual = hbs.compile('{{{css href="abc.css"}}}')(); - assert.equal(actual, ''); + equal(actual, ''); }); it('should create multiple tags from an array passed on the context:', function() { - var ctx = {styles: ['a.css', 'bcss', 'c.css'] }; - assert.equal(hbs.compile('{{{css styles}}}')(ctx), [ - '', - '', - '' - ].join('\n')); + var ctx = { styles: ['a.css', 'bcss', 'c.css'] }; + equal( + hbs.compile('{{{css styles}}}')(ctx), + [ + '', + '', + '' + ].join('\n') + ); }); it('should create a less tag (TODO: only works with array format)', function() { - var ctx = {styles: ['a.less'] }; - assert.equal(hbs.compile('{{{css styles}}}')(ctx), ''); + var ctx = { styles: ['a.less'] }; + equal( + hbs.compile('{{{css styles}}}')(ctx), + '' + ); }); }); describe('js', function() { it('should create an empty script tag', function() { - assert.equal(hbs.compile('{{{js}}}')(), ''); + equal(hbs.compile('{{{js}}}')(), ''); }); it('should use a path passed as a string', function() { - assert.equal(hbs.compile('{{{js "abc.js"}}}')(), ''); + equal( + hbs.compile('{{{js "abc.js"}}}')(), + '' + ); }); it('should use the `src` attribute on the hash', function() { - assert.equal(hbs.compile('{{{js src=""}}}')(), ''); - assert.equal(hbs.compile('{{{js src="abc.js"}}}')(), ''); + equal(hbs.compile('{{{js src=""}}}')(), ''); + equal( + hbs.compile('{{{js src="abc.js"}}}')(), + '' + ); }); it('should create multiple tags from an array passed on the context:', function() { - var ctx = {scripts: ['a.js', 'bjs', 'c.js'] }; - assert.equal(hbs.compile('{{{js scripts}}}')(ctx), [ - '', - '', - '' - ].join('\n')); + var ctx = { scripts: ['a.js', 'bjs', 'c.js'] }; + equal( + hbs.compile('{{{js scripts}}}')(ctx), + [ + '', + '', + '' + ].join('\n') + ); }); it('should create a coffeescript tag (TODO: only works with array format)', function() { - var ctx = {scripts: ['a.coffee'] }; - assert.equal(hbs.compile('{{{js scripts}}}')(ctx), ''); + var ctx = { scripts: ['a.coffee'] }; + equal( + hbs.compile('{{{js scripts}}}')(ctx), + '' + ); }); }); describe('sanitize', function() { it('should return an empty string when undefined.', function() { - assert.equal(hbs.compile('{{sanitize}}')(), ''); + equal(hbs.compile('{{sanitize}}')(), ''); }); it('should strip html from a string.', function() { var actual = hbs.compile('{{sanitize "foo"}}')(); - assert.equal(actual, 'foo'); + equal(actual, 'foo'); }); }); describe('ul', function() { it('should should return an unordered list', function() { var fn = hbs.compile('{{#ul data class="names"}}{{aaa}} {{bbb}}{{/ul}}'); - assert.equal(fn(locals), '
  • AAA BBB
  • \n
  • CCC DDD
'); + equal( + fn(locals), + '
  • AAA BBB
  • \n
  • CCC DDD
' + ); }); }); describe('ol', function() { it('should should return an ordered list', function() { var fn = hbs.compile('{{#ol data class="names"}}{{aaa}} {{bbb}}{{/ol}}'); - assert.equal(fn(locals), '
  1. AAA BBB
  2. \n
  3. CCC DDD
'); + equal( + fn(locals), + '
  1. AAA BBB
  2. \n
  3. CCC DDD
' + ); }); }); @@ -144,7 +187,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); it('should return figure with extra class "test"', function() { @@ -175,7 +218,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); it('should return figure with image that has class "test"', function() { @@ -205,7 +248,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); it('should return figure with link that has class "test"', function() { @@ -235,7 +278,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); it('should return figure without link', function() { @@ -259,7 +302,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); it('should return figure without caption', function() { @@ -284,7 +327,7 @@ describe('html', function() { '', '' ].join('\n'); - assert.equal(fn(context), comparison); + equal(fn(context), comparison); }); }); }); diff --git a/test/i18n.js b/test/i18n.js index cbc1805f..3b8f3291 100644 --- a/test/i18n.js +++ b/test/i18n.js @@ -1,59 +1,67 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { throws, equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.i18n({handlebars: hbs}); +import { i18n } from '..'; +i18n({ handlebars: hbs }); -var context = {language: 'en', en: {key: 'value', a: {b: 'c'}}, fr: {key: 'valeur'}}; +var context = { + language: 'en', + en: { key: 'value', a: { b: 'c' } }, + fr: { key: 'valeur' } +}; describe('i18n', function() { it('should throw an error when key is not a string.', function() { - assert.throws(function() { + throws(function() { hbs.compile('{{#i18n}}{{/i18n}}')(); }); }); it('should throw an error when language parameter is not a string.', function() { - assert.throws(function() { + throws(function() { hbs.compile('{{#i18n "key"}}{{/i18n}}')(); }); }); it('should throw an error when the language is not found.', function() { - assert.throws(function() { - var ctx = {language: 'foo', en: {key: 'value'}, fr: {key: 'valeur'}}; + throws(function() { + var ctx = { + language: 'foo', + en: { key: 'value' }, + fr: { key: 'valeur' } + }; hbs.compile('{{#i18n "key"}}{{/i18n}}')(ctx); }); }); it('should throw an error when a key is not found.', function() { - assert.throws(function() { - var ctx = {language: 'en', en: {key: 'value'}, fr: {key: 'valeur'}}; + throws(function() { + var ctx = { language: 'en', en: { key: 'value' }, fr: { key: 'valeur' } }; hbs.compile('{{#i18n "foo"}}{{/i18n}}')(ctx); }); }); it('should take a key and return for the default language', function() { var fn = hbs.compile('{{#i18n "key"}}{{/i18n}}'); - assert.equal(fn(context), 'value'); + equal(fn(context), 'value'); }); it('should use options passed on the context', function() { var fn = hbs.compile('{{#i18n "key"}}{{/i18n}}'); - var context = {en: {key: 'value'}, fr: {key: 'valeur'}}; - context.options = {language: 'en'}; - assert.equal(fn(context), 'value'); + var context = { en: { key: 'value' }, fr: { key: 'valeur' } }; + context.options = { language: 'en' }; + equal(fn(context), 'value'); }); it('should take a key and return for the override language', function() { var fn = hbs.compile('{{#i18n "key" language="fr"}}{{/i18n}}'); - assert.equal(fn(context), 'valeur'); + equal(fn(context), 'valeur'); }); it('should support using dot notation for the key', function() { var fn = hbs.compile('{{#i18n "a.b"}}{{/i18n}}'); - assert.equal(fn(context), 'c'); + equal(fn(context), 'c'); }); }); diff --git a/test/inflection.js b/test/inflection.js index c0e1686f..015f8c7f 100644 --- a/test/inflection.js +++ b/test/inflection.js @@ -1,32 +1,34 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.inflection({handlebars: hbs}); +import { inflection } from '..'; +inflection({ handlebars: hbs }); describe('inflection', function() { describe('inflect', function() { it('should return the plural or singular form of a word based on a value.', function() { var template = hbs.compile('{{inflect mail "junk" "mail"}}'); - assert.equal(template({mail: 3}), 'mail'); + equal(template({ mail: 3 }), 'mail'); }); it('should return the plural or singular form of a word based on a value and include the count.', function() { - var template = hbs.compile('{{inflect messages "message" "messages" true}}'); - assert.equal(template({messages: 1}), '1 message'); + var template = hbs.compile( + '{{inflect messages "message" "messages" true}}' + ); + equal(template({ messages: 1 }), '1 message'); }); }); describe('ordinalize', function() { it('should return an ordinalized string.', function() { - assert.equal(hbs.compile('{{ordinalize 1}}')(), '1st'); - assert.equal(hbs.compile('{{ordinalize 3}}')(), '3rd'); - assert.equal(hbs.compile('{{ordinalize 11}}')(), '11th'); - assert.equal(hbs.compile('{{ordinalize 21}}')(), '21st'); - assert.equal(hbs.compile('{{ordinalize 29}}')(), '29th'); - assert.equal(hbs.compile('{{ordinalize 22}}')(), '22nd'); + equal(hbs.compile('{{ordinalize 1}}')(), '1st'); + equal(hbs.compile('{{ordinalize 3}}')(), '3rd'); + equal(hbs.compile('{{ordinalize 11}}')(), '11th'); + equal(hbs.compile('{{ordinalize 21}}')(), '21st'); + equal(hbs.compile('{{ordinalize 29}}')(), '29th'); + equal(hbs.compile('{{ordinalize 22}}')(), '22nd'); }); }); }); diff --git a/test/integration/templates.js b/test/integration/templates.js index 9df07e6a..6730d346 100644 --- a/test/integration/templates.js +++ b/test/integration/templates.js @@ -1,12 +1,12 @@ 'use strict'; -var os = require('os'); -var path = require('path'); -var assert = require('assert'); -var gm = require('global-modules'); -var engine = require('engine-handlebars'); -var templates = require('templates'); -var helpers = require('../..'); +import { homedir } from 'os'; +import { resolve, join } from 'path'; +import { equal } from 'assert'; +import gm from 'global-modules'; +import engine from 'engine-handlebars'; +import templates from 'templates'; +import helpers from '../..'; var compile; var render; var app; @@ -22,7 +22,7 @@ describe('templates integration tests', function() { }; compile = function(content) { - return app.compile(app.view({path: 'string', content: content})); + return app.compile(app.view({ path: 'string', content: content })); }; render = function(content, context) { @@ -32,79 +32,98 @@ describe('templates integration tests', function() { describe('absolute', function() { it('should create an absolute file path', function() { - assert.equal(render('{{absolute "a/b/c/package.json"}}'), path.resolve('a/b/c/package.json')); - assert.equal(render('{{absolute "a/b/c/docs/toc.md"}}'), path.resolve('a/b/c/docs/toc.md')); + equal( + render('{{absolute "a/b/c/package.json"}}'), + resolve('a/b/c/package.json') + ); + equal( + render('{{absolute "a/b/c/docs/toc.md"}}'), + resolve('a/b/c/docs/toc.md') + ); }); it('should use the cwd on locals', function() { - assert.equal(render('{{absolute "a/b/c/package.json"}}', {cwd: os.homedir()}), path.resolve(os.homedir(), 'a/b/c/package.json')); - assert.equal(render('{{absolute "a/b/c/package.json"}}', {cwd: os.homedir()}), path.resolve(os.homedir(), 'a/b/c/package.json')); - assert.equal(render('{{absolute "a/b/c/docs/toc.md"}}', {cwd: os.homedir()}), path.resolve(os.homedir(), 'a/b/c/docs/toc.md')); + equal( + render('{{absolute "a/b/c/package.json"}}', { cwd: homedir() }), + resolve(homedir(), 'a/b/c/package.json') + ); + equal( + render('{{absolute "a/b/c/package.json"}}', { cwd: homedir() }), + resolve(homedir(), 'a/b/c/package.json') + ); + equal( + render('{{absolute "a/b/c/docs/toc.md"}}', { cwd: homedir() }), + resolve(homedir(), 'a/b/c/docs/toc.md') + ); }); }); describe('dirname', function() { it('should get the dirname of a file path', function() { - assert.equal(render('{{dirname "a/b/c/package.json"}}'), 'a/b/c'); - assert.equal(render('{{dirname "a/b/c/docs/toc.md"}}'), 'a/b/c/docs'); + equal(render('{{dirname "a/b/c/package.json"}}'), 'a/b/c'); + equal(render('{{dirname "a/b/c/docs/toc.md"}}'), 'a/b/c/docs'); }); }); describe('relative', function() { it('should return the relative path from file A to file B', function() { var view = compile('{{relative "dist/docs.html" "index.html"}}'); - assert.equal(view.fn(), path.join('..', 'index.html')); + equal(view.fn(), join('..', 'index.html')); }); it('should return the relative path from file A to file B in', function() { - var view = compile('{{relative "examples/result/md/path.md" "examples/assets"}}'); - assert.equal(view.fn(), path.join('..', '..', 'assets')); + var view = compile( + '{{relative "examples/result/md/path.md" "examples/assets"}}' + ); + equal(view.fn(), join('..', '..', 'assets')); }); it('should use the cwd passed on options', function() { - var view = compile('{{relative "examples/result/md/path.md" "examples/assets"}}'); - assert.equal(view.fn({cwd: gm}), path.join('..', '..', 'assets')); + var view = compile( + '{{relative "examples/result/md/path.md" "examples/assets"}}' + ); + equal(view.fn({ cwd: gm }), join('..', '..', 'assets')); }); }); describe('basename', function() { it('should get the basename of a file path', function() { - assert.equal(render('{{basename "a/b/c/package.json"}}'), 'package.json'); - assert.equal(render('{{basename "a/b/c/docs/toc.md"}}'), 'toc.md'); + equal(render('{{basename "a/b/c/package.json"}}'), 'package.json'); + equal(render('{{basename "a/b/c/docs/toc.md"}}'), 'toc.md'); }); it('should get the basename when a path has no extension', function() { var view = compile('{{basename "a/b/c/CHANGELOG"}}'); - assert.equal(view.fn(), 'CHANGELOG'); + equal(view.fn(), 'CHANGELOG'); }); }); describe('stem', function() { it('should get the stem of a file path', function() { - assert.equal(render('{{stem "a/b/c/package.json"}}'), 'package'); - assert.equal(render('{{stem "a/b/c/docs/toc.md"}}'), 'toc'); + equal(render('{{stem "a/b/c/package.json"}}'), 'package'); + equal(render('{{stem "a/b/c/docs/toc.md"}}'), 'toc'); }); it('should get the stem when a path has no extension', function() { var view = compile('{{stem "CHANGELOG"}}'); - assert.equal(view.fn(), 'CHANGELOG'); + equal(view.fn(), 'CHANGELOG'); }); }); describe('extname', function() { it('should get the extname of a file path', function() { - assert.equal(render('{{extname "a/b/c/package.json"}}'), '.json'); - assert.equal(render('{{extname "a/b/c/docs/toc.md"}}'), '.md'); + equal(render('{{extname "a/b/c/package.json"}}'), '.json'); + equal(render('{{extname "a/b/c/docs/toc.md"}}'), '.md'); }); it('should not blow up when a path has no extension', function() { var view = compile('{{extname "a/b/c/CHANGELOG"}}'); - assert.equal(view.fn(), ''); + equal(view.fn(), ''); }); }); describe('segments', function() { it('should return specified path segments:', function() { - assert.equal(render('{{segments "a/b/c/e.js" 1 3}}'), 'b/c'); - assert.equal(render('{{segments "a/b/c/e.js" 1 2}}'), 'b'); - assert.equal(render('{{segments "a/b/c/e.js" 0 3}}'), 'a/b/c'); - assert.equal(render('{{segments "a/b/c/e.js" 2 3}}'), 'c'); - assert.equal(render('{{segments "a/b/c/e.js" 0 3}}'), 'a/b/c'); + equal(render('{{segments "a/b/c/e.js" 1 3}}'), 'b/c'); + equal(render('{{segments "a/b/c/e.js" 1 2}}'), 'b'); + equal(render('{{segments "a/b/c/e.js" 0 3}}'), 'a/b/c'); + equal(render('{{segments "a/b/c/e.js" 2 3}}'), 'c'); + equal(render('{{segments "a/b/c/e.js" 0 3}}'), 'a/b/c'); }); }); }); diff --git a/test/match.js b/test/match.js index ec1f111c..c498aa3c 100644 --- a/test/match.js +++ b/test/match.js @@ -1,67 +1,77 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var assert = require('assert'); +import { readdirSync } from 'fs'; +import { join } from 'path'; +import assert, { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.match({handlebars: hbs}); +import { match as _match } from '..'; +_match({ handlebars: hbs }); -var testFiles = fs.readdirSync(__dirname); -var rootFiles = fs.readdirSync(path.join(__dirname, '..')); +var testFiles = readdirSync(__dirname); +var rootFiles = readdirSync(join(__dirname, '..')); describe('matching', function() { describe('match', function() { it('should use the main micromatch function to filter an array', function() { var fn = hbs.compile('{{match files "(a|u)*.js"}}'); - assert.equal(fn({files: testFiles}), 'array.js,url.js,utils.js,uuid.js'); + equal(fn({ files: testFiles }), 'array.js,url.js,utils.js,uuid.js'); }); it('should take an array of patterns', function() { - var ctx = {files: testFiles, patterns: ['(a|u)*.js', 'f*.js']}; + var ctx = { files: testFiles, patterns: ['(a|u)*.js', 'f*.js'] }; var fn = hbs.compile('{{match files patterns}}'); - assert.equal(fn(ctx), 'array.js,url.js,utils.js,uuid.js'); + equal(fn(ctx), 'array.js,url.js,utils.js,uuid.js'); }); it('should take options from the "options[helper name]" object', function() { - var ctx = {files: testFiles, options: {match: {dot: true}}}; + var ctx = { files: testFiles, options: { match: { dot: true } } }; var fn = hbs.compile('{{match files "*"}}'); assert(/array\.js/.test(fn(ctx))); }); it('should take options from the hash', function() { - var ctx = {files: rootFiles}; - assert(/\.gitignore/.test(hbs.compile('{{match files "*" dot=true}}')(ctx))); - assert(!/\.gitignore/.test(hbs.compile('{{match files "*" dot=false}}')(ctx))); + var ctx = { files: rootFiles }; + assert( + /\.gitignore/.test(hbs.compile('{{match files "*" dot=true}}')(ctx)) + ); + assert( + !/\.gitignore/.test(hbs.compile('{{match files "*" dot=false}}')(ctx)) + ); }); it('should use return matching items', function() { var fn = hbs.compile('{{match files "(a|u)*.js"}}'); - assert.equal(fn({files: testFiles}), 'array.js,url.js,utils.js,uuid.js'); + equal(fn({ files: testFiles }), 'array.js,url.js,utils.js,uuid.js'); }); it('should take options from the "options[helper name]" object', function() { - var ctx = {files: rootFiles, options: {match: {dot: true}}}; + var ctx = { files: rootFiles, options: { match: { dot: true } } }; var fn = hbs.compile('{{match files "*"}}'); assert(/\.gitignore/.test(fn(ctx))); }); it('should take options from the hash', function() { - var ctx = {files: rootFiles}; - assert(/\.gitignore/.test(hbs.compile('{{match files "*" dot=true}}')(ctx))); - assert(!/\.gitignore/.test(hbs.compile('{{match files "*" dot=false}}')(ctx))); + var ctx = { files: rootFiles }; + assert( + /\.gitignore/.test(hbs.compile('{{match files "*" dot=true}}')(ctx)) + ); + assert( + !/\.gitignore/.test(hbs.compile('{{match files "*" dot=false}}')(ctx)) + ); }); it('should take options passed as the last argument', function() { - var ctx = {files: rootFiles, options: {dot: true}}; - assert(/\.gitignore/.test(hbs.compile('{{match files "*" options}}')(ctx))); + var ctx = { files: rootFiles, options: { dot: true } }; + assert( + /\.gitignore/.test(hbs.compile('{{match files "*" options}}')(ctx)) + ); }); }); describe('isMatch', function() { it('should return true if the given value matches the glob', function() { - assert.equal(hbs.compile('{{isMatch "foo.js" "*.js"}}')(), 'true'); - assert.equal(hbs.compile('{{isMatch "foo.js" "*.json"}}')(), 'false'); + equal(hbs.compile('{{isMatch "foo.js" "*.js"}}')(), 'true'); + equal(hbs.compile('{{isMatch "foo.js" "*.json"}}')(), 'false'); }); }); }); diff --git a/test/math.js b/test/math.js index f06d82ed..28202495 100644 --- a/test/math.js +++ b/test/math.js @@ -1,101 +1,101 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import assert, { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.math({handlebars: hbs}); +import { math } from '..'; +math({ handlebars: hbs }); describe('math', function() { describe('add', function() { it('should return the sum of two numbers.', function() { var fn = hbs.compile('{{add value 5}}'); - assert.equal(fn({value: 5}), '10'); + equal(fn({ value: 5 }), '10'); }); }); describe('average', function() { it('should return the average of a list of numbers:', function() { var fn = hbs.compile('{{avg 1 2 3 4}}'); - assert.equal(fn(), '2.5'); + equal(fn(), '2.5'); }); it('should return the average of an array of numbers:', function() { var fn = hbs.compile('{{avg array}}'); - assert.equal(fn({array: [1, 3, 6, 9]}), '4.75'); + equal(fn({ array: [1, 3, 6, 9] }), '4.75'); }); }); describe('ceil', function() { it('should return the value rounded up to the nearest integer.', function() { var fn = hbs.compile('{{ceil value}}'); - assert.equal(fn({value: 5.6}), '6'); + equal(fn({ value: 5.6 }), '6'); }); }); describe('divide', function() { it('should return the division of two numbers.', function() { var fn = hbs.compile('{{divide value 5}}'); - assert.equal(fn({value: 5}), '1'); + equal(fn({ value: 5 }), '1'); }); }); describe('floor', function() { it('should return the value rounded down to the nearest integer.', function() { var fn = hbs.compile('{{floor value}}'); - assert.equal(fn({value: 5.6}), '5'); + equal(fn({ value: 5.6 }), '5'); }); }); describe('multiply', function() { it('should return the multiplication of two numbers.', function() { var fn = hbs.compile('{{multiply value 5}}'); - assert.equal(fn({value: 5}), '25'); + equal(fn({ value: 5 }), '25'); }); }); describe('remainder', function() { it('should return the remainder of two numbers.', function() { var fn = hbs.compile('{{remainder value 5}}'); - assert.equal(fn({value: 7}), '2'); + equal(fn({ value: 7 }), '2'); }); it('should take the sign of the dividend.', function() { var fn = hbs.compile('{{remainder 5 -3}}'); - assert.equal(fn(), '2'); + equal(fn(), '2'); }); }); describe('round', function() { it('should return the value rounded to the nearest integer.', function() { var fn = hbs.compile('{{round value}}'); - assert.equal(fn({value: 5.69}), '6'); + equal(fn({ value: 5.69 }), '6'); }); }); describe('subtract', function() { it('should return the difference of two numbers.', function() { var fn = hbs.compile('{{subtract value 5}}'); - assert.equal(fn({value: 5}), '0'); + equal(fn({ value: 5 }), '0'); }); }); describe('sum', function() { it('should return the sum of multiple numbers.', function() { var fn = hbs.compile('{{sum value 67 80}}'); - assert.equal(fn({value: 20}), '167'); + equal(fn({ value: 20 }), '167'); }); it('should return the sum of multiple numbers.', function() { var fn = hbs.compile('{{sum 1 2 3}}'); - assert.equal(fn(), '6'); + equal(fn(), '6'); }); it('should return the total sum of array.', function() { var fn = hbs.compile('{{sum value}}'); - assert.equal(fn({value: [1, 2, 3]}), '6'); + equal(fn({ value: [1, 2, 3] }), '6'); }); it('should return the total sum of array and numbers.', function() { var fn = hbs.compile('{{sum value 5}}'); - assert.equal(fn({value: [1, 2, 3]}), '11'); + equal(fn({ value: [1, 2, 3] }), '11'); }); }); diff --git a/test/misc.js b/test/misc.js index 210a6947..fe924136 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,66 +1,72 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); +import { misc } from '..'; describe('misc', function() { beforeEach(function() { - helpers.misc({handlebars: hbs}); + misc({ handlebars: hbs }); }); describe('noop', function() { it('should be a noop', function() { var fn = hbs.compile('{{#noop}}{{message}}{{/noop}}'); - assert.equal(fn({message: 'This is a test'}), 'This is a test'); + equal(fn({ message: 'This is a test' }), 'This is a test'); }); }); describe('option', function() { it('should get an option', function() { var fn = hbs.compile('{{option "a"}}'); - assert.equal(fn({options: {a: 'bbb'}}), 'bbb'); + equal(fn({ options: { a: 'bbb' } }), 'bbb'); }); it('should return an empty string when no options are found', function() { - assert.equal(hbs.compile('{{option "a"}}')(), ''); + equal(hbs.compile('{{option "a"}}')(), ''); }); it('should get a nested option', function() { var fn = hbs.compile('{{option "a.b.c"}}'); - assert.equal(fn({options: {a: {b: {c: 'ddd'}}}}), 'ddd'); + equal(fn({ options: { a: { b: { c: 'ddd' } } } }), 'ddd'); }); it('should work as a subexpression', function() { var fn = hbs.compile('{{option "a.b.c"}}'); - assert.equal(fn({options: {a: {b: {c: 'ddd'}}}}), 'ddd'); + equal(fn({ options: { a: { b: { c: 'ddd' } } } }), 'ddd'); }); }); describe('withHash', function() { it('should return an empty sting', function() { var fn = hbs.compile('{{#withHash}}{{message}}{{/withHash}}'); - var actual = fn({message: 'This is a test'}); - assert.equal(typeof actual, 'string'); - assert.equal(actual, ''); + var actual = fn({ message: 'This is a test' }); + equal(typeof actual, 'string'); + equal(actual, ''); }); it('should not blow up when no hash is defined.', function() { var fn = hbs.compile('{{#withHash}}{{/withHash}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the inverse hash when defined and the value is falsy.', function() { var fn = hbs.compile('{{#withHash}}foo{{else}}bar{{/withHash}}'); - assert.equal(fn(), 'bar'); + equal(fn(), 'bar'); }); it('should return string from the newly created context', function() { - var fn = hbs.compile('{{#withHash message="test"}}{{message}}{{/withHash}}'); - assert.equal(fn({message: 'This is a test'}), 'test'); + var fn = hbs.compile( + '{{#withHash message="test"}}{{message}}{{/withHash}}' + ); + equal(fn({ message: 'This is a test' }), 'test'); }); it('should return string from the parent context', function() { - var fn = hbs.compile('{{#withHash message=this.message}}{{message}}{{/withHash}}'); - assert.equal(fn({message: 'This is a test'}), 'This is a test'); + var fn = hbs.compile( + '{{#withHash message=this.message}}{{message}}{{/withHash}}' + ); + equal(fn({ message: 'This is a test' }), 'This is a test'); }); it('should add two attributes to the new context', function() { - var fn = hbs.compile('{{#withHash subject="Feedback" message="Hello!"}}{{subject}} - {{message}}{{/withHash}}'); - assert.equal(fn({}), 'Feedback - Hello!'); + var fn = hbs.compile( + '{{#withHash subject="Feedback" message="Hello!"}}{{subject}} - {{message}}{{/withHash}}' + ); + equal(fn({}), 'Feedback - Hello!'); }); }); }); diff --git a/test/number.js b/test/number.js index ab1031fc..e09b4083 100644 --- a/test/number.js +++ b/test/number.js @@ -1,115 +1,121 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.number({handlebars: hbs}); +import { number as _number } from '..'; +_number({ handlebars: hbs }); describe('number', function() { describe('bytes', function() { it('should format a number', function() { - assert.equal(hbs.compile('{{bytes num}}')({num: 13661855}), '13.66 MB'); - assert.equal(hbs.compile('{{bytes num}}')({num: 825399}), '825.4 kB'); - assert.equal(hbs.compile('{{bytes num}}')({num: 1396}), '1.4 kB'); - assert.equal(hbs.compile('{{bytes num}}')({num: 0}), '0 B'); - assert.equal(hbs.compile('{{bytes num}}')({num: 1}), '1 B'); - assert.equal(hbs.compile('{{bytes num}}')({num: 2}), '2 B'); + equal(hbs.compile('{{bytes num}}')({ num: 13661855 }), '13.66 MB'); + equal(hbs.compile('{{bytes num}}')({ num: 825399 }), '825.4 kB'); + equal(hbs.compile('{{bytes num}}')({ num: 1396 }), '1.4 kB'); + equal(hbs.compile('{{bytes num}}')({ num: 0 }), '0 B'); + equal(hbs.compile('{{bytes num}}')({ num: 1 }), '1 B'); + equal(hbs.compile('{{bytes num}}')({ num: 2 }), '2 B'); }); it('should return "0 B" when an invalid value is passed', function() { - assert.equal(hbs.compile('{{bytes num}}')({num: {}}), '0 B'); + equal(hbs.compile('{{bytes num}}')({ num: {} }), '0 B'); }); it('should return string length when a string is passed', function() { - assert.equal(hbs.compile('{{bytes num}}')({num: 'foo'}), '3 B'); - assert.equal(hbs.compile('{{bytes num}}')({num: 'foobar'}), '6 B'); + equal(hbs.compile('{{bytes num}}')({ num: 'foo' }), '3 B'); + equal(hbs.compile('{{bytes num}}')({ num: 'foobar' }), '6 B'); }); }); describe('phoneNumber', function() { it('Format a phone number.', function() { var fn = hbs.compile('{{phoneNumber value}}'); - assert.equal(fn({value: '8005551212'}), '(800) 555-1212'); + equal(fn({ value: '8005551212' }), '(800) 555-1212'); }); }); describe('toFixed', function() { it('should return the value rounded to the nearest integer.', function() { var fn = hbs.compile('{{toFixed value}}'); - assert.equal(fn({value: 5.53231 }), '6'); + equal(fn({ value: 5.53231 }), '6'); }); it('should return the value rounded exactly n digits after the decimal place.', function() { var fn = hbs.compile('{{toFixed value 3}}'); - assert.equal(fn({value: 5.53231 }), '5.532'); + equal(fn({ value: 5.53231 }), '5.532'); }); }); describe('toPrecision', function() { it('Returns the number in fixed-point or exponential notation rounded to n significant digits.', function() { var fn = hbs.compile('{{toPrecision value}}'); - assert.equal(fn({value: 555.322 }), '6e+2'); + equal(fn({ value: 555.322 }), '6e+2'); }); it('should return the value rounded exactly n digits after the decimal place.', function() { var fn = hbs.compile('{{toPrecision value 4}}'); - assert.equal(fn({value: 555.322 }), '555.3'); + equal(fn({ value: 555.322 }), '555.3'); }); }); describe('toExponential', function() { it('should return the number in fixed-point or exponential notation rounded to n significant digits.', function() { var fn = hbs.compile('{{toExponential value}}'); - assert.equal(fn({value: 5 }), '5e+0'); + equal(fn({ value: 5 }), '5e+0'); }); it('should return the number in fixed-point or exponential notation rounded to exactly n significant digits.', function() { var fn = hbs.compile('{{toExponential value 5}}'); - assert.equal(fn({value: 5 }), '5.00000e+0'); + equal(fn({ value: 5 }), '5.00000e+0'); }); }); describe('toInt', function() { it('should return an integer.', function() { var fn = hbs.compile('{{toInt value}}'); - assert.equal(fn({value: '3cc'}), '3'); + equal(fn({ value: '3cc' }), '3'); }); }); describe('toFloat', function() { it('should return a floating point number.', function() { var fn = hbs.compile('{{toFloat value}}'); - assert.equal(fn({value: '3.1cc'}), '3.1'); + equal(fn({ value: '3.1cc' }), '3.1'); }); }); describe('addCommas', function() { it('should add commas to a number.', function() { var fn = hbs.compile('{{addCommas value}}'); - assert.equal(fn({value: 2222222 }), '2,222,222'); + equal(fn({ value: 2222222 }), '2,222,222'); }); }); describe('toAbbr', function() { it('should abbreviate the given number.', function() { var fn = hbs.compile('{{toAbbr number}}'); - assert.equal(fn({number: 123456789 }), '123.46m'); + equal(fn({ number: 123456789 }), '123.46m'); }); it('should abbreviate a number with to the given decimal.', function() { var fn = hbs.compile('{{toAbbr number 3}}'); - assert.equal(fn({number: 123456789 }), '123.457m'); + equal(fn({ number: 123456789 }), '123.457m'); }); it('should round up to the next increment', function() { var fn = hbs.compile('{{toAbbr number}}'); - assert.equal(fn({number: 999 }), '1k'); + equal(fn({ number: 999 }), '1k'); }); it('should abbreviate a number based on a number and include decimal.', function() { - assert.equal(hbs.compile('{{toAbbr number 0}}')({number: 9999999 }), '10m'); - assert.equal(hbs.compile('{{toAbbr number}}')({number: 1000000000 }), '1b'); - assert.equal(hbs.compile('{{toAbbr number}}')({number: 1000000000000 }), '1t'); - assert.equal(hbs.compile('{{toAbbr number}}')({number: 1000000000000000 }), '1q'); - assert.equal(hbs.compile('{{toAbbr number}}')({number: 99393999393 }), '99.39b'); + equal(hbs.compile('{{toAbbr number 0}}')({ number: 9999999 }), '10m'); + equal(hbs.compile('{{toAbbr number}}')({ number: 1000000000 }), '1b'); + equal(hbs.compile('{{toAbbr number}}')({ number: 1000000000000 }), '1t'); + equal( + hbs.compile('{{toAbbr number}}')({ number: 1000000000000000 }), + '1q' + ); + equal( + hbs.compile('{{toAbbr number}}')({ number: 99393999393 }), + '99.39b' + ); }); }); }); diff --git a/test/object.js b/test/object.js index 2f5a246f..4bbd0d95 100644 --- a/test/object.js +++ b/test/object.js @@ -1,61 +1,65 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); -var support = require('./support'); -var expected = support.expected('object'); -var helpers = require('..'); +import 'mocha'; +import { equal, deepEqual } from 'assert'; +import { expected as _expected } from './support'; +var expected = _expected('object'); +import helpers, { math, object as _object } from '..'; var hbs = require('handlebars').create(); -helpers.math({handlebars: hbs}); -helpers.object({handlebars: hbs}); +math({ handlebars: hbs }); +_object({ handlebars: hbs }); -var context = {object: {a: 'b', c: 'd', e: 'f'}}; +var context = { object: { a: 'b', c: 'd', e: 'f' } }; describe('object', function() { describe('extend', function() { it('should extend multiple objects into one:', function() { var fn = hbs.compile('{{{stringify (extend a d g)}}}'); - var actual = fn({a: {b: 'c'}, d: {e: 'f'}, g: {h: 'i'}}); - assert.equal(actual, expected('extend.txt')); + var actual = fn({ a: { b: 'c' }, d: { e: 'f' }, g: { h: 'i' } }); + equal(actual, expected('extend.txt')); }); it('should work as a non-helper util:', function() { - var actual = helpers().extend({a: {b: 'c'}}, {d: {e: 'f'}}, {g: {h: 'i'}}); - assert.deepEqual(actual, { a: { b: 'c' }, d: { e: 'f' }, g: { h: 'i' } }); + var actual = helpers().extend( + { a: { b: 'c' } }, + { d: { e: 'f' } }, + { g: { h: 'i' } } + ); + deepEqual(actual, { a: { b: 'c' }, d: { e: 'f' }, g: { h: 'i' } }); }); it('should skip over sparse objects', function() { - var actual = helpers().extend({a: {b: 'c'}}, null, {g: {h: 'i'}}); - assert.deepEqual(actual, { a: { b: 'c' }, g: { h: 'i' } }); + var actual = helpers().extend({ a: { b: 'c' } }, null, { g: { h: 'i' } }); + deepEqual(actual, { a: { b: 'c' }, g: { h: 'i' } }); }); }); describe('forIn', function() { it('should iterate over each property in an object:', function() { var fn = hbs.compile('{{#forIn this}} {{@key}} {{.}} {{/forIn}}'); - assert.equal(fn(context.object), ' a b c d e f '); + equal(fn(context.object), ' a b c d e f '); }); it('should return the inverse block if no object is passed:', function() { var fn = hbs.compile('{{#forIn}} {{.}} {{else}} Nada. {{/forIn}}'); - assert.equal(fn(context.object), ' Nada. '); + equal(fn(context.object), ' Nada. '); }); it('should expose private variables:', function() { var fn = hbs.compile('{{#forIn this abc=object}} {{@abc.a}} {{/forIn}}'); - assert.equal(fn(context), ' b '); + equal(fn(context), ' b '); }); }); describe('forOwn', function() { it('should iterate over each property in an object:', function() { var fn = hbs.compile('{{#forOwn this}} {{@key}} {{.}} {{/forOwn}}'); - assert.equal(fn(context.object), ' a b c d e f '); + equal(fn(context.object), ' a b c d e f '); }); it('should return the inverse block if no object is passed:', function() { var fn = hbs.compile('{{#forOwn}} {{.}} {{else}} Nada. {{/forOwn}}'); - assert.equal(fn(context.object), ' Nada. '); + equal(fn(context.object), ' Nada. '); }); it('should only expose "own" keys:', function() { @@ -65,61 +69,72 @@ describe('object', function() { } Foo.prototype.c = 'd'; var fn = hbs.compile('{{#forOwn this}} {{.}} {{/forOwn}}'); - assert.equal(fn(new Foo()), ' b c '); + equal(fn(new Foo()), ' b c '); }); it('should expose private variables:', function() { - var fn = hbs.compile('{{#forOwn this abc=object}} {{@abc.c}} {{/forOwn}}'); - assert.equal(fn(context), ' d '); + var fn = hbs.compile( + '{{#forOwn this abc=object}} {{@abc.c}} {{/forOwn}}' + ); + equal(fn(context), ' d '); }); }); describe('getObject', function() { it('should get an object from the context', function() { - var one = hbs.compile('{{{stringify (getObject "a" this)}}}')({a: 'b'}); - assert.equal(one, '{"a":"b"}'); + var one = hbs.compile('{{{stringify (getObject "a" this)}}}')({ a: 'b' }); + equal(one, '{"a":"b"}'); - var two = hbs.compile('{{{stringify (getObject "c" this)}}}')({c: 'd'}); - assert.equal(two, '{"c":"d"}'); + var two = hbs.compile('{{{stringify (getObject "c" this)}}}')({ c: 'd' }); + equal(two, '{"c":"d"}'); }); }); describe('toPath', function() { it('should return a path from provided arguments', function() { - assert.equal(hbs.compile('{{toPath "a" "b" "c"}}')(), 'a.b.c'); + equal(hbs.compile('{{toPath "a" "b" "c"}}')(), 'a.b.c'); }); it('should return a path from calculated arguments', function() { var t = hbs.compile('{{toPath "a" (add 1 1) "b"}}')(); - assert.equal(t, 'a.2.b'); + equal(t, 'a.2.b'); }); it('should return a `get` compatible path', function() { var fn = hbs.compile('{{get (toPath "a" (add 1 1) "j") this}}'); - assert.equal(fn({a: [{b: 'c', d: 'e'}, {f: 'g', h: 'i'}, {j: 'k', l: 'm'}]}), 'k'); + equal( + fn({ + a: [ + { b: 'c', d: 'e' }, + { f: 'g', h: 'i' }, + { j: 'k', l: 'm' } + ] + }), + 'k' + ); }); }); describe('get', function() { it('should get a value from the context', function() { - assert.equal(hbs.compile('{{get "a" this}}')({a: 'b'}), 'b'); - assert.equal(hbs.compile('{{get "c" this}}')({c: 'd'}), 'd'); + equal(hbs.compile('{{get "a" this}}')({ a: 'b' }), 'b'); + equal(hbs.compile('{{get "c" this}}')({ c: 'd' }), 'd'); }); it('should get a nested value from the context', function() { var fn = hbs.compile('{{get "a.b.c.d" this}}'); - assert.equal(fn({a: {b: {c: {d: 'e'}}}}), 'e'); + equal(fn({ a: { b: { c: { d: 'e' } } } }), 'e'); }); it('should work as a block helper', function() { var fn1 = hbs.compile('{{#get "a" this}} {{.}} {{/get}}'); - assert.equal(fn1(context.object), ' b '); + equal(fn1(context.object), ' b '); var fn2 = hbs.compile('{{#get "c" this}} {{.}} {{/get}}'); - assert.equal(fn2(context.object), ' d '); + equal(fn2(context.object), ' d '); }); it('should get the inverse block if not found', function() { var fn = hbs.compile('{{#get "foo" this}} {{.}} {{else}}Nope.{{/get}}'); - assert.equal(fn(context.object), 'Nope.'); + equal(fn(context.object), 'Nope.'); }); }); @@ -132,75 +147,79 @@ describe('object', function() { it('should return true if object has own property:', function() { var fn = hbs.compile('{{hasOwn this "a"}}'); - assert.equal(fn(new Foo()), 'true'); + equal(fn(new Foo()), 'true'); }); it('should return false if object does not have own property:', function() { var fn = hbs.compile('{{hasOwn this "c"}}'); - assert.equal(fn(new Foo()), 'false'); + equal(fn(new Foo()), 'false'); }); }); describe('isObject', function() { it('should return true if value is an object:', function() { var fn = hbs.compile('{{isObject this}}'); - assert.equal(fn({a: 'b'}), 'true'); + equal(fn({ a: 'b' }), 'true'); }); it('should return false if value is not an object:', function() { var fn = hbs.compile('{{isObject this}}'); - assert.equal(fn('foo'), 'false'); + equal(fn('foo'), 'false'); }); }); describe('merge', function() { it('should deeply merge objects passed on the context:', function() { var fn = hbs.compile('{{{stringify (merge a b c)}}}'); - var actual = fn({a: {one: 'two'}, b: {one: 'three'}, c: {two: 'four'}}); - assert.equal(actual, '{"one":"three","two":"four"}'); + var actual = fn({ + a: { one: 'two' }, + b: { one: 'three' }, + c: { two: 'four' } + }); + equal(actual, '{"one":"three","two":"four"}'); }); }); describe('JSONparse', function() { it('should parse a JSON string:', function() { var fn = hbs.compile('{{lookup (JSONparse string) "name"}}'); - assert.equal(fn({string: '{"name": "Fry"}'}), 'Fry'); + equal(fn({ string: '{"name": "Fry"}' }), 'Fry'); }); }); describe('pick', function() { it('should pick a value from the context', function() { - var one = hbs.compile('{{{stringify (pick "a" this)}}}')({a: 'b'}); - assert.equal(one, '{"a":"b"}'); + var one = hbs.compile('{{{stringify (pick "a" this)}}}')({ a: 'b' }); + equal(one, '{"a":"b"}'); - var two = hbs.compile('{{{stringify (pick "c" this)}}}')({c: 'd'}); - assert.equal(two, '{"c":"d"}'); + var two = hbs.compile('{{{stringify (pick "c" this)}}}')({ c: 'd' }); + equal(two, '{"c":"d"}'); }); it('should pick a nested value from the context', function() { var fn = hbs.compile('{{{stringify (pick "a.b.c" this)}}}'); - assert.equal(fn({a: {b: {c: {d: 'e'}}}}), '{"c":{"d":"e"}}'); + equal(fn({ a: { b: { c: { d: 'e' } } } }), '{"c":{"d":"e"}}'); }); it('should work as a block helper', function() { var fn1 = hbs.compile('{{#pick "a" this}} {{{stringify .}}} {{/pick}}'); - assert.equal(fn1(context.object), ' {"a":"b"} '); + equal(fn1(context.object), ' {"a":"b"} '); var fn2 = hbs.compile('{{#pick "c" this}} {{{stringify .}}} {{/pick}}'); - assert.equal(fn2(context.object), ' {"c":"d"} '); + equal(fn2(context.object), ' {"c":"d"} '); }); it('should pick the inverse block if not found', function() { var fn = hbs.compile('{{#pick "foo" this}} {{.}} {{else}}Nope.{{/pick}}'); - assert.equal(fn(context.object), 'Nope.'); + equal(fn(context.object), 'Nope.'); }); }); describe('stringify', function() { it('should stringify an object:', function() { var fn = hbs.compile('{{{stringify data}}}'); - var res = fn({data: {name: 'Halle', age: 4, userid: 'Nicole'}}); - assert.equal(res, '{"name":"Halle","age":4,"userid":"Nicole"}'); + var res = fn({ data: { name: 'Halle', age: 4, userid: 'Nicole' } }); + equal(res, '{"name":"Halle","age":4,"userid":"Nicole"}'); }); }); }); diff --git a/test/path.js b/test/path.js index e3d54b1c..4c24a3be 100644 --- a/test/path.js +++ b/test/path.js @@ -1,89 +1,105 @@ 'use strict'; -require('mocha'); -var os = require('os'); -var assert = require('assert'); -var path = require('path'); +import 'mocha'; +import { homedir } from 'os'; +import { equal } from 'assert'; +import { resolve, join } from 'path'; var hbs = require('handlebars').create(); -var gm = require('global-modules'); -var helpers = require('..'); -helpers.path({handlebars: hbs}); +import gm from 'global-modules'; +import { path as _path } from '..'; +_path({ handlebars: hbs }); describe('assemble', function() { describe('absolute', function() { it('should create an absolute file path', function() { - assert.equal(hbs.compile('{{absolute "a/b/c/package.json"}}')(), path.resolve('a/b/c/package.json')); - assert.equal(hbs.compile('{{absolute "a/b/c/docs/toc.md"}}')(), path.resolve('a/b/c/docs/toc.md')); + equal( + hbs.compile('{{absolute "a/b/c/package.json"}}')(), + resolve('a/b/c/package.json') + ); + equal( + hbs.compile('{{absolute "a/b/c/docs/toc.md"}}')(), + resolve('a/b/c/docs/toc.md') + ); }); it('should use the cwd on locals', function() { - assert.equal(hbs.compile('{{absolute "a/b/c/package.json"}}')({cwd: os.homedir()}), path.resolve(os.homedir(), 'a/b/c/package.json')); - assert.equal(hbs.compile('{{absolute "a/b/c/docs/toc.md"}}')({cwd: gm}), path.resolve(gm, 'a/b/c/docs/toc.md')); + equal( + hbs.compile('{{absolute "a/b/c/package.json"}}')({ cwd: homedir() }), + resolve(homedir(), 'a/b/c/package.json') + ); + equal( + hbs.compile('{{absolute "a/b/c/docs/toc.md"}}')({ cwd: gm }), + resolve(gm, 'a/b/c/docs/toc.md') + ); }); }); describe('dirname', function() { it('should get the dirname of a file path', function() { - assert.equal(hbs.compile('{{dirname "a/b/c/package.json"}}')(), 'a/b/c'); - assert.equal(hbs.compile('{{dirname "a/b/c/docs/toc.md"}}')(), 'a/b/c/docs'); + equal(hbs.compile('{{dirname "a/b/c/package.json"}}')(), 'a/b/c'); + equal(hbs.compile('{{dirname "a/b/c/docs/toc.md"}}')(), 'a/b/c/docs'); }); }); describe('relative', function() { it('should return the relative path from file A to file B', function() { var fn = hbs.compile('{{relative "dist/docs.html" "index.html"}}'); - assert.equal(fn(), path.join('..', 'index.html')); + equal(fn(), join('..', 'index.html')); }); it('should return the relative path from file A to folder B', function() { - var fn = hbs.compile('{{relative "examples/result/md/path.md" "examples/assets"}}'); - assert.equal(fn(), path.join('..', '..', 'assets')); + var fn = hbs.compile( + '{{relative "examples/result/md/path.md" "examples/assets"}}' + ); + equal(fn(), join('..', '..', 'assets')); }); it('should use the cwd passed on options', function() { - var fn = hbs.compile('{{relative "examples/result/md/path.md" "examples/assets"}}'); - assert.equal(fn({cwd: gm}), path.join('..', '..', 'assets')); + var fn = hbs.compile( + '{{relative "examples/result/md/path.md" "examples/assets"}}' + ); + equal(fn({ cwd: gm }), join('..', '..', 'assets')); }); }); describe('basename', function() { it('should get the basename of a file path', function() { - assert.equal(hbs.compile('{{basename "a/b/c/package.json"}}')(), 'package.json'); - assert.equal(hbs.compile('{{basename "a/b/c/docs/toc.md"}}')(), 'toc.md'); + equal(hbs.compile('{{basename "a/b/c/package.json"}}')(), 'package.json'); + equal(hbs.compile('{{basename "a/b/c/docs/toc.md"}}')(), 'toc.md'); }); it('should get the basename when a path has no extension', function() { var fn = hbs.compile('{{basename "a/b/c/CHANGELOG"}}'); - assert.equal(fn(), 'CHANGELOG'); + equal(fn(), 'CHANGELOG'); }); }); describe('stem', function() { it('should get the stem of a file path', function() { - assert.equal(hbs.compile('{{stem "a/b/c/package.json"}}')(), 'package'); - assert.equal(hbs.compile('{{stem "a/b/c/docs/toc.md"}}')(), 'toc'); + equal(hbs.compile('{{stem "a/b/c/package.json"}}')(), 'package'); + equal(hbs.compile('{{stem "a/b/c/docs/toc.md"}}')(), 'toc'); }); it('should get the stem when a path has no extension', function() { var fn = hbs.compile('{{stem "CHANGELOG"}}'); - assert.equal(fn(), 'CHANGELOG'); + equal(fn(), 'CHANGELOG'); }); }); describe('extname', function() { it('should get the extname of a file path', function() { - assert.equal(hbs.compile('{{extname "a/b/c/package.json"}}')(), '.json'); - assert.equal(hbs.compile('{{extname "a/b/c/docs/toc.md"}}')(), '.md'); + equal(hbs.compile('{{extname "a/b/c/package.json"}}')(), '.json'); + equal(hbs.compile('{{extname "a/b/c/docs/toc.md"}}')(), '.md'); }); it('should not blow up when a path has no extension', function() { var fn = hbs.compile('{{extname "a/b/c/CHANGELOG"}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); }); describe('segments', function() { it('should return specified path segments:', function() { - assert.equal(hbs.compile('{{segments "a/b/c/e.js" 1 3}}')(), 'b/c'); - assert.equal(hbs.compile('{{segments "a/b/c/e.js" 1 2}}')(), 'b'); - assert.equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c'); - assert.equal(hbs.compile('{{segments "a/b/c/e.js" 2 3}}')(), 'c'); - assert.equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c'); + equal(hbs.compile('{{segments "a/b/c/e.js" 1 3}}')(), 'b/c'); + equal(hbs.compile('{{segments "a/b/c/e.js" 1 2}}')(), 'b'); + equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c'); + equal(hbs.compile('{{segments "a/b/c/e.js" 2 3}}')(), 'c'); + equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c'); }); }); }); diff --git a/test/string.js b/test/string.js index 493c366d..c00211d5 100644 --- a/test/string.js +++ b/test/string.js @@ -1,400 +1,434 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.string({ handlebars: hbs }); +import { string } from '..'; +string({ handlebars: hbs }); describe('string', function() { describe('camelcase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{camelcase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in camelcase', function() { var fn = hbs.compile('{{camelcase "foo bar baz qux"}}'); - assert.equal(fn(), 'fooBarBazQux'); + equal(fn(), 'fooBarBazQux'); }); it('should lowercase a single character', function() { - assert.equal(hbs.compile('{{camelcase "f"}}')(), 'f'); - assert.equal(hbs.compile('{{camelcase "A"}}')(), 'a'); + equal(hbs.compile('{{camelcase "f"}}')(), 'f'); + equal(hbs.compile('{{camelcase "A"}}')(), 'a'); }); }); describe('capitalize', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{capitalize}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should capitalize a word.', function() { var fn = hbs.compile('{{capitalize "foo"}}'); - assert.equal(fn(), 'Foo'); + equal(fn(), 'Foo'); }); }); describe('capitalizeAll', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{capitalizeAll}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string with the every word capitalized.', function() { - var fn = hbs.compile('{{capitalizeAll "bender should not bE allowed on tV"}}'); - assert.equal(fn(), 'Bender Should Not BE Allowed On TV'); + var fn = hbs.compile( + '{{capitalizeAll "bender should not bE allowed on tV"}}' + ); + equal(fn(), 'Bender Should Not BE Allowed On TV'); }); }); describe('center', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{center}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string centered by using non-breaking spaces.', function() { - var fn = hbs.compile('{{center "Bender should not be allowed on tv." 2}}'); - assert.equal(fn(), '&nbsp;&nbsp;Bender should not be allowed on tv.&nbsp;&nbsp;'); + var fn = hbs.compile( + '{{center "Bender should not be allowed on tv." 2}}' + ); + equal( + fn(), + '&nbsp;&nbsp;Bender should not be allowed on tv.&nbsp;&nbsp;' + ); }); }); describe('chop', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{chop}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should remove non-word characters from start of string', function() { var fn = hbs.compile('{{chop "- foo bar baz"}}'); - assert.equal(fn(), 'foo bar baz'); + equal(fn(), 'foo bar baz'); }); it('should remove non-word characters from end of string', function() { var fn = hbs.compile('{{chop "foo bar baz _- "}}'); - assert.equal(fn(), 'foo bar baz'); + equal(fn(), 'foo bar baz'); }); }); describe('dashcase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{dashcase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in dashcase', function() { var fn = hbs.compile('{{dashcase "foo bar baz qux"}}'); - assert.equal(fn(), 'foo-bar-baz-qux'); + equal(fn(), 'foo-bar-baz-qux'); }); it('should lowercase a single character', function() { - assert.equal(hbs.compile('{{dashcase "f"}}')(), 'f'); - assert.equal(hbs.compile('{{dashcase "A"}}')(), 'a'); + equal(hbs.compile('{{dashcase "f"}}')(), 'f'); + equal(hbs.compile('{{dashcase "A"}}')(), 'a'); }); }); describe('dotcase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{dotcase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in dotcase', function() { var fn = hbs.compile('{{dotcase "foo bar baz qux"}}'); - assert.equal(fn(), 'foo.bar.baz.qux'); + equal(fn(), 'foo.bar.baz.qux'); }); it('should lowercase a single character', function() { - assert.equal(hbs.compile('{{dotcase "f"}}')(), 'f'); - assert.equal(hbs.compile('{{dotcase "A"}}')(), 'a'); + equal(hbs.compile('{{dotcase "f"}}')(), 'f'); + equal(hbs.compile('{{dotcase "A"}}')(), 'a'); }); }); describe('ellipsis', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{ellipsis}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return then string truncated by a specified length.', function() { - var fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 31}}'); - assert.equal(fn(), 'Bender should not be allowed on…'); + var fn = hbs.compile( + '{{ellipsis "Bender should not be allowed on tv." 31}}' + ); + equal(fn(), 'Bender should not be allowed on…'); }); it('should return the string if shorter than the specified length.', function() { - var fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 100}}'); - assert.equal(fn(), 'Bender should not be allowed on tv.'); + var fn = hbs.compile( + '{{ellipsis "Bender should not be allowed on tv." 100}}' + ); + equal(fn(), 'Bender should not be allowed on tv.'); }); }); describe('hyphenate', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{hyphenate}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string with spaces replaced with hyphens.', function() { - var fn = hbs.compile('{{hyphenate "Bender should not be allowed on tv."}}'); - assert.equal(fn(), 'Bender-should-not-be-allowed-on-tv.'); + var fn = hbs.compile( + '{{hyphenate "Bender should not be allowed on tv."}}' + ); + equal(fn(), 'Bender-should-not-be-allowed-on-tv.'); }); }); describe('isString', function() { it('should return true for string', function() { - assert.equal(hbs.compile('{{isString "foo"}}')(), 'true'); + equal(hbs.compile('{{isString "foo"}}')(), 'true'); }); it('should return true for empty string', function() { - assert.equal(hbs.compile('{{isString ""}}')(), 'true'); + equal(hbs.compile('{{isString ""}}')(), 'true'); }); it('should return false for number', function() { - assert.equal(hbs.compile('{{isString 123}}')(), 'false'); + equal(hbs.compile('{{isString 123}}')(), 'false'); }); it('should return false for null', function() { - assert.equal(hbs.compile('{{isString null}}')(), 'false'); + equal(hbs.compile('{{isString null}}')(), 'false'); }); it('should return false when undefined', function() { - assert.equal(hbs.compile('{{isString}}')(), 'false'); + equal(hbs.compile('{{isString}}')(), 'false'); }); }); describe('lowercase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{lowercase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in lowercase', function() { - var fn = hbs.compile('{{lowercase "BENDER SHOULD NOT BE ALLOWED ON TV"}}'); - assert.equal(fn(), 'bender should not be allowed on tv'); + var fn = hbs.compile( + '{{lowercase "BENDER SHOULD NOT BE ALLOWED ON TV"}}' + ); + equal(fn(), 'bender should not be allowed on tv'); }); }); describe('occurrences', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{occurrences}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the number of occurrences of a string, within a string.', function() { var fn = hbs.compile('{{occurrences "Jar-Jar Binks" "Jar"}}'); - assert.equal(fn(), '2'); + equal(fn(), '2'); }); }); describe('pascalcase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{pascalcase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in pascalcase', function() { var fn = hbs.compile('{{pascalcase "foo bar baz qux"}}'); - assert.equal(fn(), 'FooBarBazQux'); + equal(fn(), 'FooBarBazQux'); }); it('should uppercase a single character', function() { - assert.equal(hbs.compile('{{pascalcase "f"}}')(), 'F'); - assert.equal(hbs.compile('{{pascalcase "A"}}')(), 'A'); + equal(hbs.compile('{{pascalcase "f"}}')(), 'F'); + equal(hbs.compile('{{pascalcase "A"}}')(), 'A'); }); }); describe('pathcase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{pathcase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in pathcase', function() { var fn = hbs.compile('{{pathcase "foo bar baz qux"}}'); - assert.equal(fn(), 'foo/bar/baz/qux'); + equal(fn(), 'foo/bar/baz/qux'); }); it('should lowercase a single character', function() { - assert.equal(hbs.compile('{{pathcase "f"}}')(), 'f'); - assert.equal(hbs.compile('{{pathcase "A"}}')(), 'a'); + equal(hbs.compile('{{pathcase "f"}}')(), 'f'); + equal(hbs.compile('{{pathcase "A"}}')(), 'a'); }); }); describe('plusify', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{plusify}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the empty string with no change.', function() { var fn = hbs.compile('{{plusify ""}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string with no change.', function() { var fn = hbs.compile('{{plusify "BenderShouldNotBeAllowedOnTv."}}'); - assert.equal(fn(), 'BenderShouldNotBeAllowedOnTv.'); + equal(fn(), 'BenderShouldNotBeAllowedOnTv.'); }); it('should return the string with spaces replaced with pluses.', function() { var fn = hbs.compile('{{plusify "Bender should not be allowed on tv."}}'); - assert.equal(fn(), 'Bender+should+not+be+allowed+on+tv.'); + equal(fn(), 'Bender+should+not+be+allowed+on+tv.'); }); }); describe('replace', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{replace}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should replace occurrences of string "A" with string "B"', function() { var fn = hbs.compile('{{replace "Bender Bending Rodriguez" "B" "M"}}'); - assert.equal(fn(), 'Mender Mending Rodriguez'); + equal(fn(), 'Mender Mending Rodriguez'); }); it('should return the string if `a` is undefined', function() { var fn = hbs.compile('{{replace "a b c"}}'); - assert.equal(fn(), 'a b c'); + equal(fn(), 'a b c'); }); it('should replace the string with `""` if `b` is undefined', function() { var fn = hbs.compile('{{replace "a b c" "a"}}'); - assert.equal(fn(), ' b c'); + equal(fn(), ' b c'); }); }); describe('reverse', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{reverse}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in reverse.', function() { var fn = hbs.compile('{{reverse "bender should NOT be allowed on TV."}}'); - assert.equal(fn(), '.VT no dewolla eb TON dluohs redneb'); + equal(fn(), '.VT no dewolla eb TON dluohs redneb'); }); }); describe('sentence', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{sentence}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should capitalize the first word of each sentence in a string and convert the rest of the sentence to lowercase.', function() { - var fn = hbs.compile('{{sentence "bender should NOT be allowed on TV. fry SHOULD be allowed on TV."}}'); - assert.equal(fn(), 'Bender should not be allowed on tv. Fry should be allowed on tv.'); + var fn = hbs.compile( + '{{sentence "bender should NOT be allowed on TV. fry SHOULD be allowed on TV."}}' + ); + equal( + fn(), + 'Bender should not be allowed on tv. Fry should be allowed on tv.' + ); }); }); describe('snakecase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{snakecase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should lowercase a single character', function() { - assert.equal(hbs.compile('{{snakecase "a"}}')(), 'a'); - assert.equal(hbs.compile('{{snakecase "A"}}')(), 'a'); + equal(hbs.compile('{{snakecase "a"}}')(), 'a'); + equal(hbs.compile('{{snakecase "A"}}')(), 'a'); }); it('should return the string in snakecase', function() { var fn = hbs.compile('{{snakecase "foo bar baz qux"}}'); - assert.equal(fn(), 'foo_bar_baz_qux'); + equal(fn(), 'foo_bar_baz_qux'); }); }); describe('split', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{split}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should split the string with the default character', function() { var fn = hbs.compile('{{#each (split "a,b,c")}}<{{.}}>{{/each}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should split the string on the given character', function() { var fn = hbs.compile('{{#each (split "a|b|c" "|")}}<{{.}}>{{/each}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); }); describe('startsWith', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{startsWith}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should render "Yes he is", from inside the block.', function() { - var fn = hbs.compile('{{#startsWith "Bender" "Bender is great"}}Yes he is{{/startsWith}}'); - assert.equal(fn(), 'Yes he is'); + var fn = hbs.compile( + '{{#startsWith "Bender" "Bender is great"}}Yes he is{{/startsWith}}' + ); + equal(fn(), 'Yes he is'); }); it('should render the Inverse block.', function() { - var fn = hbs.compile('{{#startsWith "Goodbye" "Hello, world!"}}Whoops{{else}}Bro, do you even hello world?{{/startsWith}}'); - assert.equal(fn(), 'Bro, do you even hello world?'); + var fn = hbs.compile( + '{{#startsWith "Goodbye" "Hello, world!"}}Whoops{{else}}Bro, do you even hello world?{{/startsWith}}' + ); + equal(fn(), 'Bro, do you even hello world?'); }); it('should render the Inverse block when an undefined value is passed in..', function() { - var fn = hbs.compile('{{#startsWith "myPrefix" undefined}}fn block{{else}}inverse block{{/startsWith}}'); - assert.equal(fn(), 'inverse block'); + var fn = hbs.compile( + '{{#startsWith "myPrefix" undefined}}fn block{{else}}inverse block{{/startsWith}}' + ); + equal(fn(), 'inverse block'); }); }); describe('titleize', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{titleize}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in title case.', function() { var fn = hbs.compile('{{titleize "Bender-should-Not-be-allowed_on_Tv"}}'); - assert.equal(fn(), 'Bender Should Not Be Allowed On Tv'); + equal(fn(), 'Bender Should Not Be Allowed On Tv'); }); }); describe('trim', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{trim}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should trim leading whitespace', function() { var fn = hbs.compile('{{trim " foo"}}'); - assert.equal(fn(), 'foo'); + equal(fn(), 'foo'); }); it('should trim trailing whitespace', function() { var fn = hbs.compile('{{trim "foo "}}'); - assert.equal(fn(), 'foo'); + equal(fn(), 'foo'); }); }); describe('truncate', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{truncate}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string truncated by a specified length.', function() { - var fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 31}}'); - assert.equal(fn(), 'Bender should not be allowed on'); + var fn = hbs.compile( + '{{truncate "Bender should not be allowed on tv." 31}}' + ); + equal(fn(), 'Bender should not be allowed on'); }); it('should return the string if shorter than the specified length.', function() { - var fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 100}}'); - assert.equal(fn(), 'Bender should not be allowed on tv.'); + var fn = hbs.compile( + '{{truncate "Bender should not be allowed on tv." 100}}' + ); + equal(fn(), 'Bender should not be allowed on tv.'); }); it('should return then string truncated by a specified length', function() { var fn = hbs.compile('{{truncate "foo bar baz qux" 7}}...'); - assert.equal(fn(), 'foo bar...'); + equal(fn(), 'foo bar...'); }); it('should return then string truncated by a specified length, providing a custom string to denote an omission.', function() { var fn = hbs.compile('{{truncate "foo bar baz qux" 7 "…"}}'); - assert.equal(fn(), 'foo ba…'); + equal(fn(), 'foo ba…'); }); }); describe('truncateWords', function() { it('should return then string truncated when the specified length is shorter than the word count', function() { var fn = hbs.compile('{{truncateWords "foo bar baz" 2}}'); - assert.equal(fn(), 'foo bar…'); + equal(fn(), 'foo bar…'); }); it('should be able to truncate a single word', function() { var fn = hbs.compile('{{truncateWords "foo bar baz" 1}}'); - assert.equal(fn(), 'foo…'); + equal(fn(), 'foo…'); }); it('should return the original string when the specified length matches the word count', function() { var fn = hbs.compile('{{truncateWords "foo bar baz" 3}}'); - assert.equal(fn(), 'foo bar baz'); + equal(fn(), 'foo bar baz'); }); it('should return the original string when the specified length is bigger than the word count', function() { var fn = hbs.compile('{{truncateWords "foo bar baz" 4}}'); - assert.equal(fn(), 'foo bar baz'); + equal(fn(), 'foo bar baz'); }); }); describe('uppercase', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{uppercase}}'); - assert.equal(fn(), ''); + equal(fn(), ''); }); it('should return the string in uppercase', function() { - var fn = hbs.compile('{{uppercase "bender should not be allowed on tv"}}'); - assert.equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV'); + var fn = hbs.compile( + '{{uppercase "bender should not be allowed on tv"}}' + ); + equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV'); }); it('should work as a block helper', function() { - var fn = hbs.compile('{{#uppercase}}bender should not be allowed on tv{{/uppercase}}'); - assert.equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV'); + var fn = hbs.compile( + '{{#uppercase}}bender should not be allowed on tv{{/uppercase}}' + ); + equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV'); }); }); @@ -402,45 +436,44 @@ describe('string', function() { // Bad parameters it('Should return "Lorem ipsum" only, if passed no parameters', function() { var fn = hbs.compile('{{ lorem }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return "Lorem ipsum" only, if passed a non-number (string)', function() { var fn = hbs.compile('{{ lorem a }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return "Lorem ipsum" only, if passed a non-number (array)', function() { var fn = hbs.compile('{{ lorem [1,2,3] }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return "Lorem ipsum" only, if passed a number less than 1', function() { var fn = hbs.compile('{{ lorem -1 }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return "Lorem ipsum" only, if passed a number less than 1', function() { var fn = hbs.compile('{{ lorem 0 }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return "Lorem ipsum" only, if passed a number less than 1', function() { var fn = hbs.compile('{{ lorem -999 }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); // Good parameters it('Should return a string of "Lorem ipsum" if passed 11', function() { var fn = hbs.compile('{{ lorem 11 }}'); - assert.equal(fn(), 'Lorem ipsum'); + equal(fn(), 'Lorem ipsum'); }); it('Should return a string of "Lorem" if passed 5', function() { var fn = hbs.compile('{{ lorem 5 }}'); - assert.equal(fn(), 'Lorem'); + equal(fn(), 'Lorem'); }); it('Should return a string of "Lorem ipsum dolor sit amet, consectetur adipiscing" if passed 50', function() { var fn = hbs.compile('{{ lorem 50 }}'); - assert.equal(fn(), 'Lorem ipsum dolor sit amet, consectetur adipiscing'); + equal(fn(), 'Lorem ipsum dolor sit amet, consectetur adipiscing'); }); it('Should return a string of length 8032 if passed 8032', function() { var fn = hbs.compile('{{ lorem 8032 }}'); - assert.equal(fn().length, 8032); + equal(fn().length, 8032); }); }); }); - diff --git a/test/subexpressions.js b/test/subexpressions.js index 1129691f..0ddf2963 100644 --- a/test/subexpressions.js +++ b/test/subexpressions.js @@ -1,11 +1,11 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.array({handlebars: hbs}); -helpers.string({handlebars: hbs}); +import { array, string } from '..'; +array({ handlebars: hbs }); +string({ handlebars: hbs }); var context = { one: ['A', 'B', 'C', 'D', 'E', 'F'], @@ -17,11 +17,11 @@ describe('subexpressions', function() { describe('strings', function() { it('Should return the first item in a collection, all lowercase.', function() { var fn = hbs.compile('{{lowercase (first one)}}'); - assert.equal(fn(context), 'a'); + equal(fn(context), 'a'); }); it('Should return the last item in a collection, all uppercase.', function() { var fn = hbs.compile('{{uppercase (last two)}}'); - assert.equal(fn(context), 'F'); + equal(fn(context), 'F'); }); }); }); diff --git a/test/support/index.js b/test/support/index.js index 6d83709d..eff71f15 100644 --- a/test/support/index.js +++ b/test/support/index.js @@ -1,13 +1,11 @@ 'use strict'; -var fs = require('fs'); +import { readFileSync } from 'fs'; /** * Expose `utils` */ -var utils = module.exports; - /** * Read a file at the given `filepath` * @@ -15,12 +13,12 @@ var utils = module.exports; * @return {String} */ -utils.read = function(fp) { - return fs.readFileSync(fp, 'utf8'); -}; +export function read(fp) { + return readFileSync(fp, 'utf8'); +} /** - * Returns a function for reading a test fixture + * Returns a function for reading a test fixture * of the given `type` at the given `filepath`. * * @param {String} `type` @@ -28,11 +26,11 @@ utils.read = function(fp) { * @return {String} */ -utils.fixture = function(type) { +export function fixture(type) { return function(fp) { - return utils.read('test/fixtures/' + type + '/' + fp); + return read('test/fixtures/' + type + '/' + fp); }; -}; +} /** * Returns a function for reading a file @@ -43,8 +41,8 @@ utils.fixture = function(type) { * @return {String} */ -utils.expected = function(type) { +export function expected(type) { return function(fp) { - return utils.read('test/expected/' + type + '/' + fp); + return read('test/expected/' + type + '/' + fp); }; -}; +} diff --git a/test/url.js b/test/url.js index 460caadc..0a2e0a08 100644 --- a/test/url.js +++ b/test/url.js @@ -1,52 +1,66 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); +import 'mocha'; +import { equal, deepEqual } from 'assert'; var hbs = require('handlebars').create(); -var helpers = require('..'); -helpers.object({handlebars: hbs}); -helpers.url({handlebars: hbs}); +import { object, url } from '..'; +object({ handlebars: hbs }); +url({ handlebars: hbs }); describe('url', function() { describe('urlResolve', function() { it('should resolve a URL', function() { var fn1 = hbs.compile('{{urlResolve "/one/two/three" "four"}}'); - assert.equal(fn1(), '/one/two/four'); + equal(fn1(), '/one/two/four'); var fn2 = hbs.compile('{{urlResolve "http://example.com/" "/one"}}'); - assert.equal(fn2(), 'http://example.com/one'); + equal(fn2(), 'http://example.com/one'); var fn3 = hbs.compile('{{urlResolve "http://example.com/one" "/two"}}'); - assert.equal(fn3(), 'http://example.com/two'); + equal(fn3(), 'http://example.com/two'); }); }); describe('stripQuerystring', function() { it('should return a url without its query string.', function() { - var fn = hbs.compile('{{stripQuerystring "http://example.com?tests=true"}}'); - assert.equal(fn(), 'http://example.com'); + var fn = hbs.compile( + '{{stripQuerystring "http://example.com?tests=true"}}' + ); + equal(fn(), 'http://example.com'); }); }); describe('encodeURI', function() { it('should return an encoded uri string.', function() { - var fn = hbs.compile('{{encodeURI "http://example.com?comment=Thyme &time=again"}}'); - assert.equal(fn(), 'http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain'); + var fn = hbs.compile( + '{{encodeURI "http://example.com?comment=Thyme &time=again"}}' + ); + equal( + fn(), + 'http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain' + ); }); }); describe('decodeURI', function() { it('should return an decoded uri string.', function() { - var fn = hbs.compile('{{{decodeURI "http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain"}}}'); - assert.equal(fn(), 'http://example.com?comment=Thyme &time=again'); + var fn = hbs.compile( + '{{{decodeURI "http%3A%2F%2Fexample.com%3Fcomment%3DThyme%20%26time%3Dagain"}}}' + ); + equal(fn(), 'http://example.com?comment=Thyme &time=again'); }); }); describe('urlParse', function() { it('should take a string, and return an object stringified to JSON.', function() { - var fn = hbs.compile('{{{JSONstringify (urlParse "http://foo.com/bar/baz?key=value" "json")}}}'); + var fn = hbs.compile( + '{{{JSONstringify (urlParse "http://foo.com/bar/baz?key=value" "json")}}}' + ); - assert.deepEqual(fn(), '{"protocol":"http:","slashes":true,"auth":null,"host":"foo.com","port":null,"hostname":"foo.com","hash":null,"search":"?key=value","query":"key=value","pathname":"/bar/baz","path":"/bar/baz?key=value","href":"http://foo.com/bar/baz?key=value"}'); + deepEqual( + fn(), + '{"protocol":"http:","slashes":true,"auth":null,"host":"foo.com","port":null,"hostname":"foo.com","hash":null,"search":"?key=value","query":"key=value","pathname":"/bar/baz","path":"/bar/baz?key=value","href":"http://foo.com/bar/baz?key=value"}' + ); }); }); @@ -54,27 +68,27 @@ describe('url', function() { it('should take an http url and return without the protocol', function() { var data = { testUrl: 'http://foo.bar' }; var fn = hbs.compile('{{stripProtocol testUrl}}'); - assert.equal(fn(data), '//foo.bar/'); + equal(fn(data), '//foo.bar/'); }); it('strip https protocol', function() { var data = { testUrl: 'https://foo.bar' }; var fn = hbs.compile('{{stripProtocol testUrl}}'); - assert.equal(fn(data), '//foo.bar/'); + equal(fn(data), '//foo.bar/'); }); it('should leave a relative url unchanged', function() { var expected = 'path/to/file'; var data = { testUrl: expected }; var fn = hbs.compile('{{stripProtocol testUrl}}'); - assert.equal(fn(data), expected); + equal(fn(data), expected); }); it('should leave an absolute url unchanged', function() { var expected = '/path/to/file'; var data = { testUrl: expected }; var fn = hbs.compile('{{stripProtocol testUrl}}'); - assert.equal(fn(data), expected); + equal(fn(data), expected); }); }); }); diff --git a/test/utils.js b/test/utils.js index 0129d31f..31e1461b 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,59 +1,64 @@ 'use strict'; -require('mocha'); -var assert = require('assert'); -var utils = require('../lib/utils'); -var HTML = require('../lib/utils/html'); +import 'mocha'; +import { equal } from 'assert'; +import { chop, changecase } from '../lib/utils'; +import { + condense, + padcomments, + parseAttributes, + toAttributes +} from '../lib/utils/html'; describe('utils', function() { describe('chop', function() { it('should return an empty string if undefined', function() { - assert.equal(utils.chop(), ''); + equal(chop(), ''); }); it('should remove non-word characters from start of string', function() { - assert.equal(utils.chop('- foo bar baz'), 'foo bar baz'); + equal(chop('- foo bar baz'), 'foo bar baz'); }); it('should remove non-word characters from end of string', function() { - assert.equal(utils.chop('foo bar baz _- '), 'foo bar baz'); + equal(chop('foo bar baz _- '), 'foo bar baz'); }); }); describe('changecase', function() { it('should return an empty string if undefined', function() { - assert.equal(utils.changecase(), ''); + equal(changecase(), ''); }); it('should lowercase a mixed case string', function() { - assert.equal(utils.changecase('fooBarBazQux'), 'foobarbazqux'); + equal(changecase('fooBarBazQux'), 'foobarbazqux'); }); it('should lowercase a single character', function() { - assert.equal(utils.changecase('f'), 'f'); - assert.equal(utils.changecase('A'), 'a'); + equal(changecase('f'), 'f'); + equal(changecase('A'), 'a'); }); }); describe('html', function() { describe('condense', function() { it('should condense multiple newlines into a single newline', function() { - assert.equal(HTML.condense('foo\r\n \r\n bar\n'), 'foo\n\nbar\n'); + equal(condense('foo\r\n \r\n bar\n'), 'foo\n\nbar\n'); }); }); describe('padcomments', function() { it('should add newlines around comments', function() { - assert.equal(HTML.padcomments(''), '\n'); + equal(padcomments(''), '\n'); }); }); describe('parseAttributes', function() { it('should parse attributes', function() { - assert.equal(HTML.parseAttributes({a: 'b', c: 200 }), 'a="b" c="200"'); + equal(parseAttributes({ a: 'b', c: 200 }), 'a="b" c="200"'); }); }); describe('toAttributes', function() { it('should convert an object hash into html attributes', function() { - var hash = {disabled: true, display: 'hidden', class: 'fade'}; - assert.equal(HTML.toAttributes(hash), ' disabled display="hidden" class="fade"'); + var hash = { disabled: true, display: 'hidden', class: 'fade' }; + equal(toAttributes(hash), ' disabled display="hidden" class="fade"'); }); }); }); diff --git a/test/uuid.js b/test/uuid.js index 5d845780..6a7b0885 100644 --- a/test/uuid.js +++ b/test/uuid.js @@ -1,13 +1,16 @@ 'use strict'; -require('mocha'); -const assert = require('assert'); -const uuid = require('../lib/uuid'); +import 'mocha'; +import { match } from 'assert'; +import { uuid as _uuid } from '../lib/uuid'; describe('uuid', function() { describe('generate', function() { it('should generate a valid uuid', function() { - assert.match(uuid.uuid(), /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i); + match( + _uuid(), + /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i + ); }); }); });