fix: math filters now return number, resolves #110

* fix linting
* numbers are no longer fixed:
    i.e. {{0.3 | minus 0.1}} now renders as 0.19999
    Stick to JavaScript number behavior, see: 8.0.0
This commit is contained in:
harttle
2019-02-28 13:02:51 +08:00
parent 21d674e22b
commit b4acdb463a
13 changed files with 1960 additions and 1981 deletions
+6 -3
View File
@@ -1,4 +1,5 @@
import { test } from '../../../stub/render'
import { expect } from 'chai'
import { test, liquid } from '../../../stub/render'
describe('filters/math', function () {
describe('abs', function () {
@@ -35,8 +36,10 @@ describe('filters/math', function () {
describe('modulo', function () {
it('should return "1" for 3,2', () => test('{{ 3 | modulo: 2 }}', '1'))
it('should return "3" for 24,7', () => test('{{ 24 | modulo: 7 }}', '3'))
it('should return "3.357" for 183.357,12',
() => test('{{ 183.357 | modulo: 12 }}', '3.357'))
it('should return "3.357" for 183.357,12', async () => {
const html = await liquid.parseAndRender('{{ 183.357 | modulo: 12 }}')
expect(Number(html)).to.be.closeTo(3.357, 0.001)
})
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
})
describe('plus', function () {
+6 -6
View File
@@ -7,29 +7,29 @@ describe('drop/drop', function () {
class CustomDrop extends Liquid.Types.Drop {
name: string = 'NAME'
getName() {
getName () {
return 'GETNAME'
}
}
class CustomDropWithMethodMissing extends CustomDrop {
liquidMethodMissing(key: string) {
liquidMethodMissing (key: string) {
return key.toUpperCase()
}
}
it('should call corresponding method', async function () {
const html = await liquid.parseAndRender(`{{obj.getName}}`, {obj: new CustomDrop()})
const html = await liquid.parseAndRender(`{{obj.getName}}`, { obj: new CustomDrop() })
expect(html).to.equal('GETNAME')
})
it('should read corresponding property', async function () {
const html = await liquid.parseAndRender(`{{obj.name}}`, {obj: new CustomDrop()})
const html = await liquid.parseAndRender(`{{obj.name}}`, { obj: new CustomDrop() })
expect(html).to.equal('NAME')
})
it('should output empty string if not exist', async function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDrop()})
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDrop() })
expect(html).to.equal('')
})
it('should respect liquidMethodMissing', async function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDropWithMethodMissing()})
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDropWithMethodMissing() })
expect(html).to.equal('FOO')
})
})
+8
View File
@@ -1,8 +1,10 @@
import Liquid from '../../../src/liquid'
import * as chai from 'chai'
import { mock, restore } from '../../stub/mockfs'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('Liquid', function () {
describe('#plugin()', function () {
@@ -34,6 +36,12 @@ describe('Liquid', function () {
const html = await engine.parseAndRender(tpl)
expect(html).to.equal('Welcome(to]Liquid')
})
it('should support for-in with variable', async function () {
const src = '{% assign total = 3 | minus: 1 %}' +
'{% for i in (1..total) %}{{ i }}{% endfor %}'
const html = await engine.parseAndRender(src, {})
return expect(html).to.equal('12')
})
})
describe('#express()', function () {
const liquid = new Liquid({ root: '/root' })