forked from hMatoba/piexifjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phestum.js
259 lines (234 loc) · 7.77 KB
/
phestum.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var system = require("system");
var fs = require("fs");
var phestumLib = (function () {/*
var Tests = {};
Tests.conc = {"pass":0, "fail":0};
var phestum = {
_isEqual : function (a, b) {
if (typeof(a) !== typeof(b)) {
return false;
} else if (a === undefined && b === undefined){
return true;
} else if (a instanceof Array && b instanceof Array) {
if (a.length !== b.length) {
return false;
}
for (var p=0; p<a.length; p++) {
if (!phestum._isEqual(a[p], b[p])){
return false;
}
}
return true;
} else if (typeof(a) === "object" && typeof(b) === "object") {
if (a === null && b === null) {
return true;
}
var propA = Object.getOwnPropertyNames(a);
var propB = Object.getOwnPropertyNames(b);
propA.sort();
propB.sort();
// check property names equality
if (propA.length !== propB.length) {
return false;
} else {
for (var p=0; p<propA.length; p++) {
if (!phestum._isEqual(propA[p], propB[p])){
return false;
}
}
}
// check object value equality
for (var p=0; p<propA.length; p++) {
var prop = propA[p];
if (!phestum._isEqual(a[prop], b[prop])){
return false;
}
}
return true;
} else if (typeof(a) === "string" || typeof(a) === "number") {
if (a === b) {
return true;
} else {
return false;
}
} else {
throw("Got variable that cannot compare.");
}
},
assertEqual : function (a, b) {
if (!phestum._isEqual(a, b)) {
throw("! " + JSON.stringify(a) + " != " + JSON.stringify(b));
}
},
assertNotEqual : function (a, b) {
if (phestum._isEqual(a, b)) {
throw("! " + JSON.stringify(a) + " == " + JSON.stringify(b));
}
},
assertFail : function (func) {
var failed = false;
try {
func();
} catch (e) {
failed = true;
}
if (!failed) {
throw("'phestum.assertFail' error. Given function didn't failed.");
}
},
};
*/}).toString().match(/\/\*([^]*)\*\//)[1];
eval(phestumLib);
// tests/files
if (fs.isDirectory("tests/files")) {
Tests._files = {};
var optionFiles = fs.list("tests/files/")
.filter(function (item) {
return [".", ".."].indexOf(item) == -1;
});
console.log("files: " + JSON.stringify(optionFiles));
for (var p=0; p<optionFiles.length; p++) {
Tests._files[optionFiles[p]] = fs.read("tests/files/" + optionFiles[p], {charset: "LATIN-1"});
}
}
// prepare libraries
Tests._libs = [];
if (system.args.length > 1) {
Tests._libs = system.args.slice(1);
console.log("lib: " + JSON.stringify(Tests._libs));
}
// prepare tests
Tests._tests = fs.list("tests/")
.filter(function(item) {
return item.indexOf(".js") !== -1;
})
.map(function (item) {
return 'tests/' + item;
});
// prepare test environment
var jsScripts = Tests._libs.concat(Tests._tests);
var page = require('webpage').create();
page.onError = function (msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('**TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error("**" + msgStack.join('\n'));
};
page.onConsoleMessage = function(msg, lineNum, sourceId) {
if (msg == "finished phestum tests.") {
var conc = page.evaluate(function () {return Tests.conc;});
console.log("\n\nPassed: " + conc.pass);
console.log("Failed: " + conc.fail);
if (conc.fail) {
//phantom.exit(1);
setTimeout(function(){
phantom.exit(1);
}, 0);
} else {
//phantom.exit();
setTimeout(function(){
phantom.exit();
}, 0);
}
} else {
console.log('' + msg);
}
};
page.evaluate(function (script) {
var scrEl = document.createElement("script");
scrEl.text = script;
document.body.appendChild(scrEl);
}, phestumLib);
for (var p=0; p<jsScripts.length; p++) {
page.injectJs(jsScripts[p]);
}
page.evaluate(function (files) {Tests.files = files;}, Tests._files);
// run tests
page.evaluate(function () {
Tests._testNames = Object.keys(Tests).filter(function (item) {
return (item[0] !== "_" &&
typeof(Tests[item]) === "function" &&
item.indexOf("Test") > -1);
});
console.log("test: " + JSON.stringify(Tests._testNames));
var tests = {};
for (var p=0; p<Tests._testNames.length; p++) {
(function () {
var test;
var testName = Tests._testNames[p];
if (testName.indexOf("Async") == -1) {
test = function () {
var gotError = false;
try {
Tests[testName]();
} catch (e) {
console.log(e);
if ("stack" in e) {
console.log(e["stack"].split("at phantomjs://webpage.evaluate()")[0]);
}
gotError = true;
}
return gotError;
};
} else {
test = function () {
try {
Tests[testName](function () {
Tests["_" + testName] = 1;
}, function (sec) {
Tests["__" + testName] = sec * 1000;
});
} catch (e) {
console.log(e);
if ("stack" in e) {
console.log(e["stack"].split("at phantomjs://webpage.evaluate()")[0]);
}
}
};
}
tests[testName] = test;
})();
}
function recurTests (tests) {
if (Object.keys(tests).length == 0) {
console.log("finished phestum tests.");
return;
}
var testName = Object.keys(tests)[0];
var test = tests[testName];
delete tests[testName];
console.log("\n\n" + testName);
console.log("=============");
var gotError = test();
if (testName.indexOf("Async") == -1) {
console.log("=============");
if (gotError) {
Tests.conc.fail += 1;
console.log("fail");
} else {
Tests.conc.pass += 1;
console.log("pass");
}
recurTests(tests);
} else {
var secToWait = Tests["__" + testName] || 1000;
setTimeout(function () {
console.log("=============");
if (Tests["_" + testName] == 1) {
Tests.conc.pass += 1;
console.log("pass");
} else {
console.log("Couldn't catch test finish.");
Tests.conc.fail += 1;
console.log("fail");
}
recurTests(tests);
}, secToWait);
}
}
recurTests(tests);
});