This repository has been archived by the owner on Jan 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
test-titanium.js
117 lines (99 loc) · 2.95 KB
/
test-titanium.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
Titanium.include('jsdeferred.js');
Titanium.UI.setBackgroundColor('#fff');
var container = Ti.UI.createWebView({
backgroundColor: '#fff',
});
Titanium.UI.currentWindow.add(container);
container.html = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + '/jsdeferred/test-titanium.html').read().toString();
container.addEventListener('load', function () {
function showToView (obj) {
container.evalJS('message(' + JSON.stringify(obj) + ')');
}
function msg (m) {
Ti.API.warn(m);
showToView({
type : 'info',
text : m
});
}
log = msg;
print = msg;
msg('Titanium.version: ' + Titanium.version);
msg('Titanium.Platform.model: ' + Titanium.Platform.model);
msg('Titanium.Platform.name: ' + Titanium.Platform.name);
msg('Titanium.Platform.osname: ' + Titanium.Platform.osname);
msg('Titanium.Platform.version: ' + Titanium.Platform.version);
var Global = (function () { return this })();
Deferred.define();
file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + '/jsdeferred/test-jsdeferred.js');
data = file.read().toString();
data = data.match(/\/\/ ::Test::Start::([\s\S]+)::Test::End::/)[1];
var testfuns = []; data.replace(/(ok|expect)\(.+/g, function (m) {
testfuns.push(m);
return m;
});
var expects = testfuns.length;
function uneval (obj) {
return JSON.stringify(obj);
}
function show (msg, expect, result) {
var okng = this;
var out = [];
out.push(color(46, "[", [expects - testfuns.length, expects].join("/"), "]"));
if (okng == "skip") {
out.push(" ", color(33, "skipped " + expect + " tests: " + msg));
Ti.API.info(out.join(""));
while (expect--) testfuns.pop();
showToView({
type : 'skip',
text : out.join('')
});
} else
if (okng == "ng") {
testfuns.pop();
expect = (typeof expect == "function") ? uneval(expect).match(/[^{]+/)+"..." : uneval(expect);
result = (typeof result == "function") ? uneval(result).match(/[^{]+/)+"..." : uneval(result);
out.push(["NG Test::", msg, expect, result].join("\n"));
Ti.API.error(out.join(""));
showToView({
type : 'ng',
text : out.join('')
});
} else {
testfuns.pop();
out.push(" ", color(32, "ok"));
Ti.API.info(out.join(""));
showToView({
type : 'ok',
text : out.join('')
});
}
}
function ok () {
show.apply("ok", arguments);
return true;
}
function ng () {
show.apply("ng", arguments);
return true;
}
function skip () {
show.apply("skip", arguments);
return true;
}
function expect (msg, expect, result) {
if (expect == result) {
show.apply("ok", arguments);
} else {
show.apply("ng", arguments);
}
return true;
}
function color (code) {
var str = "";
for (var i = 1; i < arguments.length; i++) str += arguments[i];
return str;
}
// run tests
eval(data);
});