diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-06-10 23:30:36 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-13 12:43:22 +0200 |
commit | b155e64b67e555c44226c715d537ecf6c34e49f0 (patch) | |
tree | 50b55dee5733f104b04c642b281f800face9affb /Libraries/LibJS/Tests/JSON.parse.js | |
parent | e8e728454c5d436a02eaa1d24bc3afe357090c52 (diff) | |
download | serenity-b155e64b67e555c44226c715d537ecf6c34e49f0.zip |
LibJS: Add JSON.parse
Diffstat (limited to 'Libraries/LibJS/Tests/JSON.parse.js')
-rw-r--r-- | Libraries/LibJS/Tests/JSON.parse.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/JSON.parse.js b/Libraries/LibJS/Tests/JSON.parse.js new file mode 100644 index 0000000000..40f79cee60 --- /dev/null +++ b/Libraries/LibJS/Tests/JSON.parse.js @@ -0,0 +1,43 @@ +load("test-common.js"); + +try { + assert(JSON.parse.length === 2); + + const properties = [ + ["5", 5], + ["null", null], + ["true", true], + ["false", false], + ['"test"', "test"], + ['[1,2,"foo"]', [1, 2, "foo"]], + ['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }], + ]; + + properties.forEach(testCase => { + assertDeepEquals(JSON.parse(testCase[0]), testCase[1]); + }); + + let syntaxErrors = [ + undefined, + NaN, + -NaN, + Infinity, + -Infinity, + '{ "foo" }', + '{ foo: "bar" }', + "[1,2,3,]", + "[1,2,3, ]", + '{ "foo": "bar",}', + '{ "foo": "bar", }', + ]; + + syntaxErrors.forEach(error => assertThrowsError(() => { + JSON.parse(error); + }, { + error: SyntaxError, + })); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |