Skip to content

Commit

Permalink
Merge pull request #124 from Autre31415/moarCoverage
Browse files Browse the repository at this point in the history
Moar coverage
  • Loading branch information
kethinov authored Apr 12, 2017
2 parents 5fcd67d + 0f6c8a2 commit b3c6da7
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 210 deletions.
346 changes: 140 additions & 206 deletions teddy.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion test/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ <h2>Warning: these tests can only be run from a web server.</h2>
'conditionals/duplicateVarInline.html',
'conditionals/duplicateOperatorInline.html',
'conditionals/ifElseRegex.html',
'conditionals/ifNestedProperties.html',
'includes/argVariableWithinArg.html',
'includes/conditionArgInStyle.html',
'includes/dynamicInclude.html',
Expand All @@ -385,6 +386,7 @@ <h2>Warning: these tests can only be run from a web server.</h2>
'includes/argRedefineModelVar.html',
'includes/includeEscapeRegex.html',
'includes/invalidIncludeMarkup.html',
'includes/includeInfiniteLoop.html',
'looping/emptyMarkupLoop.html',
'looping/largeDataSet.html',
'looping/loopArrayOfObjects.html',
Expand Down Expand Up @@ -412,7 +414,10 @@ <h2>Warning: these tests can only be run from a web server.</h2>
'misc/varEscaping.html',
'misc/varNoEscaping.html',
'misc/variable.html',
'misc/variableObjectProperty.html'
'misc/variableObjectProperty.html',
'misc/emptyModelMarkup.html',
'misc/infiniteIncludeTemplate.html',
'misc/varNotInModel.html'
];
templateLength = templateList.length;

Expand Down
9 changes: 7 additions & 2 deletions test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ describe('Conditionals', function() {
done();
});

it('should evaluate <if not:something> as false (conditionals/not.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/not.html', model), ' <p>not: false</p>');
it('should evaluate <if not:something> as false and <if not:noExist> as true (conditionals/not.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/not.html', model), ' <p>not: false</p><p>not: true</p>');
done();
});

Expand Down Expand Up @@ -180,4 +180,9 @@ describe('Conditionals', function() {
assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseRegex.html', model), '<p>False</p>');
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), '<p>Should render</p>');
done();
});
});
6 changes: 6 additions & 0 deletions test/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ describe('Includes', function() {
assert.equalIgnoreSpaces(teddy.render('includes/invalidIncludeMarkup.html', model), '<div></div>');
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.');
done();
});
});
54 changes: 54 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ if (typeof module !== 'undefined') {
assert = chai.assert,
model,
makeModel = require('./models/model'),
verbosity = '',
teddy = require('../teddy');
chai.use(chaiString);
}
Expand Down Expand Up @@ -134,4 +135,57 @@ describe('Misc', function() {
teddy.cacheRenders(false);
done();
});

it('should avoid rendering templates that are not strings', function(done) {
assert.equalIgnoreSpaces(teddy.render(5, model), '');
done();
});

it('should avoid compiling templates that are not strings', function(done) {
assert.equalIgnoreSpaces(teddy.compile(5, model), '');
done();
});

it('should render a template with missing or invalid model (misc/emptyModelMarkup.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render('misc/emptyModelMarkup.html', 1), '<div><p>Hello</p></div>');
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}');
done();
});

it('should set each verbosity level', function(done) {
teddy.setVerbosity();
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity('none');
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity(0);
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity('verbose');
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity(2);
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity('DEBUG');
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity(3);
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);
}
done();
});

it('should minify template with internal minifier (misc/plainHTML.html)', function(done) {
teddy.minify(true);
assert.equal(teddy.compile('misc/plainHTML.html', model), '<!DOCTYPE html><html lang=\'en\'> <head> <meta charset=\'utf-8\'> <meta name=\'viewport\' content=\'width=device-width,initial-scale=1\'> <meta name=\'format-detection\' content=\'telephone=no\'> <title>Plain HTML</title> <link rel=\'stylesheet\' href=\'/css/styles.css\'> </head> <body> <main> <p>This template contains no teddy tags. Just HTML.</p> </main> <script type=\'text/javascript\' src=\'/js/main.js\'></script> </body></html>');
teddy.minify(false);
done();
});
});
13 changes: 13 additions & 0 deletions test/templates/conditionals/ifNestedProperties.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{!
should evaluate if statement where elseif condition is a three character named object
!}
<if nothingToSeeHere>
<p>Should not render</p>
</if>
<elseif lol.why>
<p>Should not render</p>
</elseif>
<else>
<p>Should render</p>
</else>

10 changes: 9 additions & 1 deletion test/templates/conditionals/not.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{!
should evaluate <if not:something> as false
should evaluate <if not:something> as false and <if not:noExist> as true
!}

{! false !}
Expand All @@ -9,3 +9,11 @@
<else>
<p>not: false</p>
</else>

{! true !}
<if not:noExist>
<p>not: true</p>
</if>
<else>
<p>should not render</p>
</else>
10 changes: 10 additions & 0 deletions test/templates/includes/includeInfiniteLoop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{!
should escape from infinite loop of includes via setMaxPasses
!}
{infinity2}
<include src='misc/infiniteIncludeTemplate.html'>
<arg infinity>
<p>Recursion</p>
<include src='includes/includeInfiniteLoop.html'></include>
</arg>
</include>
7 changes: 7 additions & 0 deletions test/templates/misc/emptyModelMarkup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{!
should render a template with missing or invalid model
!}

<div>
<p>Hello</p>
</div>
4 changes: 4 additions & 0 deletions test/templates/misc/infiniteIncludeTemplate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{!
should escape from infinite loop of includes via setMaxPasses
!}
{infinity}
4 changes: 4 additions & 0 deletions test/templates/misc/varNotInModel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{!
should not render {variables} that don't exist in the model
!}
{noExist}

0 comments on commit b3c6da7

Please sign in to comment.