Testing plan for JSON.parse Options Bag

Open Beginner friendly
#5,159 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
testing

Research direction

Start by reading nearby test262 tests for JSON.parse and how the suite represents option validation, frozen results, and prototypes. Add coverage for the listed success, failure, and reviver edge cases, then run the relevant tests with the repository's test262 runner; done when each listed case is covered and passes.

Written by the indexing model from the issue text.

Description

Failure cases

  • opts.freeze set to non-undefined, non-Boolean throws a TypeError
  • opts.preferNullPrototype set to non-undefined, non-Boolean throws a TypeError
  • if any getter throws, the error it throws is propagated

Success cases

  • JSON.parse("{}", undefined) result is not frozen, prototype is not null
  • JSON.parse("{}", {}) result is not frozen, prototype is not null
  • JSON.parse("{}", { freeze: true }) result is frozen, prototype is null
  • ...same if setting preferNullPrototype to undefined in addition
  • JSON.parse("{}", { freeze: false }) result is not frozen, prototype is not null
  • ...same if setting preferNullPrototype to undefined in addition
  • JSON.parse("{}", { preferNullPrototype: true }) result is not frozen, prototype is null
  • ...same if setting freeze to undefined in addition
  • JSON.parse("{}", { preferNullPrototype: false }) result is not frozen, prototype is not null
  • ...same if setting freeze to undefined in addition
  • JSON.parse("{}", { freeze: true, preferNullPrototype: true }) result is frozen, prototype is null
  • JSON.parse("{}", { freeze: true, preferNullPrototype: false }) result is frozen, prototype is not null
  • JSON.parse("{}", { freeze: false, preferNullPrototype: true }) result is not frozen, prototype is null
  • JSON.parse("{}", { freeze: false, preferNullPrototype: false }) result is not frozen, prototype is not null
  • JSON.parse('{ "a": { "b": { "c": {} } }}', { freeze: true }) returns a frozen object; all four objects are frozen and with null prototypes
  • JSON.parse('{ "a": [1, 2, 3]}', { freeze: true }); the array does not have a null prototype but the object does; the array and object are both frozen
  • JSON.parse("{}", { freeze: true }) !== JSON.parse("{}", { freeze: true })

Edge cases: A primitive is never an options bag

  • JSON.parse(..., null) is just like no second argument was passed
  • JSON.parse(..., 0) is just like no second argument was passed, even after putting freeze and preferNullPrototype getters on Number.prototype
  • JSON.parse(...) and JSON.parse(..., undefined) behave as if no reviver was passed, even after putting freeze and preferNullPrototype on Object.prototype

Edge cases: Callable values are revivers

  • JSON.parse(..., Object) should treat Object as a reviver, not an options bag
  • JSON.parse(..., fn) for any ordinary function fn with an own freeze getter, calls fn and does not call the getter
  • JSON.parse(..., class { static get freeze() {...} } throws a TypeError without accessing freeze
  • JSON.parse(..., new Proxy(() => {...}, ...)) never hits the get trap for keys freeze or preferNullPrototype
  • JSON.parse(..., p) where p is a revoked Proxy around a function; it is still callable so treated as a reviver and thus throws TypeError.

Edge cases: Non callable objects are treated as options bags

  • JSON.parse("{}", Object.assign([], { freeze: true }) returns a frozen object
  • JSON.parse("{}", []) returns a non-frozen object
  • JSON.parse("{}", Object.create(Function.prototype)) not callable so returns a non-frozen object without throwing

Edge cases: option access is Get, not an own-property check

  • JSON.parse("{}", Object.create({ freeze: true }) returns a frozen object
  • Object.prototype.freeze = true, JSON.parse("{}", {}) returns a frozen object
  • Object.prototype.preferNullPrototype = 1, JSON.parse("{}", {}) throws a TypeError
  • JSON.parse("{}", new Proxy({}, ...)): get trap is called on freeze, then preferNullPrototype; has, getOwnPropertyDescriptor and ownKeys traps are not called; the receiver passed to each get call is the proxy
  • JSON.parse("{}", p) where p is a revoked proxy around a plain object throws a TypeError on Get

Edge cases: order of operations

  • toString is called before any getters on options bag
  • if toString throws, options are never read
  • if toString alters values for options, those alterations are respected
  • if freeze is invalid or is a throwing getter, preferNullPrototype is never accessed
  • JSON.parse("{", { freeze: 1 } ) throws a TypeError (not a SyntaxError)
  • Each getter runs exactly once
  • if freeze getter mutates preferNullPrototype the new value is respected
  • if preferNullPrototype getter mutates freeze, the new value is NOT respected
  • a freeze getter calls JSON.parse("{}", { preferNullPrototype: true }) and returns true; the inner result is not frozen but does have a null prototype; the outer result is frozen.
Dominant language
JavaScript
Stars
2.9k
Forks
571
Avg merge
1d 22h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tc39/test262

All issues in tc39/test262

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.