Skip to content

Commit

Permalink
Fixes for Swift 4.2 on Linux (#43)
Browse files Browse the repository at this point in the history
* Use same test as OSX, as issue IBM-Swift/SwiftRuntime#183 appears fixed in swift 4.2 on linux

* Use a String for the key in the json rather than an Int. Using an Int causes failure in Linux with swift 4.2. Surely all json keys are Strings anyway?

* Trap Int before Doubles before Bools in setPObjectHelper otherwise swift4.2 on linux can interpret Ints as Doubles and Bools as Ints

* Reinstate linux swift 4.2 build on travis
  • Loading branch information
sportlabsMike authored and djones6 committed Dec 20, 2018
1 parent c687182 commit f2612ea
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ matrix:
dist: trusty
sudo: required
env: SWIFT_SNAPSHOT=4.0.3
# Tests fail on Swift 4.2 on Linux.
# - os: linux
# dist: trusty
# 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=4.2.1
# - os: linux
# dist: trusty
# sudo: required
Expand Down
43 changes: 43 additions & 0 deletions Package@swift-4.2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
/**
* Copyright IBM Corporation 2016, 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import PackageDescription

let package = Package(
name: "SwiftyJSON",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SwiftyJSON",
targets: ["SwiftyJSON"]
)
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftyJSON",
dependencies: []
),
.testTarget(
name: "SwiftyJSONTests",
dependencies: ["SwiftyJSON"]
)
]
)
16 changes: 8 additions & 8 deletions Sources/SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ public struct JSON {
var type: Type
var value: Any

if let bool = newValue as? Bool {
if let number = newValue as? Int {
type = .number
value = NSNumber(value: number)
} else if let number = newValue as? Double {
type = .number
value = NSNumber(value: number)
} else if let bool = newValue as? Bool {
type = .bool
value = bool
} else if let number = newValue as? NSNumber {
Expand All @@ -248,13 +254,7 @@ public struct JSON {
type = .number
value = number
}
} else if let number = newValue as? Double {
type = .number
value = NSNumber(value: number)
} else if let number = newValue as? Int {
type = .number
value = NSNumber(value: number)
} else if let string = newValue as? String {
} else if let string = newValue as? String {
type = .string
value = string
} else if let string = newValue as? NSString {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftyJSONTests/LiteralConvertibleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LiteralConvertibleTests: XCTestCase {
XCTAssert(jsonNil_1 == nil)
let jsonNil_2:JSON = JSON(NSNull.self)
XCTAssert(jsonNil_2 != nil)
let jsonNil_3:JSON = JSON([1:2])
let jsonNil_3:JSON = JSON(["1":2])
XCTAssert(jsonNil_3 != nil)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftyJSONTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.numberValue, 9876543210.123456789)
// Number of fraction digits differs on OSX and Linux,
// issue https://github.com/IBM-Swift/SwiftRuntime/issues/183
#if os(Linux)
#if (os(Linux) && !swift(>=4.2))
XCTAssertEqual(json.stringValue, "9876543210.12346")
#else
XCTAssertEqual(json.stringValue, "9876543210.123457")
Expand Down Expand Up @@ -128,7 +128,7 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.numberValue, 9876543210.123456789)
// Number of fraction digits differs on OSX and Linux,
// issue https://github.com/IBM-Swift/SwiftRuntime/issues/183
#if os(Linux)
#if (os(Linux) && !swift(>=4.2))
XCTAssertEqual(json.stringValue, "9876543210.12346")
#else
XCTAssertEqual(json.stringValue, "9876543210.123457")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftyJSONTests/PrintableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PrintableTests: XCTestCase {
let json:JSON = 1234567890.876623
// Number of fraction digits differs on OSX and Linux,
// issue https://github.com/IBM-Swift/SwiftRuntime/issues/183
#if os(Linux)
#if (os(Linux) && !swift(>=4.2))
XCTAssertEqual(json.description, "1234567890.87662")
XCTAssertEqual(json.debugDescription, "1234567890.87662")
#else
Expand Down

0 comments on commit f2612ea

Please sign in to comment.