Skip to content
Browse files

previous dustjs test recovered. make file working.

  • Loading branch information...
1 parent 554f962 commit 1523ad65085d7bcf577c0828d91f85ef5394b022 Jairo de Morais committed Mar 19, 2012
View
8 Makefile
@@ -2,7 +2,13 @@
# Run all tests
#
test:
- node tests/server/specRunner.js
+ node test/server.js
+
+#
+# Run jasmine-test
+#
+jasmine:
+ node test/jasmine-test/server/specRunner.js
#
# Run the benchmarks
View
83 test/core.js
@@ -0,0 +1,83 @@
+(function(exports){
+
+exports.coreSetup = function(suite, auto) {
+ auto.forEach(function(test) {
+ suite.test(test.name, function(){
+ testRender(this, test.source, test.context, test.expected);
+ });
+ });
+
+ suite.test("base context", function() {
+ var base = dust.makeBase({
+ sayHello: function() { return "Hello!" }
+ });
+ testRender(this, "{sayHello} {foo}", base.push({foo: "bar"}), "Hello! bar");
+ });
+
+ suite.test("valid keys", function() {
+ testRender(this, "{_foo}{$bar}{baz1}", {_foo: 1, $bar: 2, baz1: 3}, "123");
+ });
+
+ suite.test("onLoad callback", function() {
+ var unit = this;
+ dust.onLoad = function(name, cb) {
+ cb(null, "Loaded: " + name);
+ };
+ dust.render("onLoad", {}, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "Loaded: onLoad");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("renderSource (callback)", function() {
+ var unit = this;
+ dust.renderSource('Hello World', {}, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "Hello World");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("renderSource (stream)", function() {
+ var unit = this;
+ dust.renderSource('Hello World', {}).on('data', function(data) {
+ try {
+ unit.equals('Hello World', data);
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ }).on('error', function(err) {
+ unit.fail(err);
+ });
+ });
+}
+
+function testRender(unit, source, context, expected) {
+ var name = unit.id;
+ dust.loadSource(dust.compile(source, name));
+ dust.render(name, context, function(err, output) {
+ try {
+ unit.ifError(err);
+ unit.equals(output, expected);
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+}
+
+})(typeof exports !== "undefined" ? exports : window);
View
318 test/examples.js
@@ -0,0 +1,318 @@
+(function(exports){
+
+exports.dustExamples = [
+ {
+ name: "intro",
+ source: "{#stream}{#delay}{.}{/delay}{/stream}",
+ context: (function(){
+ var d = 1;
+ return {
+ stream: function() {
+ return "asynchronous templates for the browser and node.js".split('');
+ },
+ delay: function(chunk, context, bodies) {
+ return chunk.map(function(chunk) {
+ setTimeout(function() {
+ chunk.render(bodies.block, context).end();
+ }, d++ * 15);
+ });
+ }
+ }
+ })
+ },
+ {
+ name: "plain",
+ source: "Hello World!",
+ context: {},
+ expected: "Hello World!"
+ },
+ {
+ name: "replace",
+ source: "Hello {name}! You have {count} new messages.",
+ context: { name: "Mick", count: 30 },
+ expected: "Hello Mick! You have 30 new messages."
+ },
+ {
+ name: "zero",
+ source: "{#zero}{.}{/zero}",
+ context: { zero: 0 },
+ expected: "0"
+ },
+ {
+ name: "array",
+ source: "{#names}{title} {name}{~n}{/names}",
+ context: { title: "Sir", names: [ { name: "Moe" }, { name: "Larry" }, { name: "Curly" } ] },
+ expected: "Sir Moe\nSir Larry\nSir Curly\n"
+ },
+ {
+ name: "empty_array",
+ source: "{#names}{title} {name}{~n}{/names}",
+ context: { title: "Sir", names: [] },
+ expected: ""
+ },
+ {
+ name: "implicit",
+ source: "{#names}{.}{~n}{/names}",
+ context: { names: ["Moe", "Larry", "Curly"] },
+ expected: "Moe\nLarry\nCurly\n"
+ },
+ {
+ name: "object",
+ source: "{#person}{root}: {name}, {age}{/person}",
+ context: { root: "Subject", person: { name: "Larry", age: 45 } },
+ expected: "Subject: Larry, 45"
+ },
+ {
+ name: "rename_key",
+ source: "{#person foo=root}{foo}: {name}, {age}{/person}",
+ context: { root: "Subject", person: { name: "Larry", age: 45 } },
+ expected: "Subject: Larry, 45"
+ },
+ {
+ name: "force_current",
+ source: "{#person}{.root}: {name}, {age}{/person}",
+ context: { root: "Subject", person: { name: "Larry", age: 45 } },
+ expected: ": Larry, 45"
+ },
+ {
+ name: "path",
+ source: "{foo.bar}",
+ context: { foo: {bar: "Hello!"} },
+ expected: "Hello!"
+ },
+ {
+ name: "escaped",
+ source: "{safe|s}{~n}{unsafe}",
+ context: { safe: "<script>alert('Hello!')</script>", unsafe: "<script>alert('Goodbye!')</script>" },
+ expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
+ },
+ {
+ name: "escape_pragma",
+ source: "{%esc:s}\n {unsafe}{~n}\n {%esc:h}\n {unsafe}\n {/esc}\n{/esc}",
+ context: { unsafe: "<script>alert('Goodbye!')</script>" },
+ expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
+ },
+ {
+ name: "else_block",
+ source: "{#foo}\n" +
+ " foo,{~s}\n" +
+ "{:else}\n" +
+ " not foo,{~s}\n" +
+ "{/foo}\n" +
+ "{#bar}\n" +
+ " bar!\n" +
+ "{:else}\n" +
+ " not bar!\n" +
+ "{/bar}",
+ context: { foo: true, bar: false },
+ expected: "foo, not bar!"
+ },
+ {
+ name: "conditional",
+ source: "{?tags}\n" +
+ " <ul>{~n}\n" +
+ " {#tags}\n" +
+ " {~s} <li>{.}</li>{~n}\n" +
+ " {/tags}\n" +
+ " </ul>\n" +
+ "{:else}\n" +
+ " No Tags!\n" +
+ "{/tags}\n" +
+ "{~n}\n" +
+ "{^likes}\n" +
+ " No Likes!\n" +
+ "{:else}\n" +
+ " <ul>{~n}\n" +
+ " {#likes}\n" +
+ " {~s} <li>{.}</li>{~n}\n" +
+ " {/likes}\n" +
+ " </ul>\n" +
+ "{/likes}",
+ context: { tags: [], likes: ["moe", "larry", "curly", "shemp"] },
+ expected: "No Tags!\n" +
+ "<ul>\n" +
+ " <li>moe</li>\n" +
+ " <li>larry</li>\n" +
+ " <li>curly</li>\n" +
+ " <li>shemp</li>\n" +
+ "</ul>"
+ },
+ {
+ name: "sync_key",
+ source: "Hello {type} World!",
+ context: {
+ type: function(chunk) {
+ return "Sync";
+ }
+ },
+ expected: "Hello Sync World!"
+ },
+ {
+ name: "async_key",
+ source: "Hello {type} World!",
+ context: {
+ type: function(chunk) {
+ return chunk.map(function(chunk) {
+ dust.nextTick(function() {
+ chunk.end("Async");
+ });
+ });
+ }
+ },
+ expected: "Hello Async World!"
+ },
+ {
+ name: "sync_chunk",
+ source: "Hello {type} World!",
+ context: {
+ type: function(chunk) {
+ return chunk.write("Chunky");
+ }
+ },
+ expected: "Hello Chunky World!"
+ },
+ {
+ name: "async_iterator",
+ source: "{#numbers}{#delay}{.}{/delay}{@sep}, {/sep}{/numbers}",
+ context: {
+ numbers: [3, 2, 1],
+ delay: function(chunk, context, bodies) {
+ return chunk.map(function(chunk) {
+ setTimeout(function() {
+ chunk.render(bodies.block, context).end();
+ }, Math.ceil(Math.random()*10));
+ });
+ }
+ },
+ expected: "3, 2, 1"
+ },
+ {
+ name: "filter",
+ source: "{#filter}foo {bar}{/filter}",
+ context: {
+ filter: function(chunk, context, bodies) {
+ return chunk.tap(function(data) {
+ return data.toUpperCase();
+ }).render(bodies.block, context).untap();
+ },
+
+ bar: "bar"
+ },
+ expected: "FOO BAR"
+ },
+ {
+ name: "context",
+ source: "{#list:projects}{name}{:else}No Projects!{/list}",
+ context: {
+ list: function(chunk, context, bodies) {
+ var items = context.current(),
+ len = items.length;
+
+ if (len) {
+ chunk.write("<ul>\n");
+ for (var i=0; i<len; i++) {
+ chunk = chunk.write("<li>")
+ .render(bodies.block, context.push(items[i]))
+ .write("</li>\n");
+ }
+ return chunk.write("</ul>");
+ } else if (bodies['else']) {
+ return chunk.render(bodies['else'], context);
+ }
+ return chunk;
+ },
+
+ projects: [
+ {name: "Mayhem"},
+ {name: "Flash"},
+ {name: "Thunder"}
+ ]
+ },
+ expected: "<ul>\n" +
+ "<li>Mayhem</li>\n" +
+ "<li>Flash</li>\n" +
+ "<li>Thunder</li>\n" +
+ "</ul>"
+ },
+ {
+ name: "params",
+ source: "{#helper foo=\"bar\"/}",
+ context: {
+ helper: function(chunk, context, bodies, params) {
+ return chunk.write(params.foo);
+ }
+ },
+ expected: "bar"
+ },
+ {
+ name: "partials",
+ source: '{>replace/} {>"plain"/} {>"{ref}"/}',
+ context: { name: "Jim", count: 42, ref: "plain" },
+ expected: "Hello Jim! You have 42 new messages. Hello World! Hello World!"
+ },
+ {
+ name: "partial_context",
+ source: "{>replace:.profile/}",
+ context: { profile: { name: "Mick", count: 30 } },
+ expected: "Hello Mick! You have 30 new messages."
+ },
+ {
+ name: "base_template",
+ source: "Start{~n}\n" +
+ "{+title}\n" +
+ " Base Title\n" +
+ "{/title}\n" +
+ "{~n}\n" +
+ "{+main}\n" +
+ " Base Content\n" +
+ "{/main}\n" +
+ "{~n}\n" +
+ "End",
+ context: {},
+ expected: "Start\nBase Title\nBase Content\nEnd"
+ },
+ {
+ name: "child_template",
+ source: "{^xhr}\n" +
+ " {>base_template/}\n" +
+ "{:else}\n" +
+ " {+main/}\n" +
+ "{/xhr}\n" +
+ "{<title}\n" +
+ " Child Title\n" +
+ "{/title}\n" +
+ "{<main}\n" +
+ " Child Content\n" +
+ "{/main}\n",
+ context: {xhr: false},
+ expected: "Start\nChild Title\nChild Content\nEnd"
+ },
+ {
+ name: "recursion",
+ source: "{name}{~n}{#kids}{>recursion:./}{/kids}",
+ context: {
+ name: '1',
+ kids: [
+ {
+ name: '1.1',
+ kids: [
+ {name: '1.1.1'}
+ ]
+ }
+ ]
+ },
+ expected: "1\n1.1\n1.1.1\n"
+ },
+ {
+ name: "comments",
+ source: "{!\n" +
+ " Multiline\n" +
+ " {#foo}{bar}{/foo}\n" +
+ "!}\n" +
+ "{!before!}Hello{!after!}",
+ context: {},
+ expected: "Hello"
+ }
+];
+
+})(typeof exports !== "undefined" ? exports : window);
View
0 tests/client/.DS_Store → test/jasmine-test/client/.DS_Store
File renamed without changes.
View
BIN test/jasmine-test/client/lib/.DS_Store
Binary file not shown.
View
0 tests/client/lib/jasmine-1.1.0/MIT.LICENSE → ...test/client/lib/jasmine-1.1.0/MIT.LICENSE
File renamed without changes.
View
0 .../client/lib/jasmine-1.1.0/jasmine-html.js → .../client/lib/jasmine-1.1.0/jasmine-html.js
File renamed without changes.
View
0 tests/client/lib/jasmine-1.1.0/jasmine.css → ...test/client/lib/jasmine-1.1.0/jasmine.css
File renamed without changes.
View
0 tests/client/lib/jasmine-1.1.0/jasmine.js → ...-test/client/lib/jasmine-1.1.0/jasmine.js
File renamed without changes.
View
0 ...ent/lib/jasmine-1.1.0/jasmine_favicon.png → ...ent/lib/jasmine-1.1.0/jasmine_favicon.png
File renamed without changes
View
0 tests/client/specRunner.html → test/jasmine-test/client/specRunner.html
File renamed without changes.
View
2 tests/server/specRunner.js → test/jasmine-test/server/specRunner.js
@@ -3,7 +3,7 @@ var jasmine = require('jasmine-node'),
path = require('path');
/* this should be declared global in order to access them in the spec*/
-dust = require('../../lib/dust'),
+dust = require('../../../lib/dust'),
dustExamples = require('../spec/examples');
for(key in jasmine)
View
0 tests/spec/.DS_Store → test/jasmine-test/spec/.DS_Store
File renamed without changes.
View
0 tests/spec/examples.js → test/jasmine-test/spec/examples.js
File renamed without changes.
View
0 tests/spec/renderTestSpec.js → test/jasmine-test/spec/renderTestSpec.js
File renamed without changes.
View
35 test/server.js
@@ -0,0 +1,35 @@
+var uutest = require('./uutest'),
+ dust = require('../lib/dust'),
+ tests = require('./examples').dustExamples,
+ coreSetup = require('./core').coreSetup;
+
+function dumpError(err) {
+ var out = err.testName + " -> ";
+ if (!err.message) {
+ err.message = JSON.stringify(err.expected)
+ + " " + err.operator + " " + JSON.stringify(err.actual);
+ }
+ return out + err.stack;
+}
+
+var suite = new uutest.Suite({
+ pass: function() {
+ process.stdout.write(".");
+ },
+ fail: function(err) {
+ process.stdout.write("F");
+ },
+ done: function(passed, failed, elapsed) {
+ process.stdout.write("\n");
+ console.log(passed + " passed " + failed + " failed " + "(" + elapsed + "ms)");
+ this.errors.forEach(function(err) {
+ console.log(dumpError(err));
+ });
+ }
+});
+
+global.dust = dust;
+
+coreSetup(suite, tests.slice(1));
+
+suite.run();
View
104 test/uutest.js
@@ -0,0 +1,104 @@
+(function(uutest){
+
+function Test(id, test, timeout, callback) {
+ this.id = id;
+ this.test = test;
+ this.callback = callback;
+ this.timeout = timeout;
+}
+
+Test.prototype.run = function() {
+ var self = this;
+ self.timer = setTimeout(function() {
+ self.fail(new Error("TimeoutError"));
+ }, self.timeout);
+ try {
+ self.test.call(self);
+ } catch(err) {
+ self.fail(err);
+ }
+}
+
+Test.prototype.equals = function(actual, expected, message) {
+ if (actual !== expected) {
+ var err = new Error();
+ if (message) err.message = message;
+ throw wrapAssertionError(err, actual, expected, "===");
+ }
+}
+
+Test.prototype.ifError = function(err) {
+ if (err) throw err;
+}
+
+Test.prototype.pass = function() {
+ clearTimeout(this.timer);
+ this.callback();
+}
+
+Test.prototype.fail = function(err) {
+ clearTimeout(this.timer);
+ this.callback(err);
+}
+
+uutest.Test = Test;
+
+function Suite(options) {
+ this.options = options || {};
+ this.timeout = options.timeout || 1000;
+ this.tests = [];
+}
+
+Suite.prototype.test = function(name, fn) {
+ var self = this;
+ self.tests.push(new Test(name, fn, self.timeout, function(err) {
+ if (err) {
+ err.testName = name;
+ self.errors.push(err);
+ self.emit("fail", err);
+ } else {
+ self.emit("pass", name);
+ }
+ self.pending--;
+ self.check();
+ }));
+}
+
+Suite.prototype.run = function() {
+ if (this.pending) return;
+ var self = this, len = self.tests.length;
+ self.errors = [];
+ self.emit("start", self.tests);
+ self.start = new Date().getTime();
+ self.pending = len;
+ for (var i=0; i<len; i++) {
+ self.tests[i].run();
+ }
+}
+
+Suite.prototype.check = function() {
+ if (this.pending) return;
+ var len = this.tests.length,
+ passed = len - this.errors.length,
+ failed = len - passed;
+ this.emit("done", passed, failed, new Date().getTime() - this.start);
+}
+
+Suite.prototype.emit = function(type) {
+ var event = this.options[type];
+ if (event) {
+ event.apply(this, Array.prototype.slice.call(arguments, 1));
+ }
+}
+
+uutest.Suite = Suite;
+
+function wrapAssertionError(err, actual, expected, operator) {
+ err.name = "AssertionError";
+ err.actual = actual;
+ err.expected = expected;
+ err.operator = operator;
+ return err;
+}
+
+})(typeof exports !== 'undefined' ? exports : window.uutest = {});

0 comments on commit 1523ad6

Please sign in to comment.
Something went wrong with that request. Please try again.