mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 22:10:41 -07:00
render, filter, tag
This commit is contained in:
+11
-3
@@ -1,6 +1,6 @@
|
||||
var chai = require("chai");
|
||||
var should = chai.should();
|
||||
chai.use(require("chai-as-promised"));
|
||||
var expect = chai.expect;
|
||||
|
||||
var context = require('../context.js');
|
||||
|
||||
@@ -29,9 +29,17 @@ describe('context', function() {
|
||||
ctx.get('bar[1].b[1]').should.equal(2);
|
||||
});
|
||||
|
||||
it('should merge context', function() {
|
||||
ctx.merge({foo: 'foo', foo1: 'foo1'});
|
||||
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.pop();
|
||||
expect(ctx.get('foo')).to.equal('bar');
|
||||
expect(ctx.get('foo1')).to.equal('');
|
||||
expect(ctx.get('bar[1].b[1]')).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const chai = require("chai");
|
||||
const sinon = require("sinon");
|
||||
const sinonChai = require("sinon-chai");
|
||||
const expect = chai.expect;
|
||||
|
||||
chai.use(sinonChai);
|
||||
|
||||
var filter = require('../filter.js');
|
||||
var context = require('../context.js');
|
||||
|
||||
describe('filter', function() {
|
||||
var ctx;
|
||||
beforeEach(function(){
|
||||
filter.clear();
|
||||
ctx = context.factory();
|
||||
});
|
||||
it('should throw when not registered', function() {
|
||||
expect(function() {
|
||||
filter.parse('foo');
|
||||
}).to.throw(/filter foo not found/);
|
||||
});
|
||||
|
||||
it('should register a simple filter', function(){
|
||||
filter.register('foo', x => x.toUpperCase());
|
||||
expect(filter.parse('foo').render('foo', ctx)).to.equal('FOO');
|
||||
});
|
||||
|
||||
it('should call filter with corrct arguments', function(){
|
||||
var spy = sinon.spy();
|
||||
filter.register('foo', spy);
|
||||
filter.parse('foo: 33').render('foo', ctx);
|
||||
expect(spy).to.have.been.calledWith('foo', 33);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ var chai = require("chai");
|
||||
var should = chai.should();
|
||||
chai.use(require("chai-as-promised"));
|
||||
|
||||
var identifier = require('../identifier.js');
|
||||
var identifier = require('../lexical.js');
|
||||
|
||||
describe('identifier', function() {
|
||||
it('should test boolean literal', function() {
|
||||
@@ -0,0 +1,79 @@
|
||||
const chai = require("chai");
|
||||
const sinonChai = require("sinon-chai");
|
||||
const sinon = require("sinon");
|
||||
const expect = chai.expect;
|
||||
|
||||
chai.use(sinonChai);
|
||||
|
||||
var tag = require('../tag.js');
|
||||
var context = require('../context.js');
|
||||
var filter = require('../filter');
|
||||
var render = require('../render.js');
|
||||
|
||||
describe('render', function() {
|
||||
var ctx, htmlToken, tagToken, filterToken;
|
||||
|
||||
before(function() {
|
||||
ctx = context.factory({
|
||||
x: 'XXX',
|
||||
foo: {
|
||||
bar: ['a', 2]
|
||||
}
|
||||
});
|
||||
tagToken = {
|
||||
type: 'tag',
|
||||
value: 'foo bar:x foo:"FOO" num:2.3'
|
||||
};
|
||||
htmlToken = {
|
||||
type: 'html',
|
||||
value: '<p>'
|
||||
};
|
||||
filterToken = {
|
||||
type: 'output',
|
||||
value: 'foo.bar[0] | date: "b" | time:2'
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(function(){
|
||||
filter.clear();
|
||||
tag.clear();
|
||||
});
|
||||
|
||||
it('should render html', function() {
|
||||
expect(render([htmlToken], ctx)).to.equal('<p>');
|
||||
});
|
||||
|
||||
it('should render with tag function', function() {
|
||||
tag.register('foo', {
|
||||
render: x => 'X'
|
||||
});
|
||||
expect(render([tagToken], ctx)).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, {
|
||||
bar: 'XXX',
|
||||
foo: 'FOO',
|
||||
num: 2.3
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
it('should call filter with correct arguments', function() {
|
||||
var date = sinon.stub().returns('y');
|
||||
var time = sinon.spy();
|
||||
filter.register('date', date);
|
||||
filter.register('time', time);
|
||||
render([filterToken], ctx);
|
||||
expect(date).to.have.been.calledWith('a', 'b');
|
||||
expect(time).to.have.been.calledWith('y', 2);
|
||||
});
|
||||
});
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
var chai = require("chai");
|
||||
var sinonChai = require("sinon-chai");
|
||||
var sinon = require("sinon");
|
||||
var expect = chai.expect;
|
||||
chai.use(sinonChai);
|
||||
|
||||
var tag = require('../tag.js');
|
||||
var context = require('../context.js');
|
||||
|
||||
describe('tag', function() {
|
||||
var ctx;
|
||||
before(function(){
|
||||
ctx = context.factory({
|
||||
foo: 'bar',
|
||||
arr: [2, 1]
|
||||
});
|
||||
tag.clear();
|
||||
});
|
||||
|
||||
it('should throw when not registered', function() {
|
||||
expect(function() {
|
||||
tag.parse('foo');
|
||||
}).to.throw(/tag foo not found/);
|
||||
});
|
||||
|
||||
it('should throw when render method not defined', function() {
|
||||
expect(function() {
|
||||
tag.register('foo', {});
|
||||
}).to.throw(/expect foo.render to be a function/);
|
||||
});
|
||||
|
||||
it('should register simple tag', function() {
|
||||
expect(
|
||||
function() {
|
||||
tag.register('foo', {
|
||||
render: x => 'bar'
|
||||
});
|
||||
}).not.throw();
|
||||
});
|
||||
|
||||
it('should call tag.render', function() {
|
||||
var spy = sinon.spy(),
|
||||
tokens = [];
|
||||
tag.register('foo', {
|
||||
render: spy
|
||||
});
|
||||
tag.parse('foo').render(tokens, ctx);
|
||||
expect(spy).to.have.been.called;
|
||||
});
|
||||
|
||||
it('should call tag.render with resolved hash', function() {
|
||||
var spy = sinon.spy(),
|
||||
tokens = [];
|
||||
tag.register('foo', {
|
||||
render: spy
|
||||
});
|
||||
var t = tag.parse('foo aa:foo bb: arr[0] cc: 2.3');
|
||||
t.render(tokens, ctx);
|
||||
expect(spy).to.have.been.calledWithMatch(tokens, ctx, 'foo', {
|
||||
aa: 'bar',
|
||||
bb: 2,
|
||||
cc: 2.3
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,5 @@
|
||||
var chai = require("chai");
|
||||
var should = chai.should();
|
||||
chai.use(require("chai-as-promised"));
|
||||
|
||||
var tokenizer = require('../tokenizer.js');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user