chore(TypeScript): fix linting and generate .d.ts

This commit is contained in:
harttle
2019-02-17 18:20:04 +08:00
parent 7ee87008c7
commit fc9ebdb90f
79 changed files with 809 additions and 820 deletions
+12
View File
@@ -0,0 +1,12 @@
{
"env": {
"mocha": true
},
"plugins": [
"mocha"
],
"rules": {
"no-unused-expressions": "off",
"no-new": "off"
}
}
+3 -6
View File
@@ -1,12 +1,9 @@
var chai = require('chai')
var Liquid = require('../..')
var expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
import Liquid from '../..'
describe('.evalValue()', function () {
var engine
beforeEach(() => engine = new Liquid())
beforeEach(() => { engine = new Liquid() })
it('should throw when scope undefined', function () {
expect(() => engine.evalValue('{{"foo"}}')).to.throw(/scope undefined/)
+1 -5
View File
@@ -1,12 +1,8 @@
import * as chai from 'chai'
import { expect } from 'chai'
import * as request from 'supertest'
import * as express from 'express'
import * as mock from 'mock-fs'
import Liquid from '../../dist/liquid.common.js'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('express()', function () {
var app, engine
+29 -23
View File
@@ -1,8 +1,8 @@
var chai = require('chai')
var Liquid = require('../..')
var expect = chai.expect
import Liquid from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('.parseAndRender()', function () {
var engine, strictEngine
@@ -12,19 +12,23 @@ describe('.parseAndRender()', function () {
strict_filters: true
})
})
it('should stringify object', function () {
it('should stringify object', async function () {
var ctx = { obj: { foo: 'bar' } }
return expect(engine.parseAndRender('{{obj}}', ctx)).to.eventually.equal('{"foo":"bar"}')
const html = await engine.parseAndRender('{{obj}}', ctx)
return expect(html).to.equal('{"foo":"bar"}')
})
it('should stringify array ', function () {
it('should stringify array ', async function () {
var ctx = { arr: [-2, 'a'] }
return expect(engine.parseAndRender('{{arr}}', ctx)).to.eventually.equal('[-2,"a"]')
const html = await engine.parseAndRender('{{arr}}', ctx)
return expect(html).to.equal('[-2,"a"]')
})
it('should render undefined as empty', function () {
return expect(engine.parseAndRender('foo{{zzz}}bar', {})).to.eventually.equal('foobar')
it('should render undefined as empty', async function () {
const html = await engine.parseAndRender('foo{{zzz}}bar', {})
return expect(html).to.equal('foobar')
})
it('should render as null when filter undefined', function () {
return expect(engine.parseAndRender('{{"foo" | filter1}}', {})).to.eventually.equal('foo')
it('should render as null when filter undefined', async function () {
const html = await engine.parseAndRender('{{"foo" | filter1}}', {})
return expect(html).to.equal('foo')
})
it('should throw upon undefined filter when strict_filters set', function () {
return expect(strictEngine.parseAndRender('{{"foo" | filter1}}', {})).to
@@ -38,22 +42,24 @@ describe('.parseAndRender()', function () {
engine.parse('<html><head>{{obj}}</head></html>')
}).to.not.throw()
})
it('should render template multiple times', function () {
var ctx = { obj: { foo: 'bar' } }
var 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 template multiple times', async function () {
const ctx = { obj: { foo: 'bar' } }
const template = engine.parse('{{obj}}')
const result = await engine.render(template, ctx)
expect(result).to.equal('{"foo":"bar"}')
const result2 = await engine.render(template, ctx)
expect(result2).to.equal('{"foo":"bar"}')
})
it('should render filters', function () {
it('should render filters', async function () {
var ctx = { names: ['alice', 'bob'] }
var template = engine.parse('<p>{{names | join: ","}}</p>')
return expect(engine.render(template, ctx)).to.eventually.equal('<p>alice,bob</p>')
const html = await engine.render(template, ctx)
return expect(html).to.equal('<p>alice,bob</p>')
})
it('should render accessive filters', function () {
it('should render accessive filters', async function () {
var src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
'{{ my_array | first }}'
return expect(engine.parseAndRender(src)).to.eventually.equal('apples')
const html = await engine.parseAndRender(src)
return expect(html).to.equal('apples')
})
})
+24 -26
View File
@@ -1,9 +1,6 @@
var chai = require('chai')
var mock = require('mock-fs')
var Liquid = require('../..')
var expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../..'
describe('#renderFile()', function () {
var engine
@@ -24,28 +21,28 @@ describe('#renderFile()', function () {
afterEach(function () {
mock.restore()
})
it('should render file', function () {
return expect(engine.renderFile('/root/files/foo.html', {}))
.to.eventually.equal('foo')
it('should render file', async function () {
const html = await engine.renderFile('/root/files/foo.html', {})
return expect(html).to.equal('foo')
})
it('should find files without extname', function () {
it('should find files without extname', async function () {
var engine = new Liquid({ root: '/root' })
return expect(engine.renderFile('/root/files/bar', {}))
.to.eventually.equal('bar')
const html = await engine.renderFile('/root/files/bar', {})
return expect(html).to.equal('bar')
})
it('should accept relative path', function () {
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo')
it('should accept relative path', async function () {
const html = await engine.renderFile('files/foo.html')
return expect(html).to.equal('foo')
})
it('should resolve array as root', function () {
it('should resolve array as root', async function () {
engine = new Liquid({
root: ['/boo', '/root/'],
extname: '.html'
})
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo')
const html = await engine.renderFile('files/foo.html')
return expect(html).to.equal('foo')
})
it('should default root to cwd', function () {
it('should default root to cwd', async function () {
var files = {}
files[process.cwd() + '/foo.html'] = 'FOO'
mock(files)
@@ -53,15 +50,16 @@ describe('#renderFile()', function () {
engine = new Liquid({
extname: '.html'
})
return expect(engine.renderFile('foo.html'))
.to.eventually.equal('FOO')
const html = await engine.renderFile('foo.html')
return expect(html).to.equal('FOO')
})
it('should render file with context', function () {
return expect(engine.renderFile('/root/files/name.html', { name: 'harttle' }))
.to.eventually.equal('My name is harttle.')
it('should render file with context', async function () {
const html = await engine.renderFile('/root/files/name.html', { name: 'harttle' })
return expect(html).to.equal('My name is harttle.')
})
it('should use default extname', function () {
return expect(engine.renderFile('files/name', { name: 'harttle' })).to.eventually.equal('My name is harttle.')
it('should use default extname', async function () {
const html = await engine.renderFile('files/name', { name: 'harttle' })
return expect(html).to.equal('My name is harttle.')
})
it('should throw with lookup list when file not exist', function () {
engine = new Liquid({
+5 -5
View File
@@ -1,10 +1,7 @@
import * as chai from 'chai'
import { expect } from 'chai'
import Liquid from '../..'
import * as chaiAsPromised from 'chai-as-promised'
const liquid = new Liquid()
const expect = chai.expect
chai.use(chaiAsPromised)
const cases = [
{
@@ -445,6 +442,9 @@ const cases = [
describe('Whitespace Control', function () {
cases.forEach(item => it(
item.text,
() => expect(liquid.parseAndRender(item.text)).to.eventually.equal(item.expected)
async () => {
const html = await liquid.parseAndRender(item.text)
expect(html).to.equal(item.expected)
}
))
})
+38 -42
View File
@@ -1,11 +1,10 @@
import Liquid from '../../dist/liquid.js'
import { createFakeServer, useFakeXMLHttpRequest } from 'sinon'
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { expect, use } from 'chai'
import { JSDOM } from 'jsdom'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
use(chaiAsPromised)
describe('xhr', () => {
if (+process.version.match(/^v(\d+)/)[1] < 8) {
@@ -24,7 +23,7 @@ describe('xhr', () => {
includeNodeLocations: true
});
(global as any).XMLHttpRequest = useFakeXMLHttpRequest();
(global as any).document = dom.window.document;
(global as any).document = dom.window.document
engine = new Liquid({
root: 'https://example.com/views/',
extname: '.html'
@@ -36,97 +35,94 @@ describe('xhr', () => {
delete (global as any).document
})
describe('#renderFile()', () => {
it('should support without extname', () => {
return expect(engine.renderFile('hello', { name: 'alice1' }))
.to.eventually.equal('hello alice1')
it('should support without extname', async () => {
const html = await engine.renderFile('hello', { name: 'alice1' })
return expect(html).to.equal('hello alice1')
})
it('should support with extname', () => {
return expect(engine.renderFile('hello.html', { name: 'alice2' }))
.to.eventually.equal('hello alice2')
it('should support with extname', async () => {
const html = await engine.renderFile('hello.html', { name: 'alice2' })
return expect(html).to.equal('hello alice2')
})
it('should support with absolute path', () => {
it('should support with absolute path', async () => {
server.respondWith('GET', 'https://example.com/foo.html',
[200, { 'Content-Type': 'text/plain' }, 'foo'])
return expect(engine.renderFile('/foo.html'))
.to.eventually.equal('foo')
const html = await engine.renderFile('/foo.html')
return expect(html).to.equal('foo')
})
it('should support with url', () => {
return expect(engine.renderFile('https://example.com/views/hello.html', { name: 'alice4' }))
.to.eventually.equal('hello alice4')
it('should support with url', async () => {
const html = await engine.renderFile('https://example.com/views/hello.html', { name: 'alice4' })
return expect(html).to.equal('hello alice4')
})
it('should support include', () => {
it('should support include', async () => {
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, "hello {% include 'name.html' %}"])
server.respondWith('GET', 'https://example.com/views/name.html',
[200, { 'Content-Type': 'text/plain' }, '{{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should throw 404', () => {
return expect(engine.renderFile('/not/exist.html'))
.to.be.rejectedWith('Not Found')
})
it('should throw error', function (done) {
engine.renderFile('hello.html')
.then(() => done('should not be resolved'))
.catch(function (e) {
expect(e.message).to.equal('An error occurred whilst receiving the response.')
done()
});
it('should throw error', function () {
const result = expect(engine.renderFile('hello.html'))
.to.be.rejectedWith('An error occurred whilst receiving the response.');
(global as any).XMLHttpRequest.onCreate = function (request) {
setTimeout(() => request.error())
}
return result
})
})
describe('#renderFile() with root specified', () => {
it('should support undefined root', () => {
it('should support undefined root', async () => {
engine = new Liquid({
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support empty root', () => {
it('should support empty root', async () => {
engine = new Liquid({
root: '',
extname: '.html'
})
server.respondWith('https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with relative path', () => {
it('should support with relative path', async () => {
engine = new Liquid({
root: './views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with absolute path', () => {
it('should support with absolute path', async () => {
engine = new Liquid({
root: '/views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with url', () => {
it('should support with url', async () => {
engine = new Liquid({
root: 'https://foo.com/bar/',
extname: '.html'
})
server.respondWith('GET', 'https://foo.com/bar/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
})
describe('cache options', () => {
+5 -8
View File
@@ -1,10 +1,6 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { expect } from 'chai'
import Liquid from '../../src/liquid'
chai.use(chaiAsPromised)
const expect = chai.expect
const ctx = {
date: new Date(),
foo: 'bar',
@@ -21,12 +17,13 @@ const ctx = {
}
let liquid
function test (src, dst) {
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst)
async function test (src, dst) {
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal(dst)
}
describe('filters', function () {
before(() => liquid = new Liquid())
before(() => { liquid = new Liquid() })
describe('abs', function () {
it('should return 3 for -3', () => test('{{ -3 | abs }}', '3'))
it('should return 2 for arr[0]', () => test('{{ arr[0] | abs }}', '2'))
+2 -2
View File
@@ -7,13 +7,13 @@ const expect = chai.expect
describe('Liquid', function () {
describe('#constructor()', function () {
it('should throw on illegal root', function () {
expect(() => new (Liquid as any)({root: {}})).to.throw(/illegal root/)
expect(() => new (Liquid as any)({ root: {} })).to.throw(/illegal root/)
})
})
describe('#plugin()', function () {
it('should call plugin on the instance', async function () {
const engine = new Liquid()
engine.plugin(function (Liquid) {
engine.plugin(function () {
this.registerFilter('foo', x => `foo${x}foo`)
})
const html = await engine.parseAndRender('{{"bar"|foo}}')
+1 -5
View File
@@ -1,10 +1,6 @@
import * as chai from 'chai'
import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../../../src/liquid'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('LiquidOptions#cache', function () {
let engine
+5 -8
View File
@@ -1,8 +1,5 @@
import Liquid from '../../../src/liquid'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
import Liquid from 'src/liquid'
import { expect } from 'chai'
describe('LiquidOptions#strict_*', function () {
let engine
@@ -13,9 +10,9 @@ describe('LiquidOptions#strict_*', function () {
extname: '.html'
})
})
it('should not throw when strict_variables false (default)', function () {
return expect(engine.parseAndRender('before{{notdefined}}after', ctx)).to
.eventually.equal('beforeafter')
it('should not throw when strict_variables false (default)', async function () {
const html = await engine.parseAndRender('before{{notdefined}}after', ctx)
return expect(html).to.equal('beforeafter')
})
it('should throw when strict_variables true', function () {
const tpl = engine.parse('before{{notdefined}}after')
+31 -33
View File
@@ -1,62 +1,58 @@
import * as chai from 'chai'
import { expect } from 'chai'
import Liquid from '../../../src/liquid'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('LiquidOptions#trimming', function () {
const ctx = { name: 'harttle' }
describe('tag trimming', function () {
it('should respect trim_tag_left', function () {
it('should respect trim_tag_left', async function () {
const engine = new Liquid({ trim_tag_left: true })
return expect(engine.parseAndRender(' \n \t{%if true%}foo{%endif%} '))
.to.eventually.equal('foo ')
const html = await engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
return expect(html).to.equal('foo ')
})
it('should respect trim_tag_right', function () {
it('should respect trim_tag_right', async function () {
const engine = new Liquid({ trim_tag_right: true })
return expect(engine.parseAndRender('\t{%if true%}foo{%endif%} \n'))
.to.eventually.equal('\tfoo')
const html = engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
return expect(html).to.equal('\tfoo')
})
it('should not trim value', function () {
it('should not trim value', async function () {
const engine = new 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')
const html = await engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx)
return expect(html).to.equal('a harttle b')
})
})
describe('value trimming', function () {
it('should respect trim_value_left', function () {
it('should respect trim_value_left', async function () {
const engine = new Liquid({ trim_value_left: true })
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
.to.eventually.equal('harttle ')
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
return expect(html).to.equal('harttle ')
})
it('should respect trim_value_right', function () {
it('should respect trim_value_right', async function () {
const engine = new Liquid({ trim_value_right: true })
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
.to.eventually.equal(' \n \tharttle')
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
return expect(html).to.equal(' \n \tharttle')
})
it('should respect not trim tag', function () {
it('should respect not trim tag', async function () {
const engine = new 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')
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
return expect(html).to.equal('\t aha \t')
})
})
describe('greedy', function () {
const src = '\n {%-if true-%}\n a \n{{-name-}}{%-endif-%}\n '
it('should enable greedy by default', function () {
it('should enable greedy by default', async function () {
const engine = new Liquid()
return expect(engine.parseAndRender(src, ctx))
.to.eventually.equal('aharttle')
const html = await engine.parseAndRender(src, ctx)
return expect(html).to.equal('aharttle')
})
it('should respect to greedy:false by default', function () {
it('should respect to greedy:false by default', async function () {
const engine = new Liquid({ greedy: false })
return expect(engine.parseAndRender(src, ctx))
.to.eventually.equal('\n a \nharttle ')
const html = await engine.parseAndRender(src, ctx)
return expect(html).to.equal('\n a \nharttle ')
})
})
describe('markup', function () {
it('should support trim using markup', function () {
it('should support trim using markup', async function () {
const engine = new Liquid()
const src = [
'{%- assign username = "John G. Chalmers-Smith" -%}',
@@ -67,9 +63,10 @@ describe('LiquidOptions#trimming', function () {
'{%- endif -%}'
].join('\n')
const dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
const html = await engine.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should not trim when not specified', function () {
it('should not trim when not specified', async function () {
const engine = new Liquid()
const src = [
'{% assign username = "John G. Chalmers-Smith" %}',
@@ -80,7 +77,8 @@ describe('LiquidOptions#trimming', function () {
'{% endif %}'
].join('\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)
const html = await engine.parseAndRender(src)
return expect(html).to.equal(dst)
})
})
})
+24 -19
View File
@@ -1,14 +1,9 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import Scope from '../../src/scope/scope'
import Output from '../../src/template/output'
import OutputToken from 'src/parser/output-token'
import Filter from 'src/template/filter'
chai.use(sinonChai)
chai.use(chaiAsPromised)
const expect = chai.expect
describe('Output', function () {
@@ -16,41 +11,51 @@ describe('Output', function () {
Filter.clear()
})
it('should respect to .to_liquid() method', function () {
it('should respect to .to_liquid() method', async function () {
const scope = new Scope({
bar: { to_liquid: x => 'custom' }
bar: { to_liquid: () => 'custom' }
})
return expect(new Output({value: 'bar'}).render(scope)).to.eventually.equal('custom')
const output = new Output({ value: 'bar' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('custom')
})
it('should stringify objects', function () {
it('should stringify objects', async function () {
const scope = new Scope({
foo: { obj: { arr: ['a', 2] } }
})
return expect(new Output({value: 'foo'}).render(scope)).to.eventually.equal('{"obj":{"arr":["a",2]}}')
const output = new Output({ value: 'foo' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('{"obj":{"arr":["a",2]}}')
})
it('should skip circular property', function () {
it('should skip circular property', async function () {
const ctx = { foo: { num: 2 }, bar: 'bar' } as any
ctx.foo.circular = ctx
const scope = new Scope(ctx)
return expect(new Output({value: 'foo'}).render(scope)).to.eventually.equal('{"num":2,"circular":{"bar":"bar"}}')
const output = new Output({ value: 'foo' } as OutputToken)
const html = await output.render(new Scope(ctx))
return expect(html).equal('{"num":2,"circular":{"bar":"bar"}}')
})
it('should skip function property', function () {
it('should skip function property', async function () {
const scope = new Scope({ obj: { foo: 'foo', bar: x => x } })
return expect(new Output({value: 'obj'}).render(scope)).to.eventually.equal('{"foo":"foo"}')
const output = new Output({ value: 'obj' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('{"foo":"foo"}')
})
it('should respect to .toString()', async () => {
const scope = new Scope({ obj: { toString: () => 'FOO' } })
const str = await new Output({value: 'obj'}).render(scope)
const output = new Output({ value: 'obj' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .to_s()', async () => {
const scope = new Scope({ obj: { to_s: () => 'FOO' } })
const str = await new Output({value: 'obj'}).render(scope)
const output = new Output({ value: 'obj' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .liquid_method_missing()', async () => {
const scope = new Scope({ obj: { liquid_method_missing: x => x.toUpperCase() } })
const str = await new Output({value: 'obj.foo'}).render(scope)
const output = new Output({ value: 'obj.foo' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
})
+7 -16
View File
@@ -1,24 +1,14 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import { expect } from 'chai'
import Scope from '../../src/scope/scope'
import Token from 'src/parser/token'
import Token from '../../src/parser/token'
import Tag from 'src/template/tag/tag'
import Filter from 'src/template/filter'
import Render from '../../src/render/render'
import Parser from '../../src/parser/parser'
import HTML from 'src/template/html'
chai.use(sinonChai)
chai.use(chaiAsPromised)
const expect = chai.expect
const parser = new Parser(null)
let render
describe('render', function () {
beforeEach(function () {
let render
before(function () {
Filter.clear()
Tag.clear()
render = new Render()
@@ -29,10 +19,11 @@ describe('render', function () {
expect(render.renderTemplates([])).to.be.rejectedWith(/scope undefined/)
})
it('should render html', function () {
it('should render html', async function () {
const scope = new Scope()
const token = { type: 'html', value: '<p>' } as Token
return expect(render.renderTemplates([new HTML(token)], scope)).to.eventually.equal('<p>')
const html = await render.renderTemplates([new HTML(token)], scope)
return expect(html).to.equal('<p>')
})
})
})
+5 -5
View File
@@ -1,14 +1,14 @@
import * as chai from 'chai'
import Tag from 'src/template/tag/tag'
import TagToken from 'src/parser/tag-token'
import Scope from 'src/scope/scope'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Liquid from 'src/liquid'
import TagToken from 'src/parser/tag-token'
chai.use(sinonChai)
const expect = chai.expect
const liquid = new Liquid();
const liquid = new Liquid()
describe('tag', function () {
let scope
@@ -29,14 +29,14 @@ describe('tag', function () {
type: 'tag',
value: 'foo',
name: 'foo'
}, [], liquid)
} as TagToken, [], liquid)
}).to.throw(/tag foo not found/)
})
it('should register simple tag', function () {
expect(function () {
Tag.register('foo', {
render: x => 'bar'
render: () => 'bar'
})
}).not.throw()
})
@@ -50,7 +50,7 @@ describe('tag', function () {
type: 'tag',
value: 'foo',
name: 'foo'
}
} as TagToken
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.called
})
+40 -37
View File
@@ -1,9 +1,8 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import * as sinonChai from 'chai-as-promised'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
chai.use(sinonChai)
const expect = chai.expect
use(chaiAsPromised)
describe('tags/assign', function () {
const liquid = new Liquid()
@@ -12,65 +11,69 @@ describe('tags/assign', function () {
const ctx = {}
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/)
})
it('should support assign to a string', function () {
it('should support assign to a string', async function () {
const src = '{% assign foo="bar" %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('bar')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('bar')
})
it('should support assign to a number', function () {
it('should support assign to a number', async function () {
const src = '{% assign foo=10086 %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('10086')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('10086')
})
it('should shading rather than overwriting', function () {
it('should shading rather than overwriting', async function () {
const ctx = { foo: 'foo' }
const src = '{% assign foo="FOO" %}{{foo}}'
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('FOO')
expect(ctx.foo).to.equal('foo')
})
const html = await liquid.parseAndRender(src, ctx)
expect(html).to.equal('FOO')
expect(ctx.foo).to.equal('foo')
})
it('should assign as array', function () {
it('should assign as array', async function () {
const src = '{% assign foo=(1..3) %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('[1,2,3]')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('[1,2,3]')
})
it('should assign as filter result', function () {
it('should assign as filter result', async function () {
const src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('A')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('A')
})
it('should assign as filter across multiple lines as result', function () {
it('should assign as filter across multiple lines as result', async function () {
const src = `{% assign foo="a b"
| capitalize
| split: " "
| first %}{{foo}}`
return expect(liquid.parseAndRender(src))
.to.eventually.equal('A')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('A')
})
it('should assign var-1', function () {
it('should assign var-1', async function () {
const src = '{% assign var-1 = 5 %}{{ var-1 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('5')
})
it('should assign var-', function () {
it('should assign var-', async function () {
const src = '{% assign var- = 5 %}{{ var- }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('5')
})
it('should assign -var', function () {
it('should assign -var', async function () {
const src = '{% assign -let = 5 %}{{ -let }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('5')
})
it('should assign -5-5', function () {
it('should assign -5-5', async function () {
const src = '{% assign -5-5 = 5 %}{{ -5-5 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('5')
})
it('should assign 4-3', function () {
it('should assign 4-3', async function () {
const src = '{% assign 4-3 = 5 %}{{ 4-3 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('5')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('5')
})
it('should not assign -6', function () {
it('should not assign -6', async function () {
const src = '{% assign -6 = 5 %}{{ -6 }}'
return expect(liquid.parseAndRender(src)).to.eventually.equal('-6')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-6')
})
})
+10 -12
View File
@@ -1,26 +1,24 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/capture', function () {
const liquid = new Liquid()
it('should support capture', function () {
it('should support capture', async function () {
const src = '{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('A')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('A')
})
it('should shading rather than overwriting', function () {
it('should shading rather than overwriting', async function () {
const src = '{% capture var %}10{% endcapture %}{{var}}'
const ctx = { 'var': 20 }
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('10')
expect(ctx.var).to.equal(20)
})
const html = await liquid.parseAndRender(src, ctx)
expect(html).to.equal('10')
expect(ctx.var).to.equal(20)
})
it('should throw on invalid identifier', function () {
+18 -18
View File
@@ -1,8 +1,8 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/case', function () {
const liquid = new Liquid()
@@ -12,42 +12,42 @@ describe('tags/case', function () {
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/{% case "foo"%} not closed/)
})
it('should hit the specified case', function () {
it('should hit the specified case', async function () {
const src = '{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('foo')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('foo')
})
it('should resolve empty string if not hit', function () {
it('should resolve empty string if not hit', async function () {
const src = '{% case empty %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}'
const ctx = {
empty: ''
}
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('bar')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('bar')
})
it('should accept empty string as branch name', function () {
it('should accept empty string as branch name', async function () {
const src = '{% case false %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
it('should support boolean case', function () {
it('should support boolean case', async function () {
const src = '{% case false %}' +
'{% when "foo" %}foo{% when false%}bar' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('bar')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('bar')
})
it('should support else branch', function () {
it('should support else branch', async function () {
const src = '{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('d')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('d')
})
})
+15 -15
View File
@@ -1,8 +1,8 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/comment', function () {
const liquid = new Liquid()
@@ -11,24 +11,24 @@ describe('tags/comment', function () {
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/{% comment %} not closed/)
})
it('should ignore plain string', function () {
it('should ignore plain string', async function () {
const src = 'My name is {% comment %}super{% endcomment %} Shopify.'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('My name is Shopify.')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('My name is Shopify.')
})
it('should ignore output tokens', function () {
it('should ignore output tokens', async function () {
const src = '{% comment %}\n{{ foo}} \n{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
it('should ignore tag tokens', function () {
it('should ignore tag tokens', async function () {
const src = '{% comment %}{%if true%}true{%else%}false{%endif%}{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
it('should ignore un-balenced tag tokens', function () {
it('should ignore un-balenced tag tokens', async function () {
const src = '{% comment %}{%if true%}true{%else%}false{% endcomment %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
})
+13 -15
View File
@@ -1,16 +1,16 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/cycle', function () {
const liquid = new Liquid()
it('should support cycle', function () {
it('should support cycle', async function () {
const src = "{% cycle '1', '2', '3' %}"
return expect(liquid.parseAndRender(src + src + src + src))
.to.eventually.equal('1231')
const html = await liquid.parseAndRender(src + src + src + src)
return expect(html).to.equal('1231')
})
it('should throw when cycle candidates empty', function () {
@@ -18,23 +18,21 @@ describe('tags/cycle', function () {
.to.be.rejectedWith(/empty candidates/)
})
it('should support cycle in for block', function () {
it('should support cycle in for block', async function () {
const src = '{% for i in (1..5) %}{% cycle one, "e"%}{% endfor %}'
const ctx = {
one: 1
}
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('1e1e1')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('1e1e1')
})
it('should support cycle group', function () {
it('should support cycle group', async function () {
const src = "{% cycle one: '1', '2', '3'%}" +
"{% cycle 1: '1', '2', '3'%}" +
"{% cycle 2: '1', '2', '3'%}"
const ctx = {
one: 1
}
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('121')
const ctx = { one: 1 }
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('121')
})
})
+25 -27
View File
@@ -1,8 +1,8 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/decrement', function () {
const liquid = new Liquid()
@@ -12,49 +12,47 @@ describe('tags/decrement', function () {
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/)
})
it('should decrement undefined variable', function () {
it('should decrement undefined variable', async function () {
const src = '{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-1-2-3')
})
it('should decrement defined variable', function () {
it('should decrement defined variable', async function () {
const src = '{% decrement var %}{% decrement var %}{% decrement var %}'
const ctx = { 'var': 10 }
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('987')
expect(ctx.var).to.equal(7)
})
const html = await liquid.parseAndRender(src, ctx)
expect(html).to.equal('987')
expect(ctx.var).to.equal(7)
})
it('should be independent from assign', function () {
it('should be independent from assign', async function () {
const src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-1-2-3')
})
it('should be independent from capture', function () {
it('should be independent from capture', async function () {
const src = '{% capture var %}10{% endcapture %}{% decrement var %}{% decrement var %}{% decrement var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-1-2-3')
})
it('should not shading assign', function () {
it('should not shading assign', async function () {
const src = '{% assign var=10 %}{% decrement var %}{% decrement var %}{% decrement var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('-1-2-3 10')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-1-2-3 10')
})
it('should not shading capture', function () {
it('should not shading capture', async function () {
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')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('-1-2-3 10')
})
it('should share the same variable with increment', function () {
it('should share the same variable with increment', async function () {
const src = '{%increment var%}{%increment var%}{%decrement var%}{%decrement var%}{%increment var%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('01100')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('01100')
})
})
+75 -75
View File
@@ -1,8 +1,8 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/for', function () {
let liquid, ctx
@@ -22,28 +22,28 @@ describe('tags/for', function () {
emptyArray: []
}
})
it('should support array', function () {
it('should support array', async function () {
const src = '{%for c in alpha%}{{c}}{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('abc')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('abc')
})
it('should support object', function () {
it('should support object', async function () {
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-')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('foo,bar-coo,haa-')
})
describe('scope', function () {
it('should read super scope', function () {
it('should read super scope', async function () {
const src = '{%for a in (1..2)%}{{num}}{%endfor%}'
return expect(liquid.parseAndRender(src, { num: 1 }))
.to.eventually.equal('11')
const html = await liquid.parseAndRender(src, { num: 1 })
return expect(html).to.equal('11')
})
it('should write super scope', function () {
it('should write super scope', async function () {
const src = '{%for a in (1..2)%}{{num}}{%assign num = 2%}{%endfor%}'
return expect(liquid.parseAndRender(src, { num: 1 }))
.to.eventually.equal('12')
const html = await liquid.parseAndRender(src, { num: 1 })
return expect(html).to.equal('12')
})
})
@@ -62,45 +62,45 @@ describe('tags/for', function () {
})
describe('else', function () {
it('should goto else for empty array', function () {
it('should goto else for empty array', async function () {
const src = '{%for c in emptyArray%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('b')
})
it('should treat non-empty string as one single element', function () {
it('should treat non-empty string as one single element', async function () {
const src = '{%for c in "abc"%}x{{c}}{%else%}y{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('xabc')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('xabc')
})
it('should goto else for empty string', function () {
it('should goto else for empty string', async function () {
const src = '{%for c in ""%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('b')
})
it('should goto else for empty string object', function () {
it('should goto else for empty string object', async function () {
// it should be false although `new String` is none-conform
const src = '{%for c in strObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('b')
})
it('should goto else for empty object', function () {
it('should goto else for empty object', async function () {
const src = '{%for c in emptyObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('b')
})
it('should goto else for null-prototyped object', function () {
it('should goto else for null-prototyped object', async function () {
const src = '{%for c in nullProtoObj%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('b')
})
})
it('should support for with forloop', function () {
it('should support for with forloop', async function () {
const src = '{%for c in alpha%}' +
'{{forloop.first}}.{{forloop.index}}.{{forloop.index0}}.' +
'{{forloop.last}}.{{forloop.length}}.' +
@@ -110,41 +110,41 @@ describe('tags/for', function () {
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))
.to.eventually.equal(dst)
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal(dst)
})
it('should support for with continue', function () {
it('should support for with continue', async function () {
const src = '{% for i in (1..5) %}' +
'{{i}}{% continue %}after' +
'{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12345')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('12345')
})
it('should support for with break', function () {
it('should support for with break', async function () {
const src = '{% for i in (one..5) %}' +
'{% if i == 4 %}{% break %}{% endif %}' +
'{{ i }}' +
'{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('123')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('123')
})
describe('limit', function () {
it('should support for with limit', function () {
it('should support for with limit', async function () {
const src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('12')
})
it('should set forloop.last properly', function () {
it('should set forloop.last properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('false true ')
})
it('should set forloop.first properly', function () {
it('should set forloop.first properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('true false ')
})
it('should set forloop.length properly', function () {
const src = '{%for i in (1..10) limit:2 offset:3%}{{forloop.length}} {%endfor%}'
@@ -154,50 +154,50 @@ describe('tags/for', function () {
})
describe('offset', function () {
it('should support offset with limit', function () {
it('should support offset with limit', async function () {
const src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('67')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('67')
})
it('should set index properly', function () {
it('should set index properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('1 2 ')
})
it('should set index0 properly', function () {
it('should set index0 properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('0 1 ')
})
it('should set rindex properly', function () {
it('should set rindex properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('2 1 ')
})
it('should set rindex0 properly', function () {
it('should set rindex0 properly', async function () {
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 ')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('1 0 ')
})
})
describe('reverse', function () {
it('should support for reversed in the last position', function () {
it('should support for reversed in the last position', async function () {
const src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('21')
})
it('should support for reversed in the first position', function () {
it('should support for reversed in the first position', async function () {
const src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('21')
})
it('should support for reversed in the middle position', function () {
it('should support for reversed in the middle position', async function () {
const src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('543')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('543')
})
})
})
+49 -52
View File
@@ -1,8 +1,5 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
describe('tags/if', function () {
const liquid = new Liquid()
@@ -18,99 +15,99 @@ describe('tags/if', function () {
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/tag {% if false%} not closed/)
})
it('should support nested', function () {
it('should support nested', async function () {
const src = '{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('')
})
describe('single value as condition', function () {
it('should support boolean', function () {
it('should support boolean', async function () {
const src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('2')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('2')
})
it('should treat Array truthy', function () {
it('should treat Array truthy', async function () {
const src = '{%if emptyArray%}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('a')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('a')
})
it('should return true if empty string', function () {
it('should return true if empty string', async function () {
const src = '{%if emptyString%}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('a')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('a')
})
})
describe('expression as condition', function () {
it('should support ==', function () {
it('should support ==', async function () {
const src = '{% if 2==3 %}yes{%else%}no{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should support >=', function () {
it('should support >=', async function () {
const src = '{% if 1>=2 and one<two %}a{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('')
})
it('should support !=', function () {
it('should support !=', async function () {
const src = '{% if one!=two %}yes{%else%}no{%endif%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('yes')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('yes')
})
it('should support value and expression', function () {
it('should support value and expression', async function () {
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')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('XY')
})
})
describe('comparasion to null', function () {
it('should evaluate false for null < 10', function () {
it('should evaluate false for null < 10', async function () {
const src = '{% if null < 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for null > 10', function () {
it('should evaluate false for null > 10', async function () {
const src = '{% if null > 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for null <= 10', function () {
it('should evaluate false for null <= 10', async function () {
const src = '{% if null <= 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for null >= 10', function () {
it('should evaluate false for null >= 10', async function () {
const src = '{% if null >= 10 %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for 10 < null', function () {
it('should evaluate false for 10 < null', async function () {
const src = '{% if 10 < null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for 10 > null', function () {
it('should evaluate false for 10 > null', async function () {
const src = '{% if 10 > null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for 10 <= null', function () {
it('should evaluate false for 10 <= null', async function () {
const src = '{% if 10 <= null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should evaluate false for 10 >= null', function () {
it('should evaluate false for 10 >= null', async function () {
const src = '{% if 10 >= null %}yes{% else %}no{% endif %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
})
})
+34 -37
View File
@@ -1,9 +1,6 @@
import Liquid from 'src/liquid'
import { expect } from 'chai'
import * as mock from 'mock-fs'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/include', function () {
let liquid
@@ -16,21 +13,21 @@ describe('tags/include', function () {
afterEach(function () {
mock.restore()
})
it('should support include', function () {
it('should support include', async function () {
mock({
'/current.html': 'bar{% include "bar/foo.html" %}bar',
'/bar/foo.html': 'foo'
})
return expect(liquid.renderFile('/current.html')).to
.eventually.equal('barfoobar')
const html = await liquid.renderFile('/current.html')
return expect(html).to.equal('barfoobar')
})
it('should support template string', function () {
it('should support template string', async function () {
mock({
'/current.html': 'bar{% include "bar/{{name}}" %}bar',
'/bar/foo.html': 'foo'
})
return expect(liquid.renderFile('/current.html', { name: 'foo.html' })).to
.eventually.equal('barfoobar')
const html = await liquid.renderFile('/current.html', { name: 'foo.html' })
return expect(html).to.equal('barfoobar')
})
it('should throw when not specified', function () {
@@ -53,43 +50,43 @@ describe('tags/include', function () {
})
})
it('should support include with relative path', function () {
it('should support include with relative path', async function () {
mock({
'/bar/foo.html': 'foo',
'/foo/relative.html': 'bar{% include "../bar/foo.html" %}bar'
})
return expect(liquid.renderFile('foo/relative.html')).to
.eventually.equal('barfoobar')
const html = await liquid.renderFile('foo/relative.html')
return expect(html).to.equal('barfoobar')
})
it('should support include: hash list', function () {
it('should support include: hash list', async function () {
mock({
'/hash.html': '{% assign name="harttle" %}{% include "user.html", role: "admin", alias: name %}',
'/user.html': '{{name}} : {{role}} : {{alias}}'
})
return expect(liquid.renderFile('hash.html')).to
.eventually.equal('harttle : admin : harttle')
const html = await liquid.renderFile('hash.html')
return expect(html).to.equal('harttle : admin : harttle')
})
it('should support include: parent scope', function () {
it('should support include: parent scope', async function () {
mock({
'/scope.html': '{% assign shape="triangle" %}{% assign color="yellow" %}{% include "color.html" %}',
'/color.html': 'color:{{color}}, shape:{{shape}}'
})
return expect(liquid.renderFile('scope.html')).to
.eventually.equal('color:yellow, shape:triangle')
const html = await liquid.renderFile('scope.html')
return expect(html).to.equal('color:yellow, shape:triangle')
})
it('should support include: with', function () {
it('should support include: with', async function () {
mock({
'/with.html': '{% include "color" with "red", shape: "rect" %}',
'/color.html': 'color:{{color}}, shape:{{shape}}'
})
return expect(liquid.renderFile('with.html')).to
.eventually.equal('color:red, shape:rect')
const html = await liquid.renderFile('with.html')
return expect(html).to.equal('color:red, shape:rect')
})
it('should support nested includes', function () {
it('should support nested includes', async function () {
mock({
'/personInfo.html': 'This is a person {% include "card.html" %}',
'/card.html': '<p>{{person.firstName}} {{person.lastName}}<br/>{% include "address" %}</p>',
@@ -104,49 +101,49 @@ describe('tags/include', function () {
}
}
}
return expect(liquid.renderFile('personInfo.html', ctx)).to
.eventually.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
const html = await liquid.renderFile('personInfo.html', ctx)
return expect(html).to.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
})
describe('static partial', function () {
it('should support filename with extention', function () {
it('should support filename with extention', async function () {
mock({
'/parent.html': 'X{% include child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('Xchild with redY')
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('Xchild with redY')
})
it('should support parent paths', function () {
it('should support parent paths', async function () {
mock({
'/parent.html': 'X{% include bar/./../foo/child.html %}Y',
'/foo/child.html': 'child'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('XchildY')
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('XchildY')
})
it('should support subpaths', function () {
it('should support subpaths', async function () {
mock({
'/parent.html': 'X{% include foo/child.html %}Y',
'/foo/child.html': 'child'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('XchildY')
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('XchildY')
})
it('should support comma separated arguments', function () {
it('should support comma separated arguments', async function () {
mock({
'/parent.html': 'X{% include child.html, color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
return expect(staticLiquid.renderFile('parent.html')).to
.eventually.equal('Xchild with redY')
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('Xchild with redY')
})
})
})
+22 -24
View File
@@ -1,49 +1,47 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/increment', function () {
const liquid = new Liquid()
it('should increment undefined variable', function () {
it('should increment undefined variable', async function () {
const src = '{% increment one %}{% increment one %}{% increment one %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('012')
})
it('should increment defined variable', function () {
it('should increment defined variable', async function () {
const src = '{% increment one %}{% increment one %}{% increment one %}'
const ctx = { one: 7 }
return liquid.parseAndRender(src, ctx)
.then(x => {
expect(x).to.equal('789')
expect(ctx.one).to.equal(10)
})
const html = await liquid.parseAndRender(src, ctx)
expect(html).to.equal('789')
expect(ctx.one).to.equal(10)
})
it('should be independent from assign', function () {
it('should be independent from assign', async function () {
const src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('012')
})
it('should be independent from capture', function () {
it('should be independent from capture', async function () {
const src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('012')
})
it('should not shading assign', function () {
it('should not shading assign', async function () {
const src = '{% assign var=10 %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012 10')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('012 10')
})
it('should not shading capture', function () {
it('should not shading capture', async function () {
const src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('012 10')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('012 10')
})
})
+34 -37
View File
@@ -1,9 +1,6 @@
import Liquid from 'src/liquid'
import { expect } from 'chai'
import * as mock from 'mock-fs'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
describe('tags/layout', function () {
let liquid
@@ -35,51 +32,51 @@ describe('tags/layout', function () {
})
})
describe('anonymous block', function () {
it('should handle anonymous block', function () {
it('should handle anonymous block', async function () {
mock({
'/parent.html': 'X{%block%}{%endblock%}Y'
})
const src = '{% layout "parent.html" %}{%block%}A{%endblock%}'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XAY')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('XAY')
})
it('should handle top level contents as anonymous block', function () {
it('should handle top level contents as anonymous block', async function () {
mock({
'/parent.html': 'X{%block%}{%endblock%}Y'
})
const src = '{% layout "parent.html" %}A'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XAY')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('XAY')
})
})
it('should handle named blocks', function () {
it('should handle named blocks', async function () {
mock({
'/parent.html': 'X{% block "a"%}{% endblock %}Y{% block b%}{%endblock%}Z'
})
const src = '{% layout "parent.html" %}' +
'{%block a%}A{%endblock%}' +
'{%block b%}B{%endblock%}'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XAYBZ')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('XAYBZ')
})
it('should support default block content', function () {
it('should support default block content', async function () {
mock({
'/parent.html': 'X{% block "a"%}A{% endblock %}Y{% block b%}B{%endblock%}Z'
})
const src = '{% layout "parent.html" %}{%block a%}a{%endblock%}'
return expect(liquid.parseAndRender(src)).to
.eventually.equal('XaYBZ')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('XaYBZ')
})
it('should handle nested block', function () {
it('should handle nested block', async function () {
mock({
'/grand.html': 'X{%block a%}G{%endblock%}Y',
'/parent.html': '{%layout "grand" %}{%block a%}P{%endblock%}',
'/main.html': '{%layout "parent"%}{%block a%}A{%endblock%}'
})
return expect(liquid.renderFile('/main.html')).to
.eventually.equal('XAY')
const html = await liquid.renderFile('/main.html')
return expect(html).to.equal('XAY')
})
it('should not bleed scope into included layout', function () {
it('should not bleed scope into included layout', async function () {
mock({
'/parent.html': 'X{%block a%}{%endblock%}Y{%block b%}{%endblock%}Z',
'/main.html': '{%layout "parent"%}' +
@@ -87,55 +84,55 @@ describe('tags/layout', function () {
'{%block b%}I{%include "included"%}J{%endblock%}',
'/included.html': '{%layout "parent"%}{%block a%}a{%endblock%}'
})
return expect(liquid.renderFile('main')).to
.eventually.equal('XAYIXaYZJZ')
const html = await liquid.renderFile('main')
return expect(html).to.equal('XAYIXaYZJZ')
})
it('should support hash list', function () {
it('should support hash list', async function () {
mock({
'/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout "parent.html" color:"black"%}{%block%}A{%endblock%}'
})
return expect(liquid.renderFile('/main.html')).to
.eventually.equal('blackA')
const html = await liquid.renderFile('/main.html')
return expect(html).to.equal('blackA')
})
it('should support multiple hash', function () {
it('should support multiple hash', async function () {
mock({
'/parent.html': '{{color}}{{bg}}{%block%}{%endblock%}',
'/main.html': '{% layout "parent.html" color:"black", bg:"red"%}{%block%}A{%endblock%}'
})
return expect(liquid.renderFile('/main.html')).to
.eventually.equal('blackredA')
const html = await liquid.renderFile('/main.html')
return expect(html).to.equal('blackredA')
})
describe('static partial', function () {
it('should support filename with extention', function () {
it('should support filename with extention', async function () {
mock({
'/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
})
const staticLiquid = new Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
const html = await staticLiquid.renderFile('/main.html')
return expect(html).to.equal('blackA')
})
it('should support parent paths', function () {
it('should support parent paths', async function () {
mock({
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout bar/../foo/parent.html color:"black"%}{%block%}A{%endblock%}'
})
const staticLiquid = new Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
const html = await staticLiquid.renderFile('/main.html')
return expect(html).to.equal('blackA')
})
it('should support subpaths', function () {
it('should support subpaths', async function () {
mock({
'/foo/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout foo/parent.html color:"black"%}{%block%}A{%endblock%}'
})
const staticLiquid = new Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
const html = await staticLiquid.renderFile('/main.html')
return expect(html).to.equal('blackA')
})
})
})
+6 -7
View File
@@ -1,8 +1,5 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
describe('tags/raw', function () {
const liquid = new Liquid()
@@ -13,11 +10,13 @@ describe('tags/raw', function () {
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)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support raw 3', function () {
it('should support raw 3', async function () {
const src = '{% raw %}\n{{ foo}} \n{% endraw %}'
const dst = '\n{{ foo}} \n'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
})
+25 -20
View File
@@ -1,19 +1,17 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
describe('tags/tablerow', function () {
const liquid = new Liquid()
it('should support tablerow', function () {
it('should support tablerow', async function () {
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)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support cols', function () {
it('should support cols', async function () {
const src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
const ctx = {
alpha: ['a', 'b', 'c']
@@ -21,25 +19,29 @@ describe('tags/tablerow', function () {
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)
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal(dst)
})
it('should support cols set to 0', function () {
it('should support cols set to 0', async function () {
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)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support empty tablerow', function () {
it('should support empty tablerow', async function () {
const src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}'
const dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support empty array', function () {
it('should support empty array', async function () {
const src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
const dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should throw when tablerow not closed', function () {
@@ -48,26 +50,29 @@ describe('tags/tablerow', function () {
.to.be.rejectedWith(/tag .* not closed/)
})
it('should support tablerow with range', function () {
it('should support tablerow with range', async function () {
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>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support tablerow with limit', function () {
it('should support tablerow with limit', async function () {
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)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support tablerow with offset', function () {
it('should support tablerow with offset', async function () {
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)
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
})
+15 -15
View File
@@ -1,37 +1,37 @@
import Liquid from 'src/liquid'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('tags/unless', function () {
let liquid
before(() => { liquid = new Liquid() })
it('should render else when predicate yields true', function () {
it('should render else when predicate yields true', async function () {
// 0 is truthy
const src = '{% unless 0 %}yes{%else%}no{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('no')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('no')
})
it('should render unless when predicate yields false', function () {
it('should render unless when predicate yields false', async function () {
const src = '{% unless false %}yes{%else%}no{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('yes')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('yes')
})
it('should reject when tag not closed', function () {
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 () {
it('should render unless when predicate yields false and else undefined', async function () {
const src = '{% unless 1>2 %}yes{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('yes')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('yes')
})
it('should render "" when predicate yields false and else undefined', function () {
it('should render "" when predicate yields false and else undefined', async function () {
const src = '{% unless 1<2 %}yes{%endunless%}'
return expect(liquid.parseAndRender(src))
.to.eventually.equal('')
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
})
+1 -3
View File
@@ -1,7 +1,5 @@
import { resolve } from '../../src/parser/template-browser'
import * as chai from 'chai'
const expect = chai.expect
import { expect } from 'chai'
describe('template-browser', function () {
if (+process.version.match(/^v(\d+)/)[1] < 8) {
+4 -8
View File
@@ -1,11 +1,7 @@
import { resolve } from '../../src/parser/template'
import * as mock from 'mock-fs'
import * as chai from 'chai'
import { expect } from 'chai'
import * as path from 'path'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('template', function () {
before(function () {
@@ -14,10 +10,10 @@ describe('template', function () {
})
})
describe('#resolve()', function () {
it('should resolve based on root', function () {
const filepath = resolve('bar.html', '/foo', { root: [] })
it('should resolve based on root', async function () {
const filepath = await resolve('bar.html', '/foo', { root: [] })
const expected = path.resolve('/foo/bar.html')
return expect(filepath).to.eventually.equal(expected)
return expect(filepath).to.equal(expected)
})
it('should resolve based on root', function () {
return expect(resolve('foo.html', '/foo', { root: [] }))
+9 -8
View File
@@ -1,14 +1,15 @@
import { expect } from 'chai'
import { parse } from 'src/parser/tokenizer'
import Tokenizer from 'src/parser/tokenizer'
import TagToken from 'src/parser/tag-token'
import OutputToken from 'src/parser/output-token'
import HTMLToken from 'src/parser/html-token'
describe('tokenizer', function () {
const tokenizer = new Tokenizer()
describe('parse', function () {
it('should handle plain HTML', function () {
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0].value).to.equal(html)
@@ -16,7 +17,7 @@ describe('tokenizer', function () {
})
it('should handle tag syntax', function () {
const html = '<p>{% for p in a[1]%}</p>'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(TagToken)
@@ -24,7 +25,7 @@ describe('tokenizer', function () {
})
it('should handle value syntax', function () {
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(OutputToken)
@@ -32,7 +33,7 @@ describe('tokenizer', function () {
})
it('should handle consecutive value and tags', function () {
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(4)
expect(tokens[0]).instanceOf(OutputToken)
@@ -43,7 +44,7 @@ describe('tokenizer', function () {
})
it('should keep white spaces and newlines', function () {
const html = '{%foo%}\n{%bar %} \n {%alice%}'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(5)
expect(tokens[1]).instanceOf(HTMLToken)
expect(tokens[1].raw).to.equal('\n')
@@ -52,7 +53,7 @@ describe('tokenizer', function () {
})
it('should handle multiple lines tag', function () {
const html = '{%foo\na:a\nb:1.23\n%}'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(TagToken)
expect(tokens[0].args).to.equal('a:a\nb:1.23')
@@ -60,7 +61,7 @@ describe('tokenizer', function () {
})
it('should handle multiple lines value', function () {
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
const tokens = parse(html)
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(OutputToken)
expect(tokens[0].raw).to.equal('{{foo\n|date:\n"%Y-%m-%d"\n}}')
+5 -7
View File
@@ -1,11 +1,8 @@
import Liquid from '../../../src/liquid'
import { expect } from 'chai'
import Liquid from 'src/liquid'
import * as mock from 'mock-fs'
import * as chai from 'chai'
import * as path from 'path'
const expect = chai.expect
chai.use(require('chai-as-promised'))
let engine = new Liquid()
const strictEngine = new Liquid({
strict_variables: true,
@@ -111,8 +108,9 @@ describe('error', function () {
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 not throw when variable undefined by default', async function () {
const html = await engine.parseAndRender('X{{a}}Y')
return expect(html).to.equal('XY')
})
it('should throw RenderError when variable not defined', async function () {
const err = await expect(strictEngine.parseAndRender('{{a}}')).be.rejected
+18 -13
View File
@@ -1,8 +1,9 @@
const chai = require('chai')
const sinon = require('sinon')
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
const expect = chai.expect
chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
chai.use(sinonChai)
const P = require('../../../src/util/promise')
@@ -32,10 +33,12 @@ describe('util/promise', function () {
item => Promise.reject(new Error(item)))
return expect(p).to.be.rejectedWith('third')
})
it('should resolve the value that first callback resolved', () => {
const p = P.anySeries(['first', 'second'],
item => Promise.resolve(item))
return expect(p).to.eventually.equal('first')
it('should resolve the value that first callback resolved', async () => {
const result = await P.anySeries(
['first', 'second'],
item => Promise.resolve(item)
)
return expect(result).to.equal('first')
})
it('should not call rest of callbacks once resolved', () => {
const spy = sinon.spy()
@@ -50,10 +53,12 @@ describe('util/promise', function () {
})
})
describe('.mapSeries()', function () {
it('should resolve when all resolved', function () {
const p = P.mapSeries(['first', 'second', 'third'],
item => Promise.resolve(item))
return expect(p).to.eventually.deep.equal(['first', 'second', 'third'])
it('should resolve when all resolved', async function () {
const result = P.mapSeries(
['first', 'second', 'third'],
item => Promise.resolve(item)
)
return expect(result).to.deep.equal(['first', 'second', 'third'])
})
it('should reject with the error that first callback rejected', () => {
const p = P.mapSeries(['first', 'second'],
@@ -66,7 +71,7 @@ describe('util/promise', function () {
return P
.mapSeries(
['first', 'second'],
(item, idx) => new Promise(function (resolve, reject) {
(item, idx) => new Promise(function (resolve) {
if (idx === 0) {
setTimeout(function () {
spy1()
-3
View File
@@ -1,5 +1,4 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import Scope from '../../src/scope/scope'
@@ -7,7 +6,6 @@ import Filter from 'src/template/filter'
import Value from 'src/template/value'
chai.use(sinonChai)
chai.use(chaiAsPromised)
const expect = chai.expect
const add = (l, r) => l + r
@@ -41,7 +39,6 @@ describe('Value', function () {
expect(tpl.filters.length).to.equal(2)
})
it('should eval value', function () {
Filter.register('date', (l, r) => l + r)
Filter.register('time', (l, r) => l + 3 * r)
+7 -8
View File
@@ -1,12 +1,11 @@
import { read } from 'src/parser/template-browser'
import * as sinon from 'sinon'
import * as chai from 'chai'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
const expect = chai.expect
describe('template-browser', () => {
describe('xhr', () => {
if (+process.version.match(/^v(\d+)/)[1] < 8) {
console.info('jsdom not supported, skipping xhr...')
return
@@ -24,9 +23,9 @@ describe('template-browser', () => {
delete (global as any).XMLHttpRequest
})
describe('#read()', () => {
it('should get corresponding text', () => {
return expect(read('https://example.com/views/hello.html'))
.to.eventually.equal('hello {{name}}')
it('should get corresponding text', async function () {
const html = await read('https://example.com/views/hello.html')
return expect(html).to.equal('hello {{name}}')
})
it('should throw 404', () => {
return expect(read('https://example.com/not/exist.html'))