Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / resources / standalone-pre.js
blob70a75b096bc68de820c2f57ac43f9ca3d0e5f438
1 var wasPostTestScriptParsed = false;
2 var errorMessage;
4 function description(msg)
6 print(msg);
7 print("\nOn success, you will see a series of \"PASS\" messages, followed by \"TEST COMPLETE\".\n");
8 print();
11 function debug(msg)
13 print(msg);
16 function escapeString(text)
18 return text.replace(/\0/g, "");
21 function testPassed(msg)
23 print("PASS", escapeString(msg));
26 function testFailed(msg)
28 errorMessage = msg;
29 print("FAIL", escapeString(msg));
32 function areArraysEqual(_a, _b)
34 if (Object.prototype.toString.call(_a) != Object.prototype.toString.call([]))
35 return false;
36 if (_a.length !== _b.length)
37 return false;
38 for (var i = 0; i < _a.length; i++)
39 if (_a[i] !== _b[i])
40 return false;
41 return true;
44 function isMinusZero(n)
46 // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
47 // -Infinity instead of Infinity
48 return n === 0 && 1/n < 0;
51 function isResultCorrect(_actual, _expected)
53 if (_expected === 0)
54 return _actual === _expected && (1/_actual) === (1/_expected);
55 if (_actual === _expected)
56 return true;
57 if (typeof(_expected) == "number" && isNaN(_expected))
58 return typeof(_actual) == "number" && isNaN(_actual);
59 if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
60 return areArraysEqual(_actual, _expected);
61 return false;
64 function stringify(v)
66 if (v === 0 && 1/v < 0)
67 return "-0";
68 else return "" + v;
71 function shouldBe(_a, _b)
73 if (typeof _a != "string" || typeof _b != "string")
74 debug("WARN: shouldBe() expects string arguments");
75 var exception;
76 var _av;
77 try {
78 _av = eval(_a);
79 } catch (e) {
80 exception = e;
82 var _bv = eval(_b);
84 if (exception)
85 testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
86 else if (isResultCorrect(_av, _bv))
87 testPassed(_a + " is " + _b);
88 else if (typeof(_av) == typeof(_bv))
89 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
90 else
91 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
94 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
95 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
96 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
97 function shouldBeNull(_a) { shouldBe(_a, "null"); }
99 function shouldBeEqualToString(a, b)
101 if (typeof a !== "string" || typeof b !== "string")
102 debug("WARN: shouldBeEqualToString() expects string arguments");
103 var unevaledString = JSON.stringify(b);
104 shouldBe(a, unevaledString);
107 function shouldBeUndefined(_a)
109 var exception;
110 var _av;
111 try {
112 _av = eval(_a);
113 } catch (e) {
114 exception = e;
117 if (exception)
118 testFailed(_a + " should be undefined. Threw exception " + exception);
119 else if (typeof _av == "undefined")
120 testPassed(_a + " is undefined.");
121 else
122 testFailed(_a + " should be undefined. Was " + _av);
126 function shouldThrow(_a, _e)
128 var exception;
129 var _av;
130 try {
131 _av = eval(_a);
132 } catch (e) {
133 exception = e;
136 var _ev;
137 if (_e)
138 _ev = eval(_e);
140 if (exception) {
141 if (typeof _e == "undefined" || exception == _ev)
142 testPassed(_a + " threw exception " + exception + ".");
143 else
144 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
145 } else if (typeof _av == "undefined")
146 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
147 else
148 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
151 function isSuccessfullyParsed()
153 // FIXME: Remove this and only report unexpected syntax errors.
154 if (!errorMessage)
155 successfullyParsed = true;
156 shouldBeTrue("successfullyParsed");
157 debug("\nTEST COMPLETE\n");
160 // It's possible for an async test to call finishJSTest() before standalone-post.js
161 // has been parsed.
162 function finishJSTest()
164 wasFinishJSTestCalled = true;
165 if (!wasPostTestScriptParsed)
166 return;
167 isSuccessfullyParsed();