Skip to content

Commit

Permalink
Use mocha for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 6, 2012
1 parent c09c7f5 commit 3fea961
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 291 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
= HEAD

* Converted tests to use mocha instead of vows.

= 0.7.1 / 6 Dec 2012

* Handle empty templates gracefully. Fixes #265, #267, and #270.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"keywords": ["mustache", "template", "templates", "ejs"],
"main": "./mustache.js",
"devDependencies": {
"vows": "0.6.x"
"mocha": "1.5.0"
},
"volo": {
"url": "https://raw.github.com/janl/mustache.js/0.7.1/mustache.js"
},
"scripts": {
"test": "vows --spec"
"test": "mocha test"
}
}
51 changes: 51 additions & 0 deletions test/context-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require('./helper');
var Context = Mustache.Context;

describe('A new Mustache.Context', function () {
var context;
beforeEach(function () {
context = new Context({ name: 'parent', message: 'hi', a: { b: 'b' } });
});

it('is able to lookup properties of its own view', function () {
assert.equal(context.lookup('name'), 'parent');
});

it('is able to lookup nested properties of its own view', function () {
assert.equal(context.lookup('a.b'), 'b');
});

describe('when pushed', function () {
beforeEach(function () {
context = context.push({ name: 'child', c: { d: 'd' } });
});

it('returns the child context', function () {
assert.equal(context.view.name, 'child');
assert.equal(context.parent.view.name, 'parent');
});

it('is able to lookup properties of its own view', function () {
assert.equal(context.lookup('name'), 'child');
});

it("is able to lookup properties of the parent context's view", function () {
assert.equal(context.lookup('message'), 'hi');
});

it('is able to lookup nested properties of its own view', function () {
assert.equal(context.lookup('c.d'), 'd');
});

it('is able to lookup nested properties of its parent view', function () {
assert.equal(context.lookup('a.b'), 'b');
});
});
});

describe('Mustache.Context.make', function () {
it('returns the same object when given a Context', function () {
var context = new Context;
assert.strictEqual(Context.make(context), context);
});
});
47 changes: 0 additions & 47 deletions test/context_test.js

This file was deleted.

2 changes: 2 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
assert = require('assert');
Mustache = require('../mustache');
26 changes: 11 additions & 15 deletions test/parse_test.js → test/parse-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var assert = require('assert');
var vows = require('vows');
var Mustache = require('./../mustache');
require('./helper');

// A map of templates to their expected token output. Tokens are in the format:
// [type, value, startIndex, endIndex].
// [type, value, startIndex, endIndex, subTokens].
var expectations = {
'' : [],
'{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ],
Expand Down Expand Up @@ -55,16 +53,14 @@ var expectations = {
: [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ] ] ] ] ]
};

var spec = {};
describe('Mustache.parse', function () {

for (var template in expectations) {
(function (template, tokens) {
spec['knows how to parse ' + JSON.stringify(template)] = function () {
assert.deepEqual(Mustache.parse(template), tokens);
};
})(template, expectations[template]);
}
for (var template in expectations) {
(function (template, tokens) {
it('knows how to parse ' + JSON.stringify(template), function () {
assert.deepEqual(Mustache.parse(template), tokens);
});
})(template, expectations[template]);
}

vows.describe('Mustache.parse').addBatch({
'parse': spec
}).export(module);
});
68 changes: 68 additions & 0 deletions test/render-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require('./helper');

var fs = require('fs');
var path = require('path');
var _files = path.join(__dirname, '_files');

function getContents(testName, ext) {
return fs.readFileSync(path.join(_files, testName + '.' + ext), 'utf8');
}

function getView(testName) {
var view = getContents(testName, 'js');
if (!view) throw new Error('Cannot find view for test "' + testName + '"');
return eval(view);
}

function getPartial(testName) {
try {
return getContents(testName, 'partial');
} catch (e) {
// No big deal. Not all tests need to test partial support.
}
}

function getTest(testName) {
var test = {};
test.view = getView(testName);
test.template = getContents(testName, 'mustache');
test.partial = getPartial(testName);
test.expect = getContents(testName, 'txt');
return test;
}

// You can put the name of a specific test to run in the TEST environment
// variable (e.g. TEST=backslashes vows test/render-test.js)
var testToRun = process.env.TEST;

var testNames;
if (testToRun) {
testNames = [testToRun];
} else {
testNames = fs.readdirSync(_files).filter(function (file) {
return (/\.js$/).test(file);
}).map(function (file) {
return path.basename(file).replace(/\.js$/, '');
});
}

describe('Mustache.render', function () {
beforeEach(function () {
Mustache.clearCache();
});

testNames.forEach(function (testName) {
var test = getTest(testName);

it('knows how to render ' + testName, function () {
var output;
if (test.partial) {
output = Mustache.render(test.template, test.view, { partial: test.partial });
} else {
output = Mustache.render(test.template, test.view);
}

assert.equal(output, test.expect);
});
});
});
66 changes: 0 additions & 66 deletions test/render_test.js

This file was deleted.

78 changes: 78 additions & 0 deletions test/scanner-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require('./helper');
var Scanner = Mustache.Scanner;

describe('A new Mustache.Scanner', function () {
describe('for an empty string', function () {
it('is at the end', function () {
var scanner = new Scanner('');
assert(scanner.eos());
});
});

describe('for a non-empty string', function () {
var scanner;
beforeEach(function () {
scanner = new Scanner('a b c');
});

describe('scan', function () {
describe('when the RegExp matches the entire string', function () {
it('returns the entire string', function () {
var match = scanner.scan(/a b c/);
assert.equal(match, scanner.string);
assert(scanner.eos());
});
});

describe('when the RegExp matches at index 0', function () {
it('returns the portion of the string that matched', function () {
var match = scanner.scan(/a/);
assert.equal(match, 'a');
assert.equal(scanner.pos, 1);
});
});

describe('when the RegExp matches at some index other than 0', function () {
it('returns the empty string', function () {
var match = scanner.scan(/b/);
assert.equal(match, '');
assert.equal(scanner.pos, 0);
});
});

describe('when the RegExp does not match', function () {
it('returns the empty string', function () {
var match = scanner.scan(/z/);
assert.equal(match, '');
assert.equal(scanner.pos, 0);
});
});
}); // scan

describe('scanUntil', function () {
describe('when the RegExp matches at index 0', function () {
it('returns the empty string', function () {
var match = scanner.scanUntil(/a/);
assert.equal(match, '');
assert.equal(scanner.pos, 0);
});
});

describe('when the RegExp matches at some index other than 0', function () {
it('returns the string up to that index', function () {
var match = scanner.scanUntil(/b/);
assert.equal(match, 'a ');
assert.equal(scanner.pos, 2);
});
});

describe('when the RegExp does not match', function () {
it('returns the entire string', function () {
var match = scanner.scanUntil(/z/);
assert.equal(match, scanner.string);
assert(scanner.eos());
});
});
}); // scanUntil
}); // for a non-empty string
});
Loading

0 comments on commit 3fea961

Please sign in to comment.