refactor: import rollup

This commit is contained in:
harttle
2018-08-20 23:58:41 +08:00
parent 4ece4e208f
commit bc9d8f92af
82 changed files with 8589 additions and 3963 deletions
+12 -9
View File
@@ -1,9 +1,12 @@
const chai = require('chai')
import chai from 'chai'
import request from 'supertest'
import express from 'express'
import mock from 'mock-fs'
import Liquid from '../src'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
const mock = require('mock-fs')
const request = require('supertest')
const express = require('express')
const Liquid = require('../src')
chai.use(chaiAsPromised)
describe('engine#express()', function () {
let app, engine
@@ -36,11 +39,11 @@ describe('engine#express()', function () {
.expect(200, done)
})
it('should pass error when file not found', function (done) {
let view = {
const view = {
root: []
}
let file = '/not-exist.html'
let ctx = {}
const file = '/not-exist.html'
const ctx = {}
engine.express().call(view, file, ctx, function (err) {
try {
expect(err.code).to.equal('ENOENT')
@@ -82,7 +85,7 @@ describe('engine#express()', function () {
.expect(200, done)
})
it('should respect express views (Undefined) when lookup', function (done) {
let files = {}
const files = {}
files[process.cwd() + '/views/include.html'] = '{% include file %}'
files[process.cwd() + '/views/bar.html'] = 'bar'
mock(files)
+16 -16
View File
@@ -1,21 +1,21 @@
const chai = require('chai')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const expect = chai.expect
import chai from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import Filter from '../src/filter.js'
import {factory as scopeFactory} from '../src/scope.js'
chai.use(sinonChai)
let filter = require('../src/filter.js')()
let Scope = require('../src/scope.js')
const expect = chai.expect
const filter = Filter()
describe('filter', function () {
let scope
beforeEach(function () {
filter.clear()
scope = Scope.factory()
scope = scopeFactory()
})
it('should return default filter when not registered', function () {
let result = filter.construct('foo')
const result = filter.construct('foo')
expect(result.name).to.equal('foo')
})
@@ -27,7 +27,7 @@ describe('filter', function () {
it('should parse argument syntax', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: a, "b"')
const f = filter.construct('foo: a, "b"')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['a', '"b"'])
@@ -49,7 +49,7 @@ describe('filter', function () {
})
it('should call filter with corrct arguments', function () {
let spy = sinon.spy()
const spy = sinon.spy()
filter.register('foo', spy)
filter.construct('foo: 33').render('foo', scope)
expect(spy).to.have.been.calledWith('foo', 33)
@@ -57,35 +57,35 @@ describe('filter', function () {
it('should support arguments as named key/values', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: key1: "literal1", key2: value2')
const f = filter.construct('foo: key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support arguments as named key/values with inline literals', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: "test0", key1: "literal1", key2: value2')
const f = filter.construct('foo: "test0", key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ '"test0"', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support arguments as named key/values with inline values', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: test0, key1: "literal1", key2: value2')
const f = filter.construct('foo: test0, key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ 'test0', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support argument values named same as keys', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: a: a')
const f = filter.construct('foo: a: a')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['\'a\'', 'a'])
})
it('should support argument literals named same as keys', function () {
filter.register('foo', x => x)
let f = filter.construct('foo: a: "a"')
const f = filter.construct('foo: a: "a"')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['\'a\'', '"a"'])
})
+8 -8
View File
@@ -6,7 +6,7 @@ chai.use(chaiAsPromised)
const liquid = new Liquid()
const expect = chai.expect
let ctx = {
const ctx = {
date: new Date(),
foo: 'bar',
arr: [-2, 'a'],
@@ -85,7 +85,7 @@ describe('filters', function () {
describe('date', function () {
it('should support date: %a %b %d %Y', function () {
let str = ctx.date.toDateString()
const str = ctx.date.toDateString()
return test('{{ date | date:"%a %b %d %Y"}}', str)
})
it('should create a new Date when given "now"', function () {
@@ -142,7 +142,7 @@ describe('filters', function () {
})
it('should support split/first', function () {
let src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
const src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
'{{ my_array | first }}'
return test(src, 'apples')
})
@@ -155,19 +155,19 @@ describe('filters', function () {
})
it('should support join', function () {
let src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
'{{ beatles | join: " and " }}'
return test(src, 'John and Paul and George and Ringo')
})
it('should support split/last', function () {
let src = '{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}' +
const src = '{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}' +
'{{ my_array|last }}'
return test(src, 'tiger')
})
it('should support lstrip', function () {
let src = '{{ " So much room for activities! " | lstrip }}'
const src = '{{ " So much room for activities! " | lstrip }}'
return test(src, 'So much room for activities! ')
})
@@ -193,12 +193,12 @@ describe('filters', function () {
})
it('should support string_with_newlines', function () {
let src = '{% capture string_with_newlines %}\n' +
const src = '{% capture string_with_newlines %}\n' +
'Hello\n' +
'there\n' +
'{% endcapture %}' +
'{{ string_with_newlines | newline_to_br }}'
let dst = '<br />' +
const dst = '<br />' +
'Hello<br />' +
'there<br />'
return test(src, dst)
+12 -10
View File
@@ -1,8 +1,10 @@
const chai = require('chai')
import chai from 'chai'
import mock from 'mock-fs'
import Liquid from '../src'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
const Liquid = require('../src')
const mock = require('mock-fs')
chai.use(require('chai-as-promised'))
chai.use(chaiAsPromised)
describe('liquid', function () {
let engine, strictEngine, ctx
@@ -37,7 +39,7 @@ describe('liquid', function () {
})
describe('Liquid', function () {
it('should ignore invalid root option', function () {
let liquid = Liquid({ root: /regex/ })
const liquid = Liquid({ root: /regex/ })
expect(liquid.options.root).to.deep.equal([])
})
})
@@ -68,18 +70,18 @@ describe('liquid', function () {
}).to.not.throw()
})
it('should render template multiple times', function () {
let template = engine.parse('{{obj}}')
const template = engine.parse('{{obj}}')
return engine.render(template, ctx)
.then(result => expect(result).to.equal('{"foo":"bar"}'))
.then(() => engine.render(template, ctx))
.then((result) => expect(result).to.equal('{"foo":"bar"}'))
})
it('should render filters', function () {
let template = engine.parse('<p>{{arr | join: "_"}}</p>')
const template = engine.parse('<p>{{arr | join: "_"}}</p>')
return expect(engine.render(template, ctx)).to.eventually.equal('<p>-2_a</p>')
})
it('should render accessive filters', function () {
let src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
const src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
'{{ my_array | first }}'
return expect(engine.parseAndRender(src)).to.eventually.equal('apples')
})
@@ -89,7 +91,7 @@ describe('liquid', function () {
.to.eventually.equal('foo')
})
it('should find files without extname', function () {
let engine = Liquid({root: '/root'})
const engine = Liquid({root: '/root'})
return expect(engine.renderFile('/root/files/bar', ctx))
.to.eventually.equal('bar')
})
@@ -106,7 +108,7 @@ describe('liquid', function () {
.to.eventually.equal('foo')
})
it('should default root to cwd', function () {
let files = {}
const files = {}
files[process.cwd() + '/foo.html'] = 'FOO'
mock(files)
+6 -6
View File
@@ -1,7 +1,7 @@
const chai = require('chai')
const expect = chai.expect
let lexical = require('../src/lexical.js')
const lexical = require('../src/lexical.js')
describe('lexical', function () {
it('should test filter syntax', function () {
@@ -105,26 +105,26 @@ describe('lexical', function () {
})
it('should throw if non-literal', function () {
let fn = () => lexical.parseLiteral('a')
const fn = () => lexical.parseLiteral('a')
expect(fn).to.throw("cannot parse 'a' as literal")
})
})
describe('.matchValue()', function () {
it('should match -5-5', function () {
let match = lexical.matchValue('-5-5')
const match = lexical.matchValue('-5-5')
expect(match && match[0]).to.equal('-5-5')
})
it('should match 4-3', function () {
let match = lexical.matchValue('4-3')
const match = lexical.matchValue('4-3')
expect(match && match[0]).to.equal('4-3')
})
it('should match 4-3', function () {
let match = lexical.matchValue('4-3')
const match = lexical.matchValue('4-3')
expect(match && match[0]).to.equal('4-3')
})
it('should match var-1', function () {
let match = lexical.matchValue('var-1')
const match = lexical.matchValue('var-1')
expect(match && match[0]).to.equal('var-1')
})
})
+6 -4
View File
@@ -1,8 +1,10 @@
const chai = require('chai')
import chai from 'chai'
import mock from 'mock-fs'
import Liquid from '../../src'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
const mock = require('mock-fs')
const Liquid = require('../../src')
chai.use(require('chai-as-promised'))
chai.use(chaiAsPromised)
describe('cache options', function () {
let engine
+5 -5
View File
@@ -6,7 +6,7 @@ chai.use(require('chai-as-promised'))
describe('strict options', function () {
let engine
let ctx = {}
const ctx = {}
beforeEach(function () {
engine = Liquid({
root: '/root/',
@@ -18,16 +18,16 @@ describe('strict options', function () {
.eventually.equal('beforeafter')
})
it('should throw when strict_variables true', function () {
let tpl = engine.parse('before{{notdefined}}after')
let opts = {
const tpl = engine.parse('before{{notdefined}}after')
const opts = {
strict_variables: true
}
return expect(engine.render(tpl, ctx, opts)).to
.be.rejectedWith(/undefined variable: notdefined/)
})
it('should pass strict_variables to render by parseAndRender', function () {
let html = 'before{{notdefined}}after'
let opts = {
const html = 'before{{notdefined}}after'
const opts = {
strict_variables: true
}
return expect(engine.parseAndRender(html, ctx, opts)).to
+21 -19
View File
@@ -1,62 +1,64 @@
const chai = require('chai')
import chai from 'chai'
import Liquid from '../../src'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
const Liquid = require('../../src')
chai.use(require('chai-as-promised'))
chai.use(chaiAsPromised)
describe('trimming', function () {
let ctx = {name: 'harttle'}
const ctx = {name: 'harttle'}
describe('tag trimming', function () {
it('should respect trim_tag_left', function () {
let engine = Liquid({ trim_tag_left: true })
const engine = Liquid({ trim_tag_left: true })
return expect(engine.parseAndRender(' \n \t{%if true%}foo{%endif%} '))
.to.eventually.equal('foo ')
})
it('should respect trim_tag_right', function () {
let engine = Liquid({ trim_tag_right: true })
const engine = Liquid({ trim_tag_right: true })
return expect(engine.parseAndRender('\t{%if true%}foo{%endif%} \n'))
.to.eventually.equal('\tfoo')
})
it('should not trim value', function () {
let engine = Liquid({ trim_tag_left: true, trim_tag_right: true })
const engine = Liquid({ trim_tag_left: true, trim_tag_right: true })
return expect(engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx))
.to.eventually.equal('a harttle b')
})
})
describe('value trimming', function () {
it('should respect trim_value_left', function () {
let engine = Liquid({ trim_value_left: true })
const engine = Liquid({ trim_value_left: true })
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
.to.eventually.equal('harttle ')
})
it('should respect trim_value_right', function () {
let engine = Liquid({ trim_value_right: true })
const engine = Liquid({ trim_value_right: true })
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
.to.eventually.equal(' \n \tharttle')
})
it('should respect not trim tag', function () {
let engine = Liquid({ trim_value_left: true, trim_value_right: true })
const engine = Liquid({ trim_value_left: true, trim_value_right: true })
return expect(engine.parseAndRender('\t{% if true %} aha {%endif%}\t'))
.to.eventually.equal('\t aha \t')
})
})
describe('greedy', function () {
let src = '\n {%-if true-%}\n a \n{{-name-}}{%-endif-%}\n '
const src = '\n {%-if true-%}\n a \n{{-name-}}{%-endif-%}\n '
it('should enable greedy by default', function () {
let engine = Liquid()
const engine = Liquid()
return expect(engine.parseAndRender(src, ctx))
.to.eventually.equal('aharttle')
})
it('should respect to greedy:false by default', function () {
let engine = Liquid({greedy: false})
const engine = Liquid({greedy: false})
return expect(engine.parseAndRender(src, ctx))
.to.eventually.equal('\n a \nharttle ')
})
})
describe('markup', function () {
it('should support trim using markup', function () {
let engine = Liquid()
let src = [
const engine = Liquid()
const src = [
'{%- assign username = "John G. Chalmers-Smith" -%}',
'{%- if username and username.length > 10 -%}',
' Wow, {{ username }}, you have a long name!',
@@ -64,12 +66,12 @@ describe('trimming', function () {
' Hello there!',
'{%- endif -%}'
].join('\n')
let dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
const dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
})
it('should not trim when not specified', function () {
let engine = Liquid()
let src = [
const engine = Liquid()
const src = [
'{% assign username = "John G. Chalmers-Smith" %}',
'{% if username and username.length > 10 %}',
' Wow, {{ username }}, you have a long name!',
@@ -77,7 +79,7 @@ describe('trimming', function () {
' Hello there!',
'{% endif %}'
].join('\n')
let dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
const dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
})
})
+13 -11
View File
@@ -1,15 +1,17 @@
const chai = require('chai')
import chai from 'chai'
import sinonChai from 'sinon-chai'
import Filter from '../src/filter.js'
import Tag from '../src/tag.js'
import Template from '../src/parser.js'
const expect = chai.expect
chai.use(require('sinon-chai'))
let filter = require('../src/filter.js')()
let tag = require('../src/tag.js')()
let Template = require('../src/parser.js')
const filter = Filter()
const tag = Tag()
chai.use(sinonChai)
describe('template', function () {
let template
let add = (l, r) => l + r
const add = (l, r) => l + r
beforeEach(function () {
filter.clear()
@@ -26,21 +28,21 @@ describe('template', function () {
})
it('should parse value string', function () {
let tpl = template.parseValue('foo')
const tpl = template.parseValue('foo')
expect(tpl.type).to.equal('value')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters).to.deep.equal([])
})
it('should parse value string with a simple filter', function () {
let tpl = template.parseValue('foo | add: 3, "foo"')
const tpl = template.parseValue('foo | add: 3, "foo"')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].filter).to.equal(add)
})
it('should parse value string with filters', function () {
let tpl = template.parseValue('foo | add: "|" | add')
const tpl = template.parseValue('foo | add: "|" | add')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(2)
})
+32 -32
View File
@@ -1,18 +1,20 @@
'use strict'
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const expect = chai.expect
const sinonChai = require('sinon-chai')
const sinon = require('sinon')
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai'
import sinon from 'sinon'
import Tag from '../src/tag.js'
import {factory as scopeFactory} from '../src/scope.js'
import Filter from '../src/filter'
import Render from '../src/render.js'
import parser from '../src/parser.js'
chai.use(sinonChai)
chai.use(chaiAsPromised)
let tag = require('../src/tag.js')()
let Scope = require('../src/scope.js')
let filter = require('../src/filter')()
let Render = require('../src/render.js')
let Template = require('../src/parser.js')(tag, filter)
const expect = chai.expect
const tag = Tag()
const filter = Filter()
const Template = parser(tag, filter)
let render
describe('render', function () {
@@ -24,43 +26,41 @@ describe('render', function () {
describe('.renderTemplates()', function () {
it('should throw when scope undefined', function () {
expect(function () {
render.renderTemplates([])
}).to.throw(/scope undefined/)
expect(render.renderTemplates([])).to.be.rejectedWith(/scope undefined/)
})
it('should render html', function () {
let scope = Scope.factory({})
const scope = scopeFactory({})
return expect(render.renderTemplates([{type: 'html', value: '<p>'}], scope)).to.eventually.equal('<p>')
})
})
describe('.renderValue()', function () {
it('should respect to .to_liquid() method', function () {
let scope = Scope.factory({
const scope = scopeFactory({
bar: { to_liquid: x => 'custom' }
})
let tpl = Template.parseValue('bar')
const tpl = Template.parseValue('bar')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('custom')
})
it('should stringify objects', function () {
let scope = Scope.factory({
const scope = scopeFactory({
foo: { obj: { arr: ['a', 2] } }
})
let tpl = Template.parseValue('foo')
const tpl = Template.parseValue('foo')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"obj":{"arr":["a",2]}}')
})
it('should skip circular property', function () {
let ctx = { foo: { num: 2 }, bar: 'bar' }
const ctx = { foo: { num: 2 }, bar: 'bar' }
ctx.foo.circular = ctx
let scope = Scope.factory(ctx)
let tpl = Template.parseValue('foo')
const scope = scopeFactory(ctx)
const tpl = Template.parseValue('foo')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"num":2,"circular":{"bar":"bar"}}')
})
it('should skip function property', function () {
let scope = Scope.factory({obj: {foo: 'foo', bar: x => x}})
let tpl = Template.parseValue('obj')
const scope = scopeFactory({obj: {foo: 'foo', bar: x => x}})
const tpl = Template.parseValue('obj')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"foo":"foo"}')
})
})
@@ -74,24 +74,24 @@ describe('render', function () {
it('should eval value', function () {
filter.register('date', (l, r) => l + r)
filter.register('time', (l, r) => l + 3 * r)
let tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
let scope = Scope.factory({
const tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
const scope = scopeFactory({
foo: { bar: ['a'] }
})
expect(render.evalValue(tpl, scope)).to.equal('ab6')
})
it('should reserve type', function () {
filter.register('arr', () => [1])
let tpl = Template.parseValue('"x" | arr')
expect(render.evalValue(tpl, Scope.factory())).to.deep.equal([1])
const tpl = Template.parseValue('"x" | arr')
expect(render.evalValue(tpl, scopeFactory())).to.deep.equal([1])
})
it('should eval filter with correct arguments', function () {
let date = sinon.stub().returns('y')
let time = sinon.spy()
const date = sinon.stub().returns('y')
const time = sinon.spy()
filter.register('date', date)
filter.register('time', time)
let tpl = Template.parseValue('foo.bar | date: "b" | time:2')
let scope = Scope.factory({
const tpl = Template.parseValue('foo.bar | date: "b" | time:2')
const scope = scopeFactory({
foo: {bar: 'bar'}
})
render.evalValue(tpl, scope)
+8 -8
View File
@@ -1,5 +1,5 @@
import chai from 'chai'
import Scope from '../src/scope.js'
import {factory as scopeFactory} from '../src/scope.js'
const expect = chai.expect
@@ -14,7 +14,7 @@ describe('scope', function () {
arr: ['a', 'b']
}
}
scope = Scope.factory(ctx)
scope = scopeFactory(ctx)
})
describe('#propertyAccessSeq()', function () {
@@ -88,7 +88,7 @@ describe('scope', function () {
})
it('should respect to to_liquid', function () {
let scope = Scope.factory({foo: {
const scope = scopeFactory({foo: {
to_liquid: () => ({bar: 'BAR'}),
bar: 'bar'
}})
@@ -96,7 +96,7 @@ describe('scope', function () {
})
it('should respect to toLiquid', function () {
let scope = Scope.factory({foo: {
const scope = scopeFactory({foo: {
toLiquid: () => ({bar: 'BAR'}),
bar: 'bar'
}})
@@ -162,7 +162,7 @@ describe('scope', function () {
describe('strict_variables', function () {
let scope
beforeEach(function () {
scope = Scope.factory(ctx, {
scope = scopeFactory(ctx, {
strict_variables: true
})
})
@@ -227,10 +227,10 @@ describe('scope', function () {
})
})
it('should pop specified scope', function () {
let scope1 = {
const scope1 = {
foo: 'foo'
}
let scope2 = {
const scope2 = {
bar: 'bar'
}
scope.push(scope1)
@@ -242,7 +242,7 @@ describe('scope', function () {
expect(scope.get('bar')).to.equal('bar')
})
it('should throw when specified scope not found', function () {
let scope1 = {
const scope1 = {
foo: 'foo'
}
expect(() => scope.pop(scope1)).to.throw('scope not found, cannot pop')
+6 -6
View File
@@ -1,11 +1,11 @@
const chai = require('chai')
const expect = chai.expect
let syntax = require('../src/syntax.js')
let Scope = require('../src/scope.js')
const syntax = require('../src/syntax.js')
const Scope = require('../src/scope.js')
let evalExp = syntax.evalExp
let evalValue = syntax.evalValue
let isTruthy = syntax.isTruthy
const evalExp = syntax.evalExp
const evalValue = syntax.evalValue
const isTruthy = syntax.isTruthy
describe('expression', function () {
let scope
@@ -36,7 +36,7 @@ describe('expression', function () {
})
it('should throw if not valid', function () {
let fn = () => evalValue('===')
const fn = () => evalValue('===')
expect(fn).to.throw("cannot eval '===' as value")
})
})
+41 -40
View File
@@ -1,15 +1,17 @@
const chai = require('chai')
const sinon = require('sinon')
const expect = chai.expect
chai.use(require('sinon-chai'))
import chai from 'chai'
import Tag from '../src/tag.js'
import {factory as scopeFactory} from '../src/scope.js'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
let tag = require('../src/tag.js')()
let Scope = require('../src/scope.js')
chai.use(sinonChai)
const expect = chai.expect
const tag = Tag()
describe('tag', function () {
let scope
before(function () {
scope = Scope.factory({
scope = scopeFactory({
foo: 'bar',
arr: [2, 1],
bar: {
@@ -37,19 +39,18 @@ describe('tag', function () {
}).not.throw()
})
it('should call tag.render', function () {
let spy = sinon.spy()
it('should call tag.render', async function () {
const spy = sinon.spy()
tag.register('foo', {
render: spy
})
return tag
.construct({
type: 'tag',
value: 'foo',
name: 'foo'
}, [])
.render(scope, {})
.then(() => expect(spy).to.have.been.called)
const token = {
type: 'tag',
value: 'foo',
name: 'foo'
}
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.called
})
describe('hash', function () {
@@ -66,33 +67,33 @@ describe('tag', function () {
args: 'aa:foo bb: arr[0] cc: 2.3\ndd:bar.coo'
}
})
it('should call tag.render with scope', function () {
return tag.construct(token, []).render(scope, {})
.then(() => expect(spy).to.have.been.calledWithMatch(scope))
it('should call tag.render with scope', async function () {
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.calledWithMatch(scope)
})
it('should resolve identifier hash', function () {
return tag.construct(token, []).render(scope, {})
.then(() => expect(spy).to.have.been.calledWithMatch({}, {
aa: 'bar'
}))
it('should resolve identifier hash', async function () {
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.calledWithMatch({}, {
aa: 'bar'
})
})
it('should accept space between key/value', function () {
return tag.construct(token, []).render(scope, {})
.then(() => expect(spy).to.have.been.calledWithMatch({}, {
bb: 2
}))
it('should accept space between key/value', async function () {
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.calledWithMatch({}, {
bb: 2
})
})
it('should resolve number value hash', function () {
return tag.construct(token, []).render(scope, {})
.then(() => expect(spy).to.have.been.calledWithMatch(scope, {
cc: 2.3
}))
it('should resolve number value hash', async function () {
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.calledWithMatch(scope, {
cc: 2.3
})
})
it('should resolve property access hash', function () {
return tag.construct(token, []).render(scope, {})
.then(() => expect(spy).to.have.been.calledWithMatch(scope, {
dd: 'uoo'
}))
it('should resolve property access hash', async function () {
await tag.construct(token, []).render(scope, {})
expect(spy).to.have.been.calledWithMatch(scope, {
dd: 'uoo'
})
})
})
})
+20 -19
View File
@@ -1,29 +1,30 @@
'use strict'
const Liquid = require('../../src')
const chai = require('chai')
import Liquid from '../../src'
import chai from 'chai'
import sinonChai from 'chai-as-promised'
chai.use(sinonChai)
const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/assign', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should throw when variable expression illegal', function () {
let src = '{% assign / %}'
let ctx = {}
const src = '{% assign / %}'
const ctx = {}
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/)
})
it('should support assign to a string', function () {
let src = '{% assign foo="bar" %}{{foo}}'
const src = '{% assign foo="bar" %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('bar')
})
it('should support assign to a number', function () {
let src = '{% assign foo=10086 %}{{foo}}'
const src = '{% assign foo=10086 %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('10086')
})
it('should shading rather than overwriting', function () {
let ctx = {foo: 'foo'}
let src = '{% assign foo="FOO" %}{{foo}}'
const ctx = {foo: 'foo'}
const src = '{% assign foo="FOO" %}{{foo}}'
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('FOO')
@@ -31,37 +32,37 @@ describe('tags/assign', function () {
})
})
it('should assign as array', function () {
let src = '{% assign foo=(1..3) %}{{foo}}'
const src = '{% assign foo=(1..3) %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('[1,2,3]')
})
it('should assign as filter result', function () {
let src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}'
const src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('A')
})
it('should assign var-1', function () {
let src = '{% assign var-1 = 5 %}{{ var-1 }}'
const src = '{% assign var-1 = 5 %}{{ var-1 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
})
it('should assign var-', function () {
let src = '{% assign var- = 5 %}{{ var- }}'
const src = '{% assign var- = 5 %}{{ var- }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
})
it('should assign -var', function () {
let src = '{% assign -let = 5 %}{{ -let }}'
const src = '{% assign -let = 5 %}{{ -let }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
})
it('should assign -5-5', function () {
let src = '{% assign -5-5 = 5 %}{{ -5-5 }}'
const src = '{% assign -5-5 = 5 %}{{ -5-5 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
})
it('should assign 4-3', function () {
let src = '{% assign 4-3 = 5 %}{{ 4-3 }}'
const src = '{% assign 4-3 = 5 %}{{ 4-3 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
})
it('should not assign -6', function () {
let src = '{% assign -6 = 5 %}{{ -6 }}'
const src = '{% assign -6 = 5 %}{{ -6 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('-6')
})
})
+6 -6
View File
@@ -5,17 +5,17 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/capture', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should support capture', function () {
let src = '{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}'
const src = '{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('A')
})
it('should shading rather than overwriting', function () {
let src = '{% capture var %}10{% endcapture %}{{var}}'
let ctx = {'var': 20}
const src = '{% capture var %}10{% endcapture %}{{var}}'
const ctx = {'var': 20}
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('10')
@@ -24,13 +24,13 @@ describe('tags/capture', function () {
})
it('should throw on invalid identifier', function () {
let src = '{% capture = %}{%endcapture%}'
const src = '{% capture = %}{%endcapture%}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/= not valid identifier/)
})
it('should throw when capture not closed', function () {
let src = '{%capture c%}{{c}}'
const src = '{%capture c%}{{c}}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/tag .* not closed/)
})
+8 -8
View File
@@ -5,46 +5,46 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/case', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should reject if not closed', function () {
let src = '{% case "foo"%}'
const src = '{% case "foo"%}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/{% case "foo"%} not closed/)
})
it('should hit the specified case', function () {
let src = '{% case "foo"%}' +
const src = '{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('foo')
})
it('should resolve empty string if not hit', function () {
let src = '{% case empty %}' +
const src = '{% case empty %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}'
let ctx = {
const ctx = {
empty: ''
}
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('bar')
})
it('should accept empty string as branch name', function () {
let src = '{% case false %}' +
const src = '{% case false %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
})
it('should support boolean case', function () {
let src = '{% case false %}' +
const src = '{% case false %}' +
'{% when "foo" %}foo{% when false%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('bar')
})
it('should support else branch', function () {
let src = '{% case "a" %}' +
const src = '{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
+6 -6
View File
@@ -5,29 +5,29 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/comment', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should support empty content', function () {
let src = '{% comment %}{% raw%}'
const src = '{% comment %}{% raw%}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/{% comment %} not closed/)
})
it('should ignore plain string', function () {
let src = 'My name is {% comment %}super{% endcomment %} Shopify.'
const src = 'My name is {% comment %}super{% endcomment %} Shopify.'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('My name is Shopify.')
})
it('should ignore output tokens', function () {
let src = '{% comment %}\n{{ foo}} \n{% endcomment %}'
const src = '{% comment %}\n{{ foo}} \n{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
})
it('should ignore tag tokens', function () {
let src = '{% comment %}{%if true%}true{%else%}false{%endif%}{% endcomment %}'
const src = '{% comment %}{%if true%}true{%else%}false{%endif%}{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
})
it('should ignore un-balenced tag tokens', function () {
let src = '{% comment %}{%if true%}true{%else%}false{% endcomment %}'
const src = '{% comment %}{%if true%}true{%else%}false{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
})
+6 -6
View File
@@ -5,10 +5,10 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/cycle', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should support cycle', function () {
let src = "{% cycle '1', '2', '3' %}"
const src = "{% cycle '1', '2', '3' %}"
return expect(liquid.parseAndRender(src + src + src + src))
.to.eventually.equal('1231')
})
@@ -19,8 +19,8 @@ describe('tags/cycle', function () {
})
it('should support cycle in for block', function () {
let src = '{% for i in (1..5) %}{% cycle one, "e"%}{% endfor %}'
let ctx = {
const src = '{% for i in (1..5) %}{% cycle one, "e"%}{% endfor %}'
const ctx = {
one: 1
}
return expect(liquid.parseAndRender(src, ctx))
@@ -28,10 +28,10 @@ describe('tags/cycle', function () {
})
it('should support cycle group', function () {
let src = "{% cycle one: '1', '2', '3'%}" +
const src = "{% cycle one: '1', '2', '3'%}" +
"{% cycle 1: '1', '2', '3'%}" +
"{% cycle 2: '1', '2', '3'%}"
let ctx = {
const ctx = {
one: 1
}
return expect(liquid.parseAndRender(src, ctx))
+11 -11
View File
@@ -5,22 +5,22 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/decrement', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should throw when variable expression illegal', function () {
let src = '{% decrement / %}{{var}}'
let ctx = {}
const src = '{% decrement / %}{{var}}'
const ctx = {}
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/)
})
it('should decrement undefined variable', function () {
let src = '{% decrement var %}{% decrement var %}{% decrement var %}'
const src = '{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
})
it('should decrement defined variable', function () {
let src = '{% decrement var %}{% decrement var %}{% decrement var %}'
let ctx = {'var': 10}
const src = '{% decrement var %}{% decrement var %}{% decrement var %}'
const ctx = {'var': 10}
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('987')
@@ -29,31 +29,31 @@ describe('tags/decrement', function () {
})
it('should be independent from assign', function () {
let src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %}'
const src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
})
it('should be independent from capture', function () {
let src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %}'
const src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
})
it('should not shading assign', function () {
let src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
const src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3 10')
})
it('should not shading capture', function () {
let src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
const src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3 10')
})
it('should share the same variable with increment', function () {
let src = '{%increment var%}{%increment var%}{%decrement var%}{%decrement var%}{%increment var%}'
const src = '{%increment var%}{%increment var%}{%decrement var%}{%decrement var%}{%increment var%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('01100')
})
+28 -28
View File
@@ -23,25 +23,25 @@ describe('tags/for', function () {
}
})
it('should support array', function () {
let src = '{%for c in alpha%}{{c}}{%endfor%}'
const src = '{%for c in alpha%}{{c}}{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('abc')
})
it('should support object', function () {
let src = '{%for item in obj%}{{item[0]}},{{item[1]}}-{%else%}b{%endfor%}'
const src = '{%for item in obj%}{{item[0]}},{{item[1]}}-{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('foo,bar-coo,haa-')
})
describe('scope', function () {
it('should read super scope', function () {
let src = '{%for a in (1..2)%}{{num}}{%endfor%}'
const src = '{%for a in (1..2)%}{{num}}{%endfor%}'
return expect(liquid.parseAndRender(src, {num: 1}))
.to.eventually.equal('11')
})
it('should write super scope', function () {
let src = '{%for a in (1..2)%}{{num}}{%assign num = 2%}{%endfor%}'
const src = '{%for a in (1..2)%}{{num}}{%assign num = 2%}{%endfor%}'
return expect(liquid.parseAndRender(src, {num: 1}))
.to.eventually.equal('12')
})
@@ -49,13 +49,13 @@ describe('tags/for', function () {
describe('illegal', function () {
it('should reject when for not closed', function () {
let src = '{%for c in alpha%}{{c}}'
const src = '{%for c in alpha%}{{c}}'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/tag .* not closed/)
})
it('should reject when inner templates rejected', function () {
let src = '{%for c in alpha%}{%throwingTag%}{%endfor%}'
const src = '{%for c in alpha%}{%throwingTag%}{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/intended render error/)
})
@@ -63,51 +63,51 @@ describe('tags/for', function () {
describe('else', function () {
it('should goto else for empty array', function () {
let src = '{%for c in emptyArray%}a{%else%}b{%endfor%}'
const src = '{%for c in emptyArray%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
})
it('should treat non-empty string as one single element', function () {
let src = '{%for c in "abc"%}x{{c}}{%else%}y{%endfor%}'
const src = '{%for c in "abc"%}x{{c}}{%else%}y{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('xabc')
})
it('should goto else for empty string', function () {
let src = '{%for c in ""%}a{%else%}b{%endfor%}'
const src = '{%for c in ""%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
})
it('should goto else for empty string object', function () {
// it should be false although `new String` is none-conform
let src = '{%for c in strObj%}a{%else%}b{%endfor%}'
const src = '{%for c in strObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
})
it('should goto else for empty object', function () {
let src = '{%for c in emptyObj%}a{%else%}b{%endfor%}'
const src = '{%for c in emptyObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
})
it('should goto else for null-prototyped object', function () {
let src = '{%for c in nullProtoObj%}a{%else%}b{%endfor%}'
const src = '{%for c in nullProtoObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
})
})
it('should support for with forloop', function () {
let src = '{%for c in alpha%}' +
const src = '{%for c in alpha%}' +
'{{forloop.first}}.{{forloop.index}}.{{forloop.index0}}.' +
'{{forloop.last}}.{{forloop.length}}.' +
'{{forloop.rindex}}.{{forloop.rindex0}}' +
'{{c}}\n' +
'{%endfor%}'
let dst = 'true.1.0.false.3.3.2a\n' +
const dst = 'true.1.0.false.3.3.2a\n' +
'false.2.1.false.3.2.1b\n' +
'false.3.2.true.3.1.0c\n'
return expect(liquid.parseAndRender(src, ctx))
@@ -115,14 +115,14 @@ describe('tags/for', function () {
})
it('should support for with continue', function () {
let src = '{% for i in (1..5) %}' +
const src = '{% for i in (1..5) %}' +
'{{i}}{% continue %}after' +
'{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12345')
})
it('should support for with break', function () {
let src = '{% for i in (one..5) %}' +
const src = '{% for i in (one..5) %}' +
'{% if i == 4 %}{% break %}{% endif %}' +
'{{ i }}' +
'{% endfor %}'
@@ -132,22 +132,22 @@ describe('tags/for', function () {
describe('limit', function () {
it('should support for with limit', function () {
let src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}'
const src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12')
})
it('should set forloop.last properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.last}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.last}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('false true ')
})
it('should set forloop.first properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.first}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.first}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('true false ')
})
it('should set forloop.length properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.length}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.length}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('2 2 ')
})
@@ -155,27 +155,27 @@ describe('tags/for', function () {
describe('offset', function () {
it('should support offset with limit', function () {
let src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}'
const src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('67')
})
it('should set index properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.index}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.index}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('1 2 ')
})
it('should set index0 properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.index0}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.index0}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('0 1 ')
})
it('should set rindex properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.rindex}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.rindex}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('2 1 ')
})
it('should set rindex0 properly', function () {
let src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.rindex0}} {%endfor%}'
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.rindex0}} {%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('1 0 ')
})
@@ -183,19 +183,19 @@ describe('tags/for', function () {
describe('reverse', function () {
it('should support for reversed in the last position', function () {
let src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}'
const src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
it('should support for reversed in the first position', function () {
let src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'
const src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
it('should support for reversed in the middle position', function () {
let src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'
const src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('543')
})
+20 -20
View File
@@ -5,8 +5,8 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/if', function () {
let liquid = Liquid()
let ctx = {
const liquid = Liquid()
const ctx = {
one: 1,
two: 2,
emptyString: '',
@@ -14,101 +14,101 @@ describe('tags/if', function () {
}
it('should throw if not closed', function () {
let src = '{% if false%}yes'
const src = '{% if false%}yes'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/tag {% if false%} not closed/)
})
it('should support nested', function () {
let src = '{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}'
const src = '{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('')
})
describe('single value as condition', function () {
it('should support boolean', function () {
let src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
const src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('2')
})
it('should treat Array truthy', function () {
let src = '{%if emptyArray%}a{%endif%}'
const src = '{%if emptyArray%}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('a')
})
it('should return true if empty string', function () {
let src = '{%if emptyString%}a{%endif%}'
const src = '{%if emptyString%}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('a')
})
})
describe('expression as condition', function () {
it('should support ==', function () {
let src = '{% if 2==3 %}yes{%else%}no{%endif%}'
const src = '{% if 2==3 %}yes{%else%}no{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should support >=', function () {
let src = '{% if 1>=2 and one<two %}a{%endif%}'
const src = '{% if 1>=2 and one<two %}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('')
})
it('should support !=', function () {
let src = '{% if one!=two %}yes{%else%}no{%endif%}'
const src = '{% if one!=two %}yes{%else%}no{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('yes')
})
it('should support value and expression', function () {
let src = `X{%if version and version != '' %}x{{version}}y{%endif%}Y`
let ctx = { 'version': '' }
const src = `X{%if version and version != '' %}x{{version}}y{%endif%}Y`
const ctx = { 'version': '' }
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('XY')
})
})
describe('comparasion to null', function () {
it('should evaluate false for null < 10', function () {
let src = '{% if null < 10 %}yes{% else %}no{% endif %}'
const src = '{% if null < 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for null > 10', function () {
let src = '{% if null > 10 %}yes{% else %}no{% endif %}'
const src = '{% if null > 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for null <= 10', function () {
let src = '{% if null <= 10 %}yes{% else %}no{% endif %}'
const src = '{% if null <= 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for null >= 10', function () {
let src = '{% if null >= 10 %}yes{% else %}no{% endif %}'
const src = '{% if null >= 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for 10 < null', function () {
let src = '{% if 10 < null %}yes{% else %}no{% endif %}'
const src = '{% if 10 < null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for 10 > null', function () {
let src = '{% if 10 > null %}yes{% else %}no{% endif %}'
const src = '{% if 10 > null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for 10 <= null', function () {
let src = '{% if 10 <= null %}yes{% else %}no{% endif %}'
const src = '{% if 10 <= null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
it('should evaluate false for 10 >= null', function () {
let src = '{% if 10 >= null %}yes{% else %}no{% endif %}'
const src = '{% if 10 >= null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
})
+5 -5
View File
@@ -95,7 +95,7 @@ describe('tags/include', function () {
'/card.html': '<p>{{person.firstName}} {{person.lastName}}<br/>{% include "address" %}</p>',
'/address.html': 'City: {{person.address.city}}'
})
let ctx = {
const ctx = {
person: {
firstName: 'Joe',
lastName: 'Shmoe',
@@ -114,7 +114,7 @@ describe('tags/include', function () {
'/parent.html': 'X{% include child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
let staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
const staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('Xchild with redY')
})
@@ -124,7 +124,7 @@ describe('tags/include', function () {
'/parent.html': 'X{% include bar/./../foo/child.html %}Y',
'/foo/child.html': 'child'
})
let staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
const staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('XchildY')
})
@@ -134,7 +134,7 @@ describe('tags/include', function () {
'/parent.html': 'X{% include foo/child.html %}Y',
'/foo/child.html': 'child'
})
let staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
const staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('XchildY')
})
@@ -144,7 +144,7 @@ describe('tags/include', function () {
'/parent.html': 'X{% include child.html, color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
let staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
const staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('Xchild with redY')
})
+8 -8
View File
@@ -5,17 +5,17 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/increment', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should increment undefined variable', function () {
let src = '{% increment one %}{% increment one %}{% increment one %}'
const src = '{% increment one %}{% increment one %}{% increment one %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
})
it('should increment defined variable', function () {
let src = '{% increment one %}{% increment one %}{% increment one %}'
let ctx = {one: 7}
const src = '{% increment one %}{% increment one %}{% increment one %}'
const ctx = {one: 7}
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('789')
@@ -24,25 +24,25 @@ describe('tags/increment', function () {
})
it('should be independent from assign', function () {
let src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %}'
const src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
})
it('should be independent from capture', function () {
let src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %}'
const src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
})
it('should not shading assign', function () {
let src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
const src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012 10')
})
it('should not shading capture', function () {
let src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
const src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012 10')
})
+8 -8
View File
@@ -21,7 +21,7 @@ describe('tags/layout', function () {
mock({
'/parent.html': 'parent'
})
let src = '{% layout "parent" %}{%block%}A'
const src = '{% layout "parent" %}{%block%}A'
return expect(liquid.parseAndRender(src)).to
.be.rejectedWith(/tag {%block%} not closed/)
})
@@ -39,7 +39,7 @@ describe('tags/layout', function () {
mock({
'/parent.html': 'X{%block%}{%endblock%}Y'
})
let src = '{% layout "parent.html" %}{%block%}A{%endblock%}'
const src = '{% layout "parent.html" %}{%block%}A{%endblock%}'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XAY')
})
@@ -47,7 +47,7 @@ describe('tags/layout', function () {
mock({
'/parent.html': 'X{%block%}{%endblock%}Y'
})
let src = '{% layout "parent.html" %}A'
const src = '{% layout "parent.html" %}A'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XAY')
})
@@ -56,7 +56,7 @@ describe('tags/layout', function () {
mock({
'/parent.html': 'X{% block "a"%}{% endblock %}Y{% block b%}{%endblock%}Z'
})
let src = '{% layout "parent.html" %}' +
const src = '{% layout "parent.html" %}' +
'{%block a%}A{%endblock%}' +
'{%block b%}B{%endblock%}'
return expect(liquid.parseAndRender(src)).to
@@ -66,7 +66,7 @@ describe('tags/layout', function () {
mock({
'/parent.html': 'X{% block "a"%}A{% endblock %}Y{% block b%}B{%endblock%}Z'
})
let src = '{% layout "parent.html" %}{%block a%}a{%endblock%}'
const src = '{% layout "parent.html" %}{%block a%}a{%endblock%}'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XaYBZ')
})
@@ -113,7 +113,7 @@ describe('tags/layout', function () {
'/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
})
let staticLiquid = Liquid({ root: '/', dynamicPartials: false })
const staticLiquid = Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
})
@@ -123,7 +123,7 @@ describe('tags/layout', function () {
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout bar/../foo/parent.html color:"black"%}{%block%}A{%endblock%}'
})
let staticLiquid = Liquid({ root: '/', dynamicPartials: false })
const staticLiquid = Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
})
@@ -133,7 +133,7 @@ describe('tags/layout', function () {
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout foo/parent.html color:"black"%}{%block%}A{%endblock%}'
})
let staticLiquid = Liquid({ root: '/', dynamicPartials: false })
const staticLiquid = Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
})
+11 -13
View File
@@ -5,21 +5,19 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/raw', function () {
let liquid = Liquid()
it('should support raw 1', function () {
return expect(liquid.parseAndRender('{% raw%}'))
.to.be.rejectedWith(/{% raw%} not closed/)
const liquid = Liquid()
it('should support raw 1', async function () {
const p = liquid.parseAndRender('{% raw%}')
return expect(p).be.rejectedWith(/{% raw%} not closed/)
})
it('should support raw 2', function () {
let src = '{% raw %}{{ 5 | plus: 6 }}{% endraw %} is equal to 11.'
let dst = '{{ 5 | plus: 6 }} is equal to 11.'
return expect(liquid.parseAndRender(src))
.to.eventually.equal(dst)
it('should support raw 2', async function () {
const src = '{% raw %}{{ 5 | plus: 6 }}{% endraw %} is equal to 11.'
const dst = '{{ 5 | plus: 6 }} is equal to 11.'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support raw 3', function () {
let src = '{% raw %}\n{{ foo}} \n{% endraw %}'
let dst = '\n{{ foo}} \n'
return expect(liquid.parseAndRender(src))
.to.eventually.equal(dst)
const src = '{% raw %}\n{{ foo}} \n{% endraw %}'
const dst = '\n{{ foo}} \n'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
})
+19 -19
View File
@@ -5,52 +5,52 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/tablerow', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should support tablerow', function () {
let src = '{% tablerow i in (1..3)%}{{ i }}{% endtablerow %}'
let dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
const src = '{% tablerow i in (1..3)%}{{ i }}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support cols', function () {
let src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
let ctx = {
const src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
const ctx = {
alpha: ['a', 'b', 'c']
}
let dst =
const dst =
'<tr class="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
'<tr class="row2"><td class="col1">c</td></tr>'
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst)
})
it('should support cols set to 0', function () {
let src = '{% tablerow i in (1..3) cols:0 %}{{ i }}{% endtablerow %}'
let dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
const src = '{% tablerow i in (1..3) cols:0 %}{{ i }}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support empty tablerow', function () {
let src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}'
let dst = ''
const src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}'
const dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support empty array', function () {
let src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
let dst = ''
const src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
const dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should throw when tablerow not closed', function () {
let src = '{% tablerow i in (1..0) cols:2 %}{{ i }}'
const src = '{% tablerow i in (1..0) cols:2 %}{{ i }}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/tag .* not closed/)
})
it('should support tablerow with range', function () {
let src = '{% tablerow i in (1..5) cols:2 %}{{ i }}{% endtablerow %}'
let dst =
const src = '{% tablerow i in (1..5) cols:2 %}{{ i }}{% endtablerow %}'
const dst =
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
'<tr class="row3"><td class="col1">5</td></tr>'
@@ -58,16 +58,16 @@ describe('tags/tablerow', function () {
})
it('should support tablerow with limit', function () {
let src = '{% tablerow i in (1..5) cols:2 limit:3 %}{{ i }}{% endtablerow %}'
let dst =
const src = '{% tablerow i in (1..5) cols:2 limit:3 %}{{ i }}{% endtablerow %}'
const dst =
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support tablerow with offset', function () {
let src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
let dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
const src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
})
+6 -6
View File
@@ -5,31 +5,31 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/unless', function () {
let liquid = Liquid()
const liquid = Liquid()
it('should render else when predicate yields true', function () {
// 0 is truthy
let src = '{% unless 0 %}yes{%else%}no{%endunless%}'
const src = '{% unless 0 %}yes{%else%}no{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('no')
})
it('should render unless when predicate yields false', function () {
let src = '{% unless false %}yes{%else%}no{%endunless%}'
const src = '{% unless false %}yes{%else%}no{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('yes')
})
it('should reject when tag not closed', function () {
let src = '{% unless 1>2 %}yes'
const src = '{% unless 1>2 %}yes'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/tag {% unless 1>2 %} not closed/)
})
it('should render unless when predicate yields false and else undefined', function () {
let src = '{% unless 1>2 %}yes{%endunless%}'
const src = '{% unless 1>2 %}yes{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('yes')
})
it('should render "" when predicate yields false and else undefined', function () {
let src = '{% unless 1<2 %}yes{%endunless%}'
const src = '{% unless 1<2 %}yes{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
})
+14 -14
View File
@@ -5,8 +5,8 @@ const expect = chai.expect
describe('tokenizer', function () {
describe('parse', function () {
it('should handle plain HTML', function () {
let html = '<html><body><p>Lorem Ipsum</p></body></html>'
let tokens = parse(html)
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
const tokens = parse(html)
expect(tokens.length).to.equal(1)
expect(tokens[0].value).to.equal(html)
@@ -18,24 +18,24 @@ describe('tokenizer', function () {
}).to.throw('illegal input')
})
it('should handle tag syntax', function () {
let html = '<p>{% for p in a[1]%}</p>'
let tokens = parse(html)
const html = '<p>{% for p in a[1]%}</p>'
const tokens = parse(html)
expect(tokens.length).to.equal(3)
expect(tokens[1].type).to.equal('tag')
expect(tokens[1].value).to.equal('for p in a[1]')
})
it('should handle value syntax', function () {
let html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
let tokens = parse(html)
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
const tokens = parse(html)
expect(tokens.length).to.equal(3)
expect(tokens[1].type).to.equal('value')
expect(tokens[1].value).to.equal('foo | date: "%Y-%m-%d"')
})
it('should handle consecutive value and tags', function () {
let html = '{{foo}}{{bar}}{%foo%}{%bar%}'
let tokens = parse(html)
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
const tokens = parse(html)
expect(tokens.length).to.equal(4)
expect(tokens[0].type).to.equal('value')
@@ -45,8 +45,8 @@ describe('tokenizer', function () {
expect(tokens[2].value).to.equal('foo')
})
it('should keep white spaces and newlines', function () {
let html = '{%foo%}\n{%bar %} \n {%alice%}'
let tokens = parse(html)
const html = '{%foo%}\n{%bar %} \n {%alice%}'
const tokens = parse(html)
expect(tokens.length).to.equal(5)
expect(tokens[1].type).to.equal('html')
expect(tokens[1].raw).to.equal('\n')
@@ -54,16 +54,16 @@ describe('tokenizer', function () {
expect(tokens[3].raw).to.equal(' \n ')
})
it('should handle multiple lines tag', function () {
let html = '{%foo\na:a\nb:1.23\n%}'
let tokens = parse(html)
const html = '{%foo\na:a\nb:1.23\n%}'
const tokens = parse(html)
expect(tokens.length).to.equal(1)
expect(tokens[0].type).to.equal('tag')
expect(tokens[0].args).to.equal('a:a\nb:1.23')
expect(tokens[0].raw).to.equal('{%foo\na:a\nb:1.23\n%}')
})
it('should handle multiple lines value', function () {
let html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
let tokens = parse(html)
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
const tokens = parse(html)
expect(tokens.length).to.equal(1)
expect(tokens[0].type).to.equal('value')
expect(tokens[0].raw).to.equal('{{foo\n|date:\n"%Y-%m-%d"\n}}')
+6 -5
View File
@@ -1,18 +1,19 @@
const chai = require('chai')
import chai from 'chai'
import assert from '../../src/util/assert.js'
const expect = chai.expect
const assert = require('../../src/util/assert.js')
describe('assert', function () {
it('should not throw if predicate is truthy', function () {
let fn = () => assert('foo', 'bar')
const fn = () => assert('foo', 'bar')
expect(fn).to.not.throw()
})
it('should not throw if predicate is truthy', function () {
let fn = () => assert('', 'bar')
const fn = () => assert('', 'bar')
expect(fn).to.throw(/bar/)
})
it('should populate default message', function () {
let fn = () => assert(false)
const fn = () => assert(false)
expect(fn).to.throw(/expect false to be true/)
})
})
+150 -236
View File
@@ -7,7 +7,7 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
let engine = Liquid()
let strictEngine = Liquid({
const strictEngine = Liquid({
strict_variables: true,
strict_filters: true
})
@@ -18,80 +18,59 @@ describe('error', function () {
})
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 throw TokenizationError when tag illegal', async function () {
const err = await expect(engine.parseAndRender('{% . a %}', {})).be.rejected
expect(err.name).to.equal('TokenizationError')
expect(err.message).to.contain('illegal tag syntax')
})
it('should contain template content in err.message', function () {
let html = ['1st', '2nd', 'X{% . a %} Y', '4th']
let message = [
it('should contain template content in err.message', async function () {
const html = ['1st', '2nd', 'X{% . a %} Y', '4th']
const message = [
' 1| 1st',
' 2| 2nd',
'>> 3| X{% . a %} Y',
' 4| 4th',
'TokenizationError'
]
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')
})
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
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 () {
let 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 the whole template content in err.input', async function () {
const html = 'bar\nfoo{% . a %}\nfoo'
const err = await expect(engine.parseAndRender(html)).be.rejected
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 line number in err.line', async function () {
const err = await expect(engine.parseAndRender('1\n2\n{% . a %}\n4')).be.rejected
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.message).to.contain('illegal tag syntax')
expect(err.stack).to.contain('at Object.parse')
})
it('should contain stack in err.stack', async function () {
const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected
expect(err.message).to.contain('illegal tag syntax')
expect(err.stack).to.contain('at Object.parse')
})
describe('captureStackTrace compatibility', function () {
let captureStackTrace = Error.captureStackTrace
const captureStackTrace = Error.captureStackTrace
before(() => (Error.captureStackTrace = null))
after(() => (Error.captureStackTrace = captureStackTrace))
it('should use empty string if captureStackTrace not defined', 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.not.contain('at Object.parse')
})
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', function () {
let html = '<html>\n<head>\n\n{% . a %}\n\n'
it('should contain file path in err.file', async function () {
const 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(path.resolve('/foo.html'))
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
expect(err.name).to.equal('TokenizationError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
})
@@ -106,55 +85,43 @@ describe('error', function () {
}
})
engine.registerTag('rejectingTag', {
render: function () {
return Promise.reject(new Error('intended render reject'))
render: async function () {
throw new Error('intended render reject')
}
})
engine.registerFilter('throwingFilter', () => {
throw new Error('throwed by filter')
})
})
it('should throw RenderError when tag throws', function () {
let 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 throws', async function () {
const src = '{%throwingTag%}'
const err = await expect(engine.parseAndRender(src)).be.rejected
expect(err.name).to.equal('RenderError')
expect(err.message).to.contain('intended render error')
})
it('should throw RenderError when tag rejects', function () {
let 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 tag rejects', async function () {
const src = '{%rejectingTag%}'
const err = await expect(engine.parseAndRender(src)).be.rejected
expect(err.name).to.equal('RenderError')
expect(err.message).to.contain('intended render reject')
})
it('should throw RenderError when filter throws', function () {
let 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 throw RenderError when filter throws', async function () {
const src = '{{1|throwingFilter}}'
const err = await expect(engine.parseAndRender(src)).be.rejected
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 throw RenderError when variable not defined', async function () {
const err = await expect(strictEngine.parseAndRender('{{a}}')).be.rejected
expect(err).to.have.property('name', 'RenderError')
expect(err.message).to.contain('undefined variable: a')
})
it('should contain template context in err.stack', function () {
let html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
let message = [
it('should contain template context in err.stack', async function () {
const html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
const message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
@@ -163,15 +130,12 @@ describe('error', function () {
' 7| 7th',
'RenderError'
]
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')
})
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
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 () {
it('should contain original error info for {% layout %}', async function () {
mock({
'/throwing-tag.html': [
'1st',
@@ -183,8 +147,8 @@ describe('error', function () {
'7th'
].join('\n')
})
let html = '{%layout "throwing-tag.html"%}'
let message = [
const html = '{%layout "throwing-tag.html"%}'
const message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
@@ -193,23 +157,20 @@ describe('error', function () {
' 7| 7th',
'RenderError'
]
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:${path.resolve('/throwing-tag.html')}, line:4`)
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
const err = await expect(engine.parseAndRender(html)).be.rejected
console.log(err.message)
console.log(err.stack)
expect(err.message).to.equal(`intended render error, file:${path.resolve('/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 () {
let origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
it('should contain original error info for {% include %}', async function () {
const origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
mock({
'/throwing-tag.html': origin.join('\n')
})
let html = '{%include "throwing-tag.html"%}'
let message = [
const html = '{%include "throwing-tag.html"%}'
const message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{%throwingTag%} Y',
@@ -218,54 +179,39 @@ describe('error', function () {
' 7| 7th',
'RenderError'
]
return expect(engine.parseAndRender(html)).to.eventually
.be.rejected
.then(function (err) {
expect(err.message).to.equal(`intended render error, file:${path.resolve('/throwing-tag.html')}, line:4`)
expect(err.stack).to.contain(message.join('\n'))
expect(err.name).to.equal('RenderError')
})
const err = await expect(engine.parseAndRender(html)).be.rejected
expect(err.message).to.equal(`intended render error, file:${path.resolve('/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 () {
let 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 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.line', function () {
let 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 line number in err.line', async function () {
const src = '1\n2\n{{1|throwingFilter}}\n4'
const err = await expect(engine.parseAndRender(src)).be.rejected
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.message).to.contain('intended render reject')
expect(err.stack).to.match(/at .*:\d+:\d+/)
})
it('should contain stack in err.stack', async function () {
const err = await expect(engine.parseAndRender('{%rejectingTag%}')).be.rejected
expect(err.message).to.contain('intended render reject')
expect(err.stack).to.match(/at .*:\d+:\d+/)
})
it('should contain file path in err.file', function () {
let html = '<html>\n<head>\n\n{% throwingTag %}\n\n'
it('should contain file path in err.file', async function () {
const 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()
console.log(err, err.name)
expect(err.name).to.equal('RenderError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
console.log(err, err.name)
expect(err.name).to.equal('RenderError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
})
@@ -278,53 +224,36 @@ describe('error', function () {
}
})
})
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 RenderError when filter not defined', async function () {
const err = await expect(strictEngine.parseAndRender('{{1 | a}}')).be.rejected
expect(err).to.have.property('name', 'ParseError')
expect(err.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 not closed', async function () {
const err = await expect(engine.parseAndRender('{% if %}')).be.rejected
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('tag {% if %} not closed')
})
it('should throw ParseError when tag parse throws', function () {
let 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 parse throws', async function () {
const err = await expect(engine.parseAndRender('{%throwsOnParse%}')).be.rejected
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('intended parse error')
})
it('should throw ParseError when tag not found', function () {
let 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')
})
it('should throw ParseError when tag not found', async function () {
const src = '{%if true%}\naaa{%endif%}\n{% -a %}\n3'
const err = await expect(engine.parseAndRender(src)).be.rejected
expect(err.name).to.equal('ParseError')
expect(err.message).to.contain('tag -a not found')
})
it('should throw ParseError when tag not exist', async function () {
const err = await expect(engine.parseAndRender('{% a %}')).be.rejected
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
.be.rejected
.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 () {
let html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th']
let message = [
it('should contain template context in err.stack', async function () {
const html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th']
const message = [
' 2| 2nd',
' 3| 3rd',
'>> 4| X{% a %} {% enda %} Y',
@@ -333,62 +262,47 @@ describe('error', function () {
' 7| 7th',
'ParseError: 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')
})
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
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 () {
let html = ['1st', 'X{% a %} {% enda %} Y', '3rd', '4th']
let message = [
it('should handle err.message when context not enough', async function () {
const html = ['1st', 'X{% a %} {% enda %} Y', '3rd', '4th']
const message = [
' 1| 1st',
'>> 2| X{% a %} {% enda %} Y',
' 3| 3rd',
' 4| 4th',
'ParseError: 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'))
})
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
expect(err.message).to.equal('tag a not found, line:2')
expect(err.stack).to.contain(message.join('\n'))
})
it('should contain line number in err.line', function () {
let 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)
})
it('should contain line number in err.line', async function () {
const html = '<html>\n<head>\n\n{% raw %}\n\n'
const err = await expect(engine.parseAndRender(html)).be.rejected
expect(err.line).to.equal(4)
})
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('ParseError: tag -a not found')
expect(err.stack).to.match(/at .*:\d+:\d+\)/)
})
it('should contain stack in err.stack', async function () {
const err = await expect(engine.parseAndRender('{% -a %}')).be.rejected
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', function () {
let html = '<html>\n<head>\n\n{% raw %}\n\n'
it('should contain file path in err.file', async function () {
const 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(path.resolve('/foo.html'))
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
expect(err.name).to.equal('ParseError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
})
})
+11 -11
View File
@@ -4,13 +4,13 @@ const expect = chai.expect
chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
let P = require('../../src/util/promise.js')
const P = require('../../src/util/promise.js')
describe('util/promise', function () {
describe('.anySeries()', function () {
it('should resolve in series', function () {
let spy1 = sinon.spy()
let spy2 = sinon.spy()
const spy1 = sinon.spy()
const spy2 = sinon.spy()
return P
.anySeries(
['first', 'second'],
@@ -28,17 +28,17 @@ describe('util/promise', function () {
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should reject when all rejected', function () {
let p = P.anySeries(['first', 'second', 'third'],
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', () => {
let p = P.anySeries(['first', 'second'],
const 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', () => {
let spy = sinon.spy()
const spy = sinon.spy()
return P
.anySeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
@@ -51,18 +51,18 @@ describe('util/promise', function () {
})
describe('.mapSeries()', function () {
it('should resolve when all resolved', function () {
let p = P.mapSeries(['first', 'second', 'third'],
const 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', () => {
let p = P.mapSeries(['first', 'second'],
const p = P.mapSeries(['first', 'second'],
item => Promise.reject(item))
return expect(p).to.rejectedWith('first')
})
it('should resolve in series', function () {
let spy1 = sinon.spy()
let spy2 = sinon.spy()
const spy1 = sinon.spy()
const spy2 = sinon.spy()
return P
.mapSeries(
['first', 'second'],
@@ -80,7 +80,7 @@ describe('util/promise', function () {
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should not call rest of callbacks once rejected', () => {
let spy = sinon.spy()
const spy = sinon.spy()
return P
.mapSeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
+13 -13
View File
@@ -1,7 +1,7 @@
const chai = require('chai')
const expect = chai.expect
import chai from 'chai'
import t from '../../src/util/strftime.js'
let t = require('../../src/util/strftime.js')
const expect = chai.expect
describe('util/strftime', function () {
let now
@@ -37,7 +37,7 @@ describe('util/strftime', function () {
expect(t(now, '%I')).to.equal('01')
})
it('should format %I as 12 for 00:00', function () {
let date = new Date('2016-01-01T00:00:00.000Z')
const date = new Date('2016-01-01T00:00:00.000Z')
expect(t(date, '%I')).to.equal('12')
})
describe('%j', function () {
@@ -45,11 +45,11 @@ describe('util/strftime', function () {
expect(t(then, '%j')).to.equal('066')
})
it('should take count of leap years', function () {
let date = new Date('2001-03-01')
const date = new Date('2001-03-01')
expect(t(date, '%j')).to.equal('060')
})
it('should take count of leap years', function () {
let date = new Date('2000-03-01')
const date = new Date('2000-03-01')
expect(t(date, '%j')).to.equal('061')
})
})
@@ -60,7 +60,7 @@ describe('util/strftime', function () {
expect(t(now, '%l')).to.equal(' 1')
})
it('should format %l as 12 for 00:00', function () {
let date = new Date('2016-01-01T00:00:00.000Z')
const date = new Date('2016-01-01T00:00:00.000Z')
expect(t(date, '%l')).to.equal('12')
})
it('should format %L as 0 padded millisecond', function () {
@@ -75,9 +75,9 @@ describe('util/strftime', function () {
expect(t(then, '%P')).to.equal('am')
})
it('should format %q as date suffix', function () {
let st = new Date('2016-03-01T03:05:03.000Z')
let nd = new Date('2016-03-02T03:05:03.000Z')
let rd = new Date('2016-03-03T03:05:03.000Z')
const st = new Date('2016-03-01T03:05:03.000Z')
const nd = new Date('2016-03-02T03:05:03.000Z')
const rd = new Date('2016-03-03T03:05:03.000Z')
expect(t(st, '%q')).to.equal('st')
expect(t(nd, '%q')).to.equal('nd')
expect(t(rd, '%q')).to.equal('rd')
@@ -113,7 +113,7 @@ describe('util/strftime', function () {
expect(t(now, '%z')).to.equal('+0800')
})
it('should format %z as negative time zone', function () {
let date = new Date('2016-01-04T13:15:23.000Z')
const date = new Date('2016-01-04T13:15:23.000Z')
date.getTimezoneOffset = () => 480
expect(t(date, '%z')).to.equal('-0800')
})
@@ -126,7 +126,7 @@ describe('util/strftime', function () {
})
function mockUTC () {
let p = Date.prototype
const p = Date.prototype
p._getHours = p.getHours
p.getHours = p.getUTCHours
@@ -139,7 +139,7 @@ function mockUTC () {
}
function restoreUTC () {
let p = Date.prototype
const p = Date.prototype
p.getHours = p._getHours
p.getDays = p._getDays
p.getTimezoneOffset = p._getTimezoneOffset
+12 -11
View File
@@ -1,10 +1,11 @@
import chai from 'chai'
import sinonChai from 'sinon-chai'
import sinon from 'sinon'
import {RenderError, RenderBreakError} from '../../src/util/error.js'
import _ from '../../src/util/underscore.js'
import * as _ from '../../src/util/underscore.js'
const expect = chai.expect
chai.use(require('sinon-chai'))
chai.use(sinonChai)
describe('util/underscore', function () {
describe('.isError()', function () {
@@ -12,7 +13,7 @@ describe('util/underscore', function () {
expect(_.isError(new Error())).to.be.true
})
it('should return true for RenderError', function () {
let tpl = {
const tpl = {
token: {
input: 'xx'
}
@@ -53,21 +54,21 @@ describe('util/underscore', function () {
})
describe('.forOwn()', function () {
it('should iterate all properties', function () {
let spy = sinon.spy()
let obj = {
const spy = sinon.spy()
const obj = {
foo: 'bar'
}
_.forOwn(obj, spy)
expect(spy).to.have.been.calledWith('bar', 'foo', obj)
})
it('should default to empty object', function () {
let spy = sinon.spy()
const spy = sinon.spy()
_.forOwn(undefined, spy)
expect(spy).to.have.not.been.called
})
it('should not iterate over properties on prototype', function () {
let spy = sinon.spy()
let obj = Object.create({
const spy = sinon.spy()
const obj = Object.create({
bar: 'foo'
})
obj.foo = 'bar'
@@ -76,7 +77,7 @@ describe('util/underscore', function () {
expect(spy).to.have.been.calledWith('bar', 'foo', obj)
})
it('should break when returned false', function () {
let spy = sinon.stub().returns(false)
const spy = sinon.stub().returns(false)
_.forOwn({
'foo': 'foo',
'bar': 'foo'
@@ -101,11 +102,11 @@ describe('util/underscore', function () {
})
})
it('should assign 2 objects', function () {
let src = {
const src = {
foo: 'foo',
bar: 'bar'
}
let dst = {
const dst = {
foo: 'bar',
kaa: 'kaa'
}
+10 -12
View File
@@ -1,4 +1,4 @@
import {resolve} from '../../src/util/url.js'
import {extname, resolve} from '../../src/util/url.js'
import chai from 'chai'
const expect = chai.expect
@@ -22,23 +22,23 @@ describe('util/url', function () {
})
describe('resolve', function () {
describe('root', function () {
it('should support width relative path', function () {
it('should support relative root', function () {
expect(resolve('./views', 'foo'))
.to.equal('https://example.com/foo/bar/views/foo')
expect(resolve('./views/', 'foo'))
.to.equal('https://example.com/foo/bar/views/foo')
})
it('should support width absolute path', function () {
it('should support absolute root', function () {
expect(resolve('/views', 'foo'))
.to.equal('https://example.com/views/foo')
expect(resolve('/views/', 'foo'))
.to.equal('https://example.com/views/foo')
})
it('should support with empty', function () {
it('should support empty root', function () {
expect(resolve('', 'page.html'))
.to.equal('https://example.com/foo/bar/page.html')
})
it('should support with url', function () {
it('should support full url as root', function () {
expect(resolve('https://example.com/views', 'page.html'))
.to.equal('https://example.com/views/page.html')
expect(resolve('https://example.com/views/', 'page.html'))
@@ -51,14 +51,12 @@ describe('util/url', function () {
.to.equal('https://example.com/views/page.html')
})
})
describe('path', function () {
it('should support width relative path', function () {
expect(resolve('./views/', 'page.html'))
.to.equal('https://example.com/foo/bar/views/page.html')
describe('extname', function () {
it('should support relative path', function () {
expect(extname('./views/page.html')).to.equal('.html')
})
it('should support with absolute path', function () {
expect(resolve('/views/', '/page.html'))
.to.equal('https://example.com/page.html')
it('should support absolute path', function () {
expect(extname('/views/page.xml')).to.equal('.xml')
})
})
})