refactor: strictly typed

This commit is contained in:
harttle
2019-02-23 00:49:54 +08:00
parent 51f7e66f60
commit 5b6100d12b
86 changed files with 2630 additions and 2590 deletions
+2 -44
View File
@@ -32,10 +32,10 @@ describe('error', function () {
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('TokenizationError')
})
it('should contain the whole template content in err.input', async function () {
it('should contain the whole template content in err.token.input', async function () {
const html = 'bar\nfoo{% . a %}\nfoo'
const err = await expect(engine.parseAndRender(html)).be.rejected
expect(err.input).to.equal(html)
expect(err.token.input).to.equal(html)
})
it('should contain line number in err.token.line', async function () {
const err = await expect(engine.parseAndRender('1\n2\n{% . a %}\n4')).be.rejected
@@ -49,25 +49,12 @@ describe('error', function () {
expect(err.stack).to.contain('at Liquid.parse')
})
describe('captureStackTrace compatibility', function () {
const captureStackTrace = Error.captureStackTrace
before(() => (Error.captureStackTrace = null))
after(() => (Error.captureStackTrace = captureStackTrace))
it('should be empty when captureStackTrace undefined', async function () {
const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected
expect(err.stack).to.contain('illegal tag syntax')
expect(err.stack).to.not.contain('at Object.parse')
})
})
it('should contain file path in err.file', async function () {
const html = '<html>\n<head>\n\n{% . a %}\n\n'
mock({
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
restore()
expect(err.name).to.equal('TokenizationError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
it('should throw error with line and pos if tag unmatched', async function () {
const err = await expect(engine.parseAndRender('1\n2\nfoo{% assign a = 4 }\n4')).be.rejected
expect(err.name).to.equal('TokenizationError')
@@ -187,12 +174,6 @@ describe('error', function () {
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
it('should contain the whole template content in err.input', async function () {
const html = 'bar\nfoo{%throwingTag%}\nfoo'
const err = await expect(engine.parseAndRender(html)).be.rejected
expect(err.input).to.equal(html)
expect(err.name).to.equal('RenderError')
})
it('should contain line number in err.token.line', async function () {
const src = '1\n2\n{{1|throwingFilter}}\n4'
const err = await expect(engine.parseAndRender(src)).be.rejected
@@ -204,18 +185,6 @@ describe('error', function () {
expect(err.message).to.contain('intended render reject')
expect(err.stack).to.match(/at .*:\d+:\d+/)
})
it('should contain file path in err.file', async function () {
const html = '<html>\n<head>\n\n{% throwingTag %}\n\n'
mock({
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
restore()
console.log(err, err.name)
expect(err.name).to.equal('RenderError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
})
describe('ParseError', function () {
@@ -296,16 +265,5 @@ describe('error', function () {
expect(err.stack).to.contain('ParseError: tag -a not found')
expect(err.stack).to.match(/at .*:\d+:\d+\)/)
})
it('should contain file path in err.file', async function () {
const html = '<html>\n<head>\n\n{% raw %}\n\n'
mock({
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
restore()
expect(err.name).to.equal('ParseError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
})
})
+6 -48
View File
@@ -1,68 +1,26 @@
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(sinonChai)
chai.use(chaiAsPromised)
const P = require('src/util/promise')
describe('util/promise', function () {
describe('.anySeries()', function () {
it('should resolve in series', function () {
const spy1 = sinon.spy()
const 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 () {
const 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', async () => {
const result = await P.anySeries(
['first', 'second'],
item => Promise.resolve(item)
)
return expect(result).to.equal('first')
})
it('should not call rest of callbacks once resolved', () => {
const 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()', async function () {
it('should resolve when all resolved', async function () {
const result = await P.mapSeries(
['first', 'second', 'third'],
item => Promise.resolve(item)
(item: string) => Promise.resolve(item)
)
return expect(result).to.deep.equal(['first', 'second', 'third'])
})
it('should reject with the error that first callback rejected', () => {
const p = P.mapSeries(['first', 'second'],
item => Promise.reject(item))
(item: string) => Promise.reject(item))
return expect(p).to.rejectedWith('first')
})
it('should resolve in series', function () {
@@ -71,7 +29,7 @@ describe('util/promise', function () {
return P
.mapSeries(
['first', 'second'],
(item, idx) => new Promise(function (resolve) {
(item: string, idx: number) => new Promise(function (resolve) {
if (idx === 0) {
setTimeout(function () {
spy1()
@@ -87,7 +45,7 @@ describe('util/promise', function () {
it('should not call rest of callbacks once rejected', () => {
const spy = sinon.spy()
return P
.mapSeries(['first', 'second'], (item, idx) => {
.mapSeries(['first', 'second'], (item: string, idx: number) => {
if (idx > 0) {
spy()
}
+2 -2
View File
@@ -4,8 +4,8 @@ import t from 'src/util/strftime'
const expect = chai.expect
describe('util/strftime', function () {
let now
let then
let now: Date
let then: Date
before(function () {
mockUTC()
now = new Date('2016-01-04T13:15:23.000Z')
+3 -47
View File
@@ -3,6 +3,7 @@ import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import { RenderError, RenderBreakError } from 'src/util/error'
import * as _ from 'src/util/underscore'
import ITemplate from 'src/template/itemplate'
const expect = chai.expect
chai.use(sinonChai)
@@ -17,7 +18,7 @@ describe('util/underscore', function () {
token: {
input: 'xx'
}
}
} as ITemplate
expect(_.isError(new RenderError(new Error(), tpl))).to.be.true
})
it('should return true for RenderBreakError', function () {
@@ -104,7 +105,7 @@ describe('util/underscore', function () {
})
describe('.isObject()', function () {
it('should return true for function', function () {
expect(_.isObject(x => x)).to.be.true
expect(_.isObject((x: any) => x)).to.be.true
})
it('should return true for plain object', function () {
expect(_.isObject({})).to.be.true
@@ -116,49 +117,4 @@ describe('util/underscore', function () {
expect(_.isObject(2)).to.be.false
})
})
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 () {
const src = {
foo: 'foo',
bar: 'bar'
}
const 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'])
})
})
})