forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore-test-shim.js
200 lines (169 loc) · 4.63 KB
/
underscore-test-shim.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* A shim for running underscore tests.
*
* See: http://api.qunitjs.com/
*/
var skipErrors = true;
var module = function(name) {
print('MODULE', name);
};
var assert_count;
var test = function(name, arg1, arg2) {
var emsg;
print('***', name);
assert_count = 0;
if (typeof(arg1) === 'function') {
arg1();
} else {
// deprecated
arg2();
if (assert_count !== arg1) {
emsg = 'TEST CASE FAILED: assert count mismatch (' + assert_count + ' vs ' + arg1 + ')';
if (skipErrors) {
print(emsg);
} else {
throw new Error(emsg);
}
}
}
};
var asyncTest = function(name, arg1, arg2) {
print('***', name, 'SKIPPED (asyncTest unimplemented)');
return;
assert_count = 0;
if (typeof(arg1) === 'function') {
throw new Error('unimplemented');
} else {
// deprecated
throw new Error('unimplemented');
if (assert_count !== arg1) {
emsg = 'TEST CASE FAILED: assert count mismatch (' + assert_count + ' vs ' + arg1 + ')';
if (skipErrors) {
print(emsg);
} else {
throw new Error(emsg);
}
}
}
}
var handleResult = function(bool, msg) {
var emsg;
if (bool) {
print('SUCCESS', msg);
} else {
emsg = 'FAILURE ' + msg;
if (skipErrors) {
print(emsg);
} else {
throw new Error(emsg);
}
}
}
var ok = function(bool, msg) {
assert_count++;
handleResult(bool, msg);
};
var equal = function(x, y, msg) {
assert_count++;
handleResult(x == y, msg);
};
var notEqual = function(x, y, msg) {
assert_count++;
handleResult(x != y, msg);
};
var strictEqual = function(x, y, msg) {
assert_count++;
handleResult(x === y, msg);
};
var notStrictEqual = function(x, y, msg) {
assert_count++;
handleResult(x !== y, msg);
};
var deepEqual = function(x, y, msg) {
// FIXME: incorrect as key ordering matters, but close enough maybe?
assert_count++;
handleResult(Duktape.enc('jx', x) === Duktape.enc('jx', y), msg);
};
var notDeepEqual = function(x, y, msg) {
// FIXME: incorrect as key ordering matters, but close enough maybe?
assert_count++;
handleResult(Duktape.enc('jx', x) !== Duktape.enc('jx', y), msg);
};
var raises = function(fn, expect_err, msg) {
var emsg;
var err_name = expect_err.name;
assert_count++;
try {
fn();
emsg = 'FAILURE ' + msg + ' -- did not throw ' + err_name + ' as expected';
if (skipErrors) {
print(emsg);
} else {
throw new Error(emsg);
}
} catch (e) {
if (err_name === e.name) {
print('SUCCESS', msg);
} else {
emsg = 'FAILURE ' + msg + ' -- got unexpected error type: ' + e.name + ' (expected ' + err_name + ')';
if (skipErrors) {
print(emsg);
} else {
throw new Error(emsg);
}
}
}
}
// Minimal jQuery fakery. These are needed to transform the $(document).ready(fn);
// notation into a synchronous call, and to provide enough fake data and functions
// to satisfy the tests (while not breaking the test intent).
var doc_images = [ { id: 'chart_image' } ];
var document = {
images: doc_images
};
var iObject = {};
var $ = function(arg) {
if (arg === document) {
return $;
}
if (arg === '#map-test') {
return {
children: function() {
return [ { id: 'id1' }, { id: 'id2' } ];
}
};
}
// This is a bit tricky, the test is more or less bypassed.
// equal(_.size($('<div>').add('<span>').add('<span>')), 3, 'can compute the size of jQuery objects');
if (arg === '<div>') {
var fun_count = 1;
var fun = function (add_arg) {
fun_count++;
if (fun_count == 3) {
return { length: 3 };
} else {
return fun;
}
};
fun.add = fun;
return fun;
}
throw Error('unexpected call to $: ' + arg);
};
$.ready = function(cb) {
// just call directly
cb();
};
// this disables some unwanted browser tests
$.browser = {
msie: true
};
var jQuery = $;
// document.createElement() is used by some tests
document.createElement = function createElement(name) {
return { name: name }; // Just a dummy return value
};
// the Duktape require() function confuses underscore tests
delete require;
// ... and the 'window' binding is expected
var window = new Function('return this;')();