forked from cujojs/curl
/
runner.html
170 lines (141 loc) · 4 KB
/
runner.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE HTML>
<html>
<head>
<title>tdd runner test</title>
<script>
curl = {
baseUrl: '../',
paths: {
curl: '../src/curl'
}
};
</script>
<script src="../../src/curl.js" type="text/javascript"></script>
<script type="text/javascript">
curl(
['curl/tdd/runner', 'require'],
function (runner, require) {
var runCtx, one, callbackRecorder, result;
one = 6;
callbackRecorder = [];
try {
// test if functions are run in the right order
function setup1 () {
callbackRecorder.push('setup');
}
function test1 () {
callbackRecorder.push('test');
}
function teardown1 () {
callbackRecorder.push('teardown');
}
runCtx = runner(require, setup1, teardown1);
runCtx.run(test1).then(function () {
result = callbackRecorder[0] == 'setup'
&& callbackRecorder[1] == 'test'
&& callbackRecorder[2] == 'teardown';
if (result) {
write('SUCCESS: runner should execute setup then test then teardown.');
}
else {
write('FAILED: runner should execute setup then test then teardown.');
write('- expected: setup, test, teardown. got: ' + callbackRecorder.toString());
}
});
// test that a mock define in setup provides a mock value
function setup2 (require) {
// mock of stuff/one
define('stuff/one', function () { return one; });
}
function test2 (require) {
require(['stuff/three'], function (three) {
// stuff/three requires stuff/one and stuff/two
// stuff/two also requires stuff/one
// let's see if they got our mock stuff/one
if (three == 13) {
write('SUCCESS: mock should override actual module');
}
else {
write('FAILED: mock should override actual module');
write('- expected: 13. got: ' + three);
}
});
}
runCtx = runner(require, setup2);
runCtx.run(test2).then(function () {
// test that mock and other modules are no longer defined
// sync require will fetch from cache
try {
require('stuff/one');
write('FAILED: runner should remove mocks declared using `define`');
write('- expected: exception. got: nuthin');
}
catch (ex) {
write('SUCCESS: runner should remove mocks declared using `define`');
}
try {
require('stuff/two');
write('FAILED: runner should remove modules fetched indirectly via `require`');
write('- expected: exception. got: nuthin');
}
catch (ex) {
write('SUCCESS: runner should remove modules fetched indirectly via `require`');
}
try {
require('stuff/three');
write('FAILED: runner should remove modules fetched via `require`');
write('- expected: exception. got: nuthin');
}
catch (ex) {
write('SUCCESS: runner should remove modules fetched via `require`');
}
});
// test that the teardown function is run afterwards
function setup3 () {
result = 42;
}
function teardown3 () {
result = void 0;
}
runCtx = runner(require, setup3, teardown3);
runCtx.run(function () {}).then(function () {
if (result) {
write('FAILED: should run teardown after test.');
write('- expected: undefined. got: ' + result);
}
else {
write('SUCCESS: should run teardown after test.');
}
});
// test that callback (2nd arg to run method) is executed
runCtx = runner();
runCtx.run(function () {}, function () {
result = 27;
}).then(function () {
if (result == 27) {
write('SUCCESS: should exec callback argument of run()');
}
else {
write('FAILED: should exec callback argument of run()');
write('- expected: 27. got: ', result);
}
});
}
catch (ex) {
write('FAILED: ' + ex.message);
}
},
function (ex) {
write('ERROR: ' + ex.message);
}
);
function write (msg) {
curl('domReady!', function () {
document.body.appendChild(document.createElement('div')).innerHTML = msg;
});
}
</script>
</head>
<body>
</body>
</html>