Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

create test files and build them when building for browser

  • Loading branch information...
commit e79f8e3d914908c493e518b6861e7699809c30f6 1 parent 64570b8
@paularmstrong paularmstrong authored
View
3  .gitignore
@@ -2,4 +2,5 @@
npm-debug.log
node_modules/*
dist/browser/*
-dist/test/swig.pack.js
+dist/test/swig.pack.js
+dist/test/*.test.js
View
13 dist/test/basic.js
@@ -1,13 +0,0 @@
-this.basic = {
- variable: function (test) {
- var tpl = swig.fromString('{{ foobar }}', 'foo');
- test.strictEqual(tpl.render({ foobar: 'hi!' }), 'hi!');
- test.done();
- },
-
- 'variable with filter': function (test) {
- var tpl = swig.fromString('{{ foobar|title }}');
- test.strictEqual(tpl.render({ foobar: 'oh hey tHere' }), 'Oh Hey There');
- test.done();
- }
-};
View
187 dist/test/filters.js
@@ -1,187 +0,0 @@
-function testFilter(test, filter, input, output, message) {
- var tpl = swig.fromString('{{ v|' + filter + ' }}');
- test.strictEqual(tpl.render(input), output, message);
-}
-
-this.filters = {
- add: function (test) {
- testFilter(test, 'add(2)', { v: 1 }, '3', 'add numbers');
- testFilter(test, 'add([3, 4])', { v: [1, 2] }, '1,2,3,4', 'arrays add from literal');
- testFilter(test, 'add(b)', { v: [1, 2], b: [3, 4] }, '1,2,3,4', 'arrays add from var');
- testFilter(test, 'add(2)', { v: 'foo' }, 'foo2', 'string var turns addend into a string');
- testFilter(test, 'add("bar")', { v: 'foo' }, 'foobar', 'strings concatenated');
- testFilter(test, '{{ f|add({ bar: 2 }) }}', { v: { foo: 1 } }, { foo: 1, bar: 2 }, 'add objects from object literal');
- testFilter(test, '{{ f|add(b) }}', { v: { foo: 1 }, b: { bar: 2 } }, { foo: 1, bar: 2 }, 'add objects from var');
- testFilter(test, '{{ f|add(b) }}', { v: 'foo', b: [1, 2] }, 'foo1,2', 'add array to string');
- test.done();
- },
-
- addslashes: function (test) {
- testFilter(test, 'addslashes', { v: "\"Top O' the\\ mornin\"" }, "\\\"Top O\\' the\\\\ mornin\\\"", 'add slashes to string');
- testFilter(test, 'addslashes', { v: ["\"Top", "O'", "the\\", "mornin\""] }, "\\\"Top,O\\',the\\\\,mornin\\\"", 'add slashes to array');
- test.done();
- },
-
- capitalize: function (test) {
- testFilter(test, 'capitalize', { v: 'awesome sAuce.' }, 'Awesome sauce.');
- testFilter(test, 'capitalize', { v: 345 }, '345');
- testFilter(test, 'capitalize', { v: ['foo', 'bar'] }, 'Foo,Bar');
- test.done();
- },
-
- date: function (test) {
- var date = new Date(2011, 8, 6, 9, 5, 2),
- tpl = swig.fromString('{{ d|date("d") }}');
-
- function testFormat(format, expected) {
- testFilter(test, 'date("' + format + '")', { v: date }, expected, 'format: "' + format + '" - http://php.net/date');
- }
- // Day
- testFormat('d', '06');
- testFormat('D', 'Tue');
- testFormat('j', '6');
- testFormat('l', 'Tuesday');
- testFormat('N', '2');
- testFormat('S', 'th');
- testFormat('w', '1');
-
- // Month
- testFormat('F', 'September');
- testFormat('m', '09');
- testFormat('M', 'Sep');
- testFormat('n', '9');
-
- // Year
- testFormat('Y', '2011');
- testFormat('y', '11');
-
- // Time
- testFormat('a', 'am');
- testFormat('A', 'AM');
- testFormat('g', '9');
- testFormat('G', '9');
- testFormat('h', '09');
- testFormat('H', '09');
- testFormat('i', '05');
- testFormat('s', '02');
- testFormat('d-m-Y', '06-09-2011');
-
- // These tests will fail in any other timezone. It's a bit hard to fake that out without directly implementing the methods inline.
- // Timezone
- testFormat('O', '+0700');
- testFormat('Z', '25200');
-
- // Full Date/Time
- testFormat('r', 'Tue Sep 06 2011 09:05:02 GMT-0700 (PDT)');
- testFormat('U', '1315325102');
-
- test.done();
- },
-
- default: function (test) {
- testFilter(test, 'default("blah")', { v: 'foo' }, 'foo', 'string not overridden by default');
- testFilter(test, 'default("blah")', { v: 0 }, '0', 'zero not overridden by default');
- testFilter(test, 'default("blah")', { v: '' }, 'blah', 'empty string overridden by default');
- testFilter(test, 'default("blah")', {}, 'blah', 'default overrides undefined');
- testFilter(test, 'default("blah")', { v: null }, 'blah', 'default overrides null');
- testFilter(test, 'default("blah")', { v: false }, 'blah', 'default overrides false');
- test.done();
- },
-
- first: function (test) {
- testFilter(test, 'first', { v: [1, 2, 3, 4] }, '1', 'first from array');
- testFilter(test, 'first', { v: '213' }, '2', 'first in string');
- testFilter(test, 'first', { v: { foo: 'blah', bar: 'nope' }}, '', 'empty string from object');
- test.done();
- },
-
- join: function (test) {
- testFilter(test, 'join("+")', { v: [1, 2, 3] }, '1+2+3', 'join with "+"');
- testFilter(test, 'join(" * ")', { v: [1, 2, 3] }, '1 * 2 * 3', 'join with " * "');
- testFilter(test, 'join(", ")', { v: { foo: 1, bar: 2, baz: 3 }}, '1, 2, 3', 'object values are joined');
- testFilter(test, 'join("-")', { v: 'asdf' }, 'asdf', 'Non-array input is not joined.');
- test.done();
- },
-
- json_encode: function (test) {
- testFilter(test, 'json_encode', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{"foo":"bar","baz":[1,2,3]}');
- test.done();
- },
-
- length: function (test) {
- testFilter(test, 'length', { v: [1, 2, 3] }, '3', 'array');
- testFilter(test, 'length', { v: 'foobar' }, '6', 'string');
- testFilter(test, 'length', { v: { 'h': 1, 'b': 2 }}, '2', 'object');
- test.done();
- },
-
- last: function (test) {
- var input = [1, 2, 3, 4];
- testFilter(test, 'last', { v: [1, 2, 3, 4] }, '4', 'array');
- testFilter(test, 'last', { v: '123' }, '3', 'string');
- testFilter(test, 'last', { v: { foo: 'blah', bar: 'nope' }}, '', 'object');
- test.done();
- },
-
- lower: function (test) {
- testFilter(test, 'lower', { v: 'BaR' }, 'bar', 'string');
- testFilter(test, 'lower', { v: '345' }, '345', 'number');
- testFilter(test, 'lower', { v: ['FOO', 'bAr'] }, 'foo,bar', 'array');
- testFilter(test, 'lower|join("")', { v: { foo: 'BAR' } }, 'bar', 'object');
- test.done();
- },
-
- replace: function (test) {
- testFilter(test, 'replace("o", "", "g")', { v: 'fooboo' }, 'fb');
- testFilter(test, 'replace("o", "a")', { v: 'foo' }, 'fao');
- testFilter(test, 'replace("\\W+", "-")', { v: '$*&1aZ' }, '-1aZ');
- test.done();
- },
-
- reverse: function (test) {
- testFilter(test, 'reverse', { v: [1, 2, 3] }, '3,2,1', 'reverse array');
- testFilter(test, 'reverse', { v: 'asdf' }, 'asdf', 'reverse string does nothing');
- testFilter(test, 'reverse|join("")', { v: { foo: 'bar', baz: 'bop' }}, 'barbop', 'reverse object does nothing');
- test.done();
- },
-
- striptags: function (test) {
- var input = '<h1>foo</h1> <div class="blah">hi</div>';
- testFilter(test, 'striptags', { v: '<h1>foo</h1> <div class="blah">hi</div>' }, 'foo hi', 'string');
- testFilter(test, 'striptags|join(",")', { v: ['<h1>foo</h1>', '<div class="blah">hi</div>'] }, 'foo,hi', 'array');
- testFilter(test, 'striptags|join(",")', { v: { foo: '<h1>foo</h1>', bar: '<div class="blah">hi</div>' }}, 'foo,hi', 'object');
- test.done();
- },
-
- title: function (test) {
- var input = 'this is title case';
- testFilter(test, 'title', { v: 'this is title case' }, 'This Is Title Case', 'string');
- testFilter(test, 'title|join(",")', { v: ['foo bar', 'blaHbiTTy bLAH'] }, 'Foo Bar,Blahbitty Blah');
- testFilter(test, 'title|join(",")', { v: { foo: 'foo bar', bar: 'blaHbiTTy bLAH' } }, 'Foo Bar,Blahbitty Blah');
- test.done();
- },
-
- uniq: function (test) {
- testFilter(test, 'uniq', { v: [2, 1, 2, 3, 4, 4] }, '2,1,3,4');
- test.done();
- },
-
- upper: function (test) {
- testFilter(test, 'upper', { v: 'bar' }, 'BAR', 'string');
- testFilter(test, 'upper', { v: 345 }, '345', 'number');
- testFilter(test, 'upper', { v: ['foo', 'bAr'] }, 'FOO,BAR', 'array');
- testFilter(test, 'upper|join("")', { v: { foo: 'bar' } }, 'BAR', 'object');
- test.done();
- },
-
- url_encode: function (test) {
- testFilter(test, 'url_encode', { v: "param=1&anotherParam=2" }, "param%3D1%26anotherParam%3D2");
- test.done();
- },
-
- url_decode: function (test) {
- var input = "param%3D1%26anotherParam%3D2";
- testFilter(test, 'url_decode', { v: "param%3D1%26anotherParam%3D2" }, "param=1&amp;anotherParam=2");
- test.done();
- }
-};
View
10 dist/test/index.html
@@ -7,8 +7,9 @@
<link rel="stylesheet" href="nodeunit.css" type="text/css" media="all" />
<script src="nodeunit.min.js"></script>
<script src="swig.pack.js"></script>
- <script src="basic.js"></script>
- <script src="filters.js"></script>
+ <script src="filters.test.js"></script>
+ <script src="tags.test.js"></script>
+ <script src="templates.test.js"></script>
</head>
<body>
@@ -16,8 +17,9 @@ <h1 id="nodeunit-header">Swig Browser Test Suite</h1>
<script>
swig.init({});
nodeunit.run({
- Basic: basic,
- Filters: filters
+ Filters: filters,
+ Tags: tags,
+ Templates: templates
});
</script>
View
26 scripts/browser.sh
@@ -11,7 +11,7 @@ function package() {
MIN_FILE="dist/browser/swig.min.js"
fi
- cat dist/header.js >> $BROWSER_FILE
+ cat dist/header.js > $BROWSER_FILE
echo "swig = (function () {" >> $BROWSER_FILE
echo "var swig = {}," >> $BROWSER_FILE
echo "dateformat = {}," >> $BROWSER_FILE
@@ -63,4 +63,26 @@ mkdir -p dist/browser
package
package "pack"
-cp dist/browser/swig.pack.js dist/test/swig.pack.js
+cp dist/browser/swig.pack.js dist/test/swig.pack.js
+
+function packageTest() {
+ TEST_FILE="$1.test.js"
+ TEMP_FILE="dist/.$TEST_FILE"
+
+ echo "$1 = (function (exports) {" > dist/test/$TEST_FILE
+ cat tests/$TEST_FILE >> dist/test/$TEST_FILE
+ echo "return exports;" >> dist/test/$TEST_FILE
+ echo "})({});" >> dist/test/$TEST_FILE
+
+ cp dist/test/$TEST_FILE $TEMP_FILE
+ sed "/require/d" <$TEMP_FILE > dist/test/$TEST_FILE
+ cp dist/test/$TEST_FILE $TEMP_FILE
+ sed "s/testCase/nodeunit.testCase/" <$TEMP_FILE > dist/test/$TEST_FILE
+ cp dist/test/$TEST_FILE $TEMP_FILE
+ sed "s/__dirname/\'\'/" <$TEMP_FILE > dist/test/$TEST_FILE
+ rm $TEMP_FILE
+}
+
+packageTest "filters"
+packageTest "tags"
+packageTest "templates"
View
2  scripts/config-lint.js
@@ -1,6 +1,6 @@
module.exports = {
root: __dirname + '/../',
- pathIgnore: ['*node_modules*', '*browser/underscore*', '*dist/browser/*', '*dist/test/nodeunit*']
+ pathIgnore: ['*node_modules*', '*browser/underscore*', '*dist/browser/*', '*dist/test*']
};
var options = {
View
2  scripts/config-test.js
@@ -1,5 +1,5 @@
module.exports = {
root: __dirname + '/../',
testRunner: 'default',
- pathIgnore: ['*node_modules*']
+ pathIgnore: ['*node_modules*', '*dist/test*']
};
View
49 tests/tags.test.js
@@ -22,18 +22,55 @@ exports['custom tags'] = function (test) {
exports.extends = testCase({
setUp: function (callback) {
swig.init({ root: __dirname + '/templates' });
+ this.extends_base = [
+ 'This is from the "extends_base.html" template.',
+ '',
+ '{% block one %}',
+ ' This is the default content in block \'one\'',
+ '{% endblock %}',
+ '',
+ '{% block two %}',
+ ' This is the default content in block \'two\'',
+ '{% endblock %}'
+ ].join('\n');
+ this.extends1 = [
+ '{% extends "extends_base.html" %}',
+ 'This is content from "extends_1.html", you should not see it',
+ '',
+ '{% block one %}',
+ ' This is the "extends_1.html" content in block \'one\'',
+ '{% endblock %}'
+ ].join('\n');
+ this.circular1 = "{% extends 'extends_circular2.html' %}{% block content %}Foobar{% endblock %}";
+ this.circular2 = "{% extends 'extends_circular1.html' %}{% block content %}Barfoo{% endblock %}";
callback();
},
basic: function (test) {
- var tmpl8 = swig.fromFile('extends_1.html');
+ var tmpl8;
+ if (typeof window !== 'undefined') {
+ swig.fromString(this.extends_base, 'extends_base.html');
+ tmpl8 = swig.fromString(this.extends1, 'extends1.html');
+ } else {
+ tmpl8 = swig.fromFile('extends_1.html');
+ }
test.strictEqual('This is from the "extends_base.html" template.\n\n\n This is the "extends_1.html" content in block \'one\'\n\n\n This is the default content in block \'two\'\n', tmpl8.render());
test.done();
},
circular: function (test) {
- var tmpl8 = swig.fromFile('extends_circular1.html');
- test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tmpl8.render()), 'throws an error');
+ var tmpl8;
+ if (typeof window !== 'undefined') {
+ swig.init({ allowErrors: true });
+ test.throws(function () {
+ swig.fromString(this.circular1, 'extends_circular1.html');
+ tmpl8 = swig.fromString(this.circular2, 'extends_circular2.html');
+ tmpl8.render();
+ });
+ } else {
+ tmpl8 = swig.fromFile('extends_circular1.html');
+ test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tmpl8.render()), 'throws an error');
+ }
test.done();
}
});
@@ -45,12 +82,18 @@ exports.include = testCase({
},
basic: function (test) {
+ if (typeof window !== 'undefined') {
+ swig.fromString('{{array.length}}', 'included_2.html');
+ }
var tmpl8 = swig.fromString('{% include "included_2.html" %}');
test.strictEqual(tmpl8.render({ array: ['foo'] }), '1');
test.done();
},
variable: function (test) {
+ if (typeof window !== 'undefined') {
+ swig.fromString('{{array.length}}', 'included_2.html');
+ }
var tmpl8 = swig.fromString('{% include inc %}');
test.strictEqual(tmpl8.render({ inc: 'included_2.html', array: ['foo'] }), '1');
test.done();
Please sign in to comment.
Something went wrong with that request. Please try again.