PureYAML/Tests/PureYAMLJSONTests/JSONRuleTests.swift
Mihaela Mihaljevic bf069fad39 feat(json): add PureYAMLJSON product, a standalone YAML <-> JSON converter
PureYAMLJSON is a separate library product layered on the core engine. JSON is
a subset of YAML 1.2, so the existing parser already reads both into one value
tree; this product adds the missing half and the value-level policy:

- JSON.Writer: a pure-Swift RFC-8259 serializer (no Foundation; WASI-clean),
  pretty or compact, rejecting non-string keys and non-finite numbers.
- JSON representability rules expressed in PureYAML's own Validation framework:
  mappingKeysAreStrings and numbersAreFinite join the core mappingKeysAreUnique.
- JSON.Validator: the representability rule set plus issues(for:)/isRepresentable.
- JSON.Convert: standalone convert(_:to:) with the round-trip law (parse ->
  validate representability -> emit -> re-parse must equal the source). To JSON
  rejects complex keys and non-finite numbers; to YAML rejects duplicate keys,
  which are legal in JSON but not in YAML.

19 tests cover the writer, the rules, and both conversion directions including
the dual-rejection edges. Carved out of the catch-all Sources/Tests paths via
exclude so the core PureYAML target is unchanged.
2026-06-25 00:33:40 +02:00

54 lines
2.3 KiB
Swift

import PureYAML
@testable import PureYAMLJSON
import Testing
@Suite("JSON representability rules")
struct JSONRuleTests {
private func mapping(_ pairs: [PureYAML.Model.Pair]) -> PureYAML.Model.Value {
.mapping(.init(pairs))
}
@Test("mappingKeysAreStrings fails on a complex key")
func test_mappingKeysAreStrings_fails() {
let document = mapping([.init(keyNode: .sequence([.int(1)]), value: .int(2))])
let issues = PureYAML.Validation.Validator(rules: [.mappingKeysAreStrings]).collect(document).issues
#expect(issues.count == 1)
#expect(issues.first?.reason == "Mapping key '[1]' is not a string")
}
@Test("mappingKeysAreStrings passes when every key is a string")
func test_mappingKeysAreStrings_passes() {
let document = mapping([.init(key: "a", value: .int(1))])
let issues = PureYAML.Validation.Validator(rules: [.mappingKeysAreStrings]).collect(document).issues
#expect(issues.isEmpty)
}
@Test("numbersAreFinite fails on infinity at the value's path")
func test_numbersAreFinite_fails() {
let document = mapping([.init(key: "x", value: .double(.infinity))])
let issues = PureYAML.Validation.Validator(rules: [.numbersAreFinite]).collect(document).issues
#expect(issues.count == 1)
#expect(issues.first?.reason == "Number 'inf' is not finite")
}
@Test("numbersAreFinite passes on a large finite double")
func test_numbersAreFinite_passes() {
let document = mapping([.init(key: "x", value: .double(.greatestFiniteMagnitude))])
let issues = PureYAML.Validation.Validator(rules: [.numbersAreFinite]).collect(document).issues
#expect(issues.isEmpty)
}
@Test("The representability set catches a duplicate key")
func test_representability_catchesDuplicate() {
let document = mapping([.init(key: "a", value: .int(1)), .init(key: "a", value: .int(2))])
#expect(!PureYAML.JSON.Validator.isRepresentable(document))
}
@Test("A plain string-keyed finite tree is representable")
func test_representability_acceptsPlainTree() {
let document = mapping([.init(key: "a", value: .int(1)), .init(key: "b", value: .double(1.5))])
#expect(PureYAML.JSON.Validator.isRepresentable(document))
#expect(PureYAML.JSON.Validator.issues(for: document).isEmpty)
}
}