From 985bd7baacd5eefd6bfcc502e6548b89c6b1da46 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Fri, 26 Jun 2026 09:04:54 +0200 Subject: [PATCH] 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. --- Tests/CodableTypeMatrixTests.swift | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Tests/CodableTypeMatrixTests.swift diff --git a/Tests/CodableTypeMatrixTests.swift b/Tests/CodableTypeMatrixTests.swift new file mode 100644 index 0000000..70db998 --- /dev/null +++ b/Tests/CodableTypeMatrixTests.swift @@ -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( + _ 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) + } +}