mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
@@ -59,6 +59,12 @@ describe('fs/browser', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#existsSync()', () => {
|
||||
it('should always return true', function () {
|
||||
expect(fs.existsSync('/foo/bar')).to.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#readFile()', () => {
|
||||
let server: sinon.SinonFakeServer
|
||||
beforeEach(() => {
|
||||
@@ -87,4 +93,29 @@ describe('fs/browser', function () {
|
||||
return result
|
||||
})
|
||||
})
|
||||
|
||||
describe('#readFileSync()', () => {
|
||||
let server: sinon.SinonFakeServer
|
||||
beforeEach(() => {
|
||||
server = sinon.fakeServer.create()
|
||||
server.autoRespond = true
|
||||
server.respondWith(
|
||||
'GET', 'https://example.com/views/hello.html',
|
||||
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}']
|
||||
);
|
||||
(global as any).XMLHttpRequest = sinon.useFakeXMLHttpRequest()
|
||||
})
|
||||
afterEach(() => {
|
||||
server.restore()
|
||||
delete (global as any).XMLHttpRequest
|
||||
})
|
||||
it('should get corresponding text', function () {
|
||||
const html = fs.readFileSync('https://example.com/views/hello.html')
|
||||
return expect(html).to.equal('hello {{name}}')
|
||||
})
|
||||
it('should throw 404', () => {
|
||||
return expect(() => fs.readFileSync('https://example.com/not/exist.html'))
|
||||
.to.throw('Not Found')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+23
-6
@@ -6,7 +6,7 @@ import * as chaiAsPromised from 'chai-as-promised'
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('fs', function () {
|
||||
describe('#resolve()', function () {
|
||||
describe('.resolve()', function () {
|
||||
it('should resolve based on root', async function () {
|
||||
const filepath = fs.resolve('/foo', 'bar.html', '.liquid')
|
||||
const expected = path.resolve('/foo/bar.html')
|
||||
@@ -18,23 +18,40 @@ describe('fs', function () {
|
||||
return expect(filepath).to.equal(expected)
|
||||
})
|
||||
})
|
||||
describe('#exists', () => {
|
||||
describe('.existsSync', () => {
|
||||
it('should resolve as false if not exists', () => {
|
||||
expect(fs.existsSync('/foo/bar')).to.be.false
|
||||
})
|
||||
it('should resolve as true if exists', () => {
|
||||
expect(fs.existsSync(__filename)).to.be.true
|
||||
})
|
||||
})
|
||||
describe('.exists', () => {
|
||||
it('should resolve as false if not exists', async () => {
|
||||
const result = await fs.exists('/foo/bar')
|
||||
return expect(result).to.be.false
|
||||
expect(result).to.be.false
|
||||
})
|
||||
it('should resolve as true if exists', async () => {
|
||||
const result = await fs.exists(__filename)
|
||||
return expect(result).to.be.true
|
||||
expect(result).to.be.true
|
||||
})
|
||||
})
|
||||
describe('#readFile', function () {
|
||||
describe('.readFileSync', function () {
|
||||
it('should throw when not exist', function () {
|
||||
return expect(() => fs.readFileSync('/foo/bar')).to.throw('ENOENT')
|
||||
})
|
||||
it('should read content if exists', function () {
|
||||
const content = fs.readFileSync(__filename)
|
||||
expect(content).to.contain('should read content if exists')
|
||||
})
|
||||
})
|
||||
describe('.readFile', function () {
|
||||
it('should throw when not exist', function () {
|
||||
return expect(fs.readFile('/foo/bar')).to.rejectedWith('ENOENT')
|
||||
})
|
||||
it('should read content if exists', async function () {
|
||||
const content = await fs.readFile(__filename)
|
||||
return expect(content).to.contain('should read content if exists')
|
||||
expect(content).to.contain('should read content if exists')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,51 +17,56 @@ describe('Expression', function () {
|
||||
})
|
||||
|
||||
it('should throw when context not defined', async function () {
|
||||
return expect(() => new Expression().value()).to.throw(/context not defined/)
|
||||
return expect(new Expression().value()).to.be.rejectedWith(/context not defined/)
|
||||
})
|
||||
|
||||
it('should eval simple expression', async function () {
|
||||
expect(new Expression('1 < 2').value(ctx)).to.equal(true)
|
||||
expect(new Expression('2 <= 2').value(ctx)).to.equal(true)
|
||||
expect(new Expression('one <= two').value(ctx)).to.equal(true)
|
||||
expect(new Expression('x contains "x"').value(ctx)).to.equal(false)
|
||||
expect(new Expression('x contains "X"').value(ctx)).to.equal(true)
|
||||
expect(new Expression('1 contains "x"').value(ctx)).to.equal(false)
|
||||
expect(new Expression('y contains "x"').value(ctx)).to.equal(false)
|
||||
expect(new Expression('z contains "x"').value(ctx)).to.equal(false)
|
||||
expect(new Expression('(1..5) contains 3').value(ctx)).to.equal(true)
|
||||
expect(new Expression('(1..5) contains 6').value(ctx)).to.equal(false)
|
||||
expect(new Expression('"<=" == "<="').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('1 < 2').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('1 < 2').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('2 <= 2').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('one <= two').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('x contains "x"').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('x contains "X"').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('1 contains "x"').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('y contains "x"').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('z contains "x"').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('(1..5) contains 3').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('(1..5) contains 6').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('"<=" == "<="').value(ctx)).to.equal(true)
|
||||
})
|
||||
|
||||
describe('complex expression', function () {
|
||||
it('should support value or value', async function () {
|
||||
expect(new Expression('false or true').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('false or true').value(ctx)).to.equal(true)
|
||||
})
|
||||
it('should support < and contains', async function () {
|
||||
expect(new Expression('1 < 2 and x contains "x"').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('1 < 2 and x contains "x"').value(ctx)).to.equal(false)
|
||||
})
|
||||
it('should support < or contains', async function () {
|
||||
expect(new Expression('1 < 2 or x contains "x"').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('1 < 2 or x contains "x"').value(ctx)).to.equal(true)
|
||||
})
|
||||
it('should support value and !=', async function () {
|
||||
expect(new Expression('empty and empty != ""').value(ctx)).to.equal(false)
|
||||
expect(await new Expression('empty and empty != ""').value(ctx)).to.equal(false)
|
||||
})
|
||||
it('should recognize quoted value', async function () {
|
||||
expect(new Expression('">"').value(ctx)).to.equal('>')
|
||||
expect(await new Expression('">"').value(ctx)).to.equal('>')
|
||||
})
|
||||
it('should evaluate from right to left', function () {
|
||||
expect(new Expression('true or false and false').value(ctx)).to.equal(true)
|
||||
expect(new Expression('true and false and false or true').value(ctx)).to.equal(false)
|
||||
it('should evaluate from right to left', async function () {
|
||||
expect(await new Expression('true or false and false').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('true and false and false or true').value(ctx)).to.equal(false)
|
||||
})
|
||||
it('should recognize property access', function () {
|
||||
it('should recognize property access', async function () {
|
||||
const ctx = new Context({ obj: { foo: true } })
|
||||
expect(new Expression('obj["foo"] and true').value(ctx)).to.equal(true)
|
||||
expect(await new Expression('obj["foo"] and true').value(ctx)).to.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
it('should eval range expression', async function () {
|
||||
expect(new Expression('(2..4)').value(ctx)).to.deep.equal([2, 3, 4])
|
||||
expect(new Expression('(two..4)').value(ctx)).to.deep.equal([2, 3, 4])
|
||||
expect(await new Expression('(2..4)').value(ctx)).to.deep.equal([2, 3, 4])
|
||||
expect(await new Expression('(two..4)').value(ctx)).to.deep.equal([2, 3, 4])
|
||||
})
|
||||
|
||||
it('should support sync', function () {
|
||||
expect(new Expression('empty and empty != ""').valueSync(ctx)).to.equal(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,20 +5,20 @@ import { expect } from 'chai'
|
||||
describe('Value', function () {
|
||||
it('should eval number variable', async function () {
|
||||
const ctx = new Context({ one: 1 })
|
||||
expect(new Value('one').value(ctx)).to.equal(1)
|
||||
expect(new Value('one').valueSync(ctx)).to.equal(1)
|
||||
})
|
||||
it('question mark should be valid variable name', async function () {
|
||||
const ctx = new Context({ 'has_value?': true })
|
||||
expect(new Value('has_value?').value(ctx)).to.equal(true)
|
||||
expect(new Value('has_value?').valueSync(ctx)).to.equal(true)
|
||||
})
|
||||
it('should eval string variable', async function () {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(new Value('x').value(ctx)).to.equal('XXX')
|
||||
expect(new Value('x').valueSync(ctx)).to.equal('XXX')
|
||||
})
|
||||
it('should eval null literal', async function () {
|
||||
expect(new Value('null').value({})).to.be.null
|
||||
expect(new Value('null').valueSync({})).to.be.null
|
||||
})
|
||||
it('should eval nil literal', async function () {
|
||||
expect(new Value('nil').value({})).to.be.null
|
||||
expect(new Value('nil').valueSync({})).to.be.null
|
||||
})
|
||||
})
|
||||
|
||||
@@ -60,4 +60,14 @@ describe('filter', function () {
|
||||
new Filter('/', [], false)
|
||||
}).to.not.throw()
|
||||
})
|
||||
|
||||
it('should support sync', function () {
|
||||
Filter.register('add', (a, b) => a + b)
|
||||
expect(new Filter('add', ['2'], false).renderSync(3, ctx)).to.equal(5)
|
||||
})
|
||||
|
||||
it('should support key value pairs', function () {
|
||||
Filter.register('add', (a, b) => b[0] + ':' + (a + b[1]))
|
||||
expect(new Filter('add', [['num', '2']], false).renderSync(3, ctx)).to.equal('num:5')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import * as chai from 'chai'
|
||||
import { Hash } from '../../../src/template/tag/hash'
|
||||
import { Context } from '../../../src/context/context'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('Hash', function () {
|
||||
it('should parse variable', async function () {
|
||||
const hash = await Hash.create('num:foo', new Context({ foo: 3 }))
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should parse literals', async function () {
|
||||
const hash = await Hash.create('num:3', new Context())
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should support sync', function () {
|
||||
const hash = Hash.createSync('num:3', new Context())
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
})
|
||||
@@ -10,7 +10,7 @@ chai.use(sinonChai)
|
||||
const expect = chai.expect
|
||||
const liquid = new Liquid()
|
||||
|
||||
describe('tag', function () {
|
||||
describe('Tag', function () {
|
||||
let ctx: Context
|
||||
const emitter = { write: (html: string) => (emitter.html += html), html: '' }
|
||||
before(function () {
|
||||
|
||||
Reference in New Issue
Block a user