mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 22:10:41 -07:00
filters
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
var chai = require("chai");
|
||||
var should = chai.should();
|
||||
var expect = chai.expect;
|
||||
|
||||
var context = require('../context.js');
|
||||
|
||||
describe('context', function() {
|
||||
var ctx;
|
||||
beforeEach(function(){
|
||||
ctx = context.factory({
|
||||
foo: 'bar',
|
||||
bar: ['a', {b: [1, 2]}]
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse literal', function() {
|
||||
ctx.get('2.3').should.equal(2.3);
|
||||
ctx.get('(2..4)').should.deep.equal([2,3]);
|
||||
ctx.get('"foo"').should.equal("foo");
|
||||
});
|
||||
|
||||
it('should get property', function() {
|
||||
ctx.get('foo').should.equal('bar');
|
||||
});
|
||||
|
||||
it('should set property', function() {
|
||||
ctx.set('foo', 'FOO');
|
||||
ctx.get('foo').should.equal('FOO');
|
||||
});
|
||||
|
||||
it('should get desendent property', function() {
|
||||
ctx.get('bar[0]').should.equal('a');
|
||||
ctx.get('bar[1].b').should.deep.equal([1, 2]);
|
||||
ctx.get('bar[1].b[1]').should.equal(2);
|
||||
});
|
||||
|
||||
it('should push context', function() {
|
||||
ctx.push({foo: 'foo', foo1: 'foo1'});
|
||||
ctx.get('foo').should.equal('foo');
|
||||
ctx.get('foo1').should.equal('foo1');
|
||||
ctx.get('bar[1].b[1]').should.equal(2);
|
||||
});
|
||||
|
||||
it('should pop context', function() {
|
||||
ctx.push({foo: 'foo', foo1: 'foo1'});
|
||||
ctx.pop();
|
||||
expect(ctx.get('foo')).to.equal('bar');
|
||||
expect(ctx.get('foo1')).to.equal('');
|
||||
expect(ctx.get('bar[1].b[1]')).to.equal(2);
|
||||
});
|
||||
});
|
||||
+5
-5
@@ -6,13 +6,13 @@ const expect = chai.expect;
|
||||
chai.use(sinonChai);
|
||||
|
||||
var filter = require('../filter.js')();
|
||||
var context = require('../context.js');
|
||||
var Scope = require('../scope.js');
|
||||
|
||||
describe('filter', function() {
|
||||
var ctx;
|
||||
var scope;
|
||||
beforeEach(function(){
|
||||
filter.clear();
|
||||
ctx = context.factory();
|
||||
scope = Scope.factory();
|
||||
});
|
||||
it('should throw when not registered', function() {
|
||||
expect(function() {
|
||||
@@ -22,13 +22,13 @@ describe('filter', function() {
|
||||
|
||||
it('should register a simple filter', function(){
|
||||
filter.register('foo', x => x.toUpperCase());
|
||||
expect(filter.construct('foo').render('foo', ctx)).to.equal('FOO');
|
||||
expect(filter.construct('foo').render('foo', scope)).to.equal('FOO');
|
||||
});
|
||||
|
||||
it('should call filter with corrct arguments', function(){
|
||||
var spy = sinon.spy();
|
||||
filter.register('foo', spy);
|
||||
filter.construct('foo: 33').render('foo', ctx);
|
||||
filter.construct('foo: 33').render('foo', scope);
|
||||
expect(spy).to.have.been.calledWith('foo', 33);
|
||||
});
|
||||
});
|
||||
|
||||
+38
-1
@@ -5,12 +5,49 @@ var liquid = require('..')();
|
||||
|
||||
describe('filters', function() {
|
||||
var ctx = {
|
||||
date: new Date('1995-12-17T03:24:00'),
|
||||
foo: 'bar',
|
||||
arr: [-2, 'a']
|
||||
};
|
||||
|
||||
it('should support abs', function() {
|
||||
expect(liquid.render('{{ -3 | abs }}', ctx)).to.equal('3');
|
||||
expect(liquid.render('{{ -3 | abs }}')).to.equal('3');
|
||||
expect(liquid.render('{{ arr[0] | abs }}', ctx)).to.equal('2');
|
||||
});
|
||||
|
||||
it('should support append', function() {
|
||||
expect(liquid.render('{{ -3 | append: "abc" }}')).to.equal('-3abc');
|
||||
expect(liquid.render('{{ "a" | append: foo }}', ctx)).to.equal('abar');
|
||||
});
|
||||
|
||||
it('should support capitalize', function() {
|
||||
expect(liquid.render('{{ "i am good" | capitalize }}')).to.equal('I am good');
|
||||
});
|
||||
|
||||
it('should support ceil', function() {
|
||||
expect(liquid.render('{{ 1.2 | ceil }}')).to.equal('2');
|
||||
expect(liquid.render('{{ 2.0 | ceil }}')).to.equal('2');
|
||||
expect(liquid.render('{{ "3.5" | ceil }}')).to.equal('4');
|
||||
expect(liquid.render('{{ 183.357 | ceil }}')).to.equal('184');
|
||||
});
|
||||
|
||||
it('should support date', function() {
|
||||
expect(liquid.render('{{ date | date:"%Y-%m-%d %H:%M:%S"}}', ctx)).to.equal('1995-12-17 11:24:00');
|
||||
expect(liquid.render('{{ date | date:"%a, %b %d, %y"}}', ctx)).to.equal('Sun, Dec 17, 95');
|
||||
});
|
||||
|
||||
it('should support default', function() {
|
||||
expect(liquid.render('{{false |default: "a"}}')).to.equal('a');
|
||||
});
|
||||
|
||||
it('should support divided_by', function() {
|
||||
expect(liquid.render('{{4 | divided_by: 2}}')).to.equal('2');
|
||||
expect(liquid.render('{{16 | divided_by: 4}}')).to.equal('4');
|
||||
expect(liquid.render('{{5 | divided_by: 3}}')).to.equal('1');
|
||||
});
|
||||
|
||||
it('should support downcase', function() {
|
||||
expect(liquid.render('{{ "Parker Moore" | downcase }}')).to.equal('parker moore');
|
||||
expect(liquid.render('{{ "apple" | downcase }}')).to.equal('apple');
|
||||
});
|
||||
});
|
||||
|
||||
+32
-11
@@ -6,15 +6,20 @@ const expect = chai.expect;
|
||||
chai.use(sinonChai);
|
||||
|
||||
var tag = require('../tag.js')();
|
||||
var context = require('../context.js');
|
||||
var Scope = require('../scope.js');
|
||||
var filter = require('../filter')();
|
||||
var render = require('../render.js')(filter, tag).render;
|
||||
var Render = require('../render.js')(filter, tag);
|
||||
var render = Render.render;
|
||||
var evalExp = Render.evalExp;
|
||||
var evalFilter = Render.evalFilter;
|
||||
|
||||
describe('render', function() {
|
||||
var ctx, htmlToken, tagToken, filterToken;
|
||||
var scope, htmlToken, tagToken, filterToken;
|
||||
|
||||
before(function() {
|
||||
ctx = context.factory({
|
||||
scope = Scope.factory({
|
||||
one: 1,
|
||||
two: 2,
|
||||
x: 'XXX',
|
||||
foo: {
|
||||
bar: ['a', 2]
|
||||
@@ -23,7 +28,8 @@ describe('render', function() {
|
||||
tagToken = {
|
||||
type: 'tag',
|
||||
value: 'foo bar:x foo:"FOO" num:2.3',
|
||||
name: 'foo'
|
||||
name: 'foo',
|
||||
args: 'bar:x foo:"FOO" num:2.3'
|
||||
};
|
||||
htmlToken = {
|
||||
type: 'html',
|
||||
@@ -41,21 +47,21 @@ describe('render', function() {
|
||||
});
|
||||
|
||||
it('should render html', function() {
|
||||
expect(render([htmlToken], ctx)).to.equal('<p>');
|
||||
expect(render([htmlToken], scope)).to.equal('<p>');
|
||||
});
|
||||
|
||||
it('should render with tag function', function() {
|
||||
tag.register('foo', {
|
||||
render: x => 'X'
|
||||
});
|
||||
expect(render([tagToken], ctx)).to.equal('X');
|
||||
expect(render([tagToken], scope)).to.equal('X');
|
||||
});
|
||||
|
||||
it('should call tag with correct arguments', function() {
|
||||
var spy = sinon.spy();
|
||||
tag.register('foo', { render: spy });
|
||||
render([tagToken], ctx);
|
||||
expect(spy).to.have.been.calledWithMatch([], ctx, tagToken.value, {
|
||||
render([tagToken], scope);
|
||||
expect(spy).to.have.been.calledWithMatch([], scope, tagToken, {
|
||||
bar: 'XXX',
|
||||
foo: 'FOO',
|
||||
num: 2.3
|
||||
@@ -65,7 +71,7 @@ describe('render', function() {
|
||||
it('should render with filter function', function() {
|
||||
filter.register('date', (l, r) => l + r);
|
||||
filter.register('time', (l, r) => l + 3*r);
|
||||
expect(render([filterToken], ctx)).to.equal('ab6');
|
||||
expect(render([filterToken], scope)).to.equal('ab6');
|
||||
});
|
||||
|
||||
it('should call filter with correct arguments', function() {
|
||||
@@ -73,8 +79,23 @@ describe('render', function() {
|
||||
var time = sinon.spy();
|
||||
filter.register('date', date);
|
||||
filter.register('time', time);
|
||||
render([filterToken], ctx);
|
||||
render([filterToken], scope);
|
||||
expect(date).to.have.been.calledWith('a', 'b');
|
||||
expect(time).to.have.been.calledWith('y', 2);
|
||||
});
|
||||
|
||||
it('should eval expression', function(){
|
||||
expect(evalExp('1<2', scope)).to.equal(true);
|
||||
expect(evalExp('2<=2', scope)).to.equal(true);
|
||||
expect(evalExp('one<=two', scope)).to.equal(true);
|
||||
expect(function(){
|
||||
evalExp('1 contains "x"', scope);
|
||||
}).to.throw();
|
||||
expect(evalExp('x contains "x"', scope)).to.equal(false);
|
||||
expect(evalExp('x contains "X"', scope)).to.equal(true);
|
||||
expect(evalExp('x contains z', scope)).to.equal(true);
|
||||
expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false);
|
||||
expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true);
|
||||
expect(evalExp('"<=" == "<="', scope)).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
var chai = require("chai");
|
||||
var should = chai.should();
|
||||
var expect = chai.expect;
|
||||
|
||||
var Scope = require('../scope.js');
|
||||
|
||||
describe('scope', function() {
|
||||
var scope;
|
||||
beforeEach(function(){
|
||||
scope = Scope.factory({
|
||||
foo: 'bar',
|
||||
bar: ['a', {b: [1, 2]}]
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse literal', function() {
|
||||
scope.get('2.3').should.equal(2.3);
|
||||
scope.get('(2..4)').should.deep.equal([2,3]);
|
||||
scope.get('"foo"').should.equal("foo");
|
||||
});
|
||||
|
||||
it('should get property', function() {
|
||||
scope.get('foo').should.equal('bar');
|
||||
});
|
||||
|
||||
it('should set property', function() {
|
||||
scope.set('foo', 'FOO');
|
||||
scope.get('foo').should.equal('FOO');
|
||||
});
|
||||
|
||||
it('should get desendent property', function() {
|
||||
scope.get('bar[0]').should.equal('a');
|
||||
scope.get('bar[1].b').should.deep.equal([1, 2]);
|
||||
scope.get('bar[1].b[1]').should.equal(2);
|
||||
});
|
||||
|
||||
it('should push scope', function() {
|
||||
scope.push({foo: 'foo', foo1: 'foo1'});
|
||||
scope.get('foo').should.equal('foo');
|
||||
scope.get('foo1').should.equal('foo1');
|
||||
scope.get('bar[1].b[1]').should.equal(2);
|
||||
});
|
||||
|
||||
it('should pop scope', function() {
|
||||
scope.push({foo: 'foo', foo1: 'foo1'});
|
||||
scope.pop();
|
||||
expect(scope.get('foo')).to.equal('bar');
|
||||
expect(scope.get('foo1')).to.equal('');
|
||||
expect(scope.get('bar[1].b[1]')).to.equal(2);
|
||||
});
|
||||
});
|
||||
+10
-9
@@ -5,12 +5,12 @@ var expect = chai.expect;
|
||||
chai.use(sinonChai);
|
||||
|
||||
var tag = require('../tag.js')();
|
||||
var context = require('../context.js');
|
||||
var Scope = require('../scope.js');
|
||||
|
||||
describe('tag', function() {
|
||||
var ctx;
|
||||
var scope;
|
||||
before(function() {
|
||||
ctx = context.factory({
|
||||
scope = Scope.factory({
|
||||
foo: 'bar',
|
||||
arr: [2, 1]
|
||||
});
|
||||
@@ -52,7 +52,7 @@ describe('tag', function() {
|
||||
type: 'tag',
|
||||
value: 'foo',
|
||||
name: 'foo'
|
||||
}).render(tokens, ctx);
|
||||
}).render(tokens, scope);
|
||||
expect(spy).to.have.been.called;
|
||||
});
|
||||
|
||||
@@ -62,13 +62,14 @@ describe('tag', function() {
|
||||
tag.register('foo', {
|
||||
render: spy
|
||||
});
|
||||
var t = tag.construct({
|
||||
var token = {
|
||||
type: 'tag',
|
||||
value: 'foo aa:foo bb: arr[0] cc: 2.3',
|
||||
name: 'foo'
|
||||
});
|
||||
t.render(tokens, ctx);
|
||||
expect(spy).to.have.been.calledWithMatch(tokens, ctx, 'foo', {
|
||||
name: 'foo',
|
||||
args: 'aa:foo bb: arr[0] cc: 2.3'
|
||||
};
|
||||
tag.construct(token).render(tokens, scope);
|
||||
expect(spy).to.have.been.calledWithMatch(tokens, scope, token, {
|
||||
aa: 'bar',
|
||||
bb: 2,
|
||||
cc: 2.3
|
||||
|
||||
@@ -4,6 +4,9 @@ const expect = chai.expect;
|
||||
var liquid = require('..')();
|
||||
|
||||
var ctx = {
|
||||
one: 1,
|
||||
two: 2,
|
||||
leq: '<=',
|
||||
empty: '',
|
||||
foo: 'bar',
|
||||
arr: [-2, 'a']
|
||||
@@ -30,5 +33,16 @@ describe('tags', function() {
|
||||
'{% when "b" %}b{% when "c"%}c{%else %}d' +
|
||||
'{%endcase%}')).to.equal('d');
|
||||
});
|
||||
|
||||
it('should support if', function(){
|
||||
expect(liquid.render('{% if 2==3 %}yes{%else%}no{%endif%}', ctx)).to.equal('no');
|
||||
expect(liquid.render('{% if 1==2 and one<two %}a{%endif%}', ctx)).to.equal('');
|
||||
expect(liquid.render('{% if false %}yes{%else%}no{%endif%}', ctx)).to.equal('no');
|
||||
});
|
||||
|
||||
it('should support unless', function(){
|
||||
expect(liquid.render('{% unless 1 %}yes{%else%}no{%endunless%}', ctx)).to.equal('no');
|
||||
expect(liquid.render('{% unless 1>2 %}yes{%endunless%}', ctx)).to.equal('yes');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user