1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:58:12 +00:00

LibJS: Use assertNotReached() in tests

This commit is contained in:
Linus Groh 2020-04-13 14:11:09 +01:00 committed by Andreas Kling
parent 92a9fe0e49
commit b9415dc0e9
8 changed files with 36 additions and 27 deletions

View file

@ -6,17 +6,15 @@ try {
assert(Boolean.prototype.toString.call(true) === "true"); assert(Boolean.prototype.toString.call(true) === "true");
assert(Boolean.prototype.toString.call(false) === "false"); assert(Boolean.prototype.toString.call(false) === "false");
let error = null;
try { try {
Boolean.prototype.toString.call("foo"); Boolean.prototype.toString.call("foo");
} catch (err) { assertNotReached();
error = err; } catch (e) {
assert(e instanceof Error);
assert(e.name === "TypeError");
assert(e.message === "Not a Boolean");
} }
assert(error instanceof Error);
assert(error.name === "TypeError");
assert(error.message === "Not a Boolean");
console.log("PASS"); console.log("PASS");
} catch (err) { } catch (err) {
console.log("FAIL: " + err); console.log("FAIL: " + err);

View file

@ -6,17 +6,15 @@ try {
assert(Boolean.prototype.valueOf.call(true) === true); assert(Boolean.prototype.valueOf.call(true) === true);
assert(Boolean.prototype.valueOf.call(false) === false); assert(Boolean.prototype.valueOf.call(false) === false);
let error = null;
try { try {
Boolean.prototype.valueOf.call("foo"); Boolean.prototype.valueOf.call("foo");
} catch (err) { assertNotReached();
error = err; } catch (e) {
assert(e instanceof Error);
assert(e.name === "TypeError");
assert(e.message === "Not a Boolean");
} }
assert(error instanceof Error);
assert(error.name === "TypeError");
assert(error.message === "Not a Boolean");
console.log("PASS"); console.log("PASS");
} catch (err) { } catch (err) {
console.log("FAIL: " + err); console.log("FAIL: " + err);

View file

@ -26,7 +26,7 @@ try {
try { try {
Object.defineProperty(o, "bar", { value: "xx", enumerable: false }); Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
assert(false); assertNotReached();
} catch (e) { } catch (e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
} }

View file

@ -2,6 +2,7 @@ try {
try { try {
var b = true; var b = true;
b(); b();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "true is not a function"); assert(e.message === "true is not a function");
@ -10,6 +11,7 @@ try {
try { try {
var n = 100 + 20 + 3; var n = 100 + 20 + 3;
n(); n();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "123 is not a function"); assert(e.message === "123 is not a function");
@ -18,6 +20,7 @@ try {
try { try {
var o = {}; var o = {};
o.a(); o.a();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "undefined is not a function"); assert(e.message === "undefined is not a function");
@ -25,6 +28,7 @@ try {
try { try {
Math(); Math();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "[object MathObject] is not a function"); assert(e.message === "[object MathObject] is not a function");
@ -32,6 +36,7 @@ try {
try { try {
new Math(); new Math();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "[object MathObject] is not a constructor"); assert(e.message === "[object MathObject] is not a constructor");
@ -39,6 +44,7 @@ try {
try { try {
new isNaN(); new isNaN();
assertNotReached();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "function isNaN() {\n [NativeFunction]\n} is not a constructor"); assert(e.message === "function isNaN() {\n [NativeFunction]\n} is not a constructor");

View file

@ -1,6 +1,7 @@
try { try {
try { try {
Math.abs(-20) = 40; Math.abs(-20) = 40;
assertNotReached();
} catch (e) { } catch (e) {
assert(e.name === "ReferenceError"); assert(e.name === "ReferenceError");
assert(e.message === "Invalid left-hand side in assignment"); assert(e.message === "Invalid left-hand side in assignment");
@ -8,6 +9,7 @@ try {
try { try {
512 = 256; 512 = 256;
assertNotReached();
} catch (e) { } catch (e) {
assert(e.name === "ReferenceError"); assert(e.name === "ReferenceError");
assert(e.message === "Invalid left-hand side in assignment"); assert(e.message === "Invalid left-hand side in assignment");
@ -15,6 +17,7 @@ try {
try { try {
"hello world" = "another thing?"; "hello world" = "another thing?";
assertNotReached();
} catch (e) { } catch (e) {
assert(e.name === "ReferenceError"); assert(e.name === "ReferenceError");
assert(e.message === "Invalid left-hand side in assignment"); assert(e.message === "Invalid left-hand side in assignment");

View file

@ -2,6 +2,7 @@ var a = "foo";
switch (a + "bar") { switch (a + "bar") {
case 1: case 1:
assertNotReached();
break; break;
case "foobar": case "foobar":
case 2: case 2:

View file

@ -1,11 +1,13 @@
try { try {
throw 1; throw 1;
assertNotReached();
} catch (e) { } catch (e) {
assert(e === 1); assert(e === 1);
} }
try { try {
throw [99]; throw [99];
assertNotReached();
} catch (e) { } catch (e) {
assert(typeof e === "object"); assert(typeof e === "object");
assert(e.length === 1); assert(e.length === 1);
@ -13,10 +15,12 @@ try {
function foo() { function foo() {
throw "hello"; throw "hello";
assertNotReached();
} }
try { try {
foo(); foo();
assertNotReached();
} catch (e) { } catch (e) {
assert(e === "hello"); assert(e === "hello");
} }

View file

@ -1,23 +1,22 @@
try { try {
const ConstantValue = 1; const constantValue = 1;
try { try {
ConstantValue = 2; constantValue = 2;
} catch (e) { assertNotReached();
} catch (e) {
assert(e.name === "TypeError"); assert(e.name === "TypeError");
assert(e.message === "Assignment to constant variable"); assert(e.message === "Assignment to constant variable");
assert(ConstantValue === 1); assert(constantValue === 1);
} }
// Make sure we can define new constants in inner scopes. // Make sure we can define new constants in inner scopes.
// const constantValue2 = 1;
const ConstantValue2 = 1; do {
const constantValue2 = 2;
do assert(constantValue2 === 2);
{ } while (false);
const ConstantValue2 = 2; assert(constantValue2 === 1);
}
while (false)
console.log("PASS"); console.log("PASS");
} catch (e) { } catch (e) {