summaryrefslogtreecommitdiff
path: root/frontend/delta/js/MochiKit/Test.js
Unidiff
Diffstat (limited to 'frontend/delta/js/MochiKit/Test.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/MochiKit/Test.js167
1 files changed, 167 insertions, 0 deletions
diff --git a/frontend/delta/js/MochiKit/Test.js b/frontend/delta/js/MochiKit/Test.js
new file mode 100644
index 0000000..55d226c
--- a/dev/null
+++ b/frontend/delta/js/MochiKit/Test.js
@@ -0,0 +1,167 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24/***
25
26MochiKit.Test 1.5
27
28See <http://mochikit.com/> for documentation, downloads, license, etc.
29
30(c) 2005 Bob Ippolito. All rights Reserved.
31
32***/
33
34MochiKit.Base.module(MochiKit, 'Test', '1.5', ['Base']);
35
36MochiKit.Test.runTests = function (obj) {
37 if (typeof(obj) == "string") {
38 // TODO: Remove this temporary API change advertisement
39 throw new TypeError("Automatic module import not supported, call runTests() with proper object: " + obj);
40 }
41 var suite = new MochiKit.Test.Suite();
42 suite.run(obj);
43};
44
45MochiKit.Test.Suite = function () {
46 this.testIndex = 0;
47 MochiKit.Base.bindMethods(this);
48};
49
50MochiKit.Test.Suite.prototype = {
51 run: function (obj) {
52 try {
53 obj(this);
54 } catch (e) {
55 this.traceback(e);
56 }
57 },
58 traceback: function (e) {
59 var items = MochiKit.Iter.sorted(MochiKit.Base.items(e));
60 print("not ok " + this.testIndex + " - Error thrown");
61 for (var i = 0; i < items.length; i++) {
62 var kv = items[i];
63 if (kv[0] == "stack") {
64 kv[1] = kv[1].split(/\n/)[0];
65 }
66 this.print("# " + kv.join(": "));
67 }
68 },
69 print: function (s) {
70 print(s);
71 },
72 is: function (got, expected, /* optional */message) {
73 var res = 1;
74 var msg = null;
75 try {
76 res = MochiKit.Base.compare(got, expected);
77 } catch (e) {
78 msg = "Can not compare " + typeof(got) + ":" + typeof(expected);
79 }
80 if (res) {
81 msg = "Expected value did not compare equal";
82 }
83 if (!res) {
84 return this.testResult(true, message);
85 }
86 return this.testResult(false, message,
87 [[msg], ["got:", got], ["expected:", expected]]);
88 },
89
90 testResult: function (pass, msg, failures) {
91 this.testIndex += 1;
92 if (pass) {
93 this.print("ok " + this.testIndex + " - " + msg);
94 return;
95 }
96 this.print("not ok " + this.testIndex + " - " + msg);
97 if (failures) {
98 for (var i = 0; i < failures.length; i++) {
99 this.print("# " + failures[i].join(" "));
100 }
101 }
102 },
103
104 isDeeply: function (got, expected, /* optional */message) {
105 var m = MochiKit.Base;
106 var res = 1;
107 try {
108 res = m.compare(got, expected);
109 } catch (e) {
110 // pass
111 }
112 if (res === 0) {
113 return this.ok(true, message);
114 }
115 var gk = m.keys(got);
116 var ek = m.keys(expected);
117 gk.sort();
118 ek.sort();
119 if (m.compare(gk, ek)) {
120 // differing keys
121 var cmp = {};
122 var i;
123 for (i = 0; i < gk.length; i++) {
124 cmp[gk[i]] = "got";
125 }
126 for (i = 0; i < ek.length; i++) {
127 if (ek[i] in cmp) {
128 delete cmp[ek[i]];
129 } else {
130 cmp[ek[i]] = "expected";
131 }
132 }
133 var diffkeys = m.keys(cmp);
134 diffkeys.sort();
135 var gotkeys = [];
136 var expkeys = [];
137 while (diffkeys.length) {
138 var k = diffkeys.shift();
139 if (k in Object.prototype) {
140 continue;
141 }
142 (cmp[k] == "got" ? gotkeys : expkeys).push(k);
143 }
144
145
146 }
147
148 return this.testResult((!res), msg,
149 (msg ? [["got:", got], ["expected:", expected]] : undefined)
150 );
151 },
152
153 ok: function (res, message) {
154 return this.testResult(res, message);
155 }
156};
157
158MochiKit.Test.__new__ = function () {
159 var m = MochiKit.Base;
160 this.Suite.__export__ = false;
161 m.nameFunctions(this);
162
163};
164
165MochiKit.Test.__new__();
166
167MochiKit.Base._exportSymbols(this, MochiKit.Test);