OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 function debug(message) { | |
6 var span = document.createElement("span"); | |
7 span.appendChild(document.createTextNode(message)); | |
8 span.appendChild(document.createElement("br")); | |
9 document.getElementById('status').appendChild(span); | |
10 } | |
11 | |
12 function done(message) { | |
13 if (document.location.hash == '#fail') | |
14 return; | |
15 if (message) | |
16 debug('PASS: ' + message); | |
17 else | |
18 debug('PASS'); | |
19 document.location.hash = '#pass'; | |
20 } | |
21 | |
22 function fail(message) { | |
23 debug('FAILED: ' + message); | |
24 document.location.hash = '#fail'; | |
25 } | |
26 | |
27 function getLog() { | |
28 return "" + document.getElementById('status').innerHTML; | |
29 } | |
30 | |
31 // The following functions are based on | |
32 // WebKit/LayoutTests/fast/js/resources/js-test-pre.js | |
33 // so that the tests will look similar to the existing layout tests. | |
34 function stringify(v) { | |
35 if (v === 0 && 1/v < 0) | |
36 return "-0"; | |
37 else return "" + v; | |
38 } | |
39 | |
40 function areArraysEqual(a, b) { | |
41 try { | |
42 if (a.length !== b.length) | |
43 return false; | |
44 for (var i = 0; i < a.length; i++) { | |
45 if (a[i] !== b[i]) | |
46 return false; | |
47 } | |
48 } catch (ex) { | |
49 return false; | |
50 } | |
51 return true; | |
52 } | |
53 | |
54 function isResultCorrect(_actual, _expected) | |
55 { | |
56 if (_expected === 0) | |
57 return _actual === _expected && (1/_actual) === (1/_expected); | |
58 if (_actual === _expected) | |
59 return true; | |
60 if (typeof(_expected) == "number" && isNaN(_expected)) | |
61 return typeof(_actual) == "number" && isNaN(_actual); | |
62 if (Object.prototype.toString.call(_expected) == | |
63 Object.prototype.toString.call([])) | |
64 return areArraysEqual(_actual, _expected); | |
65 return false; | |
66 } | |
67 | |
68 function shouldBe(_a, _b) | |
69 { | |
70 if (typeof _a != "string" || typeof _b != "string") | |
71 debug("WARN: shouldBe() expects string arguments"); | |
72 var exception; | |
73 var _av; | |
74 try { | |
75 _av = eval(_a); | |
76 } catch (e) { | |
77 exception = e; | |
78 } | |
79 var _bv = eval(_b); | |
80 | |
81 if (exception) | |
82 fail(_a + " should be " + _bv + ". Threw exception " + exception); | |
83 else if (isResultCorrect(_av, _bv)) | |
84 debug(_a + " is " + _b); | |
85 else if (typeof(_av) == typeof(_bv)) | |
86 fail(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); | |
87 else | |
88 fail(_a + " should be " + _bv + " (of type " + typeof _bv + "). " + | |
89 "Was " + _av + " (of type " + typeof _av + ")."); | |
90 } | |
91 | |
92 function shouldBeTrue(_a) { shouldBe(_a, "true"); } | |
93 function shouldBeFalse(_a) { shouldBe(_a, "false"); } | |
94 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } | |
95 function shouldBeNull(_a) { shouldBe(_a, "null"); } | |
96 function shouldBeEqualToString(a, b) { | |
97 var unevaledString = '"' + b.replace(/\\/g, "\\\\").replace(/"/g, "\"") + '"'; | |
98 shouldBe(a, unevaledString); | |
99 } | |
100 | |
101 if (typeof String.prototype.startsWith !== 'function') { | |
102 String.prototype.startsWith = function (str) { | |
103 return this.indexOf(str) === 0; | |
104 }; | |
105 } | |
OLD | NEW |