diff options
author | Linus Groh <mail@linusgroh.de> | 2020-04-19 23:01:45 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-20 11:38:01 +0200 |
commit | 0718f216af4d4d1c93de2279f8b28672fe7633b7 (patch) | |
tree | 5ac9d531f4a5d5c551057a0a0e722bfb4bd3fc8b /Libraries/LibJS | |
parent | c5730ed6a3128eab2ccf7275195c67877ec87708 (diff) | |
download | serenity-0718f216af4d4d1c93de2279f8b28672fe7633b7.zip |
LibJS: Add assertThrowsError() test function
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.filter.js | 22 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.forEach.js | 22 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.map.js | 22 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Boolean.prototype.toString.js | 12 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Boolean.prototype.valueOf.js | 12 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Object.defineProperty.js | 9 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/function-TypeError.js | 66 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/invalid-lhs-in-assignment.js | 35 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/test-common.js | 15 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/variable-declaration.js | 13 |
10 files changed, 111 insertions, 117 deletions
diff --git a/Libraries/LibJS/Tests/Array.prototype.filter.js b/Libraries/LibJS/Tests/Array.prototype.filter.js index a5fc677eec..9d738e4464 100644 --- a/Libraries/LibJS/Tests/Array.prototype.filter.js +++ b/Libraries/LibJS/Tests/Array.prototype.filter.js @@ -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++; }; diff --git a/Libraries/LibJS/Tests/Array.prototype.forEach.js b/Libraries/LibJS/Tests/Array.prototype.forEach.js index 5e10399aee..65f4205ab7 100644 --- a/Libraries/LibJS/Tests/Array.prototype.forEach.js +++ b/Libraries/LibJS/Tests/Array.prototype.forEach.js @@ -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 = {}; diff --git a/Libraries/LibJS/Tests/Array.prototype.map.js b/Libraries/LibJS/Tests/Array.prototype.map.js index ee72b19ff4..148b5e3e01 100644 --- a/Libraries/LibJS/Tests/Array.prototype.map.js +++ b/Libraries/LibJS/Tests/Array.prototype.map.js @@ -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++; }; diff --git a/Libraries/LibJS/Tests/Boolean.prototype.toString.js b/Libraries/LibJS/Tests/Boolean.prototype.toString.js index 843dbec593..03a7367900 100644 --- a/Libraries/LibJS/Tests/Boolean.prototype.toString.js +++ b/Libraries/LibJS/Tests/Boolean.prototype.toString.js @@ -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) { diff --git a/Libraries/LibJS/Tests/Boolean.prototype.valueOf.js b/Libraries/LibJS/Tests/Boolean.prototype.valueOf.js index 21ba4df190..7c93eb2f3b 100644 --- a/Libraries/LibJS/Tests/Boolean.prototype.valueOf.js +++ b/Libraries/LibJS/Tests/Boolean.prototype.valueOf.js @@ -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) { diff --git a/Libraries/LibJS/Tests/Object.defineProperty.js b/Libraries/LibJS/Tests/Object.defineProperty.js index fee37f9e78..f5e234d2f9 100644 --- a/Libraries/LibJS/Tests/Object.defineProperty.js +++ b/Libraries/LibJS/Tests/Object.defineProperty.js @@ -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 }); diff --git a/Libraries/LibJS/Tests/function-TypeError.js b/Libraries/LibJS/Tests/function-TypeError.js index 7ce8f888ed..3e659f5ecb 100644 --- a/Libraries/LibJS/Tests/function-TypeError.js +++ b/Libraries/LibJS/Tests/function-TypeError.js @@ -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) { diff --git a/Libraries/LibJS/Tests/invalid-lhs-in-assignment.js b/Libraries/LibJS/Tests/invalid-lhs-in-assignment.js index d7f65af2c0..5bca7137b6 100644 --- a/Libraries/LibJS/Tests/invalid-lhs-in-assignment.js +++ b/Libraries/LibJS/Tests/invalid-lhs-in-assignment.js @@ -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"); - } + assertThrowsError(() => { + 512 = 256; + }, { + error: ReferenceError, + 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(() => { "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) { diff --git a/Libraries/LibJS/Tests/test-common.js b/Libraries/LibJS/Tests/test-common.js index b466a3f010..1e7ab23b79 100644 --- a/Libraries/LibJS/Tests/test-common.js +++ b/Libraries/LibJS/Tests/test-common.js @@ -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); + } +} + diff --git a/Libraries/LibJS/Tests/variable-declaration.js b/Libraries/LibJS/Tests/variable-declaration.js index b1231925db..258b8dbd1a 100644 --- a/Libraries/LibJS/Tests/variable-declaration.js +++ b/Libraries/LibJS/Tests/variable-declaration.js @@ -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; |