1 var wasPostTestScriptParsed
= false;
4 function description(msg
)
7 print("\nOn success, you will see a series of \"PASS\" messages, followed by \"TEST COMPLETE\".\n");
16 function escapeString(text
)
18 return text
.replace(/\0/g, "");
21 function testPassed(msg
)
23 print("PASS", escapeString(msg
));
26 function testFailed(msg
)
29 print("FAIL", escapeString(msg
));
32 function areArraysEqual(_a
, _b
)
34 if (Object
.prototype.toString
.call(_a
) != Object
.prototype.toString
.call([]))
36 if (_a
.length
!== _b
.length
)
38 for (var i
= 0; i
< _a
.length
; i
++)
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
)
54 return _actual
=== _expected
&& (1/_actual) === (1/_expected
);
55 if (_actual
=== _expected
)
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
);
66 if (v
=== 0 && 1/v
< 0)
71 function shouldBe(_a
, _b
)
73 if (typeof _a
!= "string" || typeof _b
!= "string")
74 debug("WARN: shouldBe() expects string arguments");
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
) + ".");
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
)
118 testFailed(_a
+ " should be undefined. Threw exception " + exception
);
119 else if (typeof _av
== "undefined")
120 testPassed(_a
+ " is undefined.");
122 testFailed(_a
+ " should be undefined. Was " + _av
);
126 function shouldThrow(_a
, _e
)
141 if (typeof _e
== "undefined" || exception
== _ev
)
142 testPassed(_a
+ " threw exception " + exception
+ ".");
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.");
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.
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
162 function finishJSTest()
164 wasFinishJSTestCalled
= true;
165 if (!wasPostTestScriptParsed
)
167 isSuccessfullyParsed();