fix: some filters on undefined variable throws, #140

This commit is contained in:
harttle
2019-07-22 16:48:48 +08:00
committed by Jun Yang
parent dc9a6e019e
commit 6e6ea0ae58
8 changed files with 228 additions and 80 deletions
+78 -10
View File
@@ -40,17 +40,85 @@ describe('filters/array', function () {
})
})
describe('size', function () {
it('should return string length',
() => test('{{ "Ground control to Major Tom." | size }}', '28'))
it('should return array size', function () {
return test('{% assign my_array = "apples, oranges, peaches, plums"' +
' | split: ", " %}{{ my_array | size }}',
'4')
it('should return string length', async () => {
const html = await liquid.parseAndRender('{{ "Ground control to Major Tom." | size }}')
expect(html).to.equal('28')
})
it('should return array size', async () => {
const html = await liquid.parseAndRender(
'{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array | size }}')
expect(html).to.equal('4')
})
it('should be respected with <string>.size notation', async () => {
const html = await liquid.parseAndRender('{% assign my_string = "Ground control to Major Tom." %}{{ my_string.size }}')
expect(html).to.equal('28')
})
it('should be respected with <array>.size notation', async () => {
const html = await liquid.parseAndRender('{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array.size }}')
expect(html).to.equal('4')
})
it('should return 0 for false', async () => {
const html = await liquid.parseAndRender('{{ false | size }}')
expect(html).to.equal('0')
})
it('should return 0 for nil', async () => {
const html = await liquid.parseAndRender('{{ nil | size }}')
expect(html).to.equal('0')
})
it('should return 0 for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | size }}')
expect(html).to.equal('0')
})
})
describe('first', function () {
it('should support first', async () => {
const html = await liquid.parseAndRender(
'{{arr | first}}',
{ arr: [ 'zebra', 'tiger' ] }
)
expect(html).to.equal('zebra')
})
it('should return empty for nil', async () => {
const html = await liquid.parseAndRender('{{nil | first}}')
expect(html).to.equal('')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{foo | first}}')
expect(html).to.equal('')
})
it('should return empty for false', async () => {
const html = await liquid.parseAndRender('{{false | first}}')
expect(html).to.equal('')
})
it('should return empty for string', async () => {
const html = await liquid.parseAndRender('{{"zebra" | first}}')
expect(html).to.equal('')
})
})
describe('last', function () {
it('should support last', async () => {
const html = await liquid.parseAndRender(
'{{arr | last}}',
{ arr: [ 'zebra', 'tiger' ] }
)
expect(html).to.equal('tiger')
})
it('should return empty for nil', async () => {
const html = await liquid.parseAndRender('{{nil | last}}')
expect(html).to.equal('')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{foo | last}}')
expect(html).to.equal('')
})
it('should return empty for false', async () => {
const html = await liquid.parseAndRender('{{false | last}}')
expect(html).to.equal('')
})
it('should return empty for string', async () => {
const html = await liquid.parseAndRender('{{"zebra" | last}}')
expect(html).to.equal('')
})
it('should be respected with <string>.size notation',
() => test('{% assign my_string = "Ground control to Major Tom." %}{{ my_string.size }}', '28'))
it('should be respected with <array>.size notation',
() => test('{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array.size }}', '4'))
})
describe('slice', function () {
it('should slice first char by 0', () => test('{{ "Liquid" | slice: 0 }}', 'L'))
+4
View File
@@ -60,6 +60,10 @@ describe('filters/math', function () {
() => test('{{ 183.357 | plus: 12 }}', '195.357'))
it('should convert first arg as number', () => test('{{ "4" | plus: 2 }}', '6'))
it('should convert both args as number', () => test('{{ "4" | plus: "2" }}', '6'))
it('should support variable', async () => {
const html = await l.parseAndRender('{{ 4 | plus: b }}', { b: 2 })
expect(html).to.equal('6')
})
})
describe('sort_natural', function () {
+58 -12
View File
@@ -1,13 +1,30 @@
import { test } from '../../../stub/render'
import Liquid from '../../../../src/liquid'
import { expect } from 'chai'
describe('filters/string', function () {
let liquid: Liquid
beforeEach(function () {
liquid = new Liquid()
})
describe('append', function () {
it('should return "-3abc" for -3, "abc"',
() => test('{{ -3 | append: "abc" }}', '-3abc'))
it('should return "abar" for "a",foo', () => test('{{ "a" | append: foo }}', 'abar'))
})
describe('capitalize', function () {
it('should capitalize first', () => test('{{ "i am good" | capitalize }}', 'I am good'))
it('should capitalize first', async () => {
const html = await liquid.parseAndRender('{{ "i am good" | capitalize }}')
expect(html).to.equal('I am good')
})
it('should return empty for nil', async () => {
const html = await liquid.parseAndRender('{{ nil | capitalize }}')
expect(html).to.equal('')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | capitalize }}')
expect(html).to.equal('')
})
})
describe('concat', function () {
it('should concat arrays', () => test(`
@@ -45,10 +62,18 @@ describe('filters/string', function () {
`))
})
describe('downcase', function () {
it('should return "parker moore" for "Parker Moore"',
() => test('{{ "Parker Moore" | downcase }}', 'parker moore'))
it('should return "apple" for "apple"',
() => test('{{ "apple" | downcase }}', 'apple'))
it('should return "parker moore" for "Parker Moore"', async () => {
const html = await liquid.parseAndRender('{{ "Parker Moore" | downcase }}')
expect(html).to.equal('parker moore')
})
it('should return "apple" for "apple"', async () => {
const html = await liquid.parseAndRender('{{ "apple" | downcase }}')
expect(html).to.equal('apple')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | downcase }}')
expect(html).to.equal('')
})
})
describe('split', function () {
it('should support split/first', function () {
@@ -57,7 +82,16 @@ describe('filters/string', function () {
return test(src, 'apples')
})
})
it('should support upcase', () => test('{{ "Parker Moore" | upcase }}', 'PARKER MOORE'))
describe('upcase', function () {
it('should support upcase', async () => {
const html = await liquid.parseAndRender('{{ "Parker Moore" | upcase }}')
expect(html).to.equal('PARKER MOORE')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | upcase }}')
expect(html).to.equal('')
})
})
it('should support lstrip', function () {
const src = '{{ " So much room for activities! " | lstrip }}'
return test(src, 'So much room for activities! ')
@@ -78,13 +112,25 @@ describe('filters/string', function () {
'{{ "/index.html" | prepend: url }}',
'liquidmarkup.com/index.html')
})
it('should support remove', function () {
return test('{{ "I strained to see the train through the rain" | remove: "rain" }}',
'I sted to see the t through the ')
describe('remove', function () {
it('should support remove', async () => {
const html = await liquid.parseAndRender('{{ "I strained to see the train through the rain" | remove: "rain" }}')
expect(html).to.equal('I sted to see the t through the ')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | remove: "rain" }}')
expect(html).to.equal('')
})
})
it('should support remove_first', function () {
return test('{{ "I strained to see the train through the rain" | remove_first: "rain" }}',
'I sted to see the train through the rain')
describe('remove_first', function () {
it('should support remove_first', async () => {
const html = await liquid.parseAndRender('{{ "I strained to see the train through the rain" | remove_first: "rain" }}')
expect(html).to.equal('I sted to see the train through the rain')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | remove_first: "r" }}')
expect(html).to.equal('')
})
})
it('should support replace', function () {
return test('{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}',