Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / base / unittest / assertions.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 base.require('base.unittest.test_error');
8
9 base.exportTo('base.unittest', function() {
10   function forAllAssertMethodsIn_(prototype, fn) {
11     for (var fieldName in prototype) {
12       if (fieldName.indexOf('assert') != 0)
13         continue;
14       var fieldValue = prototype[fieldName];
15       if (typeof fieldValue != 'function')
16         continue;
17       fn(fieldName, fieldValue);
18     }
19   }
20
21   var Assertions = {};
22   Assertions.prototype = {
23     assertTrue: function(a, opt_message) {
24       if (a)
25         return;
26       var message = opt_message || 'Expected true, got ' + a;
27       throw new base.unittest.TestError(message);
28     },
29
30     assertFalse: function(a, opt_message) {
31       if (!a)
32         return;
33       var message = opt_message || 'Expected false, got ' + a;
34       throw new base.unittest.TestError(message);
35     },
36
37     assertUndefined: function(a, opt_message) {
38       if (a === undefined)
39         return;
40       var message = opt_message || 'Expected undefined, got ' + a;
41       throw new base.unittest.TestError(message);
42     },
43
44     assertNotUndefined: function(a, opt_message) {
45       if (a !== undefined)
46         return;
47       var message = opt_message || 'Expected not undefined, got ' + a;
48       throw new base.unittest.TestError(message);
49     },
50
51     assertNull: function(a, opt_message) {
52       if (a === null)
53         return;
54       var message = opt_message || 'Expected null, got ' + a;
55       throw new base.unittest.TestError(message);
56     },
57
58     assertNotNull: function(a, opt_message) {
59       if (a !== null)
60         return;
61       var message = opt_message || 'Expected non-null, got ' + a;
62       throw new base.unittest.TestError(message);
63     },
64
65     assertEquals: function(a, b, opt_message) {
66       if (a === b)
67         return;
68       if (opt_message)
69         throw new base.unittest.TestError(opt_message);
70
71       var message = 'Expected\n"';
72       if (typeof(a) === 'string' || a instanceof String)
73         message += a;
74       else {
75         try {
76           message += JSON.stringify(a);
77         } catch (e) {
78           message += a;
79         }
80       }
81
82       message += '"\n\ngot\n\n"';
83       if (typeof(b) === 'string' || b instanceof String)
84         message += b;
85       else {
86         try {
87           message += JSON.stringify(b);
88         } catch (e) {
89           message += b;
90         }
91       }
92
93       message += '"';
94       throw new base.unittest.TestError(message);
95     },
96
97     assertNotEquals: function(a, b, opt_message) {
98       if (a !== b)
99         return;
100       var message = opt_message || 'Expected something not equal to ' + b;
101       throw new base.unittest.TestError(message);
102     },
103
104     assertArrayEquals: function(a, b, opt_message) {
105       if (a.length === b.length) {
106         var ok = true;
107         for (var i = 0; i < a.length; i++) {
108           ok &= (a[i] === b[i]);
109         }
110         if (ok)
111           return;
112       }
113
114       var message = opt_message || 'Expected array ' + a + ', got array ' + b;
115       throw new base.unittest.TestError(message);
116     },
117
118     assertArrayShallowEquals: function(a, b, opt_message) {
119       if (a.length === b.length) {
120         var ok = true;
121         for (var i = 0; i < a.length; i++) {
122           ok &= (a[i] === b[i]);
123         }
124         if (ok)
125           return;
126       }
127
128       var message = opt_message || 'Expected array ' + b + ', got array ' + a;
129       throw new base.unittest.TestError(message);
130     },
131
132     assertArrayBufferEquals: function(a, b, opt_message) {
133       if (a.byteLength === b.byteLength) {
134         var ok = true;
135         a = new Uint8Array(a);
136         b = new Uint8Array(b);
137         for (var i = 0; i < a.length; i++) {
138           ok &= (a[i] === b[i]);
139         }
140         if (ok)
141           return;
142       }
143
144       var message = opt_message || 'Array buffers mismatch';
145       throw new base.unittest.TestError(message);
146     },
147
148     assertAlmostEquals: function(a, b, opt_message) {
149       if (Math.abs(a - b) < 0.00001)
150         return;
151       var message = opt_message || 'Expected almost ' + a + ', got ' + b;
152       throw new base.unittest.TestError(message);
153     },
154
155     assertVec2Equals: function(a, b, opt_message) {
156       if (a[0] === b[0] &&
157           a[1] === b[1])
158         return;
159       var message = opt_message || 'Expected (' + a[0] + ',' + a[1] +
160           ') but got (' + b[0] + ',' + b[1] + ')';
161       throw new base.unittest.TestError(message);
162     },
163
164     assertVec3Equals: function(a, b, opt_message) {
165       if (a[0] === b[0] &&
166           a[1] === b[1] &&
167           a[2] === b[2])
168         return;
169       var message = opt_message || 'Expected ' + vec3.toString(a) +
170           ' but got ' + vec3.toString(b);
171       throw new base.unittest.TestError(message);
172     },
173
174     assertQuadEquals: function(a, b, opt_message) {
175       var ok = true;
176       ok &= a.p1[0] === b.p1[0] && a.p1[1] === b.p1[1];
177       ok &= a.p2[0] === b.p2[0] && a.p2[1] === b.p2[1];
178       ok &= a.p3[0] === b.p3[0] && a.p3[1] === b.p3[1];
179       ok &= a.p4[0] === b.p4[0] && a.p4[1] === b.p4[1];
180       if (ok)
181         return;
182       var message = opt_message || 'Expected "' + a.toString() +
183           '", got "' + b.toString() + '"';
184       throw new base.unittest.TestError(message);
185     },
186
187     assertRectEquals: function(a, b, opt_message) {
188       var ok = true;
189       if (a.x === b.x && a.y === b.y &&
190           a.width === b.width && a.height === b.height) {
191         return;
192       }
193
194       var message = opt_message || 'Expected "' + a.toString() +
195           '", got "' + b.toString() + '"';
196       throw new base.unittest.TestError(message);
197     },
198
199     assertObjectEquals: function(a, b, opt_message) {
200       var a_json = JSON.stringify(a);
201       var b_json = JSON.stringify(b);
202       if (a_json === b_json)
203         return;
204       var message = opt_message || 'Expected ' + a_json + ', got ' + b_json;
205       throw new base.unittest.TestError(message);
206     },
207
208     assertThrows: function(fn, opt_message) {
209       try {
210         fn();
211       } catch (e) {
212         return;
213       }
214       var message = opt_message || 'Expected throw from ' + fn;
215       throw new base.unittest.TestError(message);
216     },
217
218     assertDoesNotThrow: function(fn, opt_message) {
219       try {
220         fn();
221       } catch (e) {
222         var message = opt_message || 'Expected to not throw from ' + fn +
223             ' but got: ' + e;
224         throw new base.unittest.TestError(message);
225       }
226     },
227
228     assertApproxEquals: function(a, b, opt_epsilon, opt_message) {
229       if (a === b)
230         return;
231       var epsilon = opt_epsilon || 0.000001; // 6 digits.
232       a = Math.abs(a);
233       b = Math.abs(b);
234       var relative_error = Math.abs(a - b) / (a + b);
235       if (relative_error < epsilon)
236         return;
237       var message = opt_message || 'Expect ' + a + ' and ' + b +
238           ' to be within ' + epsilon + ' was ' + relative_error;
239       throw new base.unittest.TestError(message);
240     },
241
242     assertVisible: function(elt) {
243       if (!elt.offsetHeight || !elt.offsetWidth)
244         throw new base.unittest.TestError('Expected element to be visible');
245     }
246   };
247
248   function bindGlobals_() {
249     forAllAssertMethodsIn_(Assertions.prototype,
250         function(fieldName, fieldValue) {
251           global[fieldName] = fieldValue.bind(this);
252         });
253   };
254   bindGlobals_();
255
256   return {
257     Assertions: Assertions
258   };
259 });