feat: with & for in render tag, closes #195

This commit is contained in:
harttle
2020-03-04 07:08:54 +08:00
parent aa27a6cd7b
commit 6ea6881f08
67 changed files with 1108 additions and 724 deletions
+5 -6
View File
@@ -30,12 +30,11 @@ describe('tags/include', function () {
it('should throw when not specified', function () {
mock({
'/parent.html': '{%include%}'
'/parent.html': '{%include , %}'
})
return liquid.renderFile('/parent.html').catch(function (e) {
console.log(e)
expect(e.name).to.equal('RenderError')
expect(e.message).to.match(/cannot include with empty filename/)
expect(e.name).to.equal('ParseError')
expect(e.message).to.match(/illegal argument ","/)
})
})
@@ -45,7 +44,7 @@ describe('tags/include', function () {
})
return liquid.renderFile('/parent.html').catch(function (e) {
expect(e.name).to.equal('RenderError')
expect(e.message).to.match(/cannot include with empty filename/)
expect(e.message).to.match(/illegal filename/)
})
})
@@ -183,7 +182,7 @@ describe('tags/include', function () {
})
it('should support template string', function () {
mock({
'/current.html': 'bar{% include name" %}bar',
'/current.html': 'bar{% include name %}bar',
'/bar/foo.html': 'foo'
})
const html = liquid.renderFileSync('/current.html', { name: '/bar/foo.html' })
+22 -3
View File
@@ -25,8 +25,8 @@ describe('tags/layout', function () {
'/parent.html': '{%layout%}'
})
return liquid.renderFile('/parent.html').catch(function (e) {
expect(e.name).to.equal('RenderError')
expect(e.message).to.match(/cannot apply layout with empty filename/)
expect(e.name).to.equal('ParseError')
expect(e.message).to.match(/illegal argument ""/)
})
})
describe('anonymous block', function () {
@@ -57,6 +57,14 @@ describe('tags/layout', function () {
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('XAYBZ')
})
it('should support variable as layout name', async function () {
mock({
'/parent.html': 'X{% block "a"%}{% endblock %}Y'
})
const src = '{% layout parent %}{%block a%}A{%endblock%}'
const html = await liquid.parseAndRender(src, { parent: 'parent.html' })
return expect(html).to.equal('XAY')
})
it('should support default block content', async function () {
mock({
'/parent.html': 'X{% block "a"%}A{% endblock %}Y{% block b%}B{%endblock%}Z'
@@ -74,7 +82,7 @@ describe('tags/layout', function () {
const html = await liquid.renderFile('/main.html')
return expect(html).to.equal('XAY')
})
it('should not bleed scope into included layout', async function () {
it('should not bleed scope into `include` layout', async function () {
mock({
'/parent.html': 'X{%block a%}{%endblock%}Y{%block b%}{%endblock%}Z',
'/main.html': '{%layout "parent"%}' +
@@ -85,6 +93,17 @@ describe('tags/layout', function () {
const html = await liquid.renderFile('main')
return expect(html).to.equal('XAYIXaYZJZ')
})
it('should not bleed scope into `render` layout', async function () {
mock({
'/parent.html': 'X{%block a%}{%endblock%}Y{%block b%}{%endblock%}Z',
'/main.html': '{%layout "parent"%}' +
'{%block a%}A{%endblock%}' +
'{%block b%}I{%render "included"%}J{%endblock%}',
'/included.html': '{%layout "parent"%}{%block a%}a{%endblock%}'
})
const html = await liquid.renderFile('main')
return expect(html).to.equal('XAYIXaYZJZ')
})
it('should support hash list', async function () {
mock({
'/parent.html': '{{color}}{%block%}{%endblock%}',
+64 -24
View File
@@ -17,7 +17,7 @@ describe('tags/render', function () {
'/bar/foo.html': 'foo'
})
const html = await liquid.renderFile('/current.html')
return expect(html).to.equal('barfoobar')
expect(html).to.equal('barfoobar')
})
it('should support template string', async function () {
mock({
@@ -25,7 +25,7 @@ describe('tags/render', function () {
'/bar/foo.html': 'foo'
})
const html = await liquid.renderFile('/current.html', { name: 'foo.html' })
return expect(html).to.equal('barfoobar')
expect(html).to.equal('barfoobar')
})
it('should throw when not specified', function () {
@@ -33,9 +33,8 @@ describe('tags/render', function () {
'/parent.html': '{%render%}'
})
return liquid.renderFile('/parent.html').catch(function (e) {
console.log(e)
expect(e.name).to.equal('RenderError')
expect(e.message).to.match(/cannot render with empty filename/)
expect(e.name).to.equal('ParseError')
expect(e.message).to.match(/illegal argument ""/)
})
})
@@ -45,7 +44,7 @@ describe('tags/render', function () {
})
return liquid.renderFile('/parent.html').catch(function (e) {
expect(e.name).to.equal('RenderError')
expect(e.message).to.match(/cannot render with empty filename/)
expect(e.message).to.match(/illegal filename "not-exist":"undefined"/)
})
})
@@ -55,7 +54,7 @@ describe('tags/render', function () {
'/foo/relative.html': 'bar{% render "../bar/foo.html" %}bar'
})
const html = await liquid.renderFile('foo/relative.html')
return expect(html).to.equal('barfoobar')
expect(html).to.equal('barfoobar')
})
it('should support render: hash list', async function () {
@@ -64,7 +63,7 @@ describe('tags/render', function () {
'/user.html': '{{role}} : {{alias}}'
})
const html = await liquid.renderFile('hash.html')
return expect(html).to.equal('admin : harttle')
expect(html).to.equal('admin : harttle')
})
it('should not bleed into child template', async function () {
@@ -73,7 +72,7 @@ describe('tags/render', function () {
'/user.html': 'InChild: {{name}}'
})
const html = await liquid.renderFile('hash.html')
return expect(html).to.equal('InParent: harttle InChild: ')
expect(html).to.equal('InParent: harttle InChild: ')
})
it('should be able to access globals', async function () {
@@ -86,16 +85,49 @@ describe('tags/render', function () {
}, {
globals: { name: 'Harttle' }
})
return expect(html).to.equal('InParent: harttle InChild: Harttle')
expect(html).to.equal('InParent: harttle InChild: Harttle')
})
it('should support render: with', async function () {
it('should support with', async function () {
mock({
'/with.html': '{% render "color" with "red", shape: "rect" %}',
'/color.html': 'color:{{color}}, shape:{{shape}}'
})
const html = await liquid.renderFile('with.html')
return expect(html).to.equal('color:red, shape:rect')
expect(html).to.equal('color:red, shape:rect')
})
it('should support with...as', async function () {
mock({
'/with.html': '{% render "color" with color as c %}',
'/color.html': 'color:{{c}}'
})
const html = await liquid.renderFile('with.html', { color: 'red' })
expect(html).to.equal('color:red')
})
it('should support with...as and other parameters', async function () {
mock({
'/index.html': '{% render "item" with color as c, s: shape %}',
'/item.html': 'color:{{c}}, shape:{{s}}'
})
const scope = { color: 'red', shape: 'rect' }
const html = await liquid.renderFile('index.html', scope)
expect(html).to.equal('color:red, shape:rect')
})
it('should support for...as', async function () {
mock({
'/index.html': '{% render "item" for colors as color %}',
'/item.html': '{{forloop.index}}: {{color}}\n'
})
const html = await liquid.renderFile('index.html', { colors: ['red', 'green'] })
expect(html).to.equal('1: red\n2: green\n')
})
it('should support for...as with other parameters', async function () {
mock({
'/index.html': '{% render "item" for colors as color with ".\n" as tail, sep: ". "%}',
'/item.html': '{{forloop.index}}{{sep}}{{color}}{{tail}}'
})
const html = await liquid.renderFile('index.html', { colors: ['red', 'green'] })
expect(html).to.equal('1. red.\n2. green.\n')
})
it('should support render: with as Drop', async function () {
class ColorDrop extends Drop {
@@ -141,7 +173,7 @@ describe('tags/render', function () {
}
}
const html = await liquid.renderFile('personInfo.html', ctx)
return expect(html).to.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
expect(html).to.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
})
describe('static partial', function () {
@@ -152,7 +184,7 @@ describe('tags/render', function () {
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('Xchild with redY')
expect(html).to.equal('Xchild with redY')
})
it('should support parent paths', async function () {
@@ -162,7 +194,7 @@ describe('tags/render', function () {
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('XchildY')
expect(html).to.equal('XchildY')
})
it('should support subpaths', async function () {
@@ -172,7 +204,7 @@ describe('tags/render', function () {
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('XchildY')
expect(html).to.equal('XchildY')
})
it('should support comma separated arguments', async function () {
@@ -182,7 +214,7 @@ describe('tags/render', function () {
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('Xchild with redY')
expect(html).to.equal('Xchild with redY')
})
})
describe('sync support', function () {
@@ -192,23 +224,31 @@ describe('tags/render', function () {
'/bar/foo.html': 'foo'
})
const html = liquid.renderFileSync('/current.html')
return expect(html).to.equal('barfoobar')
expect(html).to.equal('barfoobar')
})
it('should support template string', function () {
it('should support value string', function () {
mock({
'/current.html': 'bar{% render name" %}bar',
'/current.html': 'bar{% render name %}bar',
'/bar/foo.html': 'foo'
})
const html = liquid.renderFileSync('/current.html', { name: '/bar/foo.html' })
return expect(html).to.equal('barfoobar')
expect(html).to.equal('barfoobar')
})
it('should support render: with', function () {
it('should support template string', function () {
mock({
'/current.html': 'bar{% render "/bar/{{name}}" %}bar',
'/bar/foo.html': 'foo'
})
const html = liquid.renderFileSync('/current.html', { name: '/foo.html' })
expect(html).to.equal('barfoobar')
})
it('should support with', function () {
mock({
'/with.html': '{% render "color" with "red", shape: "rect" %}',
'/color.html': 'color:{{color}}, shape:{{shape}}'
})
const html = liquid.renderFileSync('with.html')
return expect(html).to.equal('color:red, shape:rect')
expect(html).to.equal('color:red, shape:rect')
})
it('should support filename with extention', function () {
mock({
@@ -217,7 +257,7 @@ describe('tags/render', function () {
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = staticLiquid.renderFileSync('parent.html')
return expect(html).to.equal('Xchild with redY')
expect(html).to.equal('Xchild with redY')
})
})
})
+52 -1
View File
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { Liquid } from '../../../src/liquid'
import { Liquid, Template } from '../../../src/liquid'
import { mock, restore } from '../../stub/mockfs'
describe('LiquidOptions#cache', function () {
@@ -18,6 +18,19 @@ describe('LiquidOptions#cache', function () {
const y = await engine.renderFile('files/foo')
expect(y).to.equal('bar')
})
it('should be disabled when cache <= 0', async function () {
const engine = new Liquid({
root: '/root/',
extname: '.html',
cache: -1
})
mock({ '/root/files/foo.html': 'foo' })
const x = await engine.renderFile('files/foo')
expect(x).to.equal('foo')
mock({ '/root/files/foo.html': 'bar' })
const y = await engine.renderFile('files/foo')
expect(y).to.equal('bar')
})
it('should respect cache=true option', async function () {
const engine = new Liquid({
root: '/root/',
@@ -31,6 +44,44 @@ describe('LiquidOptions#cache', function () {
const y = await engine.renderFile('files/foo')
expect(y).to.equal('foo')
})
it('should respect cache=2 option', async function () {
const engine = new Liquid({
root: '/root/',
extname: '.html',
cache: 2
})
mock({ '/root/files/foo.html': 'foo' })
mock({ '/root/files/bar.html': 'bar' })
mock({ '/root/files/coo.html': 'coo' })
await engine.renderFile('files/foo')
mock({ '/root/files/foo.html': 'FOO' })
await engine.renderFile('files/bar')
const x = await engine.renderFile('files/foo')
expect(x).to.equal('foo')
await engine.renderFile('files/bar')
await engine.renderFile('files/coo')
const y = await engine.renderFile('files/foo')
expect(y).to.equal('FOO')
})
it('should respect cache={} option', async function () {
let last: Template[] | undefined
const engine = new Liquid({
root: '/root/',
extname: '.html',
cache: {
read: (): Template[] | undefined => last,
has: (): boolean => !!last,
write: (key: string, value: Template[]) => { last = value }
}
})
mock({ '/root/files/foo.html': 'foo' })
mock({ '/root/files/bar.html': 'bar' })
mock({ '/root/files/coo.html': 'coo' })
expect(await engine.renderFile('files/foo')).to.equal('foo')
expect(await engine.renderFile('files/bar')).to.equal('foo')
expect(await engine.renderFile('files/coo')).to.equal('foo')
})
it('should not cache not exist file', async function () {
const engine = new Liquid({
root: '/root/',
+4 -1
View File
@@ -1,8 +1,11 @@
import { expect } from 'chai'
import { expect, use } from 'chai'
import { RenderError } from '../../../src/util/error'
import { Liquid } from '../../../src/liquid'
import * as path from 'path'
import { mock, restore } from '../../stub/mockfs'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
let engine = new Liquid()
const strictEngine = new Liquid({
+14 -14
View File
@@ -1,5 +1,5 @@
import { isString, forOwn } from '../../src/util/underscore'
import fs from '../../src/fs/node'
import * as fs from '../../src/fs/node'
import { resolve } from 'path'
interface FileDescriptor {
@@ -15,28 +15,28 @@ export function mock (options: { [path: string]: (string | FileDescriptor) }) {
files[resolve(key)] = isString(val)
? { mode: '33188', content: val }
: val as FileDescriptor
})
fs.readFile = async function (path) {
});
(fs as any).readFile = async function (path: string) {
return fs.readFileSync(path)
}
fs.readFileSync = function (path) {
};
(fs as any).readFileSync = function (path: string) {
const file = files[path]
if (file === undefined) throw new Error('ENOENT')
if (file.mode === '0000') throw new Error('EACCES')
return file.content
}
fs.exists = async function (path: string) {
};
(fs as any).exists = async function (path: string) {
return fs.existsSync(path)
}
fs.existsSync = function (path: string) {
};
(fs as any).existsSync = function (path: string) {
return !!files[path]
}
}
export function restore () {
files = {}
fs.readFileSync = readFileSync
fs.existsSync = existsSync
fs.readFile = readFile
fs.exists = exists
files = {};
(fs as any).readFileSync = readFileSync;
(fs as any).existsSync = existsSync;
(fs as any).readFile = readFile;
(fs as any).exists = exists
}
+36
View File
@@ -0,0 +1,36 @@
import { expect } from 'chai'
import { LRU } from '../../../src/cache/lru'
describe('LRU', () => {
it('should perform read()/write()', () => {
const lru = new LRU(2)
expect(lru.limit).to.equal(2)
lru.write('foo', 'FOO')
lru.write('bar', 'BAR')
expect(lru.read('foo')).to.equal('FOO')
expect(lru.read('bar')).to.equal('BAR')
})
it('should perform clear()', () => {
const lru = new LRU(2)
lru.write('foo', 'FOO')
lru.write('bar', 'BAR')
expect(lru.size).to.equal(2)
lru.clear()
expect(lru.size).to.equal(0)
expect(lru.read('foo')).to.be.undefined
})
it('should remove lrc item when full(2)', () => {
const lru = new LRU(2)
expect(lru.size).to.equal(0)
lru.write('foo', 'FOO')
expect(lru.size).to.equal(1)
lru.write('bar', 'BAR')
expect(lru.size).to.equal(2)
lru.write('coo', 'COO')
expect(lru.size).to.equal(2)
expect(lru.read('foo')).to.be.undefined
expect(lru.read('bar')).to.equal('BAR')
expect(lru.read('coo')).to.equal('COO')
})
})
+1 -1
View File
@@ -1,4 +1,4 @@
import fs from '../../../src/fs/browser'
import * as fs from '../../../src/fs/browser'
import * as sinon from 'sinon'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
+1 -1
View File
@@ -1,4 +1,4 @@
import fs from '../../../src/fs/node'
import * as fs from '../../../src/fs/node'
import * as path from 'path'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
-43
View File
@@ -1,43 +0,0 @@
import { tokenize } from '../../../src/parser/expression-tokenizer'
import { expect } from 'chai'
describe('expression tokenizer', () => {
describe('spaces', () => {
it('should tokenize a + b', () => {
expect([...tokenize('a + b')]).to.deep.equal(['a', '+', 'b'])
})
it('should tokenize a==1', () => {
expect([...tokenize('a==1')]).to.deep.equal(['a', '==', '1'])
})
})
describe('range', () => {
it('should tokenize (1..3) contains 3', () => {
expect([...tokenize('(1..3)')]).to.deep.equal(['(1..3)'])
})
})
describe('bracket', () => {
it('should tokenize a[b] = c', () => {
expect([...tokenize('a[b] = c')]).to.deep.equal(['a[b]', '=', 'c'])
})
it('should tokenize c[a["b"]] < c', () => {
expect([...tokenize('c[a["b"]] < c')]).to.deep.equal(['c[a["b"]]', '<', 'c'])
})
it('should tokenize "][" == var', () => {
expect([...tokenize('"][" == var')]).to.deep.equal(['"]["', '==', 'var'])
})
})
describe('quotes', () => {
it('should tokenize " " == var', () => {
expect([...tokenize('" " == var')]).to.deep.equal(['" "', '==', 'var'])
})
it('should tokenize "\\\'" == var', () => {
expect([...tokenize('"\\\'" == var')]).to.deep.equal(['"\\\'"', '==', 'var'])
})
it('should tokenize "\\"" == var', () => {
expect([...tokenize('"\\"" == var')]).to.deep.equal(['"\\""', '==', 'var'])
})
})
})
-12
View File
@@ -1,12 +0,0 @@
import * as chai from 'chai'
import { isRange } from '../../../src/parser/lexical'
const expect = chai.expect
describe('lexical', function () {
it('should test range literal', function () {
expect(isRange('(12..32)')).to.equal(true)
expect(isRange('(12..foo)')).to.equal(true)
expect(isRange('(foo.bar..foo)')).to.equal(true)
})
})
+197 -74
View File
@@ -4,84 +4,207 @@ 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('#tokenize()', function () {
it('should handle plain HTML', function () {
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
const tokens = tokenizer.tokenize(html)
describe('Tokenize', function () {
it('should read quoted', () => {
expect(new Tokenizer('"foo" ff').readQuoted()).to.equal('"foo"')
expect(new Tokenizer(' "foo"ff').readQuoted()).to.equal('"foo"')
})
it('should read property access', () => {
expect(new Tokenizer('a[ b][ "c d" ]').readPropertyAccess()).to.equal('a[b]["c d"]')
expect(new Tokenizer('a.b[c[d.e]]').readPropertyAccess()).to.equal('a.b[c[d.e]]')
})
it('should read value', () => {
expect(new Tokenizer('2.33.2').readValue()).to.equal('2.33.2')
expect(new Tokenizer('"foo"a').readValue()).to.equal('"foo"')
expect(new Tokenizer('a[b]["c d"]').readValue()).to.equal('a[b]["c d"]')
})
it('should read hash', () => {
expect(new Tokenizer('foo: 3').readHash()).to.deep.equal(['foo', '3'])
expect(new Tokenizer(', foo: a[ "bar"]').readHash()).to.deep.equal(['foo', 'a["bar"]'])
})
it('should read hashs', () => {
expect(new Tokenizer(', limit: 3 reverse offset:off').readHashes())
.to.deep.equal([['limit', '3'], ['reverse', ''], ['offset', 'off']])
expect(new Tokenizer('cols: 2, rows: data["rows"]').readHashes())
.to.deep.equal([['cols', '2'], ['rows', 'data["rows"]']])
})
it('should read HTML token', function () {
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(1)
expect(tokens[0].value).to.equal(html)
expect(tokens[0]).instanceOf(HTMLToken)
})
it('should handle tag syntax', function () {
const html = '<p>{% for p in a[1]%}</p>'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0].content).to.equal(html)
expect(tokens[0]).instanceOf(HTMLToken)
})
it('should read tag token', function () {
const html = '<p>{% for p in a[1]%}</p>'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(TagToken)
expect(tokens[1].value).to.equal('for p in a[1]')
})
it('should handle value syntax', function () {
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(TagToken)
expect(tokens[1].content).to.equal('for p in a[1]')
})
it('should read value token', function () {
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(OutputToken)
expect(tokens[1].value).to.equal('foo | date: "%Y-%m-%d"')
})
it('should handle consecutive value and tags', function () {
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(3)
expect(tokens[1]).instanceOf(OutputToken)
expect(tokens[1].content).to.equal('foo | date: "%Y-%m-%d"')
})
it('should handle consecutive value and tags', function () {
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(4)
expect(tokens[0]).instanceOf(OutputToken)
expect(tokens[2]).instanceOf(TagToken)
expect(tokens.length).to.equal(4)
expect(tokens[0]).instanceOf(OutputToken)
expect(tokens[2]).instanceOf(TagToken)
expect(tokens[1].value).to.equal('bar')
expect(tokens[2].value).to.equal('foo')
})
it('should keep white spaces and newlines', function () {
const html = '{%foo%}\n{%bar %} \n {%alice%}'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(5)
expect(tokens[1]).instanceOf(HTMLToken)
expect(tokens[1].raw).to.equal('\n')
expect(tokens[3]).instanceOf(HTMLToken)
expect(tokens[3].raw).to.equal(' \n ')
})
it('should handle multiple lines tag', function () {
const html = '{%foo\na:a\nb:1.23\n%}'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(TagToken)
expect((tokens[0] as TagToken).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 () {
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
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}}')
})
it('should handle complex object property access', function () {
const html = '{{ obj["my:property with anything"] }}'
const tokens = tokenizer.tokenize(html)
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(OutputToken)
expect(tokens[0].value).to.equal('obj["my:property with anything"]')
})
it('should throw if tag not closed', function () {
expect(() => {
tokenizer.tokenize('{% assign foo = bar {{foo}}')
}).to.throw(/tag "{% assign foo..." not closed/)
})
it('should throw if output not closed', function () {
expect(() => {
tokenizer.tokenize('{{name}')
}).to.throw(/output "{{name}" not closed/)
})
expect(tokens[1].content).to.equal('bar')
expect(tokens[2].content).to.equal('foo')
})
it('should keep white spaces and newlines', function () {
const html = '{%foo%}\n{%bar %} \n {%alice%}'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(5)
expect(tokens[1]).instanceOf(HTMLToken)
expect(tokens[1].raw).to.equal('\n')
expect(tokens[3]).instanceOf(HTMLToken)
expect(tokens[3].raw).to.equal(' \n ')
})
it('should handle multiple lines tag', function () {
const html = '{%foo\na:a\nb:1.23\n%}'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(TagToken)
expect((tokens[0] as TagToken).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 () {
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
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}}')
})
it('should handle complex object property access', function () {
const html = '{{ obj["my:property with anything"] }}'
const tokenizer = new Tokenizer(html)
const tokens = tokenizer.readTokens()
expect(tokens.length).to.equal(1)
expect(tokens[0]).instanceOf(OutputToken)
expect(tokens[0].content).to.equal('obj["my:property with anything"]')
})
it('should throw if tag not closed', function () {
const html = '{% assign foo = bar {{foo}}'
const tokenizer = new Tokenizer(html)
expect(() => tokenizer.readTokens()).to.throw(/tag "{% assign foo..." not closed/)
})
it('should throw if output not closed', function () {
const tokenizer = new Tokenizer('{{name}')
expect(() => tokenizer.readTokens()).to.throw(/output "{{name}" not closed/)
})
it('should read a simple filter', function () {
const tokenizer = new Tokenizer('| plus')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal([])
})
it('should read a filter with argument', function () {
const tokenizer = new Tokenizer(' | plus: 1')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal(['1'])
})
it('should read a filter with colon but no argument', function () {
const tokenizer = new Tokenizer('| plus:')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal([])
})
it('should read a filter with k/v argument', function () {
const tokenizer = new Tokenizer(' | plus: a:1')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal([['a', '1']])
})
it('should read a filter with "arr[0]" argument', function () {
const tokenizer = new Tokenizer('| plus: arr[0]')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal(['arr[0]'])
})
it('should read a filter with obj.foo argument', function () {
const tokenizer = new Tokenizer('| plus: obj.foo')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal(['obj.foo'])
})
it('should read a filter with obj["foo"] argument', function () {
const tokenizer = new Tokenizer('| plus: obj["good luck"]')
const token = tokenizer.readFilterToken()
expect(token).to.have.property('name', 'plus')
expect(token).to.have.property('args').to.deep.equal(['obj["good luck"]'])
})
it('should read simple filters', function () {
const tokenizer = new Tokenizer('| plus: 3 | capitalize')
const tokens = tokenizer.readFilterTokens()
expect(tokens).to.have.lengthOf(2)
expect(tokens[0]).to.have.property('name', 'plus')
expect(tokens[0]).to.have.property('args').to.deep.equal(['3'])
expect(tokens[1]).to.have.property('name', 'capitalize')
expect(tokens[1]).to.have.property('args').to.deep.equal([])
})
it('should read filters', function () {
const tokenizer = new Tokenizer('| plus: 3 | capitalize | append: foo[a.b["c d"]]')
const tokens = tokenizer.readFilterTokens()
expect(tokens).to.have.lengthOf(3)
expect(tokens[0]).to.have.property('name', 'plus')
expect(tokens[0]).to.have.property('args').to.deep.equal(['3'])
expect(tokens[1]).to.have.property('name', 'capitalize')
expect(tokens[1]).to.have.property('args').to.deep.equal([])
expect(tokens[2]).to.have.property('name', 'append')
expect(tokens[2]).to.have.property('args').to.deep.equal(['foo[a.b["c d"]]'])
})
it('should read expression `a==b`', () => {
const exp = new Tokenizer('a==b').readExpression()
expect([...exp]).to.deep.equal(['a', '==', 'b'])
})
it('should read expression `^`', () => {
const exp = new Tokenizer('^').readExpression()
expect([...exp]).to.deep.equal([])
})
it('should read expression `a == b`', () => {
const exp = new Tokenizer('a == b').readExpression()
expect([...exp]).to.deep.equal(['a', '==', 'b'])
})
it('should read expression `(1..3) contains 3`', () => {
const exp = new Tokenizer('(1..3) contains 3').readExpression()
expect([...exp]).to.deep.equal(['(1..3)', 'contains', '3'])
})
it('should read expression `a[b] = c`', () => {
const exp = new Tokenizer('a[b] = c').readExpression()
expect([...exp]).to.deep.equal(['a[b]', '=', 'c'])
})
it('should read expression `c[a["b"]] >= c`', () => {
const exp = new Tokenizer('c[a["b"]] >= c').readExpression()
expect([...exp]).to.deep.equal(['c[a["b"]]', '>=', 'c'])
})
it('should read expression `"][" == var`', () => {
const exp = new Tokenizer('"][" == var').readExpression()
expect([...exp]).to.deep.equal(['"]["', '==', 'var'])
})
it('should read expression `"\\\'" == "\\""`', () => {
const exp = new Tokenizer('"\\\'" == "\\""').readExpression()
expect([...exp]).to.deep.equal(['"\\\'"', '==', '"\\""'])
})
})
+1 -1
View File
@@ -14,7 +14,7 @@ describe('render', function () {
describe('.renderTemplates()', function () {
it('should render html', async function () {
const scope = new Context()
const token = { type: 'html', value: '<p>' } as Token
const token = { type: 'html', content: '<p>' } as Token
const html = await toThenable(render.renderTemplates([new HTML(token)], scope))
return expect(html).to.equal('<p>')
})
+31 -4
View File
@@ -6,12 +6,39 @@ import { Context } from '../../../src/context/context'
const expect = chai.expect
describe('Hash', function () {
it('should parse variable', async function () {
const hash = await toThenable(Hash.create('num:foo', new Context({ foo: 3 })))
it('should parse "reverse"', async function () {
const hash = await toThenable(new Hash('reverse').render(new Context({ foo: 3 })))
expect(hash).to.haveOwnProperty('reverse')
expect(hash.reverse).to.be.undefined
})
it('should parse "num:foo"', async function () {
const hash = await toThenable(new Hash('num:foo').render(new Context({ foo: 3 })))
expect(hash.num).to.equal(3)
})
it('should parse literals', async function () {
const hash = await toThenable(Hash.create('num:3', new Context()))
it('should parse "num:3"', async function () {
const hash = await toThenable(new Hash('num:3').render(new Context()))
expect(hash.num).to.equal(3)
})
it('should parse "num: arr[0]"', async function () {
const hash = await toThenable(new Hash('num:3').render(new Context({ arr: [3] })))
expect(hash.num).to.equal(3)
})
it('should parse "num: 2.3"', async function () {
const hash = await toThenable(new Hash('num:2.3').render(new Context()))
expect(hash.num).to.equal(2.3)
})
it('should parse "num:bar.coo"', async function () {
const hash = await toThenable(new Hash('num:bar.coo').render(new Context({ bar: { coo: 3 } })))
expect(hash.num).to.equal(3)
})
it('should parse "num1:2.3 reverse,num2:bar.coo\n num3: arr[0]"', async function () {
const ctx = new Context({ bar: { coo: 3 }, arr: [4] })
const hash = await toThenable(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
expect(hash).to.deep.equal({
num1: 2.3,
reverse: undefined,
num2: 3,
num3: 4
})
})
})
+4 -4
View File
@@ -19,25 +19,25 @@ describe('Output', function () {
const scope = new Context({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ value: 'foo' } as OutputToken, filters)
const output = new Output({ content: 'foo' } as OutputToken, filters)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('[object Object]')
})
it('should skip function property', async function () {
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
const output = new Output({ value: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('[object Object]')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('FOO')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('FOO')
})
+4 -44
View File
@@ -32,7 +32,8 @@ describe('Tag', function () {
expect(function () {
new Tag({ // eslint-disable-line
type: 'tag',
value: 'foo',
content: 'foo',
args: '',
name: 'not-exist'
} as TagToken, [], liquid)
}).to.throw(/tag "not-exist" not found/)
@@ -49,52 +50,11 @@ describe('Tag', function () {
liquid.registerTag('foo', { render: spy })
const token = {
type: 'tag',
value: 'foo',
content: 'foo',
args: '',
name: 'foo'
} as TagToken
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.called
})
describe('hash', function () {
let spy: sinon.SinonSpy, token: TagToken
beforeEach(function () {
spy = sinon.spy()
liquid.registerTag('foo', { render: spy })
token = {
type: 'tag',
value: 'foo aa:foo bb: arr[0] cc: 2.3\ndd:bar.coo',
name: 'foo',
args: 'aa:foo bb: arr[0] cc: 2.3\ndd:bar.coo'
} as TagToken
})
it('should call tag.render with scope', async function () {
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.calledWithMatch(ctx)
})
it('should resolve identifier hash', async function () {
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.calledWithMatch({}, {
aa: 'bar'
})
})
it('should accept space between key/value', async function () {
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.calledWithMatch({}, {
bb: 2
})
})
it('should resolve number value hash', async function () {
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.calledWithMatch(ctx, {
cc: 2.3
})
})
it('should resolve property access hash', async function () {
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
expect(spy).to.have.been.calledWithMatch(ctx, {
dd: 'uoo'
})
})
})
})
-27
View File
@@ -83,33 +83,6 @@ describe('Value', function () {
})
})
describe('#tokenize()', function () {
it('should tokenize a simple value', function () {
expect(Value.tokenize('foo')).to.eql(['foo'])
})
it('should tokenize a value with spaces', function () {
expect(Value.tokenize(' foo \t')).to.eql(['foo'])
})
it('should tokenize a simple filter', function () {
expect(Value.tokenize('foo | add')).to.eql(['foo', '|', 'add'])
})
it('should tokenize a filter with a single argument', function () {
expect(Value.tokenize('foo | add: 1')).to.eql(['foo', '|', 'add', ':', '1'])
})
it('should tokenize array indexing', function () {
expect(Value.tokenize('arr[0]')).to.eql(['arr[0]'])
})
it('should tokenize simple object access', function () {
expect(Value.tokenize('obj["foo"]')).to.eql(['obj["foo"]'])
})
it('should tokenize simple dot syntax object access', function () {
expect(Value.tokenize('obj.foo')).to.eql(['obj.foo'])
})
it('should tokenize complex object property access', function () {
expect(Value.tokenize('obj["complex:string here"]')).to.eql(['obj["complex:string here"]'])
})
})
describe('#value()', function () {
it('should call chained filters correctly', async function () {
const date = sinon.stub().returns('y')