summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/JSON.parse.js
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-06-10 23:30:36 -0700
committerAndreas Kling <kling@serenityos.org>2020-06-13 12:43:22 +0200
commitb155e64b67e555c44226c715d537ecf6c34e49f0 (patch)
tree50b55dee5733f104b04c642b281f800face9affb /Libraries/LibJS/Tests/JSON.parse.js
parente8e728454c5d436a02eaa1d24bc3afe357090c52 (diff)
downloadserenity-b155e64b67e555c44226c715d537ecf6c34e49f0.zip
LibJS: Add JSON.parse
Diffstat (limited to 'Libraries/LibJS/Tests/JSON.parse.js')
-rw-r--r--Libraries/LibJS/Tests/JSON.parse.js43
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);
+}