mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
feat: nil/null/empty/blank literals, resolves #102
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import Liquid from '../..'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('drop', function () {
|
||||
var engine: Liquid
|
||||
beforeEach(function () {
|
||||
engine = new Liquid()
|
||||
})
|
||||
it('should test blank strings', async function () {
|
||||
const src = `
|
||||
{% unless settings.fp_heading == blank %}
|
||||
<h1>{{ settings.fp_heading }}</h1>
|
||||
{% endunless %}`
|
||||
var ctx = { settings: { fp_heading: '' } }
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.match(/^\s+$/)
|
||||
})
|
||||
})
|
||||
@@ -62,4 +62,9 @@ describe('.parseAndRender()', function () {
|
||||
const html = await engine.parseAndRender(src)
|
||||
return expect(html).to.equal('apples')
|
||||
})
|
||||
it('should support nil(null, undefined) literal', async function () {
|
||||
const src = '{% if notexist == nil %}true{% endif %}'
|
||||
const html = await engine.parseAndRender(src)
|
||||
expect(html).to.equal('true')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -19,22 +19,20 @@ describe('tags/case', function () {
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('foo')
|
||||
})
|
||||
it('should resolve empty string if not hit', async function () {
|
||||
const src = '{% case empty %}' +
|
||||
'{% when "foo" %}foo{% when ""%}bar' +
|
||||
'{%endcase%}'
|
||||
const ctx = {
|
||||
empty: ''
|
||||
}
|
||||
const html = await liquid.parseAndRender(src, ctx)
|
||||
it('should resolve blank as empty string', async function () {
|
||||
const src = '{% case blank %}{% when ""%}bar{%endcase%}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('bar')
|
||||
})
|
||||
it('should resolve empty as empty string', async function () {
|
||||
const src = '{% case empty %}{% when ""%}bar{%endcase%}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('bar')
|
||||
})
|
||||
it('should accept empty string as branch name', async function () {
|
||||
const src = '{% case false %}' +
|
||||
'{% when "foo" %}foo{% when ""%}bar' +
|
||||
'{%endcase%}'
|
||||
const src = '{% case "" %}{% when ""%}bar{%endcase%}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('')
|
||||
return expect(html).to.equal('bar')
|
||||
})
|
||||
it('should support boolean case', async function () {
|
||||
const src = '{% case false %}' +
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import Liquid from 'src/liquid'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
import IContext from 'src/scope/icontext'
|
||||
import { Context } from 'src/scope/scope'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('tags/for', function () {
|
||||
let liquid: Liquid, ctx: IContext
|
||||
let liquid: Liquid, ctx: Context
|
||||
before(function () {
|
||||
liquid = new Liquid()
|
||||
liquid.registerTag('throwingTag', {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
|
||||
describe('drop/blank-drop', function () {
|
||||
let liquid: Liquid
|
||||
before(() => (liquid = new Liquid()))
|
||||
|
||||
it('render blank drop as blank string', async function () {
|
||||
const html = await liquid.parseAndRender('{{blank}}')
|
||||
expect(html).to.equal('')
|
||||
})
|
||||
it('blank equals nil', async function () {
|
||||
const src = '{%if blank == nil %}blank == nil{%else%}blank != nil{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('blank == nil')
|
||||
})
|
||||
it('false is blank', async function () {
|
||||
const src = '{%if false == blank %}false == blank{%else%}false != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('false == blank')
|
||||
})
|
||||
it('"" is blank', async function () {
|
||||
const src = '{%if "" == blank %}"" == blank{%else%}"" != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('"" == blank')
|
||||
})
|
||||
it('" " is blank', async function () {
|
||||
const src = '{%if " " == blank %}" " == blank{%else%}" " != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('" " == blank')
|
||||
})
|
||||
it('{} is blank', async function () {
|
||||
const src = '{%if obj == blank %}{} == blank{%else%}{} != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { obj: {} })
|
||||
expect(html).to.equal('{} == blank')
|
||||
})
|
||||
it('{foo: 1} is not blank', async function () {
|
||||
const src = '{%if obj == blank %}{foo: 1} == blank{%else%}{foo: 1} != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { obj: { foo: 1 } })
|
||||
expect(html).to.equal('{foo: 1} != blank')
|
||||
})
|
||||
it('[] is blank', async function () {
|
||||
const src = '{%if arr == blank %}[] == blank{%else%}[] != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { arr: [] })
|
||||
expect(html).to.equal('[] == blank')
|
||||
})
|
||||
it('[1] is not blank', async function () {
|
||||
const src = '{%if arr == blank %}[1] == blank{%else%}[1] != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { arr: [1] })
|
||||
expect(html).to.equal('[1] != blank')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,52 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
|
||||
describe('drop/empty-drop', function () {
|
||||
let liquid: Liquid
|
||||
before(() => (liquid = new Liquid()))
|
||||
|
||||
it('render empty drop as empty string', async function () {
|
||||
const html = await liquid.parseAndRender('{{empty}}')
|
||||
expect(html).to.equal('')
|
||||
})
|
||||
it('nil is not empty', async function () {
|
||||
const src = '{%if nil == empty %}nil == empty{%else%}nil != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('nil != empty')
|
||||
})
|
||||
it('false is not empty', async function () {
|
||||
const src = '{%if false == empty %}false == empty{%else%}false != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('false != empty')
|
||||
})
|
||||
it('"" is empty', async function () {
|
||||
const src = '{%if "" == empty %}"" == empty{%else%}"" != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('"" == empty')
|
||||
})
|
||||
it('" " is not empty', async function () {
|
||||
const src = '{%if " " == empty %}" " == empty{%else%}" " != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('" " != empty')
|
||||
})
|
||||
it('{} is empty', async function () {
|
||||
const src = '{%if obj == empty %}{} == empty{%else%}{} != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { obj: {} })
|
||||
expect(html).to.equal('{} == empty')
|
||||
})
|
||||
it('{foo: 1} is not empty', async function () {
|
||||
const src = '{%if obj == empty %}{foo: 1} == empty{%else%}{foo: 1} != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { obj: { foo: 1 } })
|
||||
expect(html).to.equal('{foo: 1} != empty')
|
||||
})
|
||||
it('[] is empty', async function () {
|
||||
const src = '{%if arr == empty %}[] == empty{%else%}[] != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { arr: [] })
|
||||
expect(html).to.equal('[] == empty')
|
||||
})
|
||||
it('[1] is not empty', async function () {
|
||||
const src = '{%if arr == empty %}[1] == empty{%else%}[1] != empty{% endif %}'
|
||||
const html = await liquid.parseAndRender(src, { arr: [1] })
|
||||
expect(html).to.equal('[1] != empty')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,36 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
|
||||
describe('drop/null-drop', function () {
|
||||
let liquid: Liquid
|
||||
before(() => (liquid = new Liquid()))
|
||||
|
||||
it('render nil as empty string', async function () {
|
||||
const html = await liquid.parseAndRender('{{nil}}')
|
||||
expect(html).to.equal('')
|
||||
})
|
||||
it('render null as empty string', async function () {
|
||||
const html = await liquid.parseAndRender('{{null}}')
|
||||
expect(html).to.equal('')
|
||||
})
|
||||
it('undefined variable should equal to null', async function () {
|
||||
const src = '{%if foo == nil %}foo == nil{%else%}foo != nil{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('foo == nil')
|
||||
})
|
||||
it('nil equals blank', async function () {
|
||||
const src = '{%if nil == blank %}nil == blank{%else%}nil != blank{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('nil == blank')
|
||||
})
|
||||
it('0 should not equal to null', async function () {
|
||||
const src = '{%if 0 == null %}0 == null{%else%}0 != null{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('0 != null')
|
||||
})
|
||||
it('nil should equal to null', async function () {
|
||||
const src = '{%if nil == null %}nil == null{%else%}nil != null{% endif %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
expect(html).to.equal('nil == null')
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,7 @@ import Scope from 'src/scope/scope'
|
||||
import { expect } from 'chai'
|
||||
import { evalExp, evalValue, isTruthy } from 'src/render/syntax'
|
||||
|
||||
describe('expression', function () {
|
||||
describe('render/syntax', function () {
|
||||
let scope: Scope
|
||||
|
||||
beforeEach(function () {
|
||||
@@ -29,10 +29,16 @@ describe('expression', function () {
|
||||
expect(evalValue('-23.', scope)).to.equal(-23)
|
||||
expect(evalValue('23', scope)).to.equal(23)
|
||||
})
|
||||
it('should eval literal', function () {
|
||||
it('should eval string literal', function () {
|
||||
expect(evalValue('"ab\'c"', scope)).to.equal("ab'c")
|
||||
expect(evalValue("'ab\"c'", scope)).to.equal('ab"c')
|
||||
})
|
||||
it('should eval nil literal', function () {
|
||||
expect(evalValue('nil', scope)).to.be.null
|
||||
})
|
||||
it('should eval null literal', function () {
|
||||
expect(evalValue('null', scope)).to.be.null
|
||||
})
|
||||
it('should eval scope variables', function () {
|
||||
expect(evalValue('one', scope)).to.equal(1)
|
||||
expect(evalValue('has_value?', scope)).to.equal(true)
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import * as chai from 'chai'
|
||||
import Scope from 'src/scope/scope'
|
||||
import IContext from 'src/scope/icontext'
|
||||
import Scope, { Context } from 'src/scope/scope'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('scope', function () {
|
||||
let scope: Scope, ctx: IContext
|
||||
let scope: Scope, ctx: Context
|
||||
beforeEach(function () {
|
||||
ctx = {
|
||||
foo: 'zoo',
|
||||
@@ -64,7 +63,7 @@ describe('scope', function () {
|
||||
expect(scope.get('foo')).equal('zoo')
|
||||
})
|
||||
|
||||
it('should get undefined property', function () {
|
||||
it('undefined property should yield undefined', function () {
|
||||
function fn () {
|
||||
scope.get('notdefined')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user