test: round-trip the full scalar type matrix through Codable containers
Enumerate every scalar Codable type (Int8 through Int64, Int, UInt through UInt64, Float, Double, Bool, String) through both a keyed container, via an all-scalars struct, and an unkeyed container, via per-type arrays. This drives the keyed encode and decode container methods and the encoder's scalar ladder to near-full coverage and pins exact round-trip equality for each type.
This commit is contained in:
parent
2f6de52654
commit
985bd7baac
60
Tests/CodableTypeMatrixTests.swift
Normal file
60
Tests/CodableTypeMatrixTests.swift
Normal file
@ -0,0 +1,60 @@
|
||||
@testable import PureYAML
|
||||
import Testing
|
||||
|
||||
@Suite("Codable Type Matrix")
|
||||
struct CodableTypeMatrixTests {
|
||||
struct AllScalars: Codable, Equatable {
|
||||
var int8: Int8
|
||||
var int16: Int16
|
||||
var int32: Int32
|
||||
var int64: Int64
|
||||
var intValue: Int
|
||||
var uintValue: UInt
|
||||
var uint8: UInt8
|
||||
var uint16: UInt16
|
||||
var uint32: UInt32
|
||||
var uint64: UInt64
|
||||
var floatValue: Float
|
||||
var doubleValue: Double
|
||||
var boolValue: Bool
|
||||
var stringValue: String
|
||||
}
|
||||
|
||||
@Test("Every scalar type round-trips through a keyed container")
|
||||
func test_keyedContainerRoundTripsEveryScalarType() throws {
|
||||
let value = AllScalars(
|
||||
int8: -8, int16: -16, int32: -32, int64: -64, intValue: -1,
|
||||
uintValue: 1, uint8: 8, uint16: 16, uint32: 32, uint64: 64,
|
||||
floatValue: 1.5, doubleValue: 2.5, boolValue: true, stringValue: "text",
|
||||
)
|
||||
let encoded = try PureYAML.encode(value)
|
||||
#expect(try PureYAML.decode(AllScalars.self, from: encoded) == value)
|
||||
}
|
||||
|
||||
@Test("Every scalar type round-trips through an unkeyed container")
|
||||
func test_unkeyedContainerRoundTripsEveryScalarType() throws {
|
||||
try expectArrayRoundTrip([Int8(-8), 8])
|
||||
try expectArrayRoundTrip([Int16(-16), 16])
|
||||
try expectArrayRoundTrip([Int32(-32), 32])
|
||||
try expectArrayRoundTrip([Int64(-64), 64])
|
||||
try expectArrayRoundTrip([Int(-1), 1])
|
||||
try expectArrayRoundTrip([UInt(1), 2])
|
||||
try expectArrayRoundTrip([UInt8(8), 9])
|
||||
try expectArrayRoundTrip([UInt16(16), 17])
|
||||
try expectArrayRoundTrip([UInt32(32), 33])
|
||||
try expectArrayRoundTrip([UInt64(64), 65])
|
||||
try expectArrayRoundTrip([Float(1.5), 2.5])
|
||||
try expectArrayRoundTrip([Double(3.5), 4.5])
|
||||
try expectArrayRoundTrip([true, false])
|
||||
try expectArrayRoundTrip(["a", "b"])
|
||||
}
|
||||
|
||||
private func expectArrayRoundTrip<Element: Codable & Equatable>(
|
||||
_ array: [Element],
|
||||
sourceLocation: SourceLocation = #_sourceLocation,
|
||||
) throws {
|
||||
let encoded = try PureYAML.encode(array)
|
||||
let decoded = try PureYAML.decode([Element].self, from: encoded)
|
||||
#expect(decoded == array, sourceLocation: sourceLocation)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user