diff --git a/Sources/PotentCodables/AnyValue/AnyValue.swift b/Sources/PotentCodables/AnyValue/AnyValue.swift index 7c37472ff..f328ce522 100644 --- a/Sources/PotentCodables/AnyValue/AnyValue.swift +++ b/Sources/PotentCodables/AnyValue/AnyValue.swift @@ -219,7 +219,9 @@ public enum AnyValue: Sendable { return uint } - public func integerValue(_ type: I.Type) -> I? { + public typealias AnyIntegerType = FixedWidthInteger + + public func integerValue(_ type: I.Type) -> I? { switch self { case .int8(let value): return I(value) case .int16(let value): return I(value) @@ -262,7 +264,9 @@ public enum AnyValue: Sendable { return decimal } - public func floatingPointValue(_ type: F.Type) -> F? { + public typealias AnyFloatingPointType = BinaryFloatingPoint & LosslessStringConvertible + + public func floatingPointValue(_ type: F.Type) -> F? { switch self { case .int8(let value): return F(value) case .int16(let value): return F(value) @@ -280,6 +284,17 @@ public enum AnyValue: Sendable { return nil } } + + public func numericValue(_ type: N.Type) -> N? { + switch type { + case let floatType as any AnyFloatingPointType.Type: + return floatingPointValue(floatType) as? N + case let intType as any AnyIntegerType.Type: + return integerValue(intType) as? N + default: + return nil + } + } } @@ -339,23 +354,31 @@ extension AnyValue { switch value { case let val as AnyValue: return val case let val as String: return .string(val) - case let val as Int: return .int(val) - case let val as UInt: return .uint(val) case let val as Bool: return .bool(val) - case let val as Int8: return .int8(val) - case let val as UInt8: return .uint8(val) - case let val as Int16: return .int16(val) - case let val as UInt16: return .uint16(val) - case let val as Int32: return .int32(val) - case let val as UInt32: return .uint32(val) - case let val as Int64: return .int64(val) - case let val as UInt64: return .uint64(val) - case let val as Decimal: return .decimal(val) // Before other floats (Swift Decimal -> Double allowed) - case let val as Float16: return .float16(val) - case let val as Float: return .float(val) - case let val as Double: return .double(val) + case let val as any FixedWidthInteger: + switch val { + case let val as Int8: return .int8(val) + case let val as UInt8: return .uint8(val) + case let val as Int16: return .int16(val) + case let val as UInt16: return .uint16(val) + case let val as Int32: return .int32(val) + case let val as UInt32: return .uint32(val) + case let val as Int64: return .int64(val) + case let val as UInt64: return .uint64(val) + case let val as Int: return .int(val) + case let val as UInt: return .uint(val) + default: throw Error.unsupportedValue(value) + } case let val as BigInt: return .integer(val) case let val as BigUInt: return .unsignedInteger(val) + case let val as any BinaryFloatingPoint: + switch val { + case let val as Float16: return .float16(val) + case let val as Float32: return .float(val) + case let val as Float64: return .double(val) + default: throw Error.unsupportedValue(value) + } + case let val as Decimal: return .decimal(val) // Before other floats (Swift Decimal -> Double allowed) case let val as Data: return .data(val) case let val as URL: return .url(val) case let val as UUID: return .uuid(val) diff --git a/Sources/PotentCodables/Errors.swift b/Sources/PotentCodables/Errors.swift index 2123b0c6a..e5818e3ac 100644 --- a/Sources/PotentCodables/Errors.swift +++ b/Sources/PotentCodables/Errors.swift @@ -47,7 +47,8 @@ public extension DecodingError { else { if let nonOptionalValue = value { return String(describing: type(of: nonOptionalValue)) - } else { + } + else { return "nil" } } diff --git a/Sources/PotentJSON/JSONReader.swift b/Sources/PotentJSON/JSONReader.swift index 7e5481e8d..75ade3817 100644 --- a/Sources/PotentJSON/JSONReader.swift +++ b/Sources/PotentJSON/JSONReader.swift @@ -49,10 +49,6 @@ internal struct JSONReader { struct UTF8Source { let buffer: UnsafeBufferPointer - init(buffer: UnsafeBufferPointer) { - self.buffer = buffer - } - func takeASCII(_ input: Index) -> (UInt8, Index)? { guard hasNext(input) else { return nil diff --git a/Tests/ASN1AnyStringTests.swift b/Tests/ASN1AnyStringTests.swift index f50f61e2e..5d9891da5 100644 --- a/Tests/ASN1AnyStringTests.swift +++ b/Tests/ASN1AnyStringTests.swift @@ -36,8 +36,8 @@ class ASN1AnyStringTests: XCTestCase { func testCodable() { - XCTAssertEqual(try JSONEncoder().encode(AnyString("test")), #""test""#.data(using: .utf8)) - XCTAssertEqual(try JSONDecoder().decode(AnyString.self, from: #""test""#.data(using: .utf8)!), AnyString("test")) + XCTAssertEqual(try JSONEncoder().encode(AnyString("test")), Data(#""test""#.utf8)) + XCTAssertEqual(try JSONDecoder().decode(AnyString.self, from: Data(#""test""#.utf8)), AnyString("test")) } func testUppercased() { diff --git a/Tests/ASN1AnyValueTests.swift b/Tests/ASN1AnyValueTests.swift index 494163245..a7b02bc14 100644 --- a/Tests/ASN1AnyValueTests.swift +++ b/Tests/ASN1AnyValueTests.swift @@ -133,7 +133,7 @@ class ASN1AnyValueTests: XCTestCase { var pndec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567)) var npdec: AnyValue = .decimal(Decimal(sign: .minus, exponent: 1, significand: 1234567)) var nndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567)) - var data: AnyValue = .data("Binary Data".data(using: .utf8)!) + var data: AnyValue = .data(Data("Binary Data".utf8)) var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!) var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!) var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89)) diff --git a/Tests/AnyValueTests.swift b/Tests/AnyValueTests.swift index 62fb86ec2..f5824252e 100644 --- a/Tests/AnyValueTests.swift +++ b/Tests/AnyValueTests.swift @@ -262,6 +262,9 @@ class AnyValueTests: XCTestCase { XCTAssertEqual(try AnyValue.wrapped(Float16(1.5)), .float16(1.5)) XCTAssertEqual(try AnyValue.wrapped(Float(1.5)), .float(1.5)) XCTAssertEqual(try AnyValue.wrapped(Double(1.5)), .double(1.5)) + XCTAssertEqual(try AnyValue.wrapped(Float16(0.7)), .float16(0.7)) + XCTAssertEqual(try AnyValue.wrapped(Float(0.7)), .float(0.7)) + XCTAssertEqual(try AnyValue.wrapped(Double(0.7)), .double(0.7)) XCTAssertEqual(try AnyValue.wrapped(Decimal(1.23)), .decimal(1.23)) XCTAssertEqual(try AnyValue.wrapped(Data([1, 2, 3])), .data(Data([1, 2, 3]))) let url = URL(string: "https://example.com")! @@ -410,7 +413,7 @@ class AnyValueTests: XCTestCase { var f64: AnyValue = .double(123.4567) var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567)) var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567)) - var data: AnyValue = .data("Binary Data".data(using: .utf8)!) + var data: AnyValue = .data(Data("Binary Data".utf8)) var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!) var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!) var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89)) diff --git a/Tests/CBORAnyValueTests.swift b/Tests/CBORAnyValueTests.swift index 2d7350223..a8f27e864 100644 --- a/Tests/CBORAnyValueTests.swift +++ b/Tests/CBORAnyValueTests.swift @@ -42,7 +42,7 @@ class CBORAnyValueTests: XCTestCase { let value = try CBOR.Decoder.default.decode(TestValue.self, from: cbor) XCTAssertEqual(value.unsigned, AnyValue.int64(123)) XCTAssertEqual(value.negative, AnyValue.int64(-456)) - XCTAssertEqual(value.byteString, AnyValue.data("Binary Data".data(using: .utf8)!)) + XCTAssertEqual(value.byteString, AnyValue.data(Data("Binary Data".utf8))) XCTAssertEqual(value.utf8String, AnyValue.string("Hello World!")) XCTAssertEqual(value.array, AnyValue.array([.nil, .bool(false), .int64(456), .string("a")])) XCTAssertEqual(value.map, AnyValue.dictionary(["c": .int64(1), "a": .int64(2), "d": .int64(3), "b": .int64(4)])) @@ -84,7 +84,7 @@ class CBORAnyValueTests: XCTestCase { var pndec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567)) var npdec: AnyValue = .decimal(Decimal(sign: .minus, exponent: 1, significand: 1234567)) var nndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567)) - var data: AnyValue = .data("Binary Data".data(using: .utf8)!) + var data: AnyValue = .data(Data("Binary Data".utf8)) var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!) var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!) var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89)) @@ -146,7 +146,7 @@ class CBORAnyValueTests: XCTestCase { let cbor = Data(hexEncoded: "A16762363444617461D82270516D6C7559584A35494552686447453D") let dstValue = try CBOR.Decoder.default.decode(TestValue.self, from: cbor) - XCTAssertEqual(dstValue.b64Data, .data("Binary Data".data(using: .utf8)!)) + XCTAssertEqual(dstValue.b64Data, .data(Data("Binary Data".utf8))) } func testDecodeTaggedInt() throws { diff --git a/Tests/JSONAnyValueTests.swift b/Tests/JSONAnyValueTests.swift index 886ac7c7b..29b257dd9 100644 --- a/Tests/JSONAnyValueTests.swift +++ b/Tests/JSONAnyValueTests.swift @@ -85,7 +85,7 @@ class JSONAnyValueTests: XCTestCase { var f64: AnyValue = .double(123.4567) var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567)) var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567)) - var data: AnyValue = .data("Binary Data".data(using: .utf8)!) + var data: AnyValue = .data(Data("Binary Data".utf8)) var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!) var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!) var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89)) @@ -195,7 +195,7 @@ class JSONAnyValueTests: XCTestCase { struct TestValue: Codable { var invalid: AnyValue = .dictionary([ - .data("Bad Key".data(using: .utf8)!): "Shouldn't Work!" + .data(Data("Bad Key".utf8)): "Shouldn't Work!" ]) } diff --git a/Tests/JSONDecoderTests.swift b/Tests/JSONDecoderTests.swift index b1df9d10b..b0941470f 100644 --- a/Tests/JSONDecoderTests.swift +++ b/Tests/JSONDecoderTests.swift @@ -28,7 +28,7 @@ class JSONDecoderTests: XCTestCase { let decoder = JSON.Decoder() decoder.keyDecodingStrategy = .useDefaultKeys - XCTAssertNoThrow(try decoder.decode(TestValue.self, from: json.data(using: .utf8)!)) + XCTAssertNoThrow(try decoder.decode(TestValue.self, from: Data(json.utf8))) } func testDecodeWithDefaultKeyStrategy() { @@ -253,7 +253,7 @@ class JSONDecoderTests: XCTestCase { func testDecodeBase64Data() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data @@ -270,7 +270,7 @@ class JSONDecoderTests: XCTestCase { func testDecodeBase64DataUnpadded() throws { - let data = "1234".data(using: .utf8)! + let data = Data("1234".utf8) struct TestValue: Codable { var data: Data @@ -287,7 +287,7 @@ class JSONDecoderTests: XCTestCase { func testDecodeDeferredToData() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data @@ -304,7 +304,7 @@ class JSONDecoderTests: XCTestCase { func testDecodeCustomData() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data diff --git a/Tests/RefTests.swift b/Tests/RefTests.swift index 37f15e972..69fef4d17 100644 --- a/Tests/RefTests.swift +++ b/Tests/RefTests.swift @@ -17,10 +17,6 @@ protocol RefTestValue {} struct AValue: RefTestValue, Codable { let name: String - - init(name: String) { - self.name = name - } } struct BValue: RefTestValue, Codable { diff --git a/Tests/YAMLAnyValueTests.swift b/Tests/YAMLAnyValueTests.swift index 7d3d32111..ee3540017 100644 --- a/Tests/YAMLAnyValueTests.swift +++ b/Tests/YAMLAnyValueTests.swift @@ -82,7 +82,7 @@ class YAMLAnyValueTests: XCTestCase { var f64: AnyValue = .double(123.4567) var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567)) var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567)) - var data: AnyValue = .data("Binary Data".data(using: .utf8)!) + var data: AnyValue = .data(Data("Binary Data".utf8)) var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!) var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!) var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89)) diff --git a/Tests/YAMLDecoderTests.swift b/Tests/YAMLDecoderTests.swift index f21fb323f..b9392eb8c 100644 --- a/Tests/YAMLDecoderTests.swift +++ b/Tests/YAMLDecoderTests.swift @@ -31,7 +31,7 @@ class YAMLDecoderTests: XCTestCase { let decoder = YAML.Decoder() decoder.keyDecodingStrategy = .useDefaultKeys - XCTAssertNoThrow(try decoder.decode(TestValue.self, from: yaml.data(using: .utf8)!)) + XCTAssertNoThrow(try decoder.decode(TestValue.self, from: Data(yaml.utf8))) } func testDecodeWithDefaultKeyStrategy() { @@ -80,9 +80,7 @@ class YAMLDecoderTests: XCTestCase { """ let decoder = YAML.Decoder() - decoder.keyDecodingStrategy = .custom { _ in - return AnyCodingKey(stringValue: "kebabCased") - } + decoder.keyDecodingStrategy = .custom { _ in AnyCodingKey(stringValue: "kebabCased") } XCTAssertNoThrow(try decoder.decode(TestValue.self, from: yaml)) } @@ -255,9 +253,7 @@ class YAMLDecoderTests: XCTestCase { let decoder = YAML.Decoder() decoder.dateDecodingStrategy = .formatted(formatter) - XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { error in - AssertDecodingDataCorrupted(error) - } + XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { AssertDecodingDataCorrupted($0) } } func testDecodeBadISO8601Date() throws { @@ -277,9 +273,7 @@ class YAMLDecoderTests: XCTestCase { let decoder = YAML.Decoder() decoder.dateDecodingStrategy = .iso8601 - XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { error in - AssertDecodingDataCorrupted(error) - } + XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { AssertDecodingDataCorrupted($0) } } func testDecodeDateFromNull() throws { @@ -308,8 +302,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Date.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -319,7 +312,7 @@ class YAMLDecoderTests: XCTestCase { func testDecodeBase64Data() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data @@ -342,7 +335,7 @@ class YAMLDecoderTests: XCTestCase { func testDecodeBase64DataUnpadded() throws { - let data = "1234".data(using: .utf8)! + let data = Data("1234".utf8) struct TestValue: Codable { var data: Data @@ -365,7 +358,7 @@ class YAMLDecoderTests: XCTestCase { func testDecodeDeferredToData() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data @@ -388,7 +381,7 @@ class YAMLDecoderTests: XCTestCase { func testDecodeCustomData() throws { - let data = "Hello World!".data(using: .utf8)! + let data = Data("Hello World!".utf8) struct TestValue: Codable { var data: Data @@ -403,9 +396,7 @@ class YAMLDecoderTests: XCTestCase { """ let decoder = YAML.Decoder() - decoder.dataDecodingStrategy = .custom { decoder in - return Data(hexEncoded: try decoder.singleValueContainer().decode(String.self)) - } + decoder.dataDecodingStrategy = .custom { Data(hexEncoded: try $0.singleValueContainer().decode(String.self)) } let testValue = try decoder.decode(TestValue.self, from: yaml) XCTAssertEqual(testValue.data, data) @@ -428,9 +419,7 @@ class YAMLDecoderTests: XCTestCase { let decoder = YAML.Decoder() decoder.dataDecodingStrategy = .base64 - XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { error in - AssertDecodingDataCorrupted(error) - } + XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { AssertDecodingDataCorrupted($0) } } func testDecodeIncorrectBase64Data() throws { @@ -450,9 +439,7 @@ class YAMLDecoderTests: XCTestCase { let decoder = YAML.Decoder() decoder.dataDecodingStrategy = .base64 - XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { error in - AssertDecodingTypeMismatch(error) - } + XCTAssertThrowsError(try decoder.decode(TestValue.self, from: yaml)) { AssertDecodingTypeMismatch($0) } } func testDecodeDataFromNull() throws { @@ -481,8 +468,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Data.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -572,8 +558,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(URL.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -665,8 +650,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UUID.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -739,8 +723,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(String.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -778,9 +761,7 @@ class YAMLDecoderTests: XCTestCase { let yaml = """ - --- decimal: \(decimal) - ... """ @@ -798,9 +779,7 @@ class YAMLDecoderTests: XCTestCase { let yaml = """ - --- decimal: \(integer) - ... """ @@ -818,9 +797,7 @@ class YAMLDecoderTests: XCTestCase { let yaml = """ - --- decimal: \(integer) - ... """ @@ -836,9 +813,7 @@ class YAMLDecoderTests: XCTestCase { let yaml = """ - --- decimal: .nan - ... """ @@ -910,8 +885,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Decimal.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1098,8 +1072,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Double.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1286,8 +1259,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Float.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1474,8 +1446,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Float16.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1587,8 +1558,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(BigInt.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1682,8 +1652,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(BigUInt.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1796,8 +1765,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UInt.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -1910,8 +1878,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UInt64.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2024,8 +1991,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UInt32.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2138,8 +2104,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UInt16.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2252,8 +2217,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(UInt8.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2384,8 +2348,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Int.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2516,8 +2479,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Int64.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2648,8 +2610,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Int32.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2780,8 +2741,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Int16.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2912,8 +2872,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Int8.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in @@ -2984,8 +2943,7 @@ class YAMLDecoderTests: XCTestCase { var container = try decoder.unkeyedContainer() _ = try container.decode(Bool.self) } - func encode(to encoder: Encoder) throws { - } + func encode(to encoder: Encoder) throws {} } XCTAssertThrowsError(try YAML.Decoder.default.decodeTree(TestValue.self, from: .sequence([nil]))) { error in diff --git a/Tests/YAMLTests.swift b/Tests/YAMLTests.swift index 599c3ab7a..056822128 100644 --- a/Tests/YAMLTests.swift +++ b/Tests/YAMLTests.swift @@ -9,36 +9,39 @@ // import Foundation -@testable import PotentYAML import XCTest +@testable import PotentYAML class YAMLTests: XCTestCase { func testParse() throws { - let data = """ - test1: null - test2: ~ - test3: - test4: false - test5: true - test6: 123 - test7: 123.456 - test8: "a string" - test9: - - null - - false - - 123 - - 123.456 - - "a string in an array" - test10: - a: null - b: false - c: 123 - d: 123.456 - e: "a string in a map" - """.data(using: .utf8)! + let data = + Data( + """ + test1: null + test2: ~ + test3: + test4: false + test5: true + test6: 123 + test7: 123.456 + test8: "a string" + test9: + - null + - false + - 123 + - 123.456 + - "a string in an array" + test10: + a: null + b: false + c: 123 + d: 123.456 + e: "a string in a map" + """.utf8 + ) guard let yaml = try YAMLReader.read(data: data).first else { XCTFail("No document in stream") @@ -116,7 +119,6 @@ class YAMLTests: XCTestCase { ] ) - let values: YAML = [ "a": 1, "b": "2", @@ -175,11 +177,14 @@ class YAMLTests: XCTestCase { func testObjectKeySerializationSortedOrder() throws { - let yaml = try YAMLSerialization.string(from: [ - "c": 1, - "a": 2, - "b": 3, - ], options: .sortedKeys) + let yaml = try YAMLSerialization.string( + from: [ + "c": 1, + "a": 2, + "b": 3, + ], + options: .sortedKeys + ) XCTAssertEqual( yaml, @@ -220,18 +225,30 @@ class YAMLTests: XCTestCase { XCTAssertEqual(YAML.null().description, "null") XCTAssertEqual(YAML.null(anchor: "test").description, "&test null") - XCTAssertEqual(YAML.string("my-string", style: .plain).description, - "my-string") - XCTAssertEqual(YAML.string("my-string", style: .doubleQuoted).description, - #""my-string""#) - XCTAssertEqual(YAML.string("my-string", style: .singleQuoted).description, - #"'my-string'"#) - XCTAssertEqual(YAML.string("my-string", style: .plain, anchor: "test").description, - "&test my-string") - XCTAssertEqual(YAML.string("my-string", style: .plain, tag: .str).description, - "!tag:yaml.org,2002:str my-string") - XCTAssertEqual(YAML.string("my-string", style: .plain, tag: .str, anchor: "test").description, - "&test !tag:yaml.org,2002:str my-string") + XCTAssertEqual( + YAML.string("my-string", style: .plain).description, + "my-string" + ) + XCTAssertEqual( + YAML.string("my-string", style: .doubleQuoted).description, + #""my-string""# + ) + XCTAssertEqual( + YAML.string("my-string", style: .singleQuoted).description, + #"'my-string'"# + ) + XCTAssertEqual( + YAML.string("my-string", style: .plain, anchor: "test").description, + "&test my-string" + ) + XCTAssertEqual( + YAML.string("my-string", style: .plain, tag: .str).description, + "!tag:yaml.org,2002:str my-string" + ) + XCTAssertEqual( + YAML.string("my-string", style: .plain, tag: .str, anchor: "test").description, + "&test !tag:yaml.org,2002:str my-string" + ) XCTAssertEqual(YAML.integer(123).description, "123") XCTAssertEqual(YAML.integer(123, anchor: "test").description, "&test 123") @@ -243,23 +260,39 @@ class YAMLTests: XCTestCase { XCTAssertEqual(YAML.bool(false).description, "false") XCTAssertEqual(YAML.bool(true, anchor: "test").description, "&test true") - XCTAssertEqual(YAML.sequence([1, 2, 3]).description, - "[1, 2, 3]") - XCTAssertEqual(YAML.sequence([1, 2, 3], anchor: "test").description, - "&test [1, 2, 3]") - XCTAssertEqual(YAML.sequence([1, 2, 3], tag: .seq).description, - "!tag:yaml.org,2002:seq [1, 2, 3]") - XCTAssertEqual(YAML.sequence([1, 2, 3], tag: .seq, anchor: "test").description, - "&test !tag:yaml.org,2002:seq [1, 2, 3]") - - XCTAssertEqual(YAML.mapping(["c": 1, "a": 2, "b": 3]).description, - "{c: 1, a: 2, b: 3}") - XCTAssertEqual(YAML.mapping(["c": 1, "a": 2, "b": 3], anchor: "test").description, - "&test {c: 1, a: 2, b: 3}") - XCTAssertEqual(YAML.mapping(["c": 1, "a": 2, "b": 3], tag: .map).description, - "!tag:yaml.org,2002:map {c: 1, a: 2, b: 3}") - XCTAssertEqual(YAML.mapping(["c": 1, "a": 2, "b": 3], tag: .map, anchor: "test").description, - "&test !tag:yaml.org,2002:map {c: 1, a: 2, b: 3}") + XCTAssertEqual( + YAML.sequence([1, 2, 3]).description, + "[1, 2, 3]" + ) + XCTAssertEqual( + YAML.sequence([1, 2, 3], anchor: "test").description, + "&test [1, 2, 3]" + ) + XCTAssertEqual( + YAML.sequence([1, 2, 3], tag: .seq).description, + "!tag:yaml.org,2002:seq [1, 2, 3]" + ) + XCTAssertEqual( + YAML.sequence([1, 2, 3], tag: .seq, anchor: "test").description, + "&test !tag:yaml.org,2002:seq [1, 2, 3]" + ) + + XCTAssertEqual( + YAML.mapping(["c": 1, "a": 2, "b": 3]).description, + "{c: 1, a: 2, b: 3}" + ) + XCTAssertEqual( + YAML.mapping(["c": 1, "a": 2, "b": 3], anchor: "test").description, + "&test {c: 1, a: 2, b: 3}" + ) + XCTAssertEqual( + YAML.mapping(["c": 1, "a": 2, "b": 3], tag: .map).description, + "!tag:yaml.org,2002:map {c: 1, a: 2, b: 3}" + ) + XCTAssertEqual( + YAML.mapping(["c": 1, "a": 2, "b": 3], tag: .map, anchor: "test").description, + "&test !tag:yaml.org,2002:map {c: 1, a: 2, b: 3}" + ) } @@ -286,7 +319,7 @@ class YAMLTests: XCTestCase { "b": "Testing 1.. 2.. 3..", "c": 45.678, "d": true, - "e": nil + "e": nil, ] XCTAssertEqual( @@ -327,8 +360,10 @@ class YAMLTests: XCTestCase { ) XCTAssertEqual( - try YAMLSerialization.string(from: ["dquoted": "This should be output dquoted"], - preferredStringStyle: .doubleQuoted), + try YAMLSerialization.string( + from: ["dquoted": "This should be output dquoted"], + preferredStringStyle: .doubleQuoted + ), #""" "dquoted": "This should be output dquoted" @@ -336,8 +371,10 @@ class YAMLTests: XCTestCase { ) XCTAssertEqual( - try YAMLSerialization.string(from: ["squoted": "This should be output squoted"], - preferredStringStyle: .singleQuoted), + try YAMLSerialization.string( + from: ["squoted": "This should be output squoted"], + preferredStringStyle: .singleQuoted + ), #""" 'squoted': 'This should be output squoted' @@ -345,8 +382,10 @@ class YAMLTests: XCTestCase { ) XCTAssertEqual( - try YAMLSerialization.string(from: [.string("plain", style: .plain): "This should be output squoted"], - preferredStringStyle: .singleQuoted), + try YAMLSerialization.string( + from: [.string("plain", style: .plain): "This should be output squoted"], + preferredStringStyle: .singleQuoted + ), #""" plain: 'This should be output squoted' @@ -366,8 +405,10 @@ class YAMLTests: XCTestCase { ) XCTAssertEqual( - try YAMLSerialization.string(from: ["flow": "This should be output flow"], - preferredCollectionStyle: .flow), + try YAMLSerialization.string( + from: ["flow": "This should be output flow"], + preferredCollectionStyle: .flow + ), #""" { 'flow': 'This should be output flow' @@ -378,8 +419,10 @@ class YAMLTests: XCTestCase { XCTAssertEqual( try YAMLSerialization - .string(from: .mapping([.init(key: "block", value: "This should be output block")], style: .block), - preferredCollectionStyle: .flow), + .string( + from: .mapping([.init(key: "block", value: "This should be output block")], style: .block), + preferredCollectionStyle: .flow + ), #""" block: This should be output block @@ -390,7 +433,6 @@ class YAMLTests: XCTestCase { } - private class TestValue: Codable { let a: Int let b: String