Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix warning for hash value and update swift-tools-version to 5.3 #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
10 changes: 7 additions & 3 deletions Sources/MessagePack/AnyCodingKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct AnyCodingKey: CodingKey, Equatable {
self.intValue = intValue
}

init<Key>(_ base: Key) where Key : CodingKey {
init<Key>(_ base: Key) where Key: CodingKey {
if let intValue = base.intValue {
self.init(intValue: intValue)!
} else {
Expand All @@ -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)
}
}
}
6 changes: 3 additions & 3 deletions Tests/MessagePackTests/Airport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 27 additions & 24 deletions Tests/MessagePackTests/MessagePackDecodingTests.swift
Original file line number Diff line number Diff line change
@@ -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<T>(_ 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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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])
Expand Down
40 changes: 20 additions & 20 deletions Tests/MessagePackTests/MessagePackEncodingTests.swift
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
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() {
let letters = "abcdefghijklmnopqrstuvwxyz".unicodeScalars
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 = [
Expand Down
2 changes: 1 addition & 1 deletion Tests/MessagePackTests/MessagePackPerformanceTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
@testable import MessagePack
import XCTest

class MessagePackPerformanceTests: XCTestCase {
var encoder: MessagePackEncoder!
Expand Down
Loading