diff --git a/Package.swift b/Package.swift index ba984af..716af88 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.0 +// swift-tools-version:5.3 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/README.md b/README.md index 19f1df3..583e9a3 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ let value = try! encoder.encode(["a": 1, "b": 2, "c": 3]) import MessagePack let decoder = MessagePackDecoder() -let data = Data(bytes: [0xCB, 0x40, 0x09, 0x21, 0xF9, 0xF0, 0x1B, 0x86, 0x6E]) +let data = Data([0xCB, 0x40, 0x09, 0x21, 0xF9, 0xF0, 0x1B, 0x86, 0x6E]) let value = try! decoder.decode(Double.self, from: data) // 3.14159 ``` diff --git a/Sources/MessagePack/AnyCodingKey.swift b/Sources/MessagePack/AnyCodingKey.swift index 6074769..9886e9d 100644 --- a/Sources/MessagePack/AnyCodingKey.swift +++ b/Sources/MessagePack/AnyCodingKey.swift @@ -12,7 +12,7 @@ struct AnyCodingKey: CodingKey, Equatable { self.intValue = intValue } - init(_ base: Key) where Key : CodingKey { + init(_ base: Key) where Key: CodingKey { if let intValue = base.intValue { self.init(intValue: intValue)! } else { @@ -22,7 +22,11 @@ struct AnyCodingKey: CodingKey, Equatable { } extension AnyCodingKey: Hashable { - var hashValue: Int { - return self.intValue?.hashValue ?? self.stringValue.hashValue + public func hash(into hasher: inout Hasher) { + if let intValue = self.intValue { + intValue.hash(into: &hasher) + } else { + self.stringValue.hash(into: &hasher) + } } } diff --git a/Tests/MessagePackTests/Airport.swift b/Tests/MessagePackTests/Airport.swift index b39bf95..a6b8eab 100644 --- a/Tests/MessagePackTests/Airport.swift +++ b/Tests/MessagePackTests/Airport.swift @@ -8,16 +8,16 @@ struct Airport: Codable, Equatable { enum Surface: String, Codable, Equatable { case rigid, flexible, gravel, sealed, unpaved, other } - + let direction: String let distance: Int let surface: Surface } - + let runways: [Runway] let instrumentApproachProcedures: [String] - + static var example: Airport { return Airport( name: "Portland International Airport", diff --git a/Tests/MessagePackTests/MessagePackDecodingTests.swift b/Tests/MessagePackTests/MessagePackDecodingTests.swift index 01c62ef..2f42338 100644 --- a/Tests/MessagePackTests/MessagePackDecodingTests.swift +++ b/Tests/MessagePackTests/MessagePackDecodingTests.swift @@ -1,20 +1,23 @@ -import XCTest @testable import MessagePack +import XCTest class MessagePackDecodingTests: XCTestCase { var decoder: MessagePackDecoder! override func setUp() { - self.decoder = MessagePackDecoder() + decoder = MessagePackDecoder() } func assertTypeMismatch(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, - line: UInt = #line) -> Any.Type? { + line: UInt = #line) -> Any.Type? + { var error: Error? - XCTAssertThrowsError(expression, message, - file: file, line: line) { + XCTAssertThrowsError(try expression(), + message(), + file: file, + line: line) { error = $0 } guard case .typeMismatch(let type, _) = error as? DecodingError else { @@ -25,49 +28,49 @@ class MessagePackDecodingTests: XCTestCase { } func testDecodeNil() { - let data = Data(bytes: [0xC0]) + let data = Data([0xc0]) let value = try! decoder.decode(Int?.self, from: data) XCTAssertNil(value) } func testDecodeFalse() { - let data = Data(bytes: [0xc2]) + let data = Data([0xc2]) let value = try! decoder.decode(Bool.self, from: data) XCTAssertEqual(value, false) } func testDecodeTrue() { - let data = Data(bytes: [0xc3]) + let data = Data([0xc3]) let value = try! decoder.decode(Bool.self, from: data) XCTAssertEqual(value, true) } func testDecodeInt() { - let data = Data(bytes: [0x2A]) + let data = Data([0x2a]) let value = try! decoder.decode(Int.self, from: data) XCTAssertEqual(value, 42) } func testDecodeNegativeInt() { - let data = Data(bytes: [0xFF]) + let data = Data([0xff]) let value = try! decoder.decode(Int.self, from: data) XCTAssertEqual(value, -1) } func testDecodeUInt() { - let data = Data(bytes: [0xCC, 0x80]) + let data = Data([0xcc, 0x80]) let value = try! decoder.decode(Int.self, from: data) XCTAssertEqual(value, 128) } func testDecodeFloat() { - let data = Data(bytes: [0xCA, 0x40, 0x48, 0xF5, 0xC3]) + let data = Data([0xca, 0x40, 0x48, 0xf5, 0xc3]) let value = try! decoder.decode(Float.self, from: data) XCTAssertEqual(value, 3.14) } func testDecodeFloatToDouble() { - let data = Data(bytes: [0xCA, 0x40, 0x48, 0xF5, 0xC3]) + let data = Data([0xca, 0x40, 0x48, 0xf5, 0xc3]) let type = assertTypeMismatch(try decoder.decode(Double.self, from: data)) XCTAssertTrue(type is Double.Type) decoder.nonMatchingFloatDecodingStrategy = .cast @@ -76,13 +79,13 @@ class MessagePackDecodingTests: XCTestCase { } func testDecodeDouble() { - let data = Data(bytes: [0xCB, 0x40, 0x09, 0x21, 0xF9, 0xF0, 0x1B, 0x86, 0x6E]) + let data = Data([0xcb, 0x40, 0x09, 0x21, 0xf9, 0xf0, 0x1b, 0x86, 0x6e]) let value = try! decoder.decode(Double.self, from: data) XCTAssertEqual(value, 3.14159) } func testDecodeDoubleToFloat() { - let data = Data(bytes: [0xCB, 0x40, 0x09, 0x21, 0xF9, 0xF0, 0x1B, 0x86, 0x6E]) + let data = Data([0xcb, 0x40, 0x09, 0x21, 0xf9, 0xf0, 0x1b, 0x86, 0x6e]) let type = assertTypeMismatch(try decoder.decode(Float.self, from: data)) XCTAssertTrue(type is Float.Type) decoder.nonMatchingFloatDecodingStrategy = .cast @@ -91,59 +94,59 @@ class MessagePackDecodingTests: XCTestCase { } func testDecodeFixedArray() { - let data = Data(bytes: [0x93, 0x01, 0x02, 0x03]) + let data = Data([0x93, 0x01, 0x02, 0x03]) let value = try! decoder.decode([Int].self, from: data) XCTAssertEqual(value, [1, 2, 3]) } func testDecodeVariableArray() { - let data = Data(bytes: [0xdc] + [0x00, 0x10] + Array(0x01...0x10)) + let data = Data([0xdc] + [0x00, 0x10] + Array(0x01...0x10)) let value = try! decoder.decode([Int].self, from: data) XCTAssertEqual(value, Array(1...16)) } func testDecodeFixedDictionary() { - let data = Data(bytes: [0x83, 0xA1, 0x62, 0x02, 0xA1, 0x61, 0x01, 0xA1, 0x63, 0x03]) + let data = Data([0x83, 0xa1, 0x62, 0x02, 0xa1, 0x61, 0x01, 0xa1, 0x63, 0x03]) let value = try! decoder.decode([String: Int].self, from: data) XCTAssertEqual(value, ["a": 1, "b": 2, "c": 3]) } func testDecodeData() { - let data = Data(bytes: [0xC4, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]) + let data = Data([0xc4, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f]) let value = try! decoder.decode(Data.self, from: data) XCTAssertEqual(value, "hello".data(using: .utf8)) } func testDecodeDate() { - let data = Data(bytes: [0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01]) + let data = Data([0xd6, 0xff, 0x00, 0x00, 0x00, 0x01]) let date = Date(timeIntervalSince1970: 1) let value = try! decoder.decode(Date.self, from: data) XCTAssertEqual(value, date) } func testDecodeDistantPast() { - let data = Data(bytes: [0xC7, 0x0C, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF1, 0x88, 0x6B, 0x66, 0x00]) + let data = Data([0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf1, 0x88, 0x6b, 0x66, 0x00]) let date = Date.distantPast let value = try! decoder.decode(Date.self, from: data) XCTAssertEqual(value, date) } func testDecodeDistantFuture() { - let data = Data(bytes: [0xC7, 0x0C, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xEC, 0x31, 0x88, 0x00]) + let data = Data([0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xec, 0x31, 0x88, 0x00]) let date = Date.distantFuture let value = try! decoder.decode(Date.self, from: data) XCTAssertEqual(value, date) } func testDecodeArrayWithDate() { - let data = Data(bytes: [0x91, 0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01]) + let data = Data([0x91, 0xd6, 0xff, 0x00, 0x00, 0x00, 0x01]) let date = Date(timeIntervalSince1970: 1) let value = try! decoder.decode([Date].self, from: data) XCTAssertEqual(value, [date]) } func testDecodeDictionaryWithDate() { - let data = Data(bytes: [0x81, 0xA1, 0x31, 0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01]) + let data = Data([0x81, 0xa1, 0x31, 0xd6, 0xff, 0x00, 0x00, 0x00, 0x01]) let date = Date(timeIntervalSince1970: 1) let value = try! decoder.decode([String: Date].self, from: data) XCTAssertEqual(value, ["1": date]) diff --git a/Tests/MessagePackTests/MessagePackEncodingTests.swift b/Tests/MessagePackTests/MessagePackEncodingTests.swift index 1e57684..9671130 100644 --- a/Tests/MessagePackTests/MessagePackEncodingTests.swift +++ b/Tests/MessagePackTests/MessagePackEncodingTests.swift @@ -1,66 +1,66 @@ -import XCTest @testable import MessagePack +import XCTest class MessagePackEncodingTests: XCTestCase { var encoder: MessagePackEncoder! override func setUp() { - self.encoder = MessagePackEncoder() + encoder = MessagePackEncoder() } func testEncodeNil() { let value = try! encoder.encode(nil as Int?) - XCTAssertEqual(value, Data(bytes: [0xc0])) + XCTAssertEqual(value, Data([0xc0])) } func testEncodeFalse() { let value = try! encoder.encode(false) - XCTAssertEqual(value, Data(bytes: [0xc2])) + XCTAssertEqual(value, Data([0xc2])) } func testEncodeTrue() { let value = try! encoder.encode(true) - XCTAssertEqual(value, Data(bytes: [0xc3])) + XCTAssertEqual(value, Data([0xc3])) } func testEncodeInt() { let value = try! encoder.encode(42 as Int) - XCTAssertEqual(value, Data(bytes: [0x2A])) + XCTAssertEqual(value, Data([0x2a])) } func testEncodeUInt() { let value = try! encoder.encode(128 as UInt) - XCTAssertEqual(value, Data(bytes: [0xCC, 0x80])) + XCTAssertEqual(value, Data([0xcc, 0x80])) } func testEncodeFloat() { let value = try! encoder.encode(3.14 as Float) - XCTAssertEqual(value, Data(bytes: [0xCA, 0x40, 0x48, 0xF5, 0xC3])) + XCTAssertEqual(value, Data([0xca, 0x40, 0x48, 0xf5, 0xc3])) } func testEncodeDouble() { let value = try! encoder.encode(3.14159 as Double) - XCTAssertEqual(value, Data(bytes: [0xCB, 0x40, 0x09, 0x21, 0xF9, 0xF0, 0x1B, 0x86, 0x6E])) + XCTAssertEqual(value, Data([0xcb, 0x40, 0x09, 0x21, 0xf9, 0xf0, 0x1b, 0x86, 0x6e])) } func testEncodeString() { let value = try! encoder.encode("hello") - XCTAssertEqual(value, Data(bytes: [0xA5, 0x68, 0x65, 0x6C, 0x6C, 0x6F])) + XCTAssertEqual(value, Data([0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f])) } func testEncodeFixedArray() { let value = try! encoder.encode([1, 2, 3]) - XCTAssertEqual(value, Data(bytes: [0x93, 0x01, 0x02, 0x03])) + XCTAssertEqual(value, Data([0x93, 0x01, 0x02, 0x03])) } func testEncodeVariableArray() { let value = try! encoder.encode(Array(1...16)) - XCTAssertEqual(value, Data(bytes: [0xdc] + [0x00, 0x10] + Array(0x01...0x10))) + XCTAssertEqual(value, Data([0xdc] + [0x00, 0x10] + Array(0x01...0x10))) } func testEncodeFixedDictionary() { let value = try! encoder.encode(["a": 1]) - XCTAssertEqual(value, Data(bytes: [0x81, 0xA1, 0x61, 0x01])) + XCTAssertEqual(value, Data([0x81, 0xa1, 0x61, 0x01])) } func testEncodeVariableDictionary() { @@ -68,43 +68,43 @@ class MessagePackEncodingTests: XCTestCase { let dictionary = Dictionary(uniqueKeysWithValues: zip(letters.map { String($0) }, 1...26)) let value = try! encoder.encode(dictionary) XCTAssertEqual(value.count, 81) - XCTAssert(value.starts(with: [0xde] + [0x00, 0x1A])) + XCTAssert(value.starts(with: [0xde] + [0x00, 0x1a])) } func testEncodeData() { let data = "hello".data(using: .utf8) let value = try! encoder.encode(data) - XCTAssertEqual(value, Data(bytes: [0xC4, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F])) + XCTAssertEqual(value, Data([0xc4, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f])) } func testEncodeDate() { let date = Date(timeIntervalSince1970: 1) let value = try! encoder.encode(date) - XCTAssertEqual(value, Data(bytes: [0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01])) + XCTAssertEqual(value, Data([0xd6, 0xff, 0x00, 0x00, 0x00, 0x01])) } func testEncodeDistantPast() { let date = Date.distantPast let value = try! encoder.encode(date) - XCTAssertEqual(value, Data(bytes: [0xC7, 0x0C, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF1, 0x88, 0x6B, 0x66, 0x00])) + XCTAssertEqual(value, Data([0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf1, 0x88, 0x6b, 0x66, 0x00])) } func testEncodeDistantFuture() { let date = Date.distantFuture let value = try! encoder.encode(date) - XCTAssertEqual(value, Data(bytes: [0xC7, 0x0C, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xEC, 0x31, 0x88, 0x00])) + XCTAssertEqual(value, Data([0xc7, 0x0c, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xec, 0x31, 0x88, 0x00])) } func testEncodeArrayWithDate() { let date = Date(timeIntervalSince1970: 1) let value = try! encoder.encode([date]) - XCTAssertEqual(value, Data(bytes: [0x91, 0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01])) + XCTAssertEqual(value, Data([0x91, 0xd6, 0xff, 0x00, 0x00, 0x00, 0x01])) } func testEncodeDictionaryWithDate() { let date = Date(timeIntervalSince1970: 1) let value = try! encoder.encode(["1": date]) - XCTAssertEqual(value, Data(bytes: [0x81, 0xA1, 0x31, 0xD6, 0xFF, 0x00, 0x00, 0x00, 0x01])) + XCTAssertEqual(value, Data([0x81, 0xa1, 0x31, 0xd6, 0xff, 0x00, 0x00, 0x00, 0x01])) } static var allTests = [ diff --git a/Tests/MessagePackTests/MessagePackPerformanceTests.swift b/Tests/MessagePackTests/MessagePackPerformanceTests.swift index b57a351..a0a0132 100644 --- a/Tests/MessagePackTests/MessagePackPerformanceTests.swift +++ b/Tests/MessagePackTests/MessagePackPerformanceTests.swift @@ -1,5 +1,5 @@ -import XCTest @testable import MessagePack +import XCTest class MessagePackPerformanceTests: XCTestCase { var encoder: MessagePackEncoder! diff --git a/Tests/MessagePackTests/MessagePackRoundTripTests.swift b/Tests/MessagePackTests/MessagePackRoundTripTests.swift index a0304c7..37777d7 100644 --- a/Tests/MessagePackTests/MessagePackRoundTripTests.swift +++ b/Tests/MessagePackTests/MessagePackRoundTripTests.swift @@ -1,13 +1,13 @@ -import XCTest @testable import MessagePack +import XCTest class MessagePackRoundTripTests: XCTestCase { var encoder: MessagePackEncoder! var decoder: MessagePackDecoder! - + override func setUp() { - self.encoder = MessagePackEncoder() - self.decoder = MessagePackDecoder() + encoder = MessagePackEncoder() + decoder = MessagePackDecoder() } func testRoundTripAirport() { @@ -56,46 +56,46 @@ class MessagePackRoundTripTests: XCTestCase { encoded.append(Int(n)) } - let data = Data(bytes: bytes) + let data = Data(bytes) let decoded = try! decoder.decode([Int].self, from: data) XCTAssertEqual(encoded, decoded) } func testRoundTripDictionary() { let (a, z): (UInt8, UInt8) = (0x61, 0x7a) - var bytes: [UInt8] = [0xde, 0x00, 0x1A] + var bytes: [UInt8] = [0xde, 0x00, 0x1a] var encoded: [String: Int] = [:] for n in a...z { - bytes.append(contentsOf: [0xA1, n, n]) + bytes.append(contentsOf: [0xa1, n, n]) encoded[String(Unicode.Scalar(n))] = Int(n) } - let data = Data(bytes: bytes) + let data = Data(bytes) let decoded = try! decoder.decode([String: Int].self, from: data) XCTAssertEqual(encoded, decoded) } - + func testRoundTripDate() { - var bytes: [UInt8] = [0xD6, 0xFF] + var bytes: [UInt8] = [0xd6, 0xff] let dateComponents = DateComponents(year: 2018, month: 4, day: 20) let encoded = Calendar.current.date(from: dateComponents)! - + let secondsSince1970 = UInt32(encoded.timeIntervalSince1970) bytes.append(contentsOf: secondsSince1970.bytes) - - let data = Data(bytes: bytes) + + let data = Data(bytes) let decoded = try! decoder.decode(Date.self, from: data) XCTAssertEqual(encoded, decoded) } func testRoundTripDateWithNanoseconds() { let encoded = Date() - let data = try! self.encoder.encode(encoded) - let decoded = try! self.decoder.decode(Date.self, from: data) + let data = try! encoder.encode(encoded) + let decoded = try! decoder.decode(Date.self, from: data) XCTAssertEqual(encoded.timeIntervalSinceReferenceDate, decoded.timeIntervalSinceReferenceDate, accuracy: 0.0001) } - + static var allTests = [ ("testRoundTripAirport", testRoundTripAirport), ("testRoundTripArray", testRoundTripArray),