Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add test coverage report

  • Loading branch information...
commit 64cb3a0ba0d9a08e699b096c9216e8bb9d9b4ae0 1 parent b4ce131
@paularmstrong authored
View
4 .gitignore
@@ -3,4 +3,6 @@ npm-debug.log
node_modules/*
dist/browser/*
dist/test/swig.pack.js
-dist/test/*.test.js
+dist/test/*.test.js
+lib-cov/*
+tests/coverage.html
View
16 Makefile
@@ -6,7 +6,7 @@ all:
browser:
@scripts/browser.sh
-tests := $(shell find . -name '*.test.js' ! -path "*node_modules/*" ! -path "*dist/*")
+tests := $(shell find ./tests -name '*.test.js' ! -path "*node_modules/*")
reporter = dot
opts =
test:
@@ -16,7 +16,19 @@ files := $(shell find . -name '*.js' ! -path "*node_modules/*" ! -path "*dist/*"
lint:
@node_modules/nodelint/nodelint ${files} --config=scripts/config-lint.js
+out = tests/coverage.html
+coverage:
+ # NOTE: You must have node-jscoverage installed:
+ # https://github.com/visionmedia/node-jscoverage
+ # The jscoverage npm module and original JSCoverage packages will not work
+ @jscoverage lib lib-cov
+ @SWIG_COVERAGE=1 $(MAKE) test reporter=html-cov > ${out}
+ @rm -rd lib-cov
+ @echo
+ @echo "Built Report to ${out}"
+ @echo
+
speed:
@node tests/speed.js
-.PHONY: all browser test lint speed
+.PHONY: all browser test lint coverage speed
View
174 index.js
@@ -1,173 +1,3 @@
-var fs = require('fs'),
- path = require('path'),
+var dir = (process.env.SWIG_COVERAGE) ? './lib-cov/' : './lib/';
- tags = require('./lib/tags'),
- parser = require('./lib/parser'),
- filters = require('./lib/filters'),
- helpers = require('./lib/helpers'),
- dateformat = require('./lib/dateformat'),
-
- _ = require('underscore');
-
-var config = {
- allowErrors: false,
- autoescape: true,
- cache: true,
- encoding: 'utf8',
- filters: filters,
- root: '/',
- tags: tags,
- extensions: {},
- tzOffset: 0
- },
- _config = _.extend({}, config),
- CACHE = {};
-
-// Call this before using the templates
-exports.init = function (options) {
- CACHE = {};
- _config = _.extend({}, config, options);
- _config.filters = _.extend(filters, options.filters);
- _config.tags = _.extend(tags, options.tags);
-
- dateformat.defaultTZOffset = _config.tzOffset;
-};
-
-function TemplateError(error) {
- return { render: function () {
- return '<pre>' + error.stack + '</pre>';
- }};
-}
-
-function createRenderFunc(code) {
- // The compiled render function - this is all we need
- return new Function('_context', '_parents', '_filters', '_', '_ext', [
- '_parents = _parents ? _parents.slice() : [];',
- '_context = _context || {};',
- // Prevents circular includes (which will crash node without warning)
- 'var j = _parents.length,',
- ' _output = "",',
- ' _this = this;',
- // Note: this loop averages much faster than indexOf across all cases
- 'while (j--) {',
- ' if (_parents[j] === this.id) {',
- ' return "Circular import of template " + this.id + " in " + _parents[_parents.length-1];',
- ' }',
- '}',
- // Add this template as a parent to all includes in its scope
- '_parents.push(this.id);',
- code,
- 'return _output;'
- ].join(''));
-}
-
-function createTemplate(data, id) {
- var template = {
- // Allows us to include templates from the compiled code
- compileFile: exports.compileFile,
- // These are the blocks inside the template
- blocks: {},
- // Distinguish from other tokens
- type: parser.TEMPLATE,
- // The template ID (path relative to template dir)
- id: id
- },
- tokens,
- code,
- render;
-
- // The template token tree before compiled into javascript
- if (_config.allowErrors) {
- tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
- } else {
- try {
- tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
- } catch (e) {
- return new TemplateError(e);
- }
- }
-
- template.tokens = tokens;
-
- // The raw template code
- code = parser.compile.call(template);
-
- if (code !== false) {
- render = createRenderFunc(code);
- } else {
- render = function (_context, _parents, _filters, _, _ext) {
- template.tokens = tokens;
- code = parser.compile.call(template, '', _context);
- var fn = createRenderFunc(code);
- return fn.call(this, _context, _parents, _filters, _, _ext);
- };
- }
-
- template.render = function (context, parents) {
- if (_config.allowErrors) {
- return render.call(this, context, parents, _config.filters, _, _config.extensions);
- }
- try {
- return render.call(this, context, parents, _config.filters, _, _config.extensions);
- } catch (e) {
- return new TemplateError(e);
- }
- };
-
- return template;
-}
-
-function getTemplate(source, options) {
- var key = options.filename || source;
- if (_config.cache || options.cache) {
- if (!CACHE.hasOwnProperty(key)) {
- CACHE[key] = createTemplate(source, key);
- }
-
- return CACHE[key];
- }
-
- return createTemplate(source, key);
-}
-
-exports.compileFile = function (filepath, forceAllowErrors) {
- var tpl, get;
-
- if (filepath[0] === '/') {
- filepath = filepath.substr(1);
- }
-
- if (_config.cache && CACHE.hasOwnProperty(filepath)) {
- return CACHE[filepath];
- }
-
- if (typeof window !== 'undefined') {
- throw new TemplateError({ stack: 'You must pre-compile all templates in-browser. Use `swig.compile(template);`.' });
- }
-
- get = function () {
- var file = ((/^\//).test(filepath) || (/^.:/).test(filepath)) ? filepath : _config.root + '/' + filepath,
- data = fs.readFileSync(file, config.encoding);
- tpl = getTemplate(data, { filename: filepath });
- };
-
- if (_config.allowErrors || forceAllowErrors) {
- get();
- } else {
- try {
- get();
- } catch (error) {
- tpl = new TemplateError(error);
- }
- }
- return tpl;
-};
-
-exports.compile = function (source, options) {
- options = options || {};
- var tmpl = getTemplate(source, options || {});
-
- return function (source, options) {
- return tmpl.render(source, options);
- };
-};
+module.exports = require(dir + 'swig');
View
173 lib/swig.js
@@ -0,0 +1,173 @@
+var fs = require('fs'),
+ path = require('path'),
+
+ tags = require('./tags'),
+ parser = require('./parser'),
+ filters = require('./filters'),
+ helpers = require('./helpers'),
+ dateformat = require('./dateformat'),
+
+ _ = require('underscore');
+
+var config = {
+ allowErrors: false,
+ autoescape: true,
+ cache: true,
+ encoding: 'utf8',
+ filters: filters,
+ root: '/',
+ tags: tags,
+ extensions: {},
+ tzOffset: 0
+ },
+ _config = _.extend({}, config),
+ CACHE = {};
+
+// Call this before using the templates
+exports.init = function (options) {
+ CACHE = {};
+ _config = _.extend({}, config, options);
+ _config.filters = _.extend(filters, options.filters);
+ _config.tags = _.extend(tags, options.tags);
+
+ dateformat.defaultTZOffset = _config.tzOffset;
+};
+
+function TemplateError(error) {
+ return { render: function () {
+ return '<pre>' + error.stack + '</pre>';
+ }};
+}
+
+function createRenderFunc(code) {
+ // The compiled render function - this is all we need
+ return new Function('_context', '_parents', '_filters', '_', '_ext', [
+ '_parents = _parents ? _parents.slice() : [];',
+ '_context = _context || {};',
+ // Prevents circular includes (which will crash node without warning)
+ 'var j = _parents.length,',
+ ' _output = "",',
+ ' _this = this;',
+ // Note: this loop averages much faster than indexOf across all cases
+ 'while (j--) {',
+ ' if (_parents[j] === this.id) {',
+ ' return "Circular import of template " + this.id + " in " + _parents[_parents.length-1];',
+ ' }',
+ '}',
+ // Add this template as a parent to all includes in its scope
+ '_parents.push(this.id);',
+ code,
+ 'return _output;'
+ ].join(''));
+}
+
+function createTemplate(data, id) {
+ var template = {
+ // Allows us to include templates from the compiled code
+ compileFile: exports.compileFile,
+ // These are the blocks inside the template
+ blocks: {},
+ // Distinguish from other tokens
+ type: parser.TEMPLATE,
+ // The template ID (path relative to template dir)
+ id: id
+ },
+ tokens,
+ code,
+ render;
+
+ // The template token tree before compiled into javascript
+ if (_config.allowErrors) {
+ tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
+ } else {
+ try {
+ tokens = parser.parse.call(template, data, _config.tags, _config.autoescape);
+ } catch (e) {
+ return new TemplateError(e);
+ }
+ }
+
+ template.tokens = tokens;
+
+ // The raw template code
+ code = parser.compile.call(template);
+
+ if (code !== false) {
+ render = createRenderFunc(code);
+ } else {
+ render = function (_context, _parents, _filters, _, _ext) {
+ template.tokens = tokens;
+ code = parser.compile.call(template, '', _context);
+ var fn = createRenderFunc(code);
+ return fn.call(this, _context, _parents, _filters, _, _ext);
+ };
+ }
+
+ template.render = function (context, parents) {
+ if (_config.allowErrors) {
+ return render.call(this, context, parents, _config.filters, _, _config.extensions);
+ }
+ try {
+ return render.call(this, context, parents, _config.filters, _, _config.extensions);
+ } catch (e) {
+ return new TemplateError(e);
+ }
+ };
+
+ return template;
+}
+
+function getTemplate(source, options) {
+ var key = options.filename || source;
+ if (_config.cache || options.cache) {
+ if (!CACHE.hasOwnProperty(key)) {
+ CACHE[key] = createTemplate(source, key);
+ }
+
+ return CACHE[key];
+ }
+
+ return createTemplate(source, key);
+}
+
+exports.compileFile = function (filepath, forceAllowErrors) {
+ var tpl, get;
+
+ if (filepath[0] === '/') {
+ filepath = filepath.substr(1);
+ }
+
+ if (_config.cache && CACHE.hasOwnProperty(filepath)) {
+ return CACHE[filepath];
+ }
+
+ if (typeof window !== 'undefined') {
+ throw new TemplateError({ stack: 'You must pre-compile all templates in-browser. Use `swig.compile(template);`.' });
+ }
+
+ get = function () {
+ var file = ((/^\//).test(filepath) || (/^.:/).test(filepath)) ? filepath : _config.root + '/' + filepath,
+ data = fs.readFileSync(file, config.encoding);
+ tpl = getTemplate(data, { filename: filepath });
+ };
+
+ if (_config.allowErrors || forceAllowErrors) {
+ get();
+ } else {
+ try {
+ get();
+ } catch (error) {
+ tpl = new TemplateError(error);
+ }
+ }
+ return tpl;
+};
+
+exports.compile = function (source, options) {
+ options = options || {};
+ var tmpl = getTemplate(source, options || {});
+
+ return function (source, options) {
+ return tmpl.render(source, options);
+ };
+};
View
4 scripts/browser.sh
@@ -31,7 +31,7 @@ function package() {
fi
echo "(function (exports) {" >> $BROWSER_FILE
- cat index.js >> $BROWSER_FILE
+ cat lib/swig.js >> $BROWSER_FILE
echo "})(swig);" >> $BROWSER_FILE
echo -ne "."
@@ -84,7 +84,7 @@ package "pack"
cp dist/browser/swig.pack.js dist/test/swig.pack.js
-find lib -name "*.test.js" ! -name "helpers.test.js" | while read FILE; do
+find tests -name "*.test.js" ! -name "helpers.test.js" | while read FILE; do
TEST_FILE=$(basename $FILE)
TEMP_FILE="dist/.$TEST_FILE"
NAME=$(sed -e 's/.test.js//' <<< $TEST_FILE)
View
7 lib/filters.test.js → tests/filters.test.js
@@ -1,6 +1,7 @@
-var expect = require('expect.js'),
- swig = require('../index'),
- DateZ = require('./dateformat').DateZ;
+var require = require('./testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig'),
+ DateZ = require('../lib/dateformat').DateZ;
function testFilter(filter, input, output, message) {
it(message, function () {
View
5 lib/helpers.test.js → tests/helpers.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- helpers = require('./helpers');
+var require = require('./testutils').require,
+ expect = require('expect.js'),
+ helpers = require('../lib/helpers');
describe('Helpers', function () {
describe('isLiteral', function () {
View
9 lib/parser.test.js → tests/parser.test.js
@@ -1,7 +1,8 @@
-var expect = require('expect.js'),
- swig = require('../index'),
- tags = require('./tags'),
- parser = require('./parser');
+var require = require('./testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig'),
+ tags = require('../lib/tags'),
+ parser = require('../lib/parser');
describe('Tags', function () {
it('undefined tag throws error', function () {
View
13 index.test.js → tests/swig.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('./index');
+var require = require('./testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('swig.init', function () {
@@ -33,7 +34,7 @@ describe('swig.init', function () {
it('does not throw when false, renders instead', function () {
swig.init({
- root: __dirname + '/tests/templates',
+ root: __dirname + '/templates',
allowErrors: false
});
expect(swig.compileFile('foobar.html').render())
@@ -49,7 +50,7 @@ describe('swig.compileFile', function () {
it('compiles a template from a file', function () {
swig.init({
- root: __dirname + '/tests/templates',
+ root: __dirname + '/templates',
allowErrors: true
});
expect(swig.compileFile('included_2.html').render({ array: [1, 1] }))
@@ -59,10 +60,10 @@ describe('swig.compileFile', function () {
it('can use an absolute path', function () {
swig.init({
- root: __dirname + '/tests/templates',
+ root: __dirname + '/templates',
allowErrors: true
});
- expect(swig.compileFile('/' + __dirname + '/tests/templates/included_2.html').render({ array: [1, 1] }))
+ expect(swig.compileFile('/' + __dirname + '/templates/included_2.html').render({ array: [1, 1] }))
.to.equal('2');
});
View
5 lib/tags.test.js → tests/tags.test.js
@@ -1,6 +1,7 @@
-var expect = require('expect.js'),
+var require = require('./testutils').require,
+ expect = require('expect.js'),
util = require('util'),
- swig = require('../index');
+ swig = require('../lib/swig');
describe('Custom Tags', function () {
var tags = {
View
5 lib/tags/autoescape.test.js → tests/tags/autoescape.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: autoescape', function () {
beforeEach(function () {
View
5 lib/tags/block.test.js → tests/tags/block.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: block', function () {
beforeEach(function () {
View
5 lib/tags/else.test.js → tests/tags/else.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: else', function () {
beforeEach(function () {
View
5 lib/tags/extends.test.js → tests/tags/extends.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: extends', function () {
View
5 lib/tags/filter.test.js → tests/tags/filter.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: filter', function () {
beforeEach(function () {
View
5 lib/tags/for.test.js → tests/tags/for.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: for', function () {
beforeEach(function () {
View
5 lib/tags/if.test.js → tests/tags/if.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: if', function () {
beforeEach(function () {
View
5 lib/tags/import.test.js → tests/tags/import.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: import (for macros)', function () {
beforeEach(function () {
View
7 lib/tags/include.test.js → tests/tags/include.test.js
@@ -1,10 +1,11 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: include', function () {
beforeEach(function () {
swig.init({
- root: __dirname + '/../../tests/templates',
+ root: __dirname + '/../templates',
allowErrors: true
});
});
View
5 lib/tags/macro.test.js → tests/tags/macro.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: macro', function () {
beforeEach(function () {
View
5 lib/tags/parent.test.js → tests/tags/parent.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: parent', function () {
beforeEach(function () {
View
5 lib/tags/raw.test.js → tests/tags/raw.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: raw', function () {
beforeEach(function () {
View
5 lib/tags/set.test.js → tests/tags/set.test.js
@@ -1,5 +1,6 @@
-var expect = require('expect.js'),
- swig = require('../../index');
+var require = require('../testutils').require,
+ expect = require('expect.js'),
+ swig = require('../lib/swig');
describe('Tag: set', function () {
beforeEach(function () {
View
6 tests/testutils.js
@@ -0,0 +1,6 @@
+exports.require = function (file) {
+ if (process.env.SWIG_COVERAGE) {
+ file = file.replace('/lib/', '/lib-cov/');
+ }
+ return require(file);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.