From 22fe6e23a7860a43c558d9154d94be9889437270 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Sun, 16 Apr 2017 17:22:59 -0400 Subject: [PATCH 01/16] karma testing tweaks --- .istanbul.yml | 2 ++ karma.conf.js | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 19 +++++++++++++++---- test/conditionals.js | 12 +++++++----- test/includes.js | 12 +++++++----- test/looping.js | 12 +++++++----- test/misc.js | 28 +++++++++++++++++----------- 7 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 .istanbul.yml create mode 100644 karma.conf.js diff --git a/.istanbul.yml b/.istanbul.yml new file mode 100644 index 00000000..942110f1 --- /dev/null +++ b/.istanbul.yml @@ -0,0 +1,2 @@ +reporting: + dir: ./coverage/server diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 00000000..aba6fefb --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,39 @@ +// Karma configuration + +module.exports = function(config) { + config.set({ + basePath: '', + frameworks: ['mocha'], + files: [ + 'teddy.js', + 'node_modules/mocha/mocha.js', + 'test/models/*.js', + 'node_modules/chai/chai.js', + 'node_modules/chai-string/chai-string.js', + {pattern: 'test/templates/**/*', included: false}, + 'test/*.js', + 'test/client.html' + ], + reporters: ['progress', 'coverage'], + port: 8000, + proxies: { + '/templates/': '/base/test/templates/', + }, + preprocessors: { + 'teddy.js': ['coverage'] + }, + coverageReporter: { + type: 'lcov', + dir: 'coverage/', + subdir: function(browser) { + return browser.toLowerCase().split(/[ /-]/)[0]; + } + }, + colors: true, + logLevel: config.LOG_INFO, + autoWatch: false, + singleRun: true, + browsers: ['Chrome'], + concurrency: 1 + }); +}; diff --git a/package.json b/package.json index 527a79ed..ad03e153 100755 --- a/package.json +++ b/package.json @@ -14,14 +14,24 @@ "dependencies": { }, "devDependencies": { - "eslint": "3.13.1", + "eslint": "3.19.0", "mocha": "3.2.0", "chai": "3.5.0", "chai-string": "1.3.0", "istanbul": "0.4.5", "mocha-lcov-reporter": "1.3.0", - "coveralls": "2.12.0", - "cross-env": "4.0.0" + "coveralls": "2.13.0", + "cross-env": "4.0.0", + "karma": "1.6.0", + "karma-cli": "1.0.1", + "karma-mocha": "1.3.0", + "karma-coverage": "1.1.1", + "karma-phantomjs-launcher": "1.0.4", + "karma-chrome-launcher": "2.0.0", + "karma-firefox-launcher": "1.0.1", + "karma-ie-launcher": "1.0.0", + "karma-coverage-allsources": "0.0.4", + "lcov-result-merger": "^1.2.0" }, "eslintConfig": { "env": { @@ -56,7 +66,8 @@ "scripts": { "test": "cross-env NODE_ENV=test mocha --recursive test", "eslint": "eslint .", - "cover": "cross-env NODE_ENV=cover istanbul cover _mocha --recursive test" + "cover": "cross-env NODE_ENV=cover istanbul cover _mocha --recursive test", + "karma": "karma start" }, "pre-commit": [ "test", diff --git a/test/conditionals.js b/test/conditionals.js index dcdbcf05..ebfb659a 100644 --- a/test/conditionals.js +++ b/test/conditionals.js @@ -12,11 +12,13 @@ describe('Conditionals', function() { before(function() { teddy.setTemplateRoot('test/templates'); model = makeModel(); - if (process.env.NODE_ENV === 'test') { - teddy.setVerbosity(0); - } - else if (process.env.NODE_ENV === 'cover') { - teddy.setVerbosity(3); + if (typeof process === 'object') { + if (process.env.NODE_ENV === 'test') { + teddy.setVerbosity(0); + } + else if (process.env.NODE_ENV === 'cover') { + teddy.setVerbosity(3); + } } }); diff --git a/test/includes.js b/test/includes.js index 91118786..30b62a1b 100644 --- a/test/includes.js +++ b/test/includes.js @@ -12,11 +12,13 @@ describe('Includes', function() { before(function() { teddy.setTemplateRoot('test/templates'); model = makeModel(); - if (process.env.NODE_ENV === 'test') { - teddy.setVerbosity(0); - } - else if (process.env.NODE_ENV === 'cover') { - teddy.setVerbosity(3); + if (typeof process === 'object') { + if (process.env.NODE_ENV === 'test') { + teddy.setVerbosity(0); + } + else if (process.env.NODE_ENV === 'cover') { + teddy.setVerbosity(3); + } } }); diff --git a/test/looping.js b/test/looping.js index e8bc89ca..f292d3d9 100644 --- a/test/looping.js +++ b/test/looping.js @@ -12,11 +12,13 @@ describe('Looping', function() { before(function() { teddy.setTemplateRoot('test/templates'); model = makeModel(); - if (process.env.NODE_ENV === 'test') { - teddy.setVerbosity(0); - } - else if (process.env.NODE_ENV === 'cover') { - teddy.setVerbosity(3); + if (typeof process === 'object') { + if (process.env.NODE_ENV === 'test') { + teddy.setVerbosity(0); + } + else if (process.env.NODE_ENV === 'cover') { + teddy.setVerbosity(3); + } } }); diff --git a/test/misc.js b/test/misc.js index 8212a342..543c87df 100644 --- a/test/misc.js +++ b/test/misc.js @@ -4,7 +4,7 @@ if (typeof module !== 'undefined') { assert = chai.assert, model, makeModel = require('./models/model'), - verbosity = '', + verbosity, teddy = require('../teddy'); chai.use(chaiString); } @@ -13,11 +13,13 @@ describe('Misc', function() { before(function() { teddy.setTemplateRoot('test/templates'); model = makeModel(); - if (process.env.NODE_ENV === 'test') { - teddy.setVerbosity(0); - } - else if (process.env.NODE_ENV === 'cover') { - teddy.setVerbosity(3); + if (typeof process === 'object') { + if (process.env.NODE_ENV === 'test') { + teddy.setVerbosity(0); + } + else if (process.env.NODE_ENV === 'cover') { + teddy.setVerbosity(3); + } } }); @@ -157,6 +159,7 @@ describe('Misc', function() { }); it('should set each verbosity level', function(done) { + verbosity = ''; teddy.setVerbosity(); verbosity += teddy.params.verbosity + ', '; teddy.setVerbosity('none'); @@ -173,11 +176,14 @@ describe('Misc', function() { verbosity += teddy.params.verbosity; assert.equal(verbosity, '1, 0, 0, 2, 2, 3, 3'); - if (process.env.NODE_ENV === 'test') { - teddy.setVerbosity(0); - } - else if (process.env.NODE_ENV === 'cover') { - teddy.setVerbosity(3); + verbosity = ''; + if (typeof process === 'object') { + if (process.env.NODE_ENV === 'test') { + teddy.setVerbosity(0); + } + else if (process.env.NODE_ENV === 'cover') { + teddy.setVerbosity(3); + } } done(); }); From 13dce6eb8eb8b6612b132ff37081d2e9b72310ee Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Mon, 17 Apr 2017 20:05:09 -0400 Subject: [PATCH 02/16] add karma integration to travis --- .travis.yml | 8 ++++++- karma.conf.js | 17 +++++++++++--- package.json | 3 --- test/client.html | 27 ++++++++++++++--------- test/looping.js | 16 +++++++++----- test/misc.js | 5 +++-- test/templates/misc/templateToMinify.html | 14 ++++++++++++ 7 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 test/templates/misc/templateToMinify.html diff --git a/.travis.yml b/.travis.yml index bc327d12..53579899 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,12 @@ language: node_js node_js: - "node" +before_install: + - export CHROME_BIN=chromium-browser + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start after_success: - istanbul cover ./node_modules/mocha/bin/_mocha - - cat ./coverage/lcov.info | ./node_modules/.bin/coveralls + - karma start + - lcov-result-merger 'coverage/**/lcov.info' 'coverage/fullCoverage.info' + - cat ./coverage/fullCoverage.info | ./node_modules/.bin/coveralls diff --git a/karma.conf.js b/karma.conf.js index aba6fefb..76e31fba 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,12 +1,11 @@ // Karma configuration module.exports = function(config) { - config.set({ + var configuration = { basePath: '', frameworks: ['mocha'], files: [ 'teddy.js', - 'node_modules/mocha/mocha.js', 'test/models/*.js', 'node_modules/chai/chai.js', 'node_modules/chai-string/chai-string.js', @@ -34,6 +33,18 @@ module.exports = function(config) { autoWatch: false, singleRun: true, browsers: ['Chrome'], + customLaunchers: { + Chrome_travis_ci: { + base: 'Chrome', + flags: ['--no-sandbox'] + } + }, concurrency: 1 - }); + }; + + if (process.env.TRAVIS) { + configuration.browsers = ['Chrome_travis_ci']; + } + + config.set(configuration); }; diff --git a/package.json b/package.json index ad03e153..bd8c7f87 100755 --- a/package.json +++ b/package.json @@ -26,10 +26,7 @@ "karma-cli": "1.0.1", "karma-mocha": "1.3.0", "karma-coverage": "1.1.1", - "karma-phantomjs-launcher": "1.0.4", "karma-chrome-launcher": "2.0.0", - "karma-firefox-launcher": "1.0.1", - "karma-ie-launcher": "1.0.0", "karma-coverage-allsources": "0.0.4", "lcov-result-merger": "^1.2.0" }, diff --git a/test/client.html b/test/client.html index f897019f..15674e67 100644 --- a/test/client.html +++ b/test/client.html @@ -328,13 +328,21 @@

Warning: these tests can only be run from a web server.

+ + + + + + + + + - - - - - - - diff --git a/test/looping.js b/test/looping.js index f292d3d9..b02bfd47 100644 --- a/test/looping.js +++ b/test/looping.js @@ -4,9 +4,13 @@ if (typeof module !== 'undefined') { assert = chai.assert, model, makeModel = require('./models/model'), + loopMs = 50, teddy = require('../teddy'); chai.use(chaiString); } +else { + var loopMs = 400; +} describe('Looping', function() { before(function() { @@ -67,7 +71,7 @@ describe('Looping', function() { done(); }); - it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { + it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { var start, end, time; start = new Date().getTime(); @@ -76,7 +80,7 @@ describe('Looping', function() { end = new Date().getTime(); time = end - start; - assert.isAtMost(time, 50); + assert.isAtMost(time, loopMs); done(); }); @@ -94,7 +98,7 @@ describe('Looping', function() { done(); }); - it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { + it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { var start, end, time; start = new Date().getTime(); @@ -103,7 +107,7 @@ describe('Looping', function() { end = new Date().getTime(); time = end - start; - assert.isAtMost(time, 50); + assert.isAtMost(time, loopMs); done(); }); @@ -121,7 +125,7 @@ describe('Looping', function() { done(); }); - it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { + it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) { var start, end, time; start = new Date().getTime(); @@ -131,7 +135,7 @@ describe('Looping', function() { time = end - start; teddy.cacheRenders(false); - assert.isAtMost(time, 50); + assert.isAtMost(time, loopMs); done(); }); diff --git a/test/misc.js b/test/misc.js index 543c87df..490954b9 100644 --- a/test/misc.js +++ b/test/misc.js @@ -188,9 +188,10 @@ describe('Misc', function() { done(); }); - it('should minify template with internal minifier (misc/plainHTML.html)', function(done) { + it('should minify template with internal minifier (misc/templateToMinify.html)', function(done) { teddy.minify(true); - assert.equal(teddy.compile('misc/plainHTML.html', model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); + teddy.flushCache('misc/templateToMinify.html'); + assert.equal(teddy.render('misc/templateToMinify.html', model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); teddy.minify(false); done(); }); diff --git a/test/templates/misc/templateToMinify.html b/test/templates/misc/templateToMinify.html new file mode 100644 index 00000000..c159ecf2 --- /dev/null +++ b/test/templates/misc/templateToMinify.html @@ -0,0 +1,14 @@ + + + + + + + Plain HTML + + +
+

This template contains no teddy tags. Just HTML.

+
+ + From 7cf1801022a43b352a5d2a230004b02fd2e4f443 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Mon, 17 Apr 2017 20:06:56 -0400 Subject: [PATCH 03/16] remove console.log --- test/client.html | 1 - 1 file changed, 1 deletion(-) diff --git a/test/client.html b/test/client.html index 15674e67..7e97e77d 100644 --- a/test/client.html +++ b/test/client.html @@ -453,7 +453,6 @@

Warning: these tests can only be run from a web server.

teddy.templates[template] = teddy.compile(req.response || req.responseText); if (counter++ === templateLength - 1) { mocha.run(); - console.log(teddy.templates); } } } From 51167adb78c51b0ab9078d650e8739af49354284 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Mon, 17 Apr 2017 20:17:33 -0400 Subject: [PATCH 04/16] dump the flushCache --- test/misc.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/misc.js b/test/misc.js index 490954b9..9ed66373 100644 --- a/test/misc.js +++ b/test/misc.js @@ -190,7 +190,6 @@ describe('Misc', function() { it('should minify template with internal minifier (misc/templateToMinify.html)', function(done) { teddy.minify(true); - teddy.flushCache('misc/templateToMinify.html'); assert.equal(teddy.render('misc/templateToMinify.html', model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); teddy.minify(false); done(); From d1f1e768f9e0a8273bb213007299543576de6ca1 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Thu, 20 Apr 2017 12:50:26 -0400 Subject: [PATCH 05/16] create global hooks --- package.json | 6 ++++-- test/client.html | 9 +++++---- test/conditionals.js | 23 ----------------------- test/global.js | 28 ++++++++++++++++++++++++++++ test/includes.js | 23 ----------------------- test/looping.js | 31 ++++++++----------------------- test/misc.js | 24 ------------------------ 7 files changed, 45 insertions(+), 99 deletions(-) create mode 100644 test/global.js diff --git a/package.json b/package.json index bd8c7f87..3017e86b 100755 --- a/package.json +++ b/package.json @@ -61,10 +61,12 @@ } }, "scripts": { - "test": "cross-env NODE_ENV=test mocha --recursive test", + "test": "cross-env NODE_ENV=test mocha --recursive test && karma start", + "test-server": "cross-env NODE_ENV=test mocha --recursive test", + "test-client": "karma start", "eslint": "eslint .", "cover": "cross-env NODE_ENV=cover istanbul cover _mocha --recursive test", - "karma": "karma start" + "coverage": "npm run cover" }, "pre-commit": [ "test", diff --git a/test/client.html b/test/client.html index 7e97e77d..0c1764d1 100644 --- a/test/client.html +++ b/test/client.html @@ -306,8 +306,7 @@

Warning: these tests can only be run from a web server.

Then simply visit http://localhost:8000/test/client.html

If you can't run the command, then you'll need to install Python or use some other web server.

- - +
diff --git a/test/conditionals.js b/test/conditionals.js index 5a9292dc..b0a8aa18 100644 --- a/test/conditionals.js +++ b/test/conditionals.js @@ -1,120 +1,120 @@ describe('Conditionals', function() { it('should evaluate as true (conditionals/if.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/if.html', model), '

The variable \'something\' is present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/if.html'), model), '

The variable \'something\' is present

'); done(); }); it('should evaluate as false and trigger condition (conditionals/ifElse.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifElse.html', model), '

The variable \'doesntexist\' is not present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElse.html'), model), '

The variable \'doesntexist\' is not present

'); done(); }); it('should evaluate as true (conditionals/ifValue.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifValue.html', model), '

The variable \'something\' is set to \'Some content\'

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifValue.html'), model), '

The variable \'something\' is set to \'Some content\'

'); done(); }); // #44 it('should evaluate as false (conditionals/ifEmptyArray.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifEmptyArray.html', model), '

The variable \'emptyArray\' is considered falsey

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifEmptyArray.html'), model), '

The variable \'emptyArray\' is considered falsey

'); done(); }); it('should evaluate as false and trigger condition (conditionals/ifElseValue.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseValue.html', model), '

The variable \'something\' is not set to \'no\'

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseValue.html'), model), '

The variable \'something\' is not set to \'no\'

'); done(); }); it('should evaluate as true (conditionals/unless.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unless.html', model), '

The variable \'doesntexist\' is not present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unless.html'), model), '

The variable \'doesntexist\' is not present

'); done(); }); it('should evaluate as true (conditionals/unless.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unless.html', model), '

The variable \'doesntexist\' is not present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unless.html'), model), '

The variable \'doesntexist\' is not present

'); done(); }); // #51 it('should evaluate as true (conditionals/unlessNull.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unlessNull.html', model), '

The variable \'nullVar\' is falsey

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessNull.html'), model), '

The variable \'nullVar\' is falsey

'); done(); }); it('should evaluate as true (conditionals/unlessValue.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unlessValue.html', model), '

The variable \'something\' is set to \'Some content\'

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessValue.html'), model), '

The variable \'something\' is set to \'Some content\'

'); done(); }); it('should evaluate as false and trigger condition (conditionals/unlessElseValue.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unlessElseValue.html', model), '

The variable \'something\' is not set to \'no\'

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessElseValue.html'), model), '

The variable \'something\' is not set to \'no\'

'); done(); }); it('should evaluate as false and as true (conditionals/ifElseIf.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseIf.html', model), '

The variable \'somethingElse\' is present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseIf.html'), model), '

The variable \'somethingElse\' is present

'); done(); }); it('should evaluate as false and as true (conditionals/unlessElseUnless.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/unlessElseUnless.html', model), '

The variable \'doesntexist\' is not present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessElseUnless.html'), model), '

The variable \'doesntexist\' is not present

'); done(); }); it('should evaluate as false (conditionals/and.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/and.html', model), '

and: false

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/and.html'), model), '

and: false

'); done(); }); it('should evaluate `and` truth table as

and: true

(conditionals/andTruthTable.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/andTruthTable.html', model), '

and: true true

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/andTruthTable.html'), model), '

and: true true

'); done(); }); it('should evaluate `or` truth table as

or: true true

or: true false

or: false true

(conditionals/orTruthTable.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/orTruthTable.html', model), '

or: true true

or: true false

or: false true

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/orTruthTable.html'), model), '

or: true true

or: true false

or: false true

'); done(); }); // #24 it('should evaluate as true (conditionals/orSameVar.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/orSameVar.html', model), '

or: true

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/orSameVar.html'), model), '

or: true

'); done(); }); it('should evaluate as false (conditionals/xor.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/xor.html', model), '

xor: false

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/xor.html'), model), '

xor: false

'); done(); }); it('should evaluate as true (conditionals/andOr.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/andOr.html', model), '

and + or: true

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/andOr.html'), model), '

and + or: true

'); done(); }); it('should evaluate as false and as true (conditionals/not.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/not.html', model), '

not: false

not: true

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/not.html'), model), '

not: false

not: true

'); done(); }); it('should evaluate one line if "if-something" as true (conditionals/oneLine.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLine.html', model), '

One line if.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLine.html'), model), '

One line if.

'); done(); }); // #36 it('should evaluate one line if "if-something" as true with no false condition supplied (conditionals/oneLineTrueOnly.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineTrueOnly.html', model), '

One line if.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineTrueOnly.html'), model), '

One line if.

'); done(); }); it('should evaluate one line if "if-something=\'Some content\'" as true (conditionals/oneLineValue.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineValue.html', model), '

One line if.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineValue.html'), model), '

One line if.

'); done(); }); it('should evaluate one line if "if-something=\'Some content\'" as true and still add the id attribute regardless of the if statement outcome (conditionals/oneLineValueWithAdditionalAttributesNotImpactedByIf.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineValueWithAdditionalAttributesNotImpactedByIf.html', model), '

One line if.

\ + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineValueWithAdditionalAttributesNotImpactedByIf.html'), model), '

One line if.

\

One line if.

\

One line if.

\ \ @@ -124,53 +124,53 @@ describe('Conditionals', function() { // #46 it('should evaluate one line if "if-something=\'\'" as false (conditionals/oneLineEmpty.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineEmpty.html', model), '

One line if.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineEmpty.html'), model), '

One line if.

'); done(); }); // #48 it('should evaluate both one line ifs "if-something" as true twice and apply two classes (conditionals/oneLineMulti.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineMulti.html', model), '

One line if.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineMulti.html'), model), '

One line if.

'); done(); }); it('should parse nested conditionals correctly (conditionals/nestedConditional.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/nestedConditional.html', model), '

The variable \'something\' and \'somethingElse\' are both present

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/nestedConditional.html'), model), '

The variable \'something\' and \'somethingElse\' are both present

'); done(); }); it('should render nothing if condition isn\'t met (conditionals/ifNotPresent.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifNotPresent.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifNotPresent.html'), model), '
'); done(); }); it('should evaluate one line if as false and apply no class (conditionals/oneLineFalse.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineFalse.html', model), '

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineFalse.html'), model), '

'); done(); }); it('should evaluate if statement that contains an element with a regex pattern (conditionals/ifEscapeRegex.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifEscapeRegex.html', model), ''); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifEscapeRegex.html'), model), ''); done(); }); it('should evaluate if statement that queries the same variable more than once (conditionals/duplicateVarInline.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/duplicateVarInline.html', model), '

True

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/duplicateVarInline.html'), model), '

True

'); done(); }); it('should evaluate if statement with multiple instances of the same operators inline (conditionals/duplicateOperatorInline.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/duplicateOperatorInline.html', model), '

True

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/duplicateOperatorInline.html'), model), '

True

'); done(); }); it('should evaluate containing regex pattern as false and trigger condition (conditionals/ifElseRegex.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseRegex.html', model), '

False

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseRegex.html'), model), '

False

'); done(); }); it('should evaluate if statement where elseif condition is a three character named object (conditionals/ifNestedProperties.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('conditionals/ifNestedProperties.html', model), '

Should render

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifNestedProperties.html'), model), '

Should render

'); done(); }); }); diff --git a/test/global.js b/test/global.js index 431f5db5..168f85bc 100644 --- a/test/global.js +++ b/test/global.js @@ -1,20 +1,24 @@ -var assert; - if (typeof process === 'object') { global.chai = require('chai'); global.chaiString = require('chai-string'); global.assert = chai.assert; global.makeModel = require('./models/model'); global.teddy = require('../teddy'); - global.model; -} -else { - assert = chai.assert; + global.model = makeModel(); + global.getTemplate = getTemplate; } +function getTemplate(template) { + if (typeof process === 'object') { + return template; + } + else { + return window.__html__[template]; + } +}; + before(function() { teddy.setTemplateRoot('test/templates'); - console.warn(teddy.compile('misc/templateToMinify.html', model)); model = makeModel(); if (typeof process === 'object') { chai.use(chaiString); diff --git a/test/includes.js b/test/includes.js index 7e94d7aa..33bac46c 100644 --- a/test/includes.js +++ b/test/includes.js @@ -1,82 +1,82 @@ describe('Includes', function() { it('should a template (includes/include.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/include.html', model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/include.html'), model), '

Some content

'); done(); }); it('should a template whose name is populated by a {variable} (includes/dynamicInclude.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/dynamicInclude.html', model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/dynamicInclude.html'), model), '

Some content

'); done(); }); it('should a template with arguments (includes/includeWithArguments.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/includeWithArguments.html', model), '

override

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/includeWithArguments.html'), model), '

override

'); done(); }); it('should a template with a nested include (includes/nestedInclude.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/nestedInclude.html', model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/nestedInclude.html'), model), '

Some content

'); done(); }); it('should a template with a nested include passing a text argument (includes/nestedIncludeWithArg.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/nestedIncludeWithArg.html', model), '

nested

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/nestedIncludeWithArg.html'), model), '

nested

'); done(); }); it('should a template with loop arguments (includes/nestedLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/nestedLoop.html', model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/nestedLoop.html'), model), '

a

b

c

'); done(); }); it('should ignore and skip rendering orphaned argument (includes/orphanedArgument.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/orphanedArgument.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/orphanedArgument.html'), model), '
'); done(); }); it('should a template that contains loops and variables with an argument (includes/includeLoopsAndVars.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/includeLoopsAndVars.html', model), '

a

b

c

world

guy

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/includeLoopsAndVars.html'), model), '

a

b

c

world

guy

'); done(); }); it('should a template that contains numerical {variables} (includes/numericVarInArg.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/numericVarInArg.html', model), '

STRING!

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/numericVarInArg.html'), model), '

STRING!

'); done(); }); it('should evaluate if statement within style element as an argument (includes/conditionArgInStyle.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/conditionArgInStyle.html', model), ''); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/conditionArgInStyle.html'), model), ''); done(); }); it('should a template with numeric arguments (includes/numericArgument.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/numericArgument.html', model), '

Hello!

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/numericArgument.html'), model), '

Hello!

'); done(); }); it('should escape the contents of a script when included in a template (includes/inlineScriptTag.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/inlineScriptTag.html', model), '

Hello!

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/inlineScriptTag.html'), model), '

Hello!

'); done(); }); it('should evaluate {variable} outside of include as original model value (includes/argRedefineModelVar.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/argRedefineModelVar.html', model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/argRedefineModelVar.html'), model), '

Some content

'); done(); }); it('should a template and escape regex pattern in argument (includes/includeEscapeRegex.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/includeEscapeRegex.html', model), ''); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/includeEscapeRegex.html'), model), ''); done(); }); it('should ignore includes with invalid markup (includes/invalidIncludeMarkup.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('includes/invalidIncludeMarkup.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/invalidIncludeMarkup.html'), model), '
'); done(); }); it('should escape from infinite loop of includes via setMaxPasses (includes/includeInfiniteLoop.html)', function(done) { teddy.setMaxPasses(100); - assert.equal(teddy.render('includes/includeInfiniteLoop.html', model), 'Render aborted due to max number of passes (100) exceeded; there is a possible infinite loop in your template logic.'); + assert.equal(teddy.render(getTemplate('includes/includeInfiniteLoop.html'), model), 'Render aborted due to max number of passes (100) exceeded; there is a possible infinite loop in your template logic.'); done(); }); }); diff --git a/test/looping.js b/test/looping.js index 1f66405f..8b6ff884 100644 --- a/test/looping.js +++ b/test/looping.js @@ -12,33 +12,33 @@ else { describe('Looping', function() { it('should loop through {letters} correctly (looping/loopVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/loopVal.html', model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopVal.html'), model), '

a

b

c

'); done(); }); it('should loop through {names} correctly (looping/loopKeyVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/loopKeyVal.html', model), '

jack

guy

jill

girl

hill

landscape

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopKeyVal.html'), model), '

jack

guy

jill

girl

hill

landscape

'); done(); }); it('should loop through {objects} correctly (looping/loopArrayOfObjects.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/loopArrayOfObjects.html', model), '

0

1

2

3

1

4

5

6

2

7

8

9

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopArrayOfObjects.html'), model), '

0

1

2

3

1

4

5

6

2

7

8

9

'); done(); }); it('should loop through a {nested.object} correctly (looping/nestedObjectLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/nestedObjectLoop.html', model), '

a: 4

b: 5

c: 6

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedObjectLoop.html'), model), '

a: 4

b: 5

c: 6

'); done(); }); it('should parse nested loops correctly (looping/nestedLoops.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/nestedLoops.html', model), '

1

  • 0: one
  • 1: two
  • 2: three

2

  • 0: four
  • 1: five
  • 2: six

3

  • 0: seven
  • 1: eight
  • 2: nine
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedLoops.html'), model), '

1

  • 0: one
  • 1: two
  • 2: three

2

  • 0: four
  • 1: five
  • 2: six

3

  • 0: seven
  • 1: eight
  • 2: nine
'); done(); }); // #47 and #39 it('should loop through a nested arrays correctly (looping/nestedArrays.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/nestedArrays.html', model), '

one

two

three

four

five

six

seven

eight

nine

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedArrays.html'), model), '

one

two

three

four

five

six

seven

eight

nine

'); done(); }); @@ -47,7 +47,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -60,7 +60,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -74,7 +74,7 @@ describe('Looping', function() { start = new Date().getTime(); teddy.flushCache('looping/largeDataSet.html', model); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -87,7 +87,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -101,7 +101,7 @@ describe('Looping', function() { start = new Date().getTime(); teddy.flushCache('looping/largeDataSet.html'); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -114,7 +114,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render('looping/largeDataSet.html', model); + teddy.render(getTemplate('looping/largeDataSet.html'), model); end = new Date().getTime(); time = end - start; @@ -125,32 +125,32 @@ describe('Looping', function() { }); it('should ignore loop with invalid through attribute (looping/undefinedObjectLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/undefinedObjectLoop.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/undefinedObjectLoop.html'), model), '
'); done(); }); it('should ignore loop with no contents (looping/emptyMarkupLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/emptyMarkupLoop.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/emptyMarkupLoop.html'), model), '
'); done(); }); it('should loop without nested markup (looping/noMarkupLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/noMarkupLoop.html', model), '
abc
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/noMarkupLoop.html'), model), '
abc
'); done(); }); it('should loop through {letters} correctly with numeric val (looping/numericalVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/numericalVal.html', model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/numericalVal.html'), model), '

a

b

c

'); done(); }); it('should loop through {letters} correctly with camelCase val (looping/camelCaseLoopVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/camelCaseLoopVal.html', model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/camelCaseLoopVal.html'), model), '

a

b

c

'); done(); }); it('should ignore loops with missing attributes (looping/loopInvalidAttributes.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('looping/loopInvalidAttributes.html', model), '
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopInvalidAttributes.html'), model), '
'); done(); }); }); diff --git a/test/misc.js b/test/misc.js index 87f04339..19163210 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,62 +1,62 @@ describe('Misc', function() { it('should render {variables} (misc/variable.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/variable.html', model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/variable.html'), model), '

Some content

'); done(); }); it('should render multiple {variables} (misc/multipleVariables.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/multipleVariables.html', model), '

Some content

More content
'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/multipleVariables.html'), model), '

Some content

More content
'); done(); }); it('should render nested {variables} (misc/nestedVars.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/nestedVars.html', model), '

Variable with a variable inside: And another: Some content

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/nestedVars.html'), model), '

Variable with a variable inside: And another: Some content

'); done(); }); it('should properly escape HTML entities present in {variables} (misc/varEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/varEscaping.html', model), '

<span>raw html</span>

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varEscaping.html'), model), '

<span>raw html</span>

'); done(); }); it('should not escape HTML entities present in {variables} which are properly {flagged|s} (misc/varNoEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/varNoEscaping.html', model), '

raw html

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varNoEscaping.html'), model), '

raw html

'); done(); }); it('should remove {! server side comments !} (misc/serverSideComments.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/serverSideComments.html', model), '

test test

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/serverSideComments.html'), model), '

test test

'); done(); }); // #27 and #43 it('should remove {! {! nested !} server side comments !} (misc/serverSideCommentsNested.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/serverSideCommentsNested.html', model), '

Any comments?

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/serverSideCommentsNested.html'), model), '

Any comments?

'); done(); }); it('should not break when referencing objects that don\'t exist (misc/objectDoesNotExist.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/objectDoesNotExist.html', model), '

{doesntExist.someKey}

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/objectDoesNotExist.html'), model), '

{doesntExist.someKey}

'); done(); }); it('should render plain HTML with no teddy tags with no changes (misc/plainHTML.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/plainHTML.html', model), 'Plain HTML

This template contains no teddy tags. Just HTML.

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/plainHTML.html'), model), 'Plain HTML

This template contains no teddy tags. Just HTML.

'); done(); }); it('should render {variables} within style element (misc/styleVariables.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/styleVariables.html', model), ''); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/styleVariables.html'), model), ''); done(); }); it('should access property of {variable} object with {variable} (misc/variableObjectProperty.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/variableObjectProperty.html', model), '

guy

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/variableObjectProperty.html'), model), '

guy

'); done(); }); it('should escape curly braces from regex pattern (misc/regexEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/regexEscaping.html', model), ''); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/regexEscaping.html'), model), ''); done(); }); @@ -65,7 +65,7 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.setDefaultCaches(10); for (i = 0; i < 100; i++) { - teddy.render('misc/variable.html', {something: i}); + teddy.render(getTemplate('misc/variable.html'), {something: i}); } assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

90

'); teddy.setDefaultCaches(1); @@ -77,7 +77,7 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.renderedTemplates = {}; teddy.setCacheBlacklist(['misc/variable.html']); - teddy.render('misc/variable.html', {something: 1}); + teddy.render(getTemplate('misc/variable.html'), {something: 1}); assert.equal(teddy.renderedTemplates['misc/variable.html'], undefined); teddy.setCacheBlacklist([]); teddy.cacheRenders(false); @@ -88,8 +88,8 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.renderedTemplates = {}; teddy.setCacheWhitelist({'misc/variable.html': 1}); - teddy.render('misc/plainHTML.html', {something: 1}); - teddy.render('misc/variable.html', {something: 1}); + teddy.render(getTemplate('misc/plainHTML.html'), {something: 1}); + teddy.render(getTemplate('misc/variable.html'), {something: 1}); assert.equal(teddy.renderedTemplates['misc/plainHTML.html'], undefined); assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

1

'); teddy.setCacheWhitelist({}); @@ -103,9 +103,9 @@ describe('Misc', function() { teddy.renderedTemplates = {}; teddy.setDefaultCaches(10); teddy.setCacheWhitelist({'misc/variable.html': 10}); - teddy.render('misc/plainHTML.html', {something: 1}); + teddy.render(getTemplate('misc/plainHTML.html'), {something: 1}); for (i = 0; i < 100; i++) { - teddy.render('misc/variable.html', {something: i}); + teddy.render(getTemplate('misc/variable.html'), {something: i}); } assert.equal(teddy.renderedTemplates['misc/plainHTML.html'], undefined); assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

90

'); @@ -125,12 +125,12 @@ describe('Misc', function() { }); it('should render a template with missing or invalid model (misc/emptyModelMarkup.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/emptyModelMarkup.html', 1), '

Hello

'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/emptyModelMarkup.html'), 1), '

Hello

'); done(); }); it('should not render {variables} that don\'t exist in the model (misc/varNotInModel.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render('misc/varNotInModel.html', model), '{noExist}'); + assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varNotInModel.html'), model), '{noExist}'); done(); }); @@ -166,7 +166,7 @@ describe('Misc', function() { it('should minify template with internal minifier (misc/templateToMinify.html)', function(done) { teddy.minify(true); - assert.equal(teddy.render('misc/templateToMinify.html', model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); + assert.equal(teddy.render(getTemplate('misc/templateToMinify.html'), model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); teddy.minify(false); done(); }); From 408c343262cf857eb6b797d838ac5a5cf7b710f2 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Wed, 3 May 2017 12:47:29 -0400 Subject: [PATCH 14/16] lots of cool stuff --- karma.conf.js | 10 ++++++- package.json | 3 +- teddy.js | 29 +++++++++++++++++++ test/client.html | 61 ++++++++++------------------------------ test/conditionals.js | 66 ++++++++++++++++++++++---------------------- test/global.js | 13 --------- test/includes.js | 32 ++++++++++----------- test/looping.js | 40 +++++++++++++-------------- test/misc.js | 44 +++++++++++++++-------------- 9 files changed, 146 insertions(+), 152 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 12095cbd..c4175a16 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -13,7 +13,7 @@ module.exports = function(config) { 'test/*.js', 'test/client.html' ], - reporters: ['progress', 'coverage'], + reporters: ['spec', 'coverage'], port: 8000, proxies: { '/templates/': '/base/test/templates/', @@ -25,6 +25,14 @@ module.exports = function(config) { html2JsPreprocessor: { stripPrefix: 'test/templates/' }, + specReporter: { + maxLogLines: 5, + suppressErrorSummary: false, + suppressFailed: false, + suppressPassed: false, + suppressSkipped: false, + showSpecTiming: true + }, coverageReporter: { type: 'lcov', dir: 'coverage/', diff --git a/package.json b/package.json index 44ce72b0..d9e29c0d 100755 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "karma-chrome-launcher": "2.0.0", "karma-coverage-allsources": "0.0.4", "lcov-result-merger": "^1.2.0", - "karma-html2js-preprocessor": "1.1.0" + "karma-html2js-preprocessor": "1.1.0", + "karma-spec-reporter": "0.0.31" }, "eslintConfig": { "env": { diff --git a/teddy.js b/teddy.js index 0169abec..ed115020 100644 --- a/teddy.js +++ b/teddy.js @@ -102,6 +102,20 @@ * mutator methods for public member vars */ + // sets all params to their default values + setDefaultParams: function() { + teddy.params.verbosity = 1; + teddy.params.templateRoot = './'; + teddy.params.cacheRenders = false; + teddy.params.defaultCaches = 1; + teddy.params.templateMaxCaches = {}; + teddy.params.cacheWhitelist = false; + teddy.params.cacheBlacklist = []; + teddy.params.compileAtEveryRender = false; + teddy.params.minify = false; + teddy.params.maxPasses = 25000; + }, + // mutator method to set verbosity param. takes human-readable string argument and converts it to an integer for more efficient checks against the setting setVerbosity: function(v) { switch (v) { @@ -225,6 +239,12 @@ } } } + else { + if (teddy.templates[template]) { + template = teddy.templates[template]; + register = true; + } + } // remove {! comments !} and (optionally) unnecessary whitespace do { @@ -257,6 +277,15 @@ // invalidates cache of a given template and model combination // if no model is supplied, deletes all caches of the given template flushCache: function(template, model) { + + // ensure template is a string + if (typeof template !== 'string') { + if (teddy.params.verbosity > 1) { + teddy.console.warn('teddy.flushCache attempted to invalidate cache of template that is not a string'); + } + return ''; + } + // append extension if not present if (template.slice(-5) !== '.html') { template += '.html'; diff --git a/test/client.html b/test/client.html index fb39e1de..824d6529 100644 --- a/test/client.html +++ b/test/client.html @@ -6,21 +6,6 @@ Teddy Templating Engine unit tests '); + assert.equalIgnoreSpaces(teddy.render('includes/conditionArgInStyle.html', model), ''); done(); }); it('should a template with numeric arguments (includes/numericArgument.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/numericArgument.html'), model), '

Hello!

'); + assert.equalIgnoreSpaces(teddy.render('includes/numericArgument.html', model), '

Hello!

'); done(); }); it('should escape the contents of a script when included in a template (includes/inlineScriptTag.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/inlineScriptTag.html'), model), '

Hello!

'); + assert.equalIgnoreSpaces(teddy.render('includes/inlineScriptTag.html', model), '

Hello!

'); done(); }); it('should evaluate {variable} outside of include as original model value (includes/argRedefineModelVar.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/argRedefineModelVar.html'), model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render('includes/argRedefineModelVar.html', model), '

Some content

'); done(); }); it('should a template and escape regex pattern in argument (includes/includeEscapeRegex.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/includeEscapeRegex.html'), model), ''); + assert.equalIgnoreSpaces(teddy.render('includes/includeEscapeRegex.html', model), ''); done(); }); it('should ignore includes with invalid markup (includes/invalidIncludeMarkup.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('includes/invalidIncludeMarkup.html'), model), '
'); + assert.equalIgnoreSpaces(teddy.render('includes/invalidIncludeMarkup.html', model), '
'); done(); }); it('should escape from infinite loop of includes via setMaxPasses (includes/includeInfiniteLoop.html)', function(done) { teddy.setMaxPasses(100); - assert.equal(teddy.render(getTemplate('includes/includeInfiniteLoop.html'), model), 'Render aborted due to max number of passes (100) exceeded; there is a possible infinite loop in your template logic.'); + assert.equal(teddy.render('includes/includeInfiniteLoop.html', model), 'Render aborted due to max number of passes (100) exceeded; there is a possible infinite loop in your template logic.'); done(); }); }); diff --git a/test/looping.js b/test/looping.js index 8b6ff884..55ccf077 100644 --- a/test/looping.js +++ b/test/looping.js @@ -3,42 +3,42 @@ var loopMs; if (typeof process === 'object') { loopMs = 50; if (process.env.NODE_ENV === 'cover') { - loopMs = 400; + loopMs = 500; } } else { - loopMs = 400; + loopMs = 500; } describe('Looping', function() { it('should loop through {letters} correctly (looping/loopVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopVal.html'), model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render('looping/loopVal.html', model), '

a

b

c

'); done(); }); it('should loop through {names} correctly (looping/loopKeyVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopKeyVal.html'), model), '

jack

guy

jill

girl

hill

landscape

'); + assert.equalIgnoreSpaces(teddy.render('looping/loopKeyVal.html', model), '

jack

guy

jill

girl

hill

landscape

'); done(); }); it('should loop through {objects} correctly (looping/loopArrayOfObjects.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopArrayOfObjects.html'), model), '

0

1

2

3

1

4

5

6

2

7

8

9

'); + assert.equalIgnoreSpaces(teddy.render('looping/loopArrayOfObjects.html', model), '

0

1

2

3

1

4

5

6

2

7

8

9

'); done(); }); it('should loop through a {nested.object} correctly (looping/nestedObjectLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedObjectLoop.html'), model), '

a: 4

b: 5

c: 6

'); + assert.equalIgnoreSpaces(teddy.render('looping/nestedObjectLoop.html', model), '

a: 4

b: 5

c: 6

'); done(); }); it('should parse nested loops correctly (looping/nestedLoops.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedLoops.html'), model), '

1

  • 0: one
  • 1: two
  • 2: three

2

  • 0: four
  • 1: five
  • 2: six

3

  • 0: seven
  • 1: eight
  • 2: nine
'); + assert.equalIgnoreSpaces(teddy.render('looping/nestedLoops.html', model), '

1

  • 0: one
  • 1: two
  • 2: three

2

  • 0: four
  • 1: five
  • 2: six

3

  • 0: seven
  • 1: eight
  • 2: nine
'); done(); }); // #47 and #39 it('should loop through a nested arrays correctly (looping/nestedArrays.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/nestedArrays.html'), model), '

one

two

three

four

five

six

seven

eight

nine

'); + assert.equalIgnoreSpaces(teddy.render('looping/nestedArrays.html', model), '

one

two

three

four

five

six

seven

eight

nine

'); done(); }); @@ -47,7 +47,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -60,7 +60,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -74,7 +74,7 @@ describe('Looping', function() { start = new Date().getTime(); teddy.flushCache('looping/largeDataSet.html', model); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -87,7 +87,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -101,7 +101,7 @@ describe('Looping', function() { start = new Date().getTime(); teddy.flushCache('looping/largeDataSet.html'); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -114,7 +114,7 @@ describe('Looping', function() { var start, end, time; start = new Date().getTime(); - teddy.render(getTemplate('looping/largeDataSet.html'), model); + teddy.render('looping/largeDataSet.html', model); end = new Date().getTime(); time = end - start; @@ -125,32 +125,32 @@ describe('Looping', function() { }); it('should ignore loop with invalid through attribute (looping/undefinedObjectLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/undefinedObjectLoop.html'), model), '
'); + assert.equalIgnoreSpaces(teddy.render('looping/undefinedObjectLoop.html', model), '
'); done(); }); it('should ignore loop with no contents (looping/emptyMarkupLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/emptyMarkupLoop.html'), model), '
'); + assert.equalIgnoreSpaces(teddy.render('looping/emptyMarkupLoop.html', model), '
'); done(); }); it('should loop without nested markup (looping/noMarkupLoop.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/noMarkupLoop.html'), model), '
abc
'); + assert.equalIgnoreSpaces(teddy.render('looping/noMarkupLoop.html', model), '
abc
'); done(); }); it('should loop through {letters} correctly with numeric val (looping/numericalVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/numericalVal.html'), model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render('looping/numericalVal.html', model), '

a

b

c

'); done(); }); it('should loop through {letters} correctly with camelCase val (looping/camelCaseLoopVal.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/camelCaseLoopVal.html'), model), '

a

b

c

'); + assert.equalIgnoreSpaces(teddy.render('looping/camelCaseLoopVal.html', model), '

a

b

c

'); done(); }); it('should ignore loops with missing attributes (looping/loopInvalidAttributes.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('looping/loopInvalidAttributes.html'), model), '
'); + assert.equalIgnoreSpaces(teddy.render('looping/loopInvalidAttributes.html', model), '
'); done(); }); }); diff --git a/test/misc.js b/test/misc.js index 19163210..dc54a9a9 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,62 +1,62 @@ describe('Misc', function() { it('should render {variables} (misc/variable.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/variable.html'), model), '

Some content

'); + assert.equalIgnoreSpaces(teddy.render('misc/variable.html', model), '

Some content

'); done(); }); it('should render multiple {variables} (misc/multipleVariables.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/multipleVariables.html'), model), '

Some content

More content
'); + assert.equalIgnoreSpaces(teddy.render('misc/multipleVariables.html', model), '

Some content

More content
'); done(); }); it('should render nested {variables} (misc/nestedVars.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/nestedVars.html'), model), '

Variable with a variable inside: And another: Some content

'); + assert.equalIgnoreSpaces(teddy.render('misc/nestedVars.html', model), '

Variable with a variable inside: And another: Some content

'); done(); }); it('should properly escape HTML entities present in {variables} (misc/varEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varEscaping.html'), model), '

<span>raw html</span>

'); + assert.equalIgnoreSpaces(teddy.render('misc/varEscaping.html', model), '

<span>raw html</span>

'); done(); }); it('should not escape HTML entities present in {variables} which are properly {flagged|s} (misc/varNoEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varNoEscaping.html'), model), '

raw html

'); + assert.equalIgnoreSpaces(teddy.render('misc/varNoEscaping.html', model), '

raw html

'); done(); }); it('should remove {! server side comments !} (misc/serverSideComments.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/serverSideComments.html'), model), '

test test

'); + assert.equalIgnoreSpaces(teddy.render('misc/serverSideComments.html', model), '

test test

'); done(); }); // #27 and #43 it('should remove {! {! nested !} server side comments !} (misc/serverSideCommentsNested.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/serverSideCommentsNested.html'), model), '

Any comments?

'); + assert.equalIgnoreSpaces(teddy.render('misc/serverSideCommentsNested.html', model), '

Any comments?

'); done(); }); it('should not break when referencing objects that don\'t exist (misc/objectDoesNotExist.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/objectDoesNotExist.html'), model), '

{doesntExist.someKey}

'); + assert.equalIgnoreSpaces(teddy.render('misc/objectDoesNotExist.html', model), '

{doesntExist.someKey}

'); done(); }); it('should render plain HTML with no teddy tags with no changes (misc/plainHTML.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/plainHTML.html'), model), 'Plain HTML

This template contains no teddy tags. Just HTML.

'); + assert.equalIgnoreSpaces(teddy.render('misc/plainHTML.html', model), 'Plain HTML

This template contains no teddy tags. Just HTML.

'); done(); }); it('should render {variables} within style element (misc/styleVariables.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/styleVariables.html'), model), ''); + assert.equalIgnoreSpaces(teddy.render('misc/styleVariables.html', model), ''); done(); }); it('should access property of {variable} object with {variable} (misc/variableObjectProperty.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/variableObjectProperty.html'), model), '

guy

'); + assert.equalIgnoreSpaces(teddy.render('misc/variableObjectProperty.html', model), '

guy

'); done(); }); it('should escape curly braces from regex pattern (misc/regexEscaping.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/regexEscaping.html'), model), ''); + assert.equalIgnoreSpaces(teddy.render('misc/regexEscaping.html', model), ''); done(); }); @@ -65,7 +65,7 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.setDefaultCaches(10); for (i = 0; i < 100; i++) { - teddy.render(getTemplate('misc/variable.html'), {something: i}); + teddy.render('misc/variable.html', {something: i}); } assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

90

'); teddy.setDefaultCaches(1); @@ -77,7 +77,7 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.renderedTemplates = {}; teddy.setCacheBlacklist(['misc/variable.html']); - teddy.render(getTemplate('misc/variable.html'), {something: 1}); + teddy.render('misc/variable.html', {something: 1}); assert.equal(teddy.renderedTemplates['misc/variable.html'], undefined); teddy.setCacheBlacklist([]); teddy.cacheRenders(false); @@ -88,8 +88,8 @@ describe('Misc', function() { teddy.cacheRenders(true); teddy.renderedTemplates = {}; teddy.setCacheWhitelist({'misc/variable.html': 1}); - teddy.render(getTemplate('misc/plainHTML.html'), {something: 1}); - teddy.render(getTemplate('misc/variable.html'), {something: 1}); + teddy.render('misc/plainHTML.html', {something: 1}); + teddy.render('misc/variable.html', {something: 1}); assert.equal(teddy.renderedTemplates['misc/plainHTML.html'], undefined); assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

1

'); teddy.setCacheWhitelist({}); @@ -103,9 +103,9 @@ describe('Misc', function() { teddy.renderedTemplates = {}; teddy.setDefaultCaches(10); teddy.setCacheWhitelist({'misc/variable.html': 10}); - teddy.render(getTemplate('misc/plainHTML.html'), {something: 1}); + teddy.render('misc/plainHTML.html', {something: 1}); for (i = 0; i < 100; i++) { - teddy.render(getTemplate('misc/variable.html'), {something: i}); + teddy.render('misc/variable.html', {something: i}); } assert.equal(teddy.renderedTemplates['misc/plainHTML.html'], undefined); assert.equalIgnoreSpaces(teddy.renderedTemplates['misc/variable.html'][0].renderedTemplate, '

90

'); @@ -125,12 +125,12 @@ describe('Misc', function() { }); it('should render a template with missing or invalid model (misc/emptyModelMarkup.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/emptyModelMarkup.html'), 1), '

Hello

'); + assert.equalIgnoreSpaces(teddy.render('misc/emptyModelMarkup.html', 1), '

Hello

'); done(); }); it('should not render {variables} that don\'t exist in the model (misc/varNotInModel.html)', function(done) { - assert.equalIgnoreSpaces(teddy.render(getTemplate('misc/varNotInModel.html'), model), '{noExist}'); + assert.equalIgnoreSpaces(teddy.render('misc/varNotInModel.html', model), '{noExist}'); done(); }); @@ -165,9 +165,11 @@ describe('Misc', function() { }); it('should minify template with internal minifier (misc/templateToMinify.html)', function(done) { + teddy.compileAtEveryRender(true); teddy.minify(true); - assert.equal(teddy.render(getTemplate('misc/templateToMinify.html'), model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); + assert.equal(teddy.render('misc/templateToMinify.html', model), ' Plain HTML

This template contains no teddy tags. Just HTML.

'); teddy.minify(false); + teddy.compileAtEveryRender(false); done(); }); }); From d6cc9a5eba8e03aa00b8ae6511a5b9a6ee8e2399 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Wed, 3 May 2017 13:36:25 -0400 Subject: [PATCH 15/16] more tweaks --- test/client.html | 4 ++++ test/misc.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/test/client.html b/test/client.html index 824d6529..9df4646c 100644 --- a/test/client.html +++ b/test/client.html @@ -10,6 +10,10 @@ margin: 10px !important; font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; } + + /* + * related issue: https://github.com/karma-runner/karma-mocha/issues/176 + */ #mocha-error { display: none; } diff --git a/test/misc.js b/test/misc.js index dc54a9a9..334d0c37 100644 --- a/test/misc.js +++ b/test/misc.js @@ -161,6 +161,9 @@ describe('Misc', function() { teddy.setVerbosity(3); } } + else { + teddy.setVerbosity(0); + } done(); }); @@ -172,4 +175,9 @@ describe('Misc', function() { teddy.compileAtEveryRender(false); done(); }); + + it('should avoid flushing cache of non strings', function(done) { + assert.equalIgnoreSpaces(teddy.flushCache(5), ''); + done(); + }); }); From fe19d482ee5e06cbc7e9a8c3a9da04f0a6153d4e Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Wed, 3 May 2017 13:46:19 -0400 Subject: [PATCH 16/16] use setDefaultParam function --- teddy.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/teddy.js b/teddy.js index ed115020..82930043 100644 --- a/teddy.js +++ b/teddy.js @@ -79,18 +79,7 @@ */ // default values for parameters sent to teddy - params: { - verbosity: 1, - templateRoot: './', - cacheRenders: false, - defaultCaches: 1, - templateMaxCaches: {}, - cacheWhitelist: false, - cacheBlacklist: [], - compileAtEveryRender: false, - minify: false, - maxPasses: 25000 - }, + params: {}, // compiled templates are stored as object collections, e.g. { "myTemplate.html": "

some markup

"} templates: {}, @@ -1203,6 +1192,9 @@ } }; + // set params to default values + teddy.setDefaultParams(); + /** * private utility methods */