mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibJS: Add assertThrowsError() test function
This commit is contained in:
parent
c5730ed6a3
commit
0718f216af
10 changed files with 112 additions and 118 deletions
|
@ -3,21 +3,19 @@ load("test-common.js");
|
||||||
try {
|
try {
|
||||||
assert(Array.prototype.filter.length === 1);
|
assert(Array.prototype.filter.length === 1);
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].filter();
|
[].filter();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "Array.prototype.filter() requires at least one argument"
|
||||||
assert(e.message === "Array.prototype.filter() requires at least one argument");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].filter(undefined);
|
[].filter(undefined);
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "undefined is not a function"
|
||||||
assert(e.message === "undefined is not a function");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
var callback = () => { callbackCalled++; };
|
var callback = () => { callbackCalled++; };
|
||||||
|
|
|
@ -3,21 +3,19 @@ load("test-common.js");
|
||||||
try {
|
try {
|
||||||
assert(Array.prototype.forEach.length === 1);
|
assert(Array.prototype.forEach.length === 1);
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].forEach();
|
[].forEach();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "Array.prototype.forEach() requires at least one argument"
|
||||||
assert(e.message === "Array.prototype.forEach() requires at least one argument");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].forEach(undefined);
|
[].forEach(undefined);
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "undefined is not a function"
|
||||||
assert(e.message === "undefined is not a function");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
var a = [1, 2, 3];
|
var a = [1, 2, 3];
|
||||||
var o = {};
|
var o = {};
|
||||||
|
|
|
@ -3,21 +3,19 @@ load("test-common.js");
|
||||||
try {
|
try {
|
||||||
assert(Array.prototype.map.length === 1);
|
assert(Array.prototype.map.length === 1);
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].map();
|
[].map();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "Array.prototype.map() requires at least one argument"
|
||||||
assert(e.message === "Array.prototype.map() requires at least one argument");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
[].map(undefined);
|
[].map(undefined);
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "undefined is not a function"
|
||||||
assert(e.message === "undefined is not a function");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
var callback = () => { callbackCalled++; };
|
var callback = () => { callbackCalled++; };
|
||||||
|
|
|
@ -8,14 +8,12 @@ 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");
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
Boolean.prototype.toString.call("foo");
|
Boolean.prototype.toString.call("foo");
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e instanceof Error);
|
message: "Not a Boolean"
|
||||||
assert(e.name === "TypeError");
|
});
|
||||||
assert(e.message === "Not a Boolean");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -8,14 +8,12 @@ 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);
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
Boolean.prototype.valueOf.call("foo");
|
Boolean.prototype.valueOf.call("foo");
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e instanceof Error);
|
message: "Not a Boolean"
|
||||||
assert(e.name === "TypeError");
|
});
|
||||||
assert(e.message === "Not a Boolean");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -26,12 +26,11 @@ try {
|
||||||
assert(d.writable === true);
|
assert(d.writable === true);
|
||||||
assert(d.value === "ho");
|
assert(d.value === "ho");
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
|
Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError
|
||||||
assert(e.name === "TypeError");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(o, "baz", { value: 9, configurable: true, writable: false });
|
Object.defineProperty(o, "baz", { value: 9, configurable: true, writable: false });
|
||||||
Object.defineProperty(o, "baz", { configurable: true, writable: true });
|
Object.defineProperty(o, "baz", { configurable: true, writable: true });
|
||||||
|
|
|
@ -1,56 +1,50 @@
|
||||||
load("test-common.js");
|
load("test-common.js");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
var b = true;
|
var b = true;
|
||||||
b();
|
b();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "true is not a function (evaluated from 'b')"
|
||||||
assert(e.message === "true is not a function (evaluated from 'b')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
var n = 100 + 20 + 3;
|
var n = 100 + 20 + 3;
|
||||||
n();
|
n();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "123 is not a function (evaluated from 'n')"
|
||||||
assert(e.message === "123 is not a function (evaluated from 'n')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
var o = {};
|
var o = {};
|
||||||
o.a();
|
o.a();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "undefined is not a function (evaluated from 'o.a')"
|
||||||
assert(e.message === "undefined is not a function (evaluated from 'o.a')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
Math();
|
Math();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "[object MathObject] is not a function (evaluated from 'Math')"
|
||||||
assert(e.message === "[object MathObject] is not a function (evaluated from 'Math')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
new Math();
|
new Math();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "[object MathObject] is not a constructor (evaluated from 'Math')"
|
||||||
assert(e.message === "[object MathObject] is not a constructor (evaluated from 'Math')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
new isNaN();
|
new isNaN();
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch(e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
message: "function isNaN() {\n [NativeFunction]\n} is not a constructor (evaluated from 'isNaN')"
|
||||||
assert(e.message === "function isNaN() {\n [NativeFunction]\n} is not a constructor (evaluated from 'isNaN')");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
|
|
@ -1,29 +1,26 @@
|
||||||
load("test-common.js");
|
load("test-common.js");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
Math.abs(-20) = 40;
|
|
||||||
assertNotReached();
|
|
||||||
} catch (e) {
|
|
||||||
assert(e.name === "ReferenceError");
|
|
||||||
assert(e.message === "Invalid left-hand side in assignment");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
512 = 256;
|
512 = 256;
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: ReferenceError,
|
||||||
assert(e.name === "ReferenceError");
|
message: "Invalid left-hand side in assignment"
|
||||||
assert(e.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?";
|
"hello world" = "another thing?";
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: ReferenceError,
|
||||||
assert(e.name === "ReferenceError");
|
message: "Invalid left-hand side in assignment"
|
||||||
assert(e.message === "Invalid left-hand side in assignment");
|
});
|
||||||
}
|
|
||||||
|
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -14,3 +14,18 @@ function assert(value) {
|
||||||
function assertNotReached() {
|
function assertNotReached() {
|
||||||
throw new AssertionError("assertNotReached() was reached!");
|
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 {
|
try {
|
||||||
|
|
||||||
const constantValue = 1;
|
const constantValue = 1;
|
||||||
try {
|
assertThrowsError(() => {
|
||||||
constantValue = 2;
|
constantValue = 2;
|
||||||
assertNotReached();
|
}, {
|
||||||
} catch (e) {
|
error: TypeError,
|
||||||
assert(e.name === "TypeError");
|
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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue