Skip to content

Commit

Permalink
lots of cool stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Autre31415 committed May 3, 2017
1 parent c12ff12 commit 408c343
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 152 deletions.
10 changes: 9 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand All @@ -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/',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 29 additions & 0 deletions teddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -225,6 +239,12 @@
}
}
}
else {
if (teddy.templates[template]) {
template = teddy.templates[template];
register = true;
}
}

// remove {! comments !} and (optionally) unnecessary whitespace
do {
Expand Down Expand Up @@ -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';
Expand Down
61 changes: 14 additions & 47 deletions test/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
<meta name='format-detection' content='telephone=no'>
<title>Teddy Templating Engine unit tests</title>
<style>
#crossOriginError {
animation: 1s fadeIn;
animation-fill-mode: forwards;
animation-delay: 1s;
visibility: hidden;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
visibility: visible;
opacity: 1;
}
}
body {
margin: 10px !important;
font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
Expand Down Expand Up @@ -297,30 +282,8 @@
<body>
<h1>Teddy Templating Engine unit tests</h1>

<article id='crossOriginError'>
<h2>Warning: these tests can only be run from a web server.</h2>
<p>Due to the <a href='http://en.wikipedia.org/wiki/Same_origin_policy'>same origin policy</a> applying to files loaded directly from the local filesystem, these unit tests can only be executed from a real HTTP server.</p>
<p>To start a simple web server to run these tests with, open your terminal and run this command from the "teddy" directory:</p>
<pre>python -m SimpleHTTPServer</pre>
<p><em>(For python 2.x.)</em></p>
<p>If that command doesn't work, try the python 3.x version instead:</p>
<pre>python -m http.server</pre><p><em>(For python 3.x.)</em></p>
<p>Then simply visit <a href='http://localhost:8000/test/client.html'>http://localhost:8000/test/client.html</a></p>
<p>If you can't run the command, then you'll need to <a href='http://www.python.org/'>install Python</a> or use some other web server.</p>
</article>

<div id='mocha'></div>

<script>
// test for old IE
var oldIE = document.createElement('p');
oldIE.innerHTML = '<!--[if lte IE 9]><i></i><![endif]-->';
oldIE = oldIE.getElementsByTagName('i').length === 1 ? true : false;
if (oldIE) {
document.getElementById('crossOriginError').style.visibility = 'visible';
}
</script>

<!-- include teddy library -->
<script src='../teddy.js'></script>
<script src='models/model.js'></script>
Expand All @@ -330,18 +293,22 @@ <h2>Warning: these tests can only be run from a web server.</h2>
<script src='../node_modules/chai/chai.js'></script>
<script src='../node_modules/chai-string/chai-string.js'></script>

<!-- include mocha tests -->
<script>mocha.setup('bdd');</script>
<script src='conditionals.js'></script>
<script src='includes.js'></script>
<script src='looping.js'></script>
<script src='misc.js'></script>

<!-- run tests -->
<!-- setup test suite -->
<script>
var assert = chai.assert;
var assert = chai.assert,
templates = window.__html__,
i;

teddy.setVerbosity(0);
mocha.run();

for (i in templates) {
teddy.templates[i] = teddy.compile(templates[i]);
}

mocha.setup('bdd');
</script>

<!-- run tests -->
<script>mocha.run();</script>
</body>
</html>
66 changes: 33 additions & 33 deletions test/conditionals.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
describe('Conditionals', function() {
it('should evaluate <if something> as true (conditionals/if.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/if.html'), model), '<p>The variable \'something\' is present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/if.html', model), '<p>The variable \'something\' is present</p>');
done();
});

it('should evaluate <if doesntexist> as false and trigger <else> condition (conditionals/ifElse.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElse.html'), model), '<p>The variable \'doesntexist\' is not present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifElse.html', model), '<p>The variable \'doesntexist\' is not present</p>');
done();
});

it('should evaluate <if something=\'Some content\'> as true (conditionals/ifValue.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifValue.html'), model), '<p>The variable \'something\' is set to \'Some content\'</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifValue.html', model), '<p>The variable \'something\' is set to \'Some content\'</p>');
done();
});

// #44
it('should evaluate <if emptyArray> as false (conditionals/ifEmptyArray.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifEmptyArray.html'), model), '<p>The variable \'emptyArray\' is considered falsey</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifEmptyArray.html', model), '<p>The variable \'emptyArray\' is considered falsey</p>');
done();
});

it('should evaluate <if something=\'no\'> as false and trigger <else> condition (conditionals/ifElseValue.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseValue.html'), model), '<p>The variable \'something\' is not set to \'no\'</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseValue.html', model), '<p>The variable \'something\' is not set to \'no\'</p>');
done();
});

it('should evaluate <unless doesntexist> as true (conditionals/unless.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unless.html'), model), '<p>The variable \'doesntexist\' is not present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unless.html', model), '<p>The variable \'doesntexist\' is not present</p>');
done();
});

it('should evaluate <unless doesntexist> as true (conditionals/unless.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unless.html'), model), '<p>The variable \'doesntexist\' is not present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unless.html', model), '<p>The variable \'doesntexist\' is not present</p>');
done();
});

// #51
it('should evaluate <unless nullVar> as true (conditionals/unlessNull.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessNull.html'), model), '<p>The variable \'nullVar\' is falsey</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unlessNull.html', model), '<p>The variable \'nullVar\' is falsey</p>');
done();
});

it('should evaluate <unless something=\'Some content\'> as true (conditionals/unlessValue.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessValue.html'), model), '<p>The variable \'something\' is set to \'Some content\'</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unlessValue.html', model), '<p>The variable \'something\' is set to \'Some content\'</p>');
done();
});

it('should evaluate <unless something=\'no\'> as false and trigger <else> condition (conditionals/unlessElseValue.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessElseValue.html'), model), '<p>The variable \'something\' is not set to \'no\'</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unlessElseValue.html', model), '<p>The variable \'something\' is not set to \'no\'</p>');
done();
});

it('should evaluate <if something=\'no\'> as false and <elseif somethingElse> as true (conditionals/ifElseIf.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseIf.html'), model), '<p>The variable \'somethingElse\' is present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifElseIf.html', model), '<p>The variable \'somethingElse\' is present</p>');
done();
});

it('should evaluate <unless something> as false and <elseunless doesntexist> as true (conditionals/unlessElseUnless.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/unlessElseUnless.html'), model), '<p>The variable \'doesntexist\' is not present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/unlessElseUnless.html', model), '<p>The variable \'doesntexist\' is not present</p>');
done();
});

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

it('should evaluate `and` truth table as <p>and: true</p> (conditionals/andTruthTable.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/andTruthTable.html'), model), ' <p>and: true true</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/andTruthTable.html', model), ' <p>and: true true</p>');
done();
});

it('should evaluate `or` truth table as <p>or: true true</p> <p>or: true false</p> <p>or: false true</p> (conditionals/orTruthTable.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/orTruthTable.html'), model), ' <p>or: true true</p> <p>or: true false</p> <p>or: false true</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/orTruthTable.html', model), ' <p>or: true true</p> <p>or: true false</p> <p>or: false true</p>');
done();
});

// #24
it('should evaluate <if something=\'Some content\' or something=\'Nope\'> as true (conditionals/orSameVar.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/orSameVar.html'), model), ' <p>or: true</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/orSameVar.html', model), ' <p>or: true</p>');
done();
});

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

it('should evaluate <if something and notDefined or somethingElse> as true (conditionals/andOr.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/andOr.html'), model), ' <p>and + or: true</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/andOr.html', model), ' <p>and + or: true</p>');
done();
});

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

it('should evaluate one line if "if-something" as true (conditionals/oneLine.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLine.html'), model), '<p class=\'something-is-present\'>One line if.</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLine.html', model), '<p class=\'something-is-present\'>One line if.</p>');
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(getTemplate('conditionals/oneLineTrueOnly.html'), model), '<p class=\'something-is-present\'>One line if.</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineTrueOnly.html', model), '<p class=\'something-is-present\'>One line if.</p>');
done();
});

it('should evaluate one line if "if-something=\'Some content\'" as true (conditionals/oneLineValue.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineValue.html'), model), '<p class=\'something-is-value\'>One line if.</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineValue.html', model), '<p class=\'something-is-value\'>One line if.</p>');
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(getTemplate('conditionals/oneLineValueWithAdditionalAttributesNotImpactedByIf.html'), model), '<p class=\'something-is-present\' id=\'someId\'>One line if.</p> \
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineValueWithAdditionalAttributesNotImpactedByIf.html', model), '<p class=\'something-is-present\' id=\'someId\'>One line if.</p> \
<p id=\'someId\'>One line if.</p> \
<p disabled id=\'someId\'>One line if.</p> \
<option selected value=\'3\'>One line if.</option> \
Expand All @@ -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(getTemplate('conditionals/oneLineEmpty.html'), model), '<p class=\'something-is-not-empty\'>One line if.</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineEmpty.html', model), '<p class=\'something-is-not-empty\'>One line if.</p>');
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(getTemplate('conditionals/oneLineMulti.html'), model), '<p class=\'something-is-present\' data-only-renders-when-something-is-not-empty data-should-render>One line if.</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineMulti.html', model), '<p class=\'something-is-present\' data-only-renders-when-something-is-not-empty data-should-render>One line if.</p>');
done();
});

it('should parse nested conditionals correctly (conditionals/nestedConditional.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/nestedConditional.html'), model), '<p>The variable \'something\' and \'somethingElse\' are both present</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/nestedConditional.html', model), '<p>The variable \'something\' and \'somethingElse\' are both present</p>');
done();
});

it('should render nothing if condition isn\'t met (conditionals/ifNotPresent.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifNotPresent.html'), model), '<div></div>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifNotPresent.html', model), '<div></div>');
done();
});

it('should evaluate one line if as false and apply no class (conditionals/oneLineFalse.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/oneLineFalse.html'), model), '<p></p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineFalse.html', model), '<p></p>');
done();
});

it('should evaluate if statement that contains an element with a regex pattern (conditionals/ifEscapeRegex.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifEscapeRegex.html'), model), '<input type=\'text\' name=\'date\' placeholder=\'DD/MM/YYYY\' id=\'date\' pattern=\'^(3[0-1]|[1-2]\\d|[1-9]|0\\d)\\/(1[0-2]|[1-9]|0\\d)\\/[1-2]\\d{3}$\'>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifEscapeRegex.html', model), '<input type=\'text\' name=\'date\' placeholder=\'DD/MM/YYYY\' id=\'date\' pattern=\'^(3[0-1]|[1-2]\\d|[1-9]|0\\d)\\/(1[0-2]|[1-9]|0\\d)\\/[1-2]\\d{3}$\'>');
done();
});

it('should evaluate if statement that queries the same variable more than once (conditionals/duplicateVarInline.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/duplicateVarInline.html'), model), '<p>True</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/duplicateVarInline.html', model), '<p>True</p>');
done();
});

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

it('should evaluate <if noExist> containing regex pattern as false and trigger <else> condition (conditionals/ifElseRegex.html)', function(done) {
assert.equalIgnoreSpaces(teddy.render(getTemplate('conditionals/ifElseRegex.html'), model), '<p>False</p>');
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(getTemplate('conditionals/ifNestedProperties.html'), model), '<p>Should render</p>');
assert.equalIgnoreSpaces(teddy.render('conditionals/ifNestedProperties.html', model), '<p>Should render</p>');
done();
});
});
Loading

0 comments on commit 408c343

Please sign in to comment.