Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate staging tests for JSON-parse-with-source #4265

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/built-ins/JSON/isRawJSON/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-json.israwjson
description: Basic functionality of JSON.isRawJSON()
info: |
JSON.isRawJSON ( O )

1. If Type(O) is Object and O has an [[IsRawJSON]] internal slot, return true.
2. Return false.

features: [json-parse-with-source]
---*/

const values = [1, 1.1, null, false, true, '123'];
for (const value of values) {
assert(!JSON.isRawJSON(value));
assert(JSON.isRawJSON(JSON.rawJSON(value)));
}
assert(!JSON.isRawJSON(undefined));
assert(!JSON.isRawJSON(Symbol('123')));
assert(!JSON.isRawJSON([]));
assert(!JSON.isRawJSON({}));
assert(!JSON.isRawJSON({ rawJSON: '123' }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-json.parse
description: >
JSON.parse reviver is called with the correct arguments when the object is
modified
includes: [compareArray.js]
features: [json-parse-with-source]
---*/

// Test Array append
{
let log = [];
const o = JSON.parse('[1,[]]', function reviver(k, v, { source }) {
log.push(`key: |${k}| value: ${JSON.stringify(v)} source: |${source}|`);
if (v === 1) {
this[1].push('barf');
}
return this[k];
});
assert.compareArray(log, [
'key: |0| value: 1 source: |1|',
'key: |0| value: "barf" source: |undefined|',
'key: |1| value: ["barf"] source: |undefined|',
'key: || value: [1,["barf"]] source: |undefined|',
]);
}

// Test Object add property
{
let log = [];
const o = JSON.parse('{"p":1,"q":{}}', function (k, v, { source }) {
log.push(`key: |${k}| value: ${JSON.stringify(v)} source: |${source}|`);
if (v === 1) {
this.q.added = 'barf';
}
return this[k];
});
assert.compareArray(log, [
'key: |p| value: 1 source: |1|',
'key: |added| value: "barf" source: |undefined|',
'key: |q| value: {"added":"barf"} source: |undefined|',
'key: || value: {"p":1,"q":{"added":"barf"}} source: |undefined|',
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-json.parse
description: >
Context argument and its source property behave as expected when parsing an
ArrayLiteral JSON string
includes: [compareArray.js, propertyHelper.js]
features: [json-parse-with-source]
---*/

function assertOnlyOwnProperties(object, props, message) {
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`);
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`);
}

function reviverWithExpectedSources(expectedSources) {
let i = 0;
return function reviver(key, value, context) {
assert.sameValue(typeof context, "object", "context should be an object");
assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object");
if (expectedSources[i] !== undefined) {
assertOnlyOwnProperties(context, ["source"],
"the JSON value is a primitve value, its context should only have a source property");
verifyProperty(context, "source", {
value: expectedSources[i++],
configurable: true,
enumerable: true,
writable: true,
}, { restore: true });
} else {
assertOnlyOwnProperties(context, [],
"the JSON value is an Array or Object, its context should have no property");
i++;
}
return value;
};
}

assert.compareArray(JSON.parse('[1.0]', reviverWithExpectedSources(['1.0'])), [1]);
assert.compareArray(
JSON.parse('[1.1]', reviverWithExpectedSources(['1.1'])),
[1.1]
);
assert.compareArray(JSON.parse('[]', reviverWithExpectedSources([])), []);

const longArray = JSON.parse(
'[1, "2", true, null, {"x": 1, "y": 1}]',
reviverWithExpectedSources(['1', '"2"', 'true', 'null', '1', '1'])
);
assert.sameValue(longArray[0], 1, "array, element 0");
assert.sameValue(longArray[1], "2", "array, element 1");
assert.sameValue(longArray[2], true, "array, element 2");
assert.sameValue(longArray[3], null, "array, element 3");
assertOnlyOwnProperties(longArray[4], ["x", "y"], "array, element 5");
assert.sameValue(longArray[4].x, 1, "array, element 5, prop x");
assert.sameValue(longArray[4].y, 1, "array, element 5, prop y");
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-json.parse
description: >
Context argument and its source property behave as expected when parsing an
ObjectLiteral JSON string
includes: [compareArray.js, propertyHelper.js]
features: [json-parse-with-source]
---*/

function assertOnlyOwnProperties(object, props, message) {
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`);
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`);
}

function reviverWithExpectedSources(expectedSources) {
let i = 0;
return function reviver(key, value, context) {
assert.sameValue(typeof context, "object", "context should be an object");
assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object");
if (expectedSources[i] !== undefined) {
assertOnlyOwnProperties(context, ["source"],
"the JSON value is a primitve value, its context should only have a source property");
verifyProperty(context, "source", {
value: expectedSources[i++],
configurable: true,
enumerable: true,
writable: true,
}, { restore: true });
} else {
assertOnlyOwnProperties(context, [],
"the JSON value is an Array or Object, its context should have no property");
i++;
}
return value;
};
}

assertOnlyOwnProperties(
JSON.parse('{}', reviverWithExpectedSources([])),
[],
"empty object"
);

const singleProp = JSON.parse('{"42":37}', reviverWithExpectedSources(['37']));
assertOnlyOwnProperties(singleProp, ["42"], "single numeric property key");
assert.sameValue(singleProp[42], 37, "value of single numeric property key");

const multipleProps = JSON.parse('{"x": 1, "y": 2}', reviverWithExpectedSources(['1', '2']));
assertOnlyOwnProperties(multipleProps, ["x", "y"], "multiple properties");
assert.sameValue(multipleProps.x, 1, "multiple properties, value of x");
assert.sameValue(multipleProps.y, 2, "multiple properties, value of y");

// undefined means the json value is JSObject or JSArray and the passed
// context to the reviver function has no source property.
const arrayProps = JSON.parse(
'{"x": [1,2], "y": [2,3]}',
reviverWithExpectedSources(['1', '2', undefined, '2', '3', undefined])
);
assertOnlyOwnProperties(arrayProps, ["x", "y"], "array-valued properties");
assert.compareArray(arrayProps.x, [1, 2], "array-valued properties, value of x");
assert.compareArray(arrayProps.y, [2, 3], "array-valued properties, value of y");

const objectProps = JSON.parse(
'{"x": {"x": 1, "y": 2}}',
reviverWithExpectedSources(['1', '2', undefined, undefined])
);
assertOnlyOwnProperties(objectProps, ["x"], "object-valued properties");
assertOnlyOwnProperties(objectProps.x, ["x", "y"], "object-valued properties, value of x");
assert.sameValue(objectProps.x.x, 1, "object-valued properties, value of x.x");
assert.sameValue(objectProps.x.y, 2, "object-valued properties, value of x.y");
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-json.parse
description: >
Context argument and its source property behave as expected when parsing a
NumericLiteral, NullLiteral, BoolLiteral, or StringLiteral JSON string
includes: [compareArray.js, propertyHelper.js]
features: [json-parse-with-source]
---*/

function assertOnlyOwnProperties(object, props, message) {
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`);
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`);
}

function reviverWithExpectedSources(expectedSources) {
let i = 0;
return function reviver(key, value, context) {
assert.sameValue(typeof context, "object", "context should be an object");
assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object");
if (expectedSources[i] !== undefined) {
assertOnlyOwnProperties(context, ["source"],
"the JSON value is a primitve value, its context should only have a source property");
verifyProperty(context, "source", {
value: expectedSources[i++],
configurable: true,
enumerable: true,
writable: true,
}, { restore: true });
} else {
assertOnlyOwnProperties(context, [],
"the JSON value is an Array or Object, its context should have no property");
i++;
}
return value;
};
}

assert.sameValue(1, JSON.parse('1', reviverWithExpectedSources(['1'])));
assert.sameValue(1.1, JSON.parse('1.1', reviverWithExpectedSources(['1.1'])));
assert.sameValue(-1, JSON.parse('-1', reviverWithExpectedSources(['-1'])));
assert.sameValue(
-1.1,
JSON.parse('-1.1', reviverWithExpectedSources(['-1.1']))
);
assert.sameValue(
11,
JSON.parse('1.1e1', reviverWithExpectedSources(['1.1e1']))
);
assert.sameValue(
11,
JSON.parse('1.1e+1', reviverWithExpectedSources(['1.1e+1']))
);
assert.sameValue(
0.11,
JSON.parse('1.1e-1', reviverWithExpectedSources(['1.1e-1']))
);
assert.sameValue(
11,
JSON.parse('1.1E1', reviverWithExpectedSources(['1.1E1']))
);
assert.sameValue(
11,
JSON.parse('1.1E+1', reviverWithExpectedSources(['1.1E+1']))
);
assert.sameValue(
0.11,
JSON.parse('1.1E-1', reviverWithExpectedSources(['1.1E-1']))
);

// Test NullLiteral, BoolLiteral, StringLiteral
assert.sameValue(
JSON.parse('null', reviverWithExpectedSources(['null'])),
null
);
assert.sameValue(
JSON.parse('true', reviverWithExpectedSources(['true'])),
true
);
assert.sameValue(
JSON.parse('false', reviverWithExpectedSources(['false'])),
false
);
assert.sameValue(
JSON.parse('"foo"', reviverWithExpectedSources(['"foo"'])),
"foo"
);
Loading
Loading