Skip to content

Add key must be absent option#15

Merged
timkimadobe merged 17 commits intoadobe:mainfrom
timkimadobe:add-key-must-be-absent-option
Dec 20, 2023
Merged

Add key must be absent option#15
timkimadobe merged 17 commits intoadobe:mainfrom
timkimadobe:add-key-must-be-absent-option

Conversation

@timkimadobe
Copy link
Copy Markdown
Contributor

@timkimadobe timkimadobe commented Dec 13, 2023

Description

This new JSON comparison option allows test writers to specify key paths whose key names should not be present. It also demonstrates the relative ease of adding new comparison options using the new NodeConfig architecture introduced in #9.

At a high level it contains:

  1. New path option struct KeyMustBeAbsent which conforms to MultiPathConfig
    • Allows for specifying both:
      1. Which part of the JSON hierarchy the absent check should be applied
      2. An unbounded array of keys to check for absence

Example:

assertExactMatch(expected: expected, actual: actual, pathOptions: KeyMustBeAbsent(paths: nil, keyNames: "key2", "key3"))
  1. Unit test coverage for:
    1. General path options logic
    2. Specific path options: KeyMustBeAbsent
  2. Creates new negative validation methods (the family of validateActual) which recursively traverses the actual JSON structure to support cases like KeyMustBeAbsent
  3. Refactors assertExactMatch and assertTypeMatch to funnel into the same validation method since their only difference is subtree validation defaults

Questions for reviewers

  1. What is your opinion on runtime test failure on missing keyNames args when using the variadic param format for the KeyMustBeAbsent path option?
    • My thinking was that variadic can be easier to use because less syntax is required (that is, no array brackets) and the test failure on no values passed makes sure that even if keyNames is missed initially, it must be fixed before committing.
    • See example test case:
      func testKeyMustBeAbsent_WithMissingKeyNames_Fails() {
      let expectedJSONString = """
      {}
      """
      let actualJSONString = """
      {
      "key1": 1
      }
      """
      let expected = getAnyCodable(expectedJSONString)!
      let actual = getAnyCodable(actualJSONString)!
      XCTExpectFailure("Validation should fail when key names not provided") {
      assertExactMatch(expected: expected, actual: actual, pathOptions: KeyMustBeAbsent(paths: nil))
      }

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@timkimadobe timkimadobe mentioned this pull request Dec 13, 2023
10 tasks
assertExactMatch(expected: expected, actual: actual, pathOptions: KeyMustBeAbsent(paths: "key1"))
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a few more tests which show the power of the customizable nature of the paths? Something with more complex paths would be great.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great suggestion! I've added a bunch of new tests covering general path options and the new KeyMustBeAbsent option in the new file: AnyCodablePathOptionsTests.swift

# Conflicts:
#	Sources/NodeConfig.swift
#	Sources/XCTestCase+AnyCodableAsserts.swift
Add additional docs for test case
…nstead of single Bool

Refactor JSON validation to traverse actual for negative validation cases like key must be absent
Refactor assertExactMatch and assertTypeMatch to funnel into same validation method since only initial subtree state is different
Add tests for path options
@timkimadobe timkimadobe merged commit fd729dd into adobe:main Dec 20, 2023
@timkimadobe timkimadobe deleted the add-key-must-be-absent-option branch December 20, 2023 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants