Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Numeric convenience properties more strict in their type conversions #62

Merged
merged 1 commit into from
Feb 22, 2018
Merged
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
144 changes: 140 additions & 4 deletions Sources/MessagePack/ConvenienceProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ extension MessagePackValue {
}
}

/// The integer value if `.Int` or an appropriately valued `.UInt`, `nil` otherwise.
// MARK: Signed integer values

/// The integer value if `.int` or an appropriately valued `.uint`, `nil` otherwise.
@available(*, deprecated, message: "use int64Value: instead")
public var integerValue: Int64? {
switch self {
case .int(let value):
Expand All @@ -55,7 +58,75 @@ extension MessagePackValue {
}
}

/// The unsigned integer value if `.UInt` or positive `.Int`, `nil` otherwise.
/// The signed platform-dependent width integer value if `.int` or an
/// appropriately valued `.uint`, `nil` otherwise.
public var intValue: Int? {
switch self {
case .int(let value):
return Int(exactly: value)
case .uint(let value):
return Int(exactly: value)
default:
return nil
}
}

/// The signed 8-bit integer value if `.int` or an appropriately valued
/// `.uint`, `nil` otherwise.
public var int8Value: Int8? {
switch self {
case .int(let value):
return Int8(exactly: value)
case .uint(let value):
return Int8(exactly: value)
default:
return nil
}
}

/// The signed 16-bit integer value if `.int` or an appropriately valued
/// `.uint`, `nil` otherwise.
public var int16Value: Int16? {
switch self {
case .int(let value):
return Int16(exactly: value)
case .uint(let value):
return Int16(exactly: value)
default:
return nil
}
}

/// The signed 32-bit integer value if `.int` or an appropriately valued
/// `.uint`, `nil` otherwise.
public var int32Value: Int32? {
switch self {
case .int(let value):
return Int32(exactly: value)
case .uint(let value):
return Int32(exactly: value)
default:
return nil
}
}

/// The signed 64-bit integer value if `.int` or an appropriately valued
/// `.uint`, `nil` otherwise.
public var int64Value: Int64? {
switch self {
case .int(let value):
return value
case .uint(let value):
return Int64(exactly: value)
default:
return nil
}
}

// MARK: Unsigned integer values

/// The unsigned integer value if `.uint` or positive `.int`, `nil` otherwise.
@available(*, deprecated, message: "use uint64Value: instead")
public var unsignedIntegerValue: UInt64? {
switch self {
case .int(let value) where value >= 0:
Expand All @@ -67,6 +138,71 @@ extension MessagePackValue {
}
}

/// The unsigned platform-dependent width integer value if `.uint` or an
/// appropriately valued `.int`, `nil` otherwise.
public var uintValue: UInt? {
switch self {
case .int(let value):
return UInt(exactly: value)
case .uint(let value):
return UInt(exactly: value)
default:
return nil
}
}

/// The unsigned 8-bit integer value if `.uint` or an appropriately valued
/// `.int`, `nil` otherwise.
public var uint8Value: UInt8? {
switch self {
case .int(let value):
return UInt8(exactly: value)
case .uint(let value):
return UInt8(exactly: value)
default:
return nil
}
}

/// The unsigned 16-bit integer value if `.uint` or an appropriately valued
/// `.int`, `nil` otherwise.
public var uint16Value: UInt16? {
switch self {
case .int(let value):
return UInt16(exactly: value)
case .uint(let value):
return UInt16(exactly: value)
default:
return nil
}
}

/// The unsigned 32-bit integer value if `.uint` or an appropriately valued
/// `.int`, `nil` otherwise.
public var uint32Value: UInt32? {
switch self {
case .int(let value):
return UInt32(exactly: value)
case .uint(let value):
return UInt32(exactly: value)
default:
return nil
}
}

/// The unsigned 64-bit integer value if `.uint` or an appropriately valued
/// `.int`, `nil` otherwise.
public var uint64Value: UInt64? {
switch self {
case .int(let value):
return UInt64(exactly: value)
case .uint(let value):
return value
default:
return nil
}
}

/// The contained array if `.Array`, `nil` otherwise.
public var arrayValue: [MessagePackValue]? {
switch self {
Expand All @@ -93,7 +229,7 @@ extension MessagePackValue {
case .float(let value):
return value
case .double(let value):
return Float(value)
return Float(exactly: value)
default:
return nil
}
Expand All @@ -103,7 +239,7 @@ extension MessagePackValue {
public var doubleValue: Double? {
switch self {
case .float(let value):
return Double(value)
return Double(exactly: value)
case .double(let value):
return value
default:
Expand Down
114 changes: 101 additions & 13 deletions Tests/MessagePackTests/ConveniencePropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ class ConveniencePropertiesTests: XCTestCase {
("testIndexedSubscript", testIndexedSubscript),
("testKeyedSubscript", testKeyedSubscript),
("testIsNil", testIsNil),
("testIntegerValue", testIntegerValue),
("testUnsignedIntegerValue", testUnsignedIntegerValue),
("testIntValue", testIntValue),
("testInt8Value", testInt8Value),
("testInt16Value", testInt16Value),
("testIn32Value", testInt32Value),
("testInt64Value", testInt64Value),
("testUIntValue", testUIntValue),
("testUInt8Value", testUInt8Value),
("testUInt16Value", testUInt16Value),
("testUInt32Value", testUInt32Value),
("testUInt64Value", testUInt64Value),
("testArrayValue", testArrayValue),
("testBoolValue", testBoolValue),
("testFloatValue", testFloatValue),
Expand Down Expand Up @@ -46,17 +54,98 @@ class ConveniencePropertiesTests: XCTestCase {
XCTAssertFalse(MessagePackValue.bool(true).isNil)
}

func testIntegerValue() {
XCTAssert(MessagePackValue.int(-1).integerValue == -1)
XCTAssert(MessagePackValue.uint(1).integerValue == 1)
XCTAssert(MessagePackValue.nil.integerValue == nil)
func testIntValue() {
XCTAssert(MessagePackValue.int(-1).intValue == -1)
XCTAssert(MessagePackValue.uint(1).intValue == 1)
XCTAssertNil(MessagePackValue.nil.intValue)
}

func testUnsignedIntegerValue() {
XCTAssert(MessagePackValue.int(-1).unsignedIntegerValue == nil)
XCTAssert(MessagePackValue.int(1).unsignedIntegerValue == 1)
XCTAssert(MessagePackValue.uint(1).unsignedIntegerValue == 1)
XCTAssert(MessagePackValue.nil.unsignedIntegerValue == nil)
func testInt8Value() {
XCTAssert(MessagePackValue.int(-1).int8Value == -1)
XCTAssert(MessagePackValue.int(1).int8Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(Int8.min) - 1).int8Value)
XCTAssertNil(MessagePackValue.int(Int64(Int8.max) + 1).int8Value)

XCTAssert(MessagePackValue.uint(1).int8Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(Int8.max) + 1).int8Value)
XCTAssertNil(MessagePackValue.nil.int8Value)
}

func testInt16Value() {
XCTAssert(MessagePackValue.int(-1).int16Value == -1)
XCTAssert(MessagePackValue.int(1).int16Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(Int16.min) - 1).int16Value)
XCTAssertNil(MessagePackValue.int(Int64(Int16.max) + 1).int16Value)

XCTAssert(MessagePackValue.uint(1).int16Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(Int16.max) + 1).int16Value)
XCTAssertNil(MessagePackValue.nil.int16Value)
}

func testInt32Value() {
XCTAssert(MessagePackValue.int(-1).int32Value == -1)
XCTAssert(MessagePackValue.int(1).int32Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(Int32.min) - 1).int32Value)
XCTAssertNil(MessagePackValue.int(Int64(Int32.max) + 1).int32Value)

XCTAssert(MessagePackValue.uint(1).int32Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(Int32.max) + 1).int32Value)
XCTAssertNil(MessagePackValue.nil.int32Value)
}

func testInt64Value() {
XCTAssert(MessagePackValue.int(-1).int64Value == -1)
XCTAssert(MessagePackValue.int(1).int64Value == 1)

XCTAssert(MessagePackValue.uint(1).int64Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(Int64.max) + 1).int64Value)
XCTAssertNil(MessagePackValue.nil.int64Value)
}

func testUIntValue() {
XCTAssert(MessagePackValue.uint(1).uintValue == 1)

XCTAssertNil(MessagePackValue.int(-1).uintValue)
XCTAssert(MessagePackValue.int(1).uintValue == 1)
XCTAssertNil(MessagePackValue.nil.uintValue)
}

func testUInt8Value() {
XCTAssert(MessagePackValue.uint(1).uint8Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(UInt8.max) + 1).uint8Value)

XCTAssertNil(MessagePackValue.int(-1).uint8Value)
XCTAssert(MessagePackValue.int(1).uint8Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(UInt8.max) + 1).uint8Value)
XCTAssertNil(MessagePackValue.nil.uint8Value)
}

func testUInt16Value() {
XCTAssert(MessagePackValue.uint(1).uint16Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(UInt16.max) + 1).uint16Value)

XCTAssertNil(MessagePackValue.int(-1).uint16Value)
XCTAssert(MessagePackValue.int(1).uint16Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(UInt16.max) + 1).uint16Value)
XCTAssertNil(MessagePackValue.nil.uint16Value)
}

func testUInt32Value() {
XCTAssert(MessagePackValue.uint(1).uint32Value == 1)
XCTAssertNil(MessagePackValue.uint(UInt64(UInt32.max) + 1).uint32Value)

XCTAssertNil(MessagePackValue.int(-1).uint32Value)
XCTAssert(MessagePackValue.int(1).uint32Value == 1)
XCTAssertNil(MessagePackValue.int(Int64(UInt32.max) + 1).uint32Value)
XCTAssertNil(MessagePackValue.nil.uint32Value)
}

func testUInt64Value() {
XCTAssert(MessagePackValue.uint(1).uint64Value == 1)

XCTAssertNil(MessagePackValue.int(-1).uint64Value)
XCTAssert(MessagePackValue.int(1).uint64Value == 1)
XCTAssertNil(MessagePackValue.nil.uint8Value)
}

func testArrayValue() {
Expand All @@ -80,8 +169,7 @@ class ConveniencePropertiesTests: XCTestCase {
XCTAssertEqual(floatValue!, 3.14, accuracy: 0.0001)

floatValue = MessagePackValue.double(3.14).floatValue
XCTAssertNotNil(floatValue)
XCTAssertEqual(floatValue!, 3.14, accuracy: 0.0001)
XCTAssertNil(floatValue)
}

func testDoubleValue() {
Expand Down