summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/JSON.stringify.js
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-06-10 11:01:00 -0700
committerAndreas Kling <kling@serenityos.org>2020-06-13 12:43:22 +0200
commit39576b22385a2e6b6fc4fbf5e90e6b72157e9ee2 (patch)
tree780b45d855c52048c36a73adac9caaf49ff918e7 /Libraries/LibJS/Tests/JSON.stringify.js
parentb4577ffcf31ef521dcf914d783403f5a57af96b5 (diff)
downloadserenity-39576b22385a2e6b6fc4fbf5e90e6b72157e9ee2.zip
LibJS: Add JSON.stringify
Diffstat (limited to 'Libraries/LibJS/Tests/JSON.stringify.js')
-rw-r--r--Libraries/LibJS/Tests/JSON.stringify.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/JSON.stringify.js b/Libraries/LibJS/Tests/JSON.stringify.js
new file mode 100644
index 0000000000..d071ea961b
--- /dev/null
+++ b/Libraries/LibJS/Tests/JSON.stringify.js
@@ -0,0 +1,71 @@
+load("test-common.js");
+
+try {
+ assert(JSON.stringify.length === 3);
+
+ assertThrowsError(() => {
+ JSON.stringify(5n);
+ }, {
+ error: TypeError,
+ message: "Cannot serialize BigInt value to JSON",
+ });
+
+ const properties = [
+ [5, "5"],
+ [undefined, undefined],
+ [null, "null"],
+ [NaN, "null"],
+ [-NaN, "null"],
+ [Infinity, "null"],
+ [-Infinity, "null"],
+ [true, "true"],
+ [false, "false"],
+ ["test", '"test"'],
+ [new Number(5), "5"],
+ [new Boolean(false), "false"],
+ [new String("test"), '"test"'],
+ [() => {}, undefined],
+ [[1, 2, "foo"], '[1,2,"foo"]'],
+ [{ foo: 1, bar: "baz", qux() {} }, '{"foo":1,"bar":"baz"}'],
+ [
+ {
+ var1: 1,
+ var2: 2,
+ toJSON(key) {
+ let o = this;
+ o.var2 = 10;
+ return o;
+ }
+ },
+ '{"var1":1,"var2":10}',
+ ],
+ ];
+
+ properties.forEach(testCase => {
+ assert(JSON.stringify(testCase[0]) === testCase[1]);
+ });
+
+ let bad1 = {};
+ bad1.foo = bad1;
+ let bad2 = [];
+ bad2[5] = [[[bad2]]];
+
+ let bad3a = { foo: "bar" };
+ let bad3b = [1, 2, bad3a];
+ bad3a.bad = bad3b;
+
+ [bad1, bad2, bad3a].forEach(bad => assertThrowsError(() => {
+ JSON.stringify(bad);
+ }, {
+ error: TypeError,
+ message: "Cannot stringify circular object",
+ }));
+
+ let o = { foo: "bar" };
+ Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
+ assert(JSON.stringify(o) === '{"foo":"bar"}');
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}