This commit is contained in:
harttle
2017-04-24 21:31:30 +08:00
parent 82f84d96c1
commit 2632e3e97e
50 changed files with 3941 additions and 3903 deletions
+344 -344
View File
@@ -1,386 +1,386 @@
const chai = require("chai");
const expect = chai.expect;
const mock = require('mock-fs');
chai.use(require("chai-as-promised"));
const chai = require('chai')
const expect = chai.expect
const mock = require('mock-fs')
chai.use(require('chai-as-promised'))
var engine = require('../..')();
var engine = require('../..')()
var strictEngine = require('../..')({
strict_variables: true,
strict_filters: true
});
strict_variables: true,
strict_filters: true
})
describe('error', function() {
afterEach(function() {
mock.restore();
});
describe('error', function () {
afterEach(function () {
mock.restore()
})
describe('TokenizationError', function() {
it('should throw TokenizationError when tag illegal', function() {
return expect(engine.parseAndRender('{% . a %}', {})).to.eventually
describe('TokenizationError', function () {
it('should throw TokenizationError when tag illegal', function () {
return expect(engine.parseAndRender('{% . a %}', {})).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.contain('illegal tag syntax');
});
});
it('should contain template content in err.message', function() {
var html = ['1st', '2nd', 'X{% . a %} Y', '4th'];
var message = [
' 1| 1st',
' 2| 2nd',
'>> 3| X{% . a %} Y',
' 4| 4th',
'TokenizationError: illegal tag syntax',
];
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.then(function (err) {
expect(err.name).to.equal('TokenizationError')
expect(err.message).to.contain('illegal tag syntax')
})
})
it('should contain template content in err.message', function () {
var html = ['1st', '2nd', 'X{% . a %} Y', '4th']
var message = [
' 1| 1st',
' 2| 2nd',
'>> 3| X{% . a %} Y',
' 4| 4th',
'TokenizationError: illegal tag syntax'
]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected
.then(function(err) {
expect(err.message).to.equal('illegal tag syntax, line:3');
expect(err.stack).to.contain(message.join('\n'));
expect(err.name).to.equal('TokenizationError');
});
});
it('should contain the whole template content in err.input', function() {
var html = 'bar\nfoo{% . a %}\nfoo';
return expect(engine.parseAndRender(html)).to.eventually
.then(function (err) {
expect(err.message).to.equal('illegal tag syntax, line:3')
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('TokenizationError')
})
})
it('should contain the whole template content in err.input', function () {
var html = 'bar\nfoo{% . a %}\nfoo'
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
expect(err.input).to.equal(html);
});
});
it('should contain line number in err.line', function() {
return expect(engine.parseAndRender('1\n2\n{% . a %}\n4', {})).to.eventually
.then(function (err) {
expect(err.input).to.equal(html)
})
})
it('should contain line number in err.line', function () {
return expect(engine.parseAndRender('1\n2\n{% . a %}\n4', {})).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('TokenizationError');
expect(err.line).to.equal(3);
});
});
it('should contain stack in err.stack', function() {
return expect(engine.parseAndRender('{% . a %}')).to.eventually
.then(function (err) {
expect(err.name).to.equal('TokenizationError')
expect(err.line).to.equal(3)
})
})
it('should contain stack in err.stack', function () {
return expect(engine.parseAndRender('{% . a %}')).to.eventually
.be.rejected
.then(function(err) {
expect(err.stack).to.contain('illegal tag syntax');
expect(err.stack).to.contain('at Object.parse');
});
});
it('should contain file path in err.file', function() {
var html = '<html>\n<head>\n\n{% . a %}\n\n';
mock({
"/foo.html": html
});
return expect(engine.renderFile('/foo.html')).to.eventually
.then(function (err) {
expect(err.stack).to.contain('illegal tag syntax')
expect(err.stack).to.contain('at Object.parse')
})
})
it('should contain file path in err.file', function () {
var html = '<html>\n<head>\n\n{% . a %}\n\n'
mock({
'/foo.html': html
})
return expect(engine.renderFile('/foo.html')).to.eventually
.be.rejected
.then(function(err) {
mock.restore();
expect(err.name).to.equal('TokenizationError');
expect(err.file).to.equal('/foo.html');
});
});
});
.then(function (err) {
mock.restore()
expect(err.name).to.equal('TokenizationError')
expect(err.file).to.equal('/foo.html')
})
})
})
describe('RenderError', function() {
beforeEach(function() {
engine = require('../..')({
root: '/'
});
engine.registerTag('throwingTag', {
render: function() {
throw new Error('intended render error');
}
});
engine.registerTag('rejectingTag', {
render: function() {
return Promise.reject(new Error('intended render reject'));
}
});
engine.registerFilter('throwingFilter', () => {
throw new Error('throwed by filter');
});
});
it('should throw RenderError when tag throws', function() {
var src = '{%throwingTag%}';
return expect(engine.parseAndRender(src)).to.eventually
describe('RenderError', function () {
beforeEach(function () {
engine = require('../..')({
root: '/'
})
engine.registerTag('throwingTag', {
render: function () {
throw new Error('intended render error')
}
})
engine.registerTag('rejectingTag', {
render: function () {
return Promise.reject(new Error('intended render reject'))
}
})
engine.registerFilter('throwingFilter', () => {
throw new Error('throwed by filter')
})
})
it('should throw RenderError when tag throws', function () {
var src = '{%throwingTag%}'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('RenderError');
expect(err.message).to.contain('intended render error');
});
});
it('should throw RenderError when tag rejects', function() {
var src = '{%rejectingTag%}';
return expect(engine.parseAndRender(src)).to.eventually
.then(function (err) {
expect(err.name).to.equal('RenderError')
expect(err.message).to.contain('intended render error')
})
})
it('should throw RenderError when tag rejects', function () {
var src = '{%rejectingTag%}'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('RenderError');
expect(err.message).to.contain('intended render reject');
});
});
it('should throw RenderError when filter throws', function() {
var src = '{{1|throwingFilter}}';
return expect(engine.parseAndRender(src)).to.eventually
.then(function (err) {
expect(err.name).to.equal('RenderError')
expect(err.message).to.contain('intended render reject')
})
})
it('should throw RenderError when filter throws', function () {
var src = '{{1|throwingFilter}}'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('RenderError');
expect(err.message).to.contain('throwed by filter');
});
});
it('should not throw when variable undefined by default', function() {
return expect(engine.parseAndRender('X{{a}}Y')).to.eventually.equal('XY');
});
it('should throw RenderError when variable not defined', function() {
return expect(strictEngine.parseAndRender('{{a}}')).to.eventually
.then(function (err) {
expect(err.name).to.equal('RenderError')
expect(err.message).to.contain('throwed by filter')
})
})
it('should not throw when variable undefined by default', function () {
return expect(engine.parseAndRender('X{{a}}Y')).to.eventually.equal('XY')
})
it('should throw RenderError when variable not defined', function () {
return expect(strictEngine.parseAndRender('{{a}}')).to.eventually
.be.rejected
.then(function(e) {
expect(e).to.have.property('name', 'RenderError');
expect(e.message).to.contain('undefined variable: a');
});
});
it('should contain template context in err.stack', function() {
var html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th'];
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'Error: intended render error',
];
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.then(function (e) {
expect(e).to.have.property('name', 'RenderError')
expect(e.message).to.contain('undefined variable: a')
})
})
it('should contain template context in err.stack', function () {
var html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'Error: intended render error'
]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected
.then(function(err) {
expect(err.message).to.equal('intended render error, line:4');
expect(err.stack).to.contain(message.join('\n'));
expect(err.name).to.equal('RenderError');
});
});
it('should contain original error info for {% layout %}', function() {
mock({
'/throwing-tag.html': [
'1st',
'2nd',
'3rd',
'X{%throwingTag%} Y',
'5th',
'{%block%}{%endblock%}',
'7th'
].join('\n')
});
var html = '{%layout "throwing-tag.html"%}';
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| {%block%}{%endblock%}',
' 7| 7th',
'Error: intended render error',
];
return expect(engine.parseAndRender(html)).to.eventually
.then(function (err) {
expect(err.message).to.equal('intended render error, line:4')
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
})
it('should contain original error info for {% layout %}', function () {
mock({
'/throwing-tag.html': [
'1st',
'2nd',
'3rd',
'X{%throwingTag%} Y',
'5th',
'{%block%}{%endblock%}',
'7th'
].join('\n')
})
var html = '{%layout "throwing-tag.html"%}'
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| {%block%}{%endblock%}',
' 7| 7th',
'Error: intended render error'
]
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
console.log(err.message);
console.log(err.stack);
expect(err.message).to.equal('intended render error, file:/throwing-tag.html, line:4');
expect(err.stack).to.contain(message.join('\n'));
expect(err.name).to.equal('RenderError');
});
});
it('should contain original error info for {% include %}', function() {
var origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th'];
mock({
'/throwing-tag.html': origin.join('\n')
});
var html = '{%include "throwing-tag.html"%}';
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'Error: intended render error',
];
return expect(engine.parseAndRender(html)).to.eventually
.then(function (err) {
console.log(err.message)
console.log(err.stack)
expect(err.message).to.equal('intended render error, file:/throwing-tag.html, line:4')
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
})
it('should contain original error info for {% include %}', function () {
var origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
mock({
'/throwing-tag.html': origin.join('\n')
})
var html = '{%include "throwing-tag.html"%}'
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'Error: intended render error'
]
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
expect(err.message).to.equal('intended render error, file:/throwing-tag.html, line:4');
expect(err.stack).to.contain(message.join('\n'));
expect(err.name).to.equal('RenderError');
});
});
it('should contain the whole template content in err.input', function() {
var html = 'bar\nfoo{%throwingTag%}\nfoo';
return expect(engine.parseAndRender(html)).to.eventually
.then(function (err) {
expect(err.message).to.equal('intended render error, file:/throwing-tag.html, line:4')
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
})
it('should contain the whole template content in err.input', function () {
var html = 'bar\nfoo{%throwingTag%}\nfoo'
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
expect(err.input).to.equal(html);
expect(err.name).to.equal('RenderError');
});
});
it('should contain line number in err.line', function() {
var src = '1\n2\n{{1|throwingFilter}}\n4';
return expect(engine.parseAndRender(src)).to.eventually
.then(function (err) {
expect(err.input).to.equal(html)
expect(err.name).to.equal('RenderError')
})
})
it('should contain line number in err.line', function () {
var src = '1\n2\n{{1|throwingFilter}}\n4'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.line).to.equal(3);
expect(err.name).to.equal('RenderError');
});
});
it('should contain stack in err.stack', function() {
return expect(engine.parseAndRender('{%rejectingTag%}')).to.eventually
.then(function (err) {
expect(err.line).to.equal(3)
expect(err.name).to.equal('RenderError')
})
})
it('should contain stack in err.stack', function () {
return expect(engine.parseAndRender('{%rejectingTag%}')).to.eventually
.be.rejected
.then(function(err) {
expect(err.stack).to.contain('intended render reject');
expect(err.stack).to.match(/at .*:\d+:\d+\)/);
});
});
.then(function (err) {
expect(err.stack).to.contain('intended render reject')
expect(err.stack).to.match(/at .*:\d+:\d+\)/)
})
})
it('should contain file path in err.file', function() {
var html = '<html>\n<head>\n\n{% throwingTag %}\n\n';
mock({
"/foo.html": html
});
return expect(engine.renderFile('/foo.html')).to.eventually
it('should contain file path in err.file', function () {
var html = '<html>\n<head>\n\n{% throwingTag %}\n\n'
mock({
'/foo.html': html
})
return expect(engine.renderFile('/foo.html')).to.eventually
.be.rejected
.then(function(err) {
mock.restore();
expect(err.name).to.equal('RenderError');
expect(err.file).to.equal('/foo.html');
});
});
});
.then(function (err) {
mock.restore()
expect(err.name).to.equal('RenderError')
expect(err.file).to.equal('/foo.html')
})
})
})
describe('ParseError', function() {
beforeEach(function() {
engine = require('../..')();
engine.registerTag('throwsOnParse', {
parse: function() {
throw new Error('intended parse error');
}
});
});
it('should throw RenderError when filter not defined', function() {
return expect(strictEngine.parseAndRender('{{1 | a}}')).to.eventually
describe('ParseError', function () {
beforeEach(function () {
engine = require('../..')()
engine.registerTag('throwsOnParse', {
parse: function () {
throw new Error('intended parse error')
}
})
})
it('should throw RenderError when filter not defined', function () {
return expect(strictEngine.parseAndRender('{{1 | a}}')).to.eventually
.be.rejected
.then(function(e) {
expect(e).to.have.property('name', 'ParseError');
expect(e.message).to.contain('undefined filter: a');
});
});
it('should throw ParseError when tag not closed', function() {
return expect(engine.parseAndRender('{% if %}')).to.eventually
.then(function (e) {
expect(e).to.have.property('name', 'ParseError')
expect(e.message).to.contain('undefined filter: a')
})
})
it('should throw ParseError when tag not closed', function () {
return expect(engine.parseAndRender('{% if %}')).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.contain('tag {% if %} not closed');
});
});
it('should throw ParseError when tag parse throws', function() {
var src = '{%throwsOnParse%}';
return expect(engine.parseAndRender(src)).to.eventually
.then(function (err) {
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('tag {% if %} not closed')
})
})
it('should throw ParseError when tag parse throws', function () {
var src = '{%throwsOnParse%}'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.contain('intended parse error');
});
});
it('should throw ParseError when tag not found', function() {
var src = '{%if true%}\naaa{%endif%}\n{% -a %}\n3';
return expect(engine.parseAndRender(src)).to.eventually
.then(function (err) {
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('intended parse error')
})
})
it('should throw ParseError when tag not found', function () {
var src = '{%if true%}\naaa{%endif%}\n{% -a %}\n3'
return expect(engine.parseAndRender(src)).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.contain('tag -a not found');
});
});
.then(function (err) {
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('tag -a not found')
})
})
it('should throw ParseError when tag not exist', function() {
return expect(engine.parseAndRender('{% a %}')).to.eventually
it('should throw ParseError when tag not exist', function () {
return expect(engine.parseAndRender('{% a %}')).to.eventually
.be.rejected
.then(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.contain('tag a not found');
});
});
.then(function (err) {
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('tag a not found')
})
})
it('should contain template context in err.stack', function() {
var html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th'];
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{% a %} {% enda %} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'AssertionError: tag a not found',
];
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
it('should contain template context in err.stack', function () {
var html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th']
var message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{% a %} {% enda %} Y',
' 5| 5th',
' 6| 6th',
' 7| 7th',
'AssertionError: tag a not found'
]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected
.then(function(err) {
expect(err.message).to.equal('tag a not found, line:4');
expect(err.stack).to.contain(message.join('\n'));
expect(err.name).to.equal('ParseError');
});
});
.then(function (err) {
expect(err.message).to.equal('tag a not found, line:4')
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('ParseError')
})
})
it('should handle err.message when context not enough', function() {
var html = ['1st', 'X{% a %} {% enda %} Y', '3rd', '4th'];
var message = [
' 1| 1st',
'>> 2| X{% a %} {% enda %} Y',
' 3| 3rd',
' 4| 4th',
'AssertionError: tag a not found',
];
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
it('should handle err.message when context not enough', function () {
var html = ['1st', 'X{% a %} {% enda %} Y', '3rd', '4th']
var message = [
' 1| 1st',
'>> 2| X{% a %} {% enda %} Y',
' 3| 3rd',
' 4| 4th',
'AssertionError: tag a not found'
]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected
.then(function(err) {
expect(err.message).to.equal('tag a not found, line:2');
expect(err.stack).to.contain(message.join('\n'));
});
});
.then(function (err) {
expect(err.message).to.equal('tag a not found, line:2')
expect(err.stack).to.contain(message.join('\n'))
})
})
it('should contain the whole template content in err.input', function() {
var html = 'bar\nfoo{% a %}\nfoo';
return expect(engine.parseAndRender(html)).to.eventually
it('should contain the whole template content in err.input', function () {
var html = 'bar\nfoo{% a %}\nfoo'
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
expect(err.input).to.equal(html);
});
});
.then(function (err) {
expect(err.input).to.equal(html)
})
})
it('should contain line number in err.line', function() {
var html = '<html>\n<head>\n\n{% raw %}\n\n';
return expect(engine.parseAndRender(html)).to.eventually
it('should contain line number in err.line', function () {
var html = '<html>\n<head>\n\n{% raw %}\n\n'
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function(err) {
expect(err.line).to.equal(4);
});
});
.then(function (err) {
expect(err.line).to.equal(4)
})
})
it('should contain stack in err.stack', function() {
return expect(engine.parseAndRender('{% -a %}')).to.eventually
it('should contain stack in err.stack', function () {
return expect(engine.parseAndRender('{% -a %}')).to.eventually
.be.rejected
.then(function(err) {
expect(err.stack).to.contain('AssertionError: tag -a not found');
expect(err.stack).to.match(/at .*:\d+:\d+\)/);
});
});
.then(function (err) {
expect(err.stack).to.contain('AssertionError: tag -a not found')
expect(err.stack).to.match(/at .*:\d+:\d+\)/)
})
})
it('should contain file path in err.file', function() {
var html = '<html>\n<head>\n\n{% raw %}\n\n';
mock({
"/foo.html": html
});
return expect(engine.renderFile('/foo.html')).to.eventually
it('should contain file path in err.file', function () {
var html = '<html>\n<head>\n\n{% raw %}\n\n'
mock({
'/foo.html': html
})
return expect(engine.renderFile('/foo.html')).to.eventually
.be.rejected
.then(function(err) {
mock.restore();
expect(err.name).to.equal('ParseError');
expect(err.file).to.equal('/foo.html');
});
});
});
});
.then(function (err) {
mock.restore()
expect(err.name).to.equal('ParseError')
expect(err.file).to.equal('/foo.html')
})
})
})
})
+92 -90
View File
@@ -1,92 +1,94 @@
const chai = require("chai");
const sinon = require('sinon');
const expect = chai.expect;
chai.use(require("chai-as-promised"));
chai.use(require("sinon-chai"));
const chai = require('chai')
const sinon = require('sinon')
const expect = chai.expect
chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
var P = require('../../src/util/promise.js');
var P = require('../../src/util/promise.js')
describe('util/promise', function() {
describe('.anySeries()', function() {
it('should resolve in series', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();
return P
.anySeries(
['first', 'second'],
(item, idx) => new Promise(function(resolve, reject) {
if (idx === 0) {
setTimeout(function() {
spy1();
reject(new Error('first cb'));
}, 10);
} else {
spy2();
resolve('foo');
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1));
});
it('should reject when all rejected', function() {
var p = P.anySeries(['first', 'second', 'third'],
item => Promise.reject(new Error(item)));
return expect(p).to.be.rejectedWith("third");
});
it('should resolve the value that first callback resolved', () => {
var p = P.anySeries(['first', 'second'],
item => Promise.resolve(item));
return expect(p).to.eventually.equal('first');
});
it('should not call rest of callbacks once resolved', () => {
var spy = sinon.spy();
return P.anySeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
spy();
}
return Promise.resolve(item);
})
.then(() => expect(spy).to.not.have.been.called);
});
});
describe('.mapSeries()', function() {
it('should resolve when all resolved', function() {
var p = P.mapSeries(['first', 'second', 'third'],
item => Promise.resolve(item));
return expect(p).to.eventually.deep.equal(['first', 'second', "third"]);
});
it('should reject with the error that first callback rejected', () => {
var p = P.mapSeries(['first', 'second'],
item => Promise.reject(item));
return expect(p).to.rejectedWith('first');
});
it('should resolve in series', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();
return P
.mapSeries(
['first', 'second'],
(item, idx) => new Promise(function(resolve, reject) {
if (idx === 0) {
setTimeout(function() {
spy1();
resolve('first cb');
}, 10);
} else {
spy2();
resolve('foo');
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1));
});
it('should not call rest of callbacks once rejected', () => {
var spy = sinon.spy();
return P.mapSeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
spy();
}
return Promise.reject(new Error(item));
})
.catch(() => expect(spy).to.not.have.been.called);
});
});
});
describe('util/promise', function () {
describe('.anySeries()', function () {
it('should resolve in series', function () {
var spy1 = sinon.spy()
var spy2 = sinon.spy()
return P
.anySeries(
['first', 'second'],
(item, idx) => new Promise(function (resolve, reject) {
if (idx === 0) {
setTimeout(function () {
spy1()
reject(new Error('first cb'))
}, 10)
} else {
spy2()
resolve('foo')
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should reject when all rejected', function () {
var p = P.anySeries(['first', 'second', 'third'],
item => Promise.reject(new Error(item)))
return expect(p).to.be.rejectedWith('third')
})
it('should resolve the value that first callback resolved', () => {
var p = P.anySeries(['first', 'second'],
item => Promise.resolve(item))
return expect(p).to.eventually.equal('first')
})
it('should not call rest of callbacks once resolved', () => {
var spy = sinon.spy()
return P
.anySeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
spy()
}
return Promise.resolve(item)
})
.then(() => expect(spy).to.not.have.been.called)
})
})
describe('.mapSeries()', function () {
it('should resolve when all resolved', function () {
var p = P.mapSeries(['first', 'second', 'third'],
item => Promise.resolve(item))
return expect(p).to.eventually.deep.equal(['first', 'second', 'third'])
})
it('should reject with the error that first callback rejected', () => {
var p = P.mapSeries(['first', 'second'],
item => Promise.reject(item))
return expect(p).to.rejectedWith('first')
})
it('should resolve in series', function () {
var spy1 = sinon.spy()
var spy2 = sinon.spy()
return P
.mapSeries(
['first', 'second'],
(item, idx) => new Promise(function (resolve, reject) {
if (idx === 0) {
setTimeout(function () {
spy1()
resolve('first cb')
}, 10)
} else {
spy2()
resolve('foo')
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should not call rest of callbacks once rejected', () => {
var spy = sinon.spy()
return P
.mapSeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
spy()
}
return Promise.reject(new Error(item))
})
.catch(() => expect(spy).to.not.have.been.called)
})
})
})
+110 -110
View File
@@ -1,120 +1,120 @@
const chai = require("chai");
const expect = chai.expect;
const chai = require('chai')
const expect = chai.expect
var t = require('../../src/util/strftime.js');
var t = require('../../src/util/strftime.js')
describe('util/strftime', function() {
var now;
before(function() {
mockUTC();
now = new Date('2016-01-04T13:15:23');
then = new Date('2016-03-06T03:05:03');
});
after(function() {
restoreUTC();
});
describe('util/strftime', function () {
var now
var then
before(function () {
mockUTC()
now = new Date('2016-01-04T13:15:23')
then = new Date('2016-03-06T03:05:03')
})
after(function () {
restoreUTC()
})
it('should format UTC datetime', function() {
expect(t(now, '%Y-%m-%dT%H:%M:%S')).to.equal('2016-01-04T13:15:23');
});
it('should format %A as Monday', function() {
expect(t(now, '%A')).to.equal('Monday');
});
it('should format %B as month name', function() {
expect(t(now, '%B')).to.equal('January');
});
it('should format %C as century', function() {
expect(t(now, '%C')).to.equal('20');
});
it('should format %c as local string', function() {
expect(t(now, '%c')).to.equal(now.toLocaleString());
});
it('should format %e as space padded date', function() {
expect(t(now, '%e')).to.equal(' 4');
});
it('should format %I as 0 padded hour12', function() {
expect(t(now, '%I')).to.equal('01');
});
it('should format %j as day of year', function() {
expect(t(then, '%j')).to.equal('066');
});
it('should format %k as space padded hour', function() {
expect(t(then, '%k')).to.equal(' 3');
});
it('should format %l as space padded hour12', function() {
expect(t(now, '%l')).to.equal(' 1');
});
it('should format %L as 0 padded millisecond', function() {
expect(t(then, '%L')).to.equal('000');
});
it('should format %p as upper cased am/pm', function() {
expect(t(now, '%p')).to.equal('PM');
expect(t(then, '%p')).to.equal('AM');
});
it('should format %P as lower cased am/pm', function() {
expect(t(now, '%P')).to.equal('pm');
expect(t(then, '%P')).to.equal('am');
});
it('should format %q as date suffix', function(){
var st = new Date('2016-03-01T03:05:03');
var nd = new Date('2016-03-02T03:05:03');
var rd = new Date('2016-03-03T03:05:03');
expect(t(st, '%q')).to.equal('st');
expect(t(nd, '%q')).to.equal('nd');
expect(t(rd, '%q')).to.equal('rd');
expect(t(now, '%q')).to.equal('th');
});
it('should format %s as UNIX seconds', function(){
expect(t(now, '%s')).to.be.match(/\d+/);
});
it('should format %u as day of week(1-7)', function(){
expect(t(now, '%u')).to.be.equal('1');
expect(t(then, '%u')).to.be.equal('7');
});
it('should format %U as week of year, starts with 0', function() {
expect(t(now, '%U')).to.equal('01');
});
it('should format %w as day of month(0-7)', function(){
expect(t(now, '%w')).to.be.equal('1');
expect(t(then, '%w')).to.be.equal('0');
});
it('should format %W as week of year, starts with 1', function(){
expect(t(now, '%W')).to.be.equal('01');
});
it('should format %x as local date string', function() {
expect(t(now, '%x')).to.equal(now.toLocaleDateString());
});
it('should format %X as local time string', function() {
expect(t(now, '%X')).to.equal(now.toLocaleTimeString());
});
it('should format %y as 2-digit year', function() {
expect(t(now, '%y')).to.equal('16');
});
it('should format %z as time zone', function() {
expect(t(now, '%z')).to.equal('+0800');
});
it('should escape %% as %', function() {
expect(t(now, '%%')).to.equal('%');
});
});
it('should format UTC datetime', function () {
expect(t(now, '%Y-%m-%dT%H:%M:%S')).to.equal('2016-01-04T13:15:23')
})
it('should format %A as Monday', function () {
expect(t(now, '%A')).to.equal('Monday')
})
it('should format %B as month name', function () {
expect(t(now, '%B')).to.equal('January')
})
it('should format %C as century', function () {
expect(t(now, '%C')).to.equal('20')
})
it('should format %c as local string', function () {
expect(t(now, '%c')).to.equal(now.toLocaleString())
})
it('should format %e as space padded date', function () {
expect(t(now, '%e')).to.equal(' 4')
})
it('should format %I as 0 padded hour12', function () {
expect(t(now, '%I')).to.equal('01')
})
it('should format %j as day of year', function () {
expect(t(then, '%j')).to.equal('066')
})
it('should format %k as space padded hour', function () {
expect(t(then, '%k')).to.equal(' 3')
})
it('should format %l as space padded hour12', function () {
expect(t(now, '%l')).to.equal(' 1')
})
it('should format %L as 0 padded millisecond', function () {
expect(t(then, '%L')).to.equal('000')
})
it('should format %p as upper cased am/pm', function () {
expect(t(now, '%p')).to.equal('PM')
expect(t(then, '%p')).to.equal('AM')
})
it('should format %P as lower cased am/pm', function () {
expect(t(now, '%P')).to.equal('pm')
expect(t(then, '%P')).to.equal('am')
})
it('should format %q as date suffix', function () {
var st = new Date('2016-03-01T03:05:03')
var nd = new Date('2016-03-02T03:05:03')
var rd = new Date('2016-03-03T03:05:03')
expect(t(st, '%q')).to.equal('st')
expect(t(nd, '%q')).to.equal('nd')
expect(t(rd, '%q')).to.equal('rd')
expect(t(now, '%q')).to.equal('th')
})
it('should format %s as UNIX seconds', function () {
expect(t(now, '%s')).to.be.match(/\d+/)
})
it('should format %u as day of week(1-7)', function () {
expect(t(now, '%u')).to.be.equal('1')
expect(t(then, '%u')).to.be.equal('7')
})
it('should format %U as week of year, starts with 0', function () {
expect(t(now, '%U')).to.equal('01')
})
it('should format %w as day of month(0-7)', function () {
expect(t(now, '%w')).to.be.equal('1')
expect(t(then, '%w')).to.be.equal('0')
})
it('should format %W as week of year, starts with 1', function () {
expect(t(now, '%W')).to.be.equal('01')
})
it('should format %x as local date string', function () {
expect(t(now, '%x')).to.equal(now.toLocaleDateString())
})
it('should format %X as local time string', function () {
expect(t(now, '%X')).to.equal(now.toLocaleTimeString())
})
it('should format %y as 2-digit year', function () {
expect(t(now, '%y')).to.equal('16')
})
it('should format %z as time zone', function () {
expect(t(now, '%z')).to.equal('+0800')
})
it('should escape %% as %', function () {
expect(t(now, '%%')).to.equal('%')
})
})
function mockUTC () {
var p = Date.prototype
function mockUTC() {
var p = Date.prototype;
p._getHours = p.getHours
p.getHours = p.getUTCHours
p._getHours = p.getHours;
p.getHours = p.getUTCHours;
p._getDays = p.getDays
p.getDays = p.getUTCDays
p._getDays = p.getDays;
p.getDays = p.getUTCDays;
p._getTimezoneOffset = p._getTimezoneOffset;
p.getTimezoneOffset = () => -480;
p._getTimezoneOffset = p._getTimezoneOffset
p.getTimezoneOffset = () => -480
}
function restoreUTC() {
var p = Date.prototype;
p.getHours = p._getHours;
p.getDays = p._getDays;
p.getTimezoneOffset = p._getTimezoneOffset;
function restoreUTC () {
var p = Date.prototype
p.getHours = p._getHours
p.getDays = p._getDays
p.getTimezoneOffset = p._getTimezoneOffset
}
+126 -126
View File
@@ -1,128 +1,128 @@
const chai = require("chai");
const sinon = require('sinon');
const expect = chai.expect;
const Errors = require('../../src/util/error.js');
chai.use(require("sinon-chai"));
const chai = require('chai')
const sinon = require('sinon')
const expect = chai.expect
const Errors = require('../../src/util/error.js')
chai.use(require('sinon-chai'))
var _ = require('../../src/util/underscore.js');
var _ = require('../../src/util/underscore.js')
describe('util/underscore', function() {
describe('.isError()', function() {
it('should return true for new Error', function() {
expect(_.isError(new Error())).to.be.true;
});
it('should return true for RenderError', function() {
var tpl = {
token: {
input: 'xx'
}
};
expect(_.isError(new Errors.RenderError(new Error(), tpl))).to.be.true;
});
it('should return true for RenderBreakError', function() {
expect(_.isError(new Errors.RenderBreakError())).to.be.true;
});
});
describe('.isString()', function() {
it('should return true for literal string', function() {
expect(_.isString('foo')).to.be.true;
});
it('should return true String instance', function() {
expect(_.isString(String('foo'))).to.be.true;
});
it('should return false for 123 ', function() {
expect(_.isString(123)).to.be.false;
});
});
describe('.forOwn()', function() {
it('should iterate all properties', function() {
var spy = sinon.spy();
var obj = {
foo: "bar"
};
_.forOwn(obj, spy);
expect(spy).to.have.been.calledWith('bar', 'foo', obj);
});
it('should not iterate over properties on prototype', function() {
var spy = sinon.spy();
var obj = Object.create({
bar: 'foo'
});
obj.foo = 'bar';
_.forOwn(obj, spy);
expect(spy).to.have.been.calledOnce;
expect(spy).to.have.been.calledWith('bar', 'foo', obj);
});
it('should break when returned false', function() {
var spy = sinon.stub().returns(false);
_.forOwn({
'foo': 'foo',
'bar': 'foo'
}, spy);
expect(spy).to.have.been.calledOnce;
});
});
describe('.isArray()', function() {
it('should return true for []', function() {
expect(_.isArray([])).to.be.true;
});
it('should return false for "foo"', function() {
expect(_.isArray("foo")).to.be.false;
});
});
describe('.echo()', function() {
it('should be transparent', function() {
expect(_.echo('foo')('bar')).to.equal('bar');
});
it('should log the arguments', function() {
var log = sinon.spy(console, 'log');
_.echo('foo')('bar');
expect(log).to.have.been.calledWith('[foo]', 'bar');
});
});
describe('.assign()', function() {
it('should handle null dst', function() {
expect(_.assign(null, {
foo: 'bar'
})).to.deep.equal({
foo: 'bar'
});
});
it('should assign 2 objects', function() {
var src = {
foo: 'foo',
bar: 'bar'
};
var dst = {
foo: 'bar',
kaa: 'kaa'
};
expect(_.assign(dst, src)).to.deep.equal({
foo: 'foo',
bar: 'bar',
kaa: 'kaa'
});
});
it('should assign 3 objects', function() {
expect(_.assign({
foo: 'foo'
}, {
bar: 'bar'
}, {
car: 'car'
})).to.deep.equal({
foo: 'foo',
bar: 'bar',
car: 'car'
});
});
});
describe('.uniq()', function() {
it('should handle empty array', function() {
expect(_.uniq([])).to.deep.equal([]);
});
it('should do uniq', function() {
expect(_.uniq([1, 'a', 'a', 1])).to.deep.equal([1, 'a']);
});
});
});
describe('util/underscore', function () {
describe('.isError()', function () {
it('should return true for new Error', function () {
expect(_.isError(new Error())).to.be.true
})
it('should return true for RenderError', function () {
var tpl = {
token: {
input: 'xx'
}
}
expect(_.isError(new Errors.RenderError(new Error(), tpl))).to.be.true
})
it('should return true for RenderBreakError', function () {
expect(_.isError(new Errors.RenderBreakError())).to.be.true
})
})
describe('.isString()', function () {
it('should return true for literal string', function () {
expect(_.isString('foo')).to.be.true
})
it('should return true String instance', function () {
expect(_.isString(String('foo'))).to.be.true
})
it('should return false for 123 ', function () {
expect(_.isString(123)).to.be.false
})
})
describe('.forOwn()', function () {
it('should iterate all properties', function () {
var spy = sinon.spy()
var obj = {
foo: 'bar'
}
_.forOwn(obj, spy)
expect(spy).to.have.been.calledWith('bar', 'foo', obj)
})
it('should not iterate over properties on prototype', function () {
var spy = sinon.spy()
var obj = Object.create({
bar: 'foo'
})
obj.foo = 'bar'
_.forOwn(obj, spy)
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith('bar', 'foo', obj)
})
it('should break when returned false', function () {
var spy = sinon.stub().returns(false)
_.forOwn({
'foo': 'foo',
'bar': 'foo'
}, spy)
expect(spy).to.have.been.calledOnce
})
})
describe('.isArray()', function () {
it('should return true for []', function () {
expect(_.isArray([])).to.be.true
})
it('should return false for "foo"', function () {
expect(_.isArray('foo')).to.be.false
})
})
describe('.echo()', function () {
it('should be transparent', function () {
expect(_.echo('foo')('bar')).to.equal('bar')
})
it('should log the arguments', function () {
var log = sinon.spy(console, 'log')
_.echo('foo')('bar')
expect(log).to.have.been.calledWith('[foo]', 'bar')
})
})
describe('.assign()', function () {
it('should handle null dst', function () {
expect(_.assign(null, {
foo: 'bar'
})).to.deep.equal({
foo: 'bar'
})
})
it('should assign 2 objects', function () {
var src = {
foo: 'foo',
bar: 'bar'
}
var dst = {
foo: 'bar',
kaa: 'kaa'
}
expect(_.assign(dst, src)).to.deep.equal({
foo: 'foo',
bar: 'bar',
kaa: 'kaa'
})
})
it('should assign 3 objects', function () {
expect(_.assign({
foo: 'foo'
}, {
bar: 'bar'
}, {
car: 'car'
})).to.deep.equal({
foo: 'foo',
bar: 'bar',
car: 'car'
})
})
})
describe('.uniq()', function () {
it('should handle empty array', function () {
expect(_.uniq([])).to.deep.equal([])
})
it('should do uniq', function () {
expect(_.uniq([1, 'a', 'a', 1])).to.deep.equal([1, 'a'])
})
})
})