mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibJS: Add assertThrowsError() test function
This commit is contained in:
parent
c5730ed6a3
commit
0718f216af
Notes:
sideshowbarker
2024-07-19 07:27:17 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/0718f216af4 Pull-request: https://github.com/SerenityOS/serenity/pull/1888
10 changed files with 112 additions and 118 deletions
|
@ -3,21 +3,19 @@ load("test-common.js");
|
|||
try {
|
||||
assert(Array.prototype.filter.length === 1);
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].filter();
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Array.prototype.filter() requires at least one argument");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.filter() requires at least one argument"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].filter(undefined);
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "undefined is not a function");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
|
|
@ -3,21 +3,19 @@ load("test-common.js");
|
|||
try {
|
||||
assert(Array.prototype.forEach.length === 1);
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].forEach();
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Array.prototype.forEach() requires at least one argument");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.forEach() requires at least one argument"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].forEach(undefined);
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "undefined is not a function");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var a = [1, 2, 3];
|
||||
var o = {};
|
||||
|
|
|
@ -3,21 +3,19 @@ load("test-common.js");
|
|||
try {
|
||||
assert(Array.prototype.map.length === 1);
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].map();
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Array.prototype.map() requires at least one argument");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.map() requires at least one argument"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
[].map(undefined);
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "undefined is not a function");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
|
|
@ -8,14 +8,12 @@ try {
|
|||
assert(Boolean.prototype.toString.call(true) === "true");
|
||||
assert(Boolean.prototype.toString.call(false) === "false");
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
Boolean.prototype.toString.call("foo");
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e instanceof Error);
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Not a Boolean");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Boolean"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
|
|
|
@ -8,14 +8,12 @@ try {
|
|||
assert(Boolean.prototype.valueOf.call(true) === true);
|
||||
assert(Boolean.prototype.valueOf.call(false) === false);
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
Boolean.prototype.valueOf.call("foo");
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e instanceof Error);
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Not a Boolean");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Boolean"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
|
|
|
@ -26,12 +26,11 @@ try {
|
|||
assert(d.writable === true);
|
||||
assert(d.value === "ho");
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
}
|
||||
}, {
|
||||
error: TypeError
|
||||
});
|
||||
|
||||
Object.defineProperty(o, "baz", { value: 9, configurable: true, writable: false });
|
||||
Object.defineProperty(o, "baz", { configurable: true, writable: true });
|
||||
|
|
|
@ -1,56 +1,50 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
var b = true;
|
||||
b();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "true is not a function (evaluated from 'b')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "true is not a function (evaluated from 'b')"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
var n = 100 + 20 + 3;
|
||||
n();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "123 is not a function (evaluated from 'n')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "123 is not a function (evaluated from 'n')"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
var o = {};
|
||||
o.a();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "undefined is not a function (evaluated from 'o.a')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function (evaluated from 'o.a')"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
Math();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "[object MathObject] is not a function (evaluated from 'Math')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a function (evaluated from 'Math')"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
new Math();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "[object MathObject] is not a constructor (evaluated from 'Math')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a constructor (evaluated from 'Math')"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
new isNaN();
|
||||
assertNotReached();
|
||||
} catch(e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "function isNaN() {\n [NativeFunction]\n} is not a constructor (evaluated from 'isNaN')");
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "function isNaN() {\n [NativeFunction]\n} is not a constructor (evaluated from 'isNaN')"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch(e) {
|
||||
|
|
|
@ -1,29 +1,26 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
try {
|
||||
Math.abs(-20) = 40;
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "ReferenceError");
|
||||
assert(e.message === "Invalid left-hand side in assignment");
|
||||
}
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
512 = 256;
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "ReferenceError");
|
||||
assert(e.message === "Invalid left-hand side in assignment");
|
||||
}
|
||||
}, {
|
||||
error: ReferenceError,
|
||||
message: "Invalid left-hand side in assignment"
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
512 = 256;
|
||||
}, {
|
||||
error: ReferenceError,
|
||||
message: "Invalid left-hand side in assignment"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
"hello world" = "another thing?";
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "ReferenceError");
|
||||
assert(e.message === "Invalid left-hand side in assignment");
|
||||
}
|
||||
}, {
|
||||
error: ReferenceError,
|
||||
message: "Invalid left-hand side in assignment"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
|
|
|
@ -14,3 +14,18 @@ function assert(value) {
|
|||
function assertNotReached() {
|
||||
throw new AssertionError("assertNotReached() was reached!");
|
||||
}
|
||||
|
||||
function assertThrowsError(testFunction, options) {
|
||||
try {
|
||||
testFunction();
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
if (options.error !== undefined)
|
||||
assert(e instanceof options.error);
|
||||
if (options.name !== undefined)
|
||||
assert(e.name === options.name);
|
||||
if (options.message !== undefined)
|
||||
assert(e.message === options.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,13 @@ load("test-common.js");
|
|||
try {
|
||||
|
||||
const constantValue = 1;
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
constantValue = 2;
|
||||
assertNotReached();
|
||||
} catch (e) {
|
||||
assert(e.name === "TypeError");
|
||||
assert(e.message === "Assignment to constant variable");
|
||||
assert(constantValue === 1);
|
||||
}
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Assignment to constant variable"
|
||||
});
|
||||
assert(constantValue === 1);
|
||||
|
||||
// Make sure we can define new constants in inner scopes.
|
||||
const constantValue2 = 1;
|
||||
|
|
Loading…
Add table
Reference in a new issue