Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
convert else tag test to mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Oct 27, 2012
1 parent f9db906 commit 3c2198d
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 deletions lib/tags/else.test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
var testCase = require('nodeunit').testCase,
swig = require('../../index');
var swig = require('../../index');

exports['else'] = testCase({
setUp: function (callback) {
describe('Tag: else', function () {
beforeEach(function () {
swig.init({
allowErrors: true
});
callback();
},
});

'basic': function (test) {
it('gets used', function () {
var tpl = swig.compile('{% if foo|length > 1 %}hi!{% else %}nope{% endif %}');
test.strictEqual(tpl({ foo: [1, 2, 3] }), 'hi!');
test.strictEqual(tpl({ foo: [1] }), 'nope');
tpl({ foo: [1, 2, 3] }).should.equal('hi!');
tpl({ foo: [1] }).should.equal('nope');
});

test.throws(function () {
it('throws if used outside of "if" context', function () {
var fn = function () {
swig.compile('{% else %}');
}, Error, 'Cannot call else tag outside of "if" context.');
test.done();
},

'else if': function (test) {
var tpl = swig.compile('{% if foo|length > 2 %}foo{% else if foo|length < 2 %}bar{% endif %}');
test.strictEqual(tpl({ foo: [1, 2, 3] }), 'foo');
test.strictEqual(tpl({ foo: [1, 2] }), '');
test.strictEqual(tpl({ foo: [1] }), 'bar');

tpl = swig.compile('{% if foo %}foo{% else if bar && baz %}bar{% endif %}');
test.strictEqual(tpl({ bar: true, baz: true }), 'bar');
};
fn.should.throw();
});

describe('else if', function () {
it('works nicely', function () {
var tpl = swig.compile('{% if foo|length > 2 %}foo{% else if foo|length < 2 %}bar{% endif %}');
tpl({ foo: [1, 2, 3] }).should.equal('foo');
tpl({ foo: [1, 2] }).should.equal('');
tpl({ foo: [1] }).should.equal('bar');
});

test.done();
},
it('accepts conditionals', function () {
var tpl = swig.compile('{% if foo %}foo{% else if bar && baz %}bar{% endif %}');
tpl({ bar: true, baz: true }).should.equal('bar');
});
});

'multiple else if and else': function (test) {
it('can have multiple else if and else conditions', function () {
var tpl = swig.compile('{% if foo %}foo{% else if bar === "bar" %}bar{% else if 3 in baz %}baz{% else %}bop{% endif %}');
test.strictEqual(tpl({ foo: true }), 'foo');
test.strictEqual(tpl({ bar: "bar" }), 'bar');
test.strictEqual(tpl({ baz: [3] }), 'baz');
test.strictEqual(tpl({ baz: [2] }), 'bop');
test.strictEqual(tpl({ bar: false }), 'bop');

test.done();
},

'for else': function (test) {
var tpl = swig.compile('{% for foo in bar %}blah{% else %}hooray!{% endfor %}');
test.strictEqual(tpl({ bar: [] }), 'hooray!', 'else in array');
test.strictEqual(tpl({ bar: {}}), 'hooray!', 'else in object');

test.strictEqual(tpl({ bar: [1] }), 'blah', 'not else in array');
test.strictEqual(tpl({ bar: { foo: 'foo' }}), 'blah', 'not else in object');

test.throws(function () {
swig.compile('{% for foo in bar %}hi!{% else if blah %}nope{% endfor %}');
}, Error, '"else" tag cannot accept arguments in the "for" context.');
test.done();
}
tpl({ foo: true }).should.equal('foo');
tpl({ bar: "bar" }).should.equal('bar');
tpl({ baz: [3] }).should.equal('baz');
tpl({ baz: [2] }).should.equal('bop');
tpl({ bar: false }).should.equal('bop');
});

describe('in "for" tags', function () {
it('can be used as fallback', function () {
var tpl = swig.compile('{% for foo in bar %}blah{% else %}hooray!{% endfor %}');
tpl({ bar: [] }).should.equal('hooray!');
tpl({ bar: {}}).should.equal('hooray!');

tpl({ bar: [1] }).should.equal('blah');
tpl({ bar: { foo: 'foo' }}).should.equal('blah');
});

it('throws if using "else if"', function () {
var fn = function () {
swig.compile('{% for foo in bar %}hi!{% else if blah %}nope{% endfor %}');
};
fn.should.throw();
});
});
});

0 comments on commit 3c2198d

Please sign in to comment.