From 408c343262cf857eb6b797d838ac5a5cf7b710f2 Mon Sep 17 00:00:00 2001 From: Troy Coutu Date: Wed, 3 May 2017 12:47:29 -0400 Subject: [PATCH] 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(); }); });