Skip to content

Commit

Permalink
Tolerate Swift 5.1 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 committed Jul 16, 2019
1 parent f2612ea commit f9b4754
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1
5.0.1
47 changes: 34 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
# Travis CI build file.

# whitelist (branches that should be built)
branches:
only:
- master
- /^issue.*$/

# the matrix of builds should cover each combination of Swift version
# and platform that is supported. The version of Swift used is specified
# by .swift-version, unless SWIFT_SNAPSHOT is specified.
matrix:
include:
- os: linux
dist: trusty
dist: xenial
sudo: required
services: docker
env: DOCKER_IMAGE=swift:4.0.3 SWIFT_SNAPSHOT=4.0.3
- os: linux
dist: trusty
dist: xenial
sudo: required
env: SWIFT_SNAPSHOT=4.0.3
services: docker
env: DOCKER_IMAGE=swift:4.1.3 SWIFT_SNAPSHOT=4.1.3
- os: linux
dist: trusty
dist: xenial
sudo: required
env: DOCKER_IMAGE=ubuntu:16.04 SWIFT_SNAPSHOT=4.2.1
# - os: linux
# dist: trusty
# sudo: required
# env: DOCKER_IMAGE=ubuntu:16.04 SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT
- os: osx
osx_image: xcode8.3
services: docker
env: DOCKER_IMAGE=swift:4.2.4 SWIFT_SNAPSHOT=4.2.4
- os: linux
dist: xenial
sudo: required
services: docker
env: DOCKER_IMAGE=swift:5.0.1-xenial
- os: linux
dist: xenial
sudo: required
services: docker
env: DOCKER_IMAGE=swift:5.0.1 SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT
- os: osx
osx_image: xcode9.2
sudo: required
env: SWIFT_SNAPSHOT=4.0.3
- os: osx
osx_image: xcode9.4
sudo: required
env: SWIFT_SNAPSHOT=4.1.2
env: SWIFT_SNAPSHOT=4.1.2 JAZZY_ELIGIBLE=true
- os: osx
osx_image: xcode10.1
sudo: required
env: SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT
env: SWIFT_SNAPSHOT=4.2.1
- os: osx
osx_image: xcode10.2
sudo: required
# Pending Travis Xcode 11 image
# - os: osx
# osx_image: xcode11
# sudo: required
# env: SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT

before_install:
- git clone https://github.com/IBM-Swift/Package-Builder.git
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
/**
* Copyright IBM Corporation 2016, 2017
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,9 @@ func ==(lhs: NSNumber, rhs: NSNumber) -> Bool {
func !=(lhs: NSNumber, rhs: NSNumber) -> Bool {
return !(lhs == rhs)
}

#if os(Linux) && swift(>=5.1)
// TODO: why must this be excluded, only on Linux, with Swift 5.1?
#else
func <(lhs: NSNumber, rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
Expand All @@ -1610,7 +1612,7 @@ func <(lhs: NSNumber, rhs: NSNumber) -> Bool {
return lhs.compare(rhs) == ComparisonResult.orderedAscending
}
}

#endif
func >(lhs: NSNumber, rhs: NSNumber) -> Bool {

switch (lhs.isBool, rhs.isBool) {
Expand Down
17 changes: 13 additions & 4 deletions Tests/SwiftyJSONTests/BaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ class BaseTests: XCTestCase {
break
}

#if !swift(>=5.0)
// Invalid as of Swift 5, as dictionary order is not predictable
let index = 0
let keys = Array(json[1].dictionaryObject!.keys)
for (aKey, aJson) in json[1] {
XCTAssertEqual(aKey, keys[index])
XCTAssertEqual(aJson, json[1][keys[index]])
break
}
#endif
}

func testJSONNumberCompare() {
Expand All @@ -195,10 +198,16 @@ class BaseTests: XCTestCase {
XCTAssertEqual(JSON(999.9823).stringValue, "999.9823")
XCTAssertEqual(JSON(true).number!.stringValue, "1")
XCTAssertEqual(JSON(false).number!.stringValue, "0")
XCTAssertEqual(JSON("hello").numberValue.stringValue, "0")
XCTAssertEqual(JSON(NSNull()).numberValue.stringValue, "0")
XCTAssertEqual(JSON(["a","b","c","d"]).numberValue.stringValue, "0")
XCTAssertEqual(JSON(["a":"b","c":"d"]).numberValue.stringValue, "0")
#if os(Linux) && swift(>=4.2)
// https://github.com/apple/swift-corelibs-foundation/pull/1724
let expectedValue = "0.0"
#else
let expectedValue = "0"
#endif
XCTAssertEqual(JSON("hello").numberValue.stringValue, expectedValue)
XCTAssertEqual(JSON(NSNull()).numberValue.stringValue, expectedValue)
XCTAssertEqual(JSON(["a","b","c","d"]).numberValue.stringValue, expectedValue)
XCTAssertEqual(JSON(["a":"b","c":"d"]).numberValue.stringValue, expectedValue)
}

func testNumberPrint(){
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftyJSONTests/LiteralConvertibleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LiteralConvertibleTests: XCTestCase {
// END OF GENERATED CODE

func testNumber() {
var json:JSON = 1234567890.876623
let json:JSON = 1234567890.876623
XCTAssertEqual(json.int!, 1234567890)
XCTAssertEqual(json.intValue, 1234567890)
XCTAssertEqual(json.double!, 1234567890.876623)
Expand All @@ -51,16 +51,16 @@ class LiteralConvertibleTests: XCTestCase {
}

func testBool() {
var jsonTrue:JSON = true
let jsonTrue:JSON = true
XCTAssertEqual(jsonTrue.bool!, true)
XCTAssertEqual(jsonTrue.boolValue, true)
var jsonFalse:JSON = false
let jsonFalse:JSON = false
XCTAssertEqual(jsonFalse.bool!, false)
XCTAssertEqual(jsonFalse.boolValue, false)
}

func testString() {
var json:JSON = "abcd efg, HIJK;LMn"
let json:JSON = "abcd efg, HIJK;LMn"
XCTAssertEqual(json.string!, "abcd efg, HIJK;LMn")
XCTAssertEqual(json.stringValue, "abcd efg, HIJK;LMn")
}
Expand Down
9 changes: 8 additions & 1 deletion Tests/SwiftyJSONTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,21 @@ class NumberTests: XCTestCase {

json.string = "1000000000000000000000000000.1"
XCTAssertNil(json.number)

#if !os(Linux)
// blocked by defect https://bugs.swift.org/browse/SR-1464?jql=text%20~%20%22NSNumber%22
//TODO: remove ifdef once the defect is resolved
XCTAssertEqual(json.numberValue.description, "1000000000000000000000000000.1")
#endif

json.string = "1e+27"
XCTAssertEqual(json.numberValue.description, "1000000000000000000000000000")
#if os(Linux) && swift(>=4.2)
// TODO: is this actually correct?
let expectedValue="1e+27"
#else
let expectedValue="1000000000000000000000000000"
#endif
XCTAssertEqual(json.numberValue.description, expectedValue)

//setter
json.number = NSNumber(value: 123456789.0987654321)
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftyJSONTests/RawRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RawRepresentableTests: XCTestCase {
// END OF GENERATED CODE

func testNumber() {
var json:JSON = JSON(rawValue: 948394394.347384 as NSNumber)!
let json:JSON = JSON(rawValue: 948394394.347384 as NSNumber)!
XCTAssertEqual(json.int!, 948394394)
XCTAssertEqual(json.intValue, 948394394)
XCTAssertEqual(json.double!, 948394394.347384)
Expand All @@ -59,7 +59,7 @@ class RawRepresentableTests: XCTestCase {
}

func testBool() {
var jsonTrue:JSON = JSON(rawValue: true as NSNumber)!
let jsonTrue:JSON = JSON(rawValue: true as NSNumber)!

// Blocked by https://bugs.swift.org/browse/SR-5803
#if !(os(Linux) && swift(>=3.2))
Expand All @@ -68,7 +68,7 @@ class RawRepresentableTests: XCTestCase {

XCTAssertEqual(jsonTrue.boolValue, true)

var jsonFalse:JSON = JSON(rawValue: false)!
let jsonFalse:JSON = JSON(rawValue: false)!
XCTAssertEqual(jsonFalse.bool!, false)
XCTAssertEqual(jsonFalse.boolValue, false)

Expand Down
22 changes: 11 additions & 11 deletions Tests/SwiftyJSONTests/SequenceTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SequenceTypeTests: XCTestCase {
}

func testArrayAllNumber() {
var json:JSON = [1,2.0,3.3,123456789,987654321.123456789]
let json:JSON = [1,2.0,3.3,123456789,987654321.123456789]
XCTAssertEqual(json.count, 5)

var index = 0
Expand All @@ -94,7 +94,7 @@ class SequenceTypeTests: XCTestCase {
}

func testArrayAllBool() {
var json:JSON = JSON([true, false, false, true, true])
let json:JSON = JSON([true, false, false, true, true])
XCTAssertEqual(json.count, 5)

var index = 0
Expand All @@ -111,7 +111,7 @@ class SequenceTypeTests: XCTestCase {
}

func testArrayAllString() {
var json:JSON = JSON(rawValue: ["aoo","bpp","zoo"])!
let json:JSON = JSON(rawValue: ["aoo","bpp","zoo"])!
XCTAssertEqual(json.count, 3)

var index = 0
Expand All @@ -129,7 +129,7 @@ class SequenceTypeTests: XCTestCase {

func testArrayWithNull() {
#if os(Linux)
var json:JSON = JSON(rawValue: ["aoo","bpp", NSNull() ,"zoo"] as [Any?])!
let json:JSON = JSON(rawValue: ["aoo","bpp", NSNull() ,"zoo"] as [Any?])!
#else
var json:JSON = JSON(rawValue: ["aoo","bpp", NSNull() ,"zoo"])!
#endif
Expand All @@ -152,7 +152,7 @@ class SequenceTypeTests: XCTestCase {
}

func testArrayAllDictionary() {
var json:JSON = [["1":1, "2":2], ["a":"A", "b":"B"], ["null":NSNull()]]
let json:JSON = [["1":1, "2":2], ["a":"A", "b":"B"], ["null":NSNull()]]
XCTAssertEqual(json.count, 3)

var index = 0
Expand All @@ -178,7 +178,7 @@ class SequenceTypeTests: XCTestCase {
}

func testDictionaryAllNumber() {
var json:JSON = ["double":1.11111, "int":987654321]
let json:JSON = ["double":1.11111, "int":987654321]
XCTAssertEqual(json.count, 2)

var index = 0
Expand All @@ -195,7 +195,7 @@ class SequenceTypeTests: XCTestCase {
}

func testDictionaryAllBool() {
var json:JSON = ["t":true, "f":false, "false":false, "tr":true, "true":true]
let json:JSON = ["t":true, "f":false, "false":false, "tr":true, "true":true]
XCTAssertEqual(json.count, 5)

var index = 0
Expand All @@ -212,7 +212,7 @@ class SequenceTypeTests: XCTestCase {
}

func testDictionaryAllString() {
var json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","z":"zoo"])!
let json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","z":"zoo"])!
XCTAssertEqual(json.count, 3)

var index = 0
Expand All @@ -230,9 +230,9 @@ class SequenceTypeTests: XCTestCase {

func testDictionaryWithNull() {
#if os(Linux)
var json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","null":NSNull(), "z":"zoo"] as [String:Any?])!
let json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","null":NSNull(), "z":"zoo"] as [String:Any?])!
#else
var json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","null":NSNull(), "z":"zoo"])!
let json:JSON = JSON(rawValue: ["a":"aoo","bb":"bpp","null":NSNull(), "z":"zoo"])!
#endif
XCTAssertEqual(json.count, 4)

Expand All @@ -253,7 +253,7 @@ class SequenceTypeTests: XCTestCase {
}

func testDictionaryAllArray() {
var json:JSON = JSON (["Number":[NSNumber(value:1),NSNumber(value:2.123456),NSNumber(value:123456789)], "String":["aa","bbb","cccc"], "Mix":[true, "766", NSNull(), 655231.9823]])
let json:JSON = JSON (["Number":[NSNumber(value:1),NSNumber(value:2.123456),NSNumber(value:123456789)], "String":["aa","bbb","cccc"], "Mix":[true, "766", NSNull(), 655231.9823]])

XCTAssertEqual(json.count, 3)

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftyJSONTests/SubscriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SubscriptTests: XCTestCase {
}

func testArrayAllDictionary() {
var json:JSON = [["1":1, "2":2], ["a":"A", "b":"B"], ["null":NSNull()]]
let json:JSON = [["1":1, "2":2], ["a":"A", "b":"B"], ["null":NSNull()]]
XCTAssertTrue(json[0] == ["1":1, "2":2])
XCTAssertEqual(json[1].dictionary!, ["a":"A", "b":"B"])
XCTAssertEqual(json[2], JSON(["null":NSNull()]))
Expand Down

0 comments on commit f9b4754

Please sign in to comment.