Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | /*** |
2 | ||
3 | MochiKit.Test 1.4 | |
4 | ||
5 | See <http://mochikit.com/> for documentation, downloads, license, etc. | |
6 | ||
7 | (c) 2005 Bob Ippolito. All rights Reserved. | |
8 | ||
9 | ***/ | |
10 | ||
11 | if (typeof(dojo) != 'undefined') { | |
12 | dojo.provide('MochiKit.Test'); | |
13 | dojo.require('MochiKit.Base'); | |
14 | } | |
15 | ||
16 | if (typeof(JSAN) != 'undefined') { | |
17 | JSAN.use("MochiKit.Base", []); | |
18 | } | |
19 | ||
20 | try { | |
21 | if (typeof(MochiKit.Base) == 'undefined') { | |
22 | throw ""; | |
23 | } | |
24 | } catch (e) { | |
25 | throw "MochiKit.Test depends on MochiKit.Base!"; | |
26 | } | |
27 | ||
28 | if (typeof(MochiKit.Test) == 'undefined') { | |
29 | MochiKit.Test = {}; | |
30 | } | |
31 | ||
32 | MochiKit.Test.NAME = "MochiKit.Test"; | |
33 | MochiKit.Test.VERSION = "1.4"; | |
34 | MochiKit.Test.__repr__ = function () { | |
35 | return "[" + this.NAME + " " + this.VERSION + "]"; | |
36 | }; | |
37 | ||
38 | MochiKit.Test.toString = function () { | |
39 | return this.__repr__(); | |
40 | }; | |
41 | ||
42 | ||
43 | MochiKit.Test.EXPORT = ["runTests"]; | |
44 | MochiKit.Test.EXPORT_OK = []; | |
45 | ||
46 | MochiKit.Test.runTests = function (obj) { | |
47 | if (typeof(obj) == "string") { | |
48 | obj = JSAN.use(obj); | |
49 | } | |
50 | var suite = new MochiKit.Test.Suite(); | |
51 | suite.run(obj); | |
52 | }; | |
53 | ||
54 | MochiKit.Test.Suite = function () { | |
55 | this.testIndex = 0; | |
56 | MochiKit.Base.bindMethods(this); | |
57 | }; | |
58 | ||
59 | MochiKit.Test.Suite.prototype = { | |
60 | run: function (obj) { | |
61 | try { | |
62 | obj(this); | |
63 | } catch (e) { | |
64 | this.traceback(e); | |
65 | } | |
66 | }, | |
67 | traceback: function (e) { | |
68 | var items = MochiKit.Iter.sorted(MochiKit.Base.items(e)); | |
69 | print("not ok " + this.testIndex + " - Error thrown"); | |
70 | for (var i = 0; i < items.length; i++) { | |
71 | var kv = items[i]; | |
72 | if (kv[0] == "stack") { | |
73 | kv[1] = kv[1].split(/\n/)[0]; | |
74 | } | |
75 | this.print("# " + kv.join(": ")); | |
76 | } | |
77 | }, | |
78 | print: function (s) { | |
79 | print(s); | |
80 | }, | |
81 | is: function (got, expected, /* optional */message) { | |
82 | var res = 1; | |
83 | var msg = null; | |
84 | try { | |
85 | res = MochiKit.Base.compare(got, expected); | |
86 | } catch (e) { | |
87 | msg = "Can not compare " + typeof(got) + ":" + typeof(expected); | |
88 | } | |
89 | if (res) { | |
90 | msg = "Expected value did not compare equal"; | |
91 | } | |
92 | if (!res) { | |
93 | return this.testResult(true, message); | |
94 | } | |
95 | return this.testResult(false, message, | |
96 | [[msg], ["got:", got], ["expected:", expected]]); | |
97 | }, | |
98 | ||
99 | testResult: function (pass, msg, failures) { | |
100 | this.testIndex += 1; | |
101 | if (pass) { | |
102 | this.print("ok " + this.testIndex + " - " + msg); | |
103 | return; | |
104 | } | |
105 | this.print("not ok " + this.testIndex + " - " + msg); | |
106 | if (failures) { | |
107 | for (var i = 0; i < failures.length; i++) { | |
108 | this.print("# " + failures[i].join(" ")); | |
109 | } | |
110 | } | |
111 | }, | |
112 | ||
113 | isDeeply: function (got, expected, /* optional */message) { | |
114 | var m = MochiKit.Base; | |
115 | var res = 1; | |
116 | try { | |
117 | res = m.compare(got, expected); | |
118 | } catch (e) { | |
119 | // pass | |
120 | } | |
121 | if (res === 0) { | |
122 | return this.ok(true, message); | |
123 | } | |
124 | var gk = m.keys(got); | |
125 | var ek = m.keys(expected); | |
126 | gk.sort(); | |
127 | ek.sort(); | |
128 | if (m.compare(gk, ek)) { | |
129 | // differing keys | |
130 | var cmp = {}; | |
131 | var i; | |
132 | for (i = 0; i < gk.length; i++) { | |
133 | cmp[gk[i]] = "got"; | |
134 | } | |
135 | for (i = 0; i < ek.length; i++) { | |
136 | if (ek[i] in cmp) { | |
137 | delete cmp[ek[i]]; | |
138 | } else { | |
139 | cmp[ek[i]] = "expected"; | |
140 | } | |
141 | } | |
142 | var diffkeys = m.keys(cmp); | |
143 | diffkeys.sort(); | |
144 | var gotkeys = []; | |
145 | var expkeys = []; | |
146 | while (diffkeys.length) { | |
147 | var k = diffkeys.shift(); | |
148 | if (k in Object.prototype) { | |
149 | continue; | |
150 | } | |
151 | (cmp[k] == "got" ? gotkeys : expkeys).push(k); | |
152 | } | |
153 | ||
154 | ||
155 | } | |
156 | ||
157 | return this.testResult((!res), msg, | |
158 | (msg ? [["got:", got], ["expected:", expected]] : undefined) | |
159 | ); | |
160 | }, | |
161 | ||
162 | ok: function (res, message) { | |
163 | return this.testResult(res, message); | |
164 | } | |
165 | }; | |
166 | ||
167 | MochiKit.Test.__new__ = function () { | |
168 | var m = MochiKit.Base; | |
169 | ||
170 | this.EXPORT_TAGS = { | |
171 | ":common": this.EXPORT, | |
172 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) | |
173 | }; | |
174 | ||
175 | m.nameFunctions(this); | |
176 | ||
177 | }; | |
178 | ||
179 | MochiKit.Test.__new__(); | |
180 | ||
181 | MochiKit.Base._exportSymbols(this, MochiKit.Test); |