Skip to content

Commit b0d376f

Browse files
author
Nick Fisher
committed
First working demo of Tyrtle.
1 parent b18c1ca commit b0d376f

File tree

5 files changed

+421
-20
lines changed

5 files changed

+421
-20
lines changed

Tyrtle.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Myrtle - A JavaScript Unit Testing Framework
2+
* Tyrtle - A JavaScript Unit Testing Framework
33
*
44
* Copyright (c) 2011 Nick Fisher
55
* Licensed under the Creative Commons BY-SA License
@@ -88,27 +88,32 @@
8888
Tyrtle.PASS = PASS;
8989
Tyrtle.FAIL = FAIL;
9090
Tyrtle.SKIP = SKIP;
91+
Tyrtle.setRenderer = function (renderer) {
92+
this.renderer = renderer;
93+
};
9194
extend(Tyrtle, {
9295
passes : 0,
9396
fails : 0,
9497
errors : 0,
9598
skips : 0,
9699
////
97100
module : function (name, body) {
98-
this.modules.push(new Module(name, body));
101+
var m = new Module(name, body);
102+
m.tyrtle = this;
103+
this.modules.push(m);
99104
},
100105
run : function (options) {
101106
var runNext,
102107
i = -1,
103108
l = this.modules.length,
104109
tyrtle = this
105110
;
106-
111+
Tyrtle.renderer.beforeRun(this);
107112
runNext = function () {
108113
var mod;
109114
++i;
110115
if (i === l) {
111-
// all done
116+
Tyrtle.renderer.afterRun(tyrtle);
112117
tyrtle.callback();
113118
} else {
114119
mod = tyrtle.modules[i];
@@ -123,7 +128,12 @@
123128
runNext();
124129
},
125130
runModule : function (mod, callback) {
126-
mod.run(callback);
131+
var self = this;
132+
Tyrtle.renderer.beforeModule(mod, this);
133+
mod.run(function () {
134+
Tyrtle.renderer.afterModule(mod, self);
135+
callback();
136+
});
127137
}
128138
});
129139
}());
@@ -133,22 +143,21 @@
133143
(function () {
134144
Module = function (name, body) {
135145
this.name = name;
136-
this.body = body;
146+
this.tests = [];
147+
body.call(this); // TODO: could provide a reduced api here
137148
};
138149
extend(Module, {
139-
tests : null,
140-
passes : 0,
141-
fails : 0,
142-
skips : 0,
143-
errors : 0,
150+
tests : null, // array of tests
151+
tyrtle : null, // reference to the owner Tyrtle instance
152+
passes : 0, // }
153+
fails : 0, // } counts of the test results
154+
skips : 0, // }
155+
errors : 0, // }
156+
//////////////////
144157
test : function (name, fn) {
145158
this.tests.push(new Test(name, fn));
146159
},
147160
run : function (callback) {
148-
if (this.tests === null) {
149-
this.tests = [];
150-
this.body();
151-
}
152161
var runNext,
153162
i = -1,
154163
l = this.tests.length,
@@ -184,8 +193,12 @@
184193
runNext();
185194
},
186195
runTest : function (test, callback) {
187-
// todo: befores and afters here
188-
test.run(callback);
196+
var m = this, t = this.tyrtle;
197+
Tyrtle.renderer.beforeTest(test, m, t);
198+
test.run(function () {
199+
Tyrtle.renderer.afterTest(test, m, t);
200+
callback();
201+
});
189202
}
190203
});
191204
}());
@@ -227,14 +240,19 @@
227240
});
228241
}());
229242

230-
AssertionError = function (msg) {
243+
AssertionError = function (msg, args, userMessage) {
231244
this.name = "AssertionError";
232-
this.message = msg;
245+
this.message = msg + (userMessage ? ": " + userMessage : "");
246+
this.args = args;
233247
};
234248

235249
SkipMe = function (reason) {
236250
this.message = reason;
237251
};
252+
253+
//////////////////
254+
// Assertions //
255+
//////////////////
238256
(function () {
239257
var AssertThat, fail, assertions;
240258

@@ -244,7 +262,7 @@
244262
assert.that = assert;
245263

246264
fail = function (message, args, userMessage) {
247-
throw new AssertionError(message);
265+
throw new AssertionError(message, args, userMessage);
248266
};
249267

250268
AssertThat = function (actual) {
@@ -289,6 +307,7 @@
289307
});
290308
extend(AssertThat, assertions);
291309
}());
310+
292311
//#JSCOVERAGE_IF
293312
if (typeof module !== 'undefined') {
294313
module.exports = Tyrtle;

demo/demo-tests.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*globals Tyrtle */
2+
(function () {
3+
var tests = new Tyrtle();
4+
5+
tests.module("Tyrtle tests", function () {
6+
7+
this.test("Checking for equality", function (assert) {
8+
var x = 3;
9+
assert.that(x).is(3).since("x should be three");
10+
assert.that(x).is.not("3").since("x should not be a string");
11+
});
12+
});
13+
tests.module("Failing assertions (these should all fail)", function () {
14+
this.test("Equality checking is strict", function (assert) {
15+
var x = 3;
16+
assert.that(x).is("3").since("Comparing to a string should fail");
17+
});
18+
});
19+
tests.run();
20+
}());

demo/demo.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" href="../tyrtle.css" type="text/css" />
4+
</head>
5+
<body>
6+
<script type="text/javascript" src="../jquery.js"></script>
7+
<script type="text/javascript" src="../Tyrtle.js"></script>
8+
<script type="text/javascript" src="../renderers/html.js"></script>
9+
<script type="text/javascript" src="demo-tests.js"></script>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)