Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added more unit tests for float and int when converting from String. …
…Also updated Int code to ensure that "3.14" returns nil (to be more inlines with Int("3.14")), and to return 0 for intValue.
  • Loading branch information
Causaelity authored and Causaelity committed Nov 15, 2017
commit ea5cdae97371fc3e7f87f58c79d0e3eb09fa34ae
14 changes: 14 additions & 0 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ extension JSON {

public var int: Int? {
get {
if let num = self.number as? NSDecimalNumber {
let returnInt = num.intValue
if num.isEqual(returnInt){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration. (opening_brace)

return returnInt
}
return nil
}
return self.number?.intValue
}
set {
Expand All @@ -1080,6 +1087,13 @@ extension JSON {

public var intValue: Int {
get {
if let num = self.numberValue as? NSDecimalNumber {
let returnInt = num.intValue
if num.isEqual(returnInt) {
return returnInt
}
return 0
}
return self.numberValue.intValue
}
set {
Expand Down
30 changes: 30 additions & 0 deletions Tests/SwiftyJSONTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.doubleValue, 9876543210.123456789)
XCTAssertEqual(json.numberValue, 9876543210.123456789)
XCTAssertEqual(json.stringValue, "9876543210.123456789")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json.string = "swift"
XCTAssertNil(json.double)
XCTAssertEqual(json.doubleValue, 0)

}

func testFloat() {
Expand All @@ -128,6 +133,17 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.float!, -98766.23)
XCTAssertEqual(json.floatValue, -98766.23)
XCTAssertEqual(json.numberValue, NSNumber(value: -98766.23))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "9876543210.123456789"
XCTAssertEqual(json.float!, 9876543210.123456789)
XCTAssertEqual(json.floatValue, 9876543210.123456789)
XCTAssertEqual(json.numberValue, 9876543210.123456789)
XCTAssertEqual(json.stringValue, "9876543210.123456789")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json.string = "swift"
XCTAssertNil(json.float)
XCTAssertEqual(json.floatValue, 0)

}

func testInt() {
Expand All @@ -153,6 +169,20 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.int!, 98765421)
XCTAssertEqual(json.intValue, 98765421)
XCTAssertEqual(json.numberValue, NSNumber(value: 98765421))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "9876543210"
XCTAssertEqual(json.int!, 9876543210)
XCTAssertEqual(json.intValue, 9876543210)
XCTAssertEqual(json.numberValue, 9876543210)
XCTAssertEqual(json.stringValue, "9876543210")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "3.14"
XCTAssertNil(json.int)
XCTAssertEqual(json.intValue, 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "swift"
XCTAssertNil(json.int)
XCTAssertEqual(json.intValue, 0)
}

func testUInt() {
Expand Down