Skip to content

Commit

Permalink
Merge pull request #49 from freshOS/swift_6
Browse files Browse the repository at this point in the history
Swift 6
  • Loading branch information
s4cha authored Sep 24, 2024
2 parents 0c8d337 + 4228ceb commit 2f087d0
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 202 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// swift-tools-version:5.9
// swift-tools-version:6.0

import PackageDescription

let package = Package(
Expand Down
20 changes: 11 additions & 9 deletions Sources/Arrow/Arrow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import CoreGraphics


/**
This is the protocol that makes your swift Models JSON parsable.
Expand Down Expand Up @@ -45,8 +45,6 @@ public extension ArrowParsable {
}
}

private var dateFormatter: DateFormatter? = DateFormatter()
private var useReferenceDate = false

/**
This is used to configure NSDate parsing on a global scale.
Expand All @@ -60,14 +58,18 @@ For more fine grained control, use `dateFormat` on a per field basis :
createdAt <-- json["created_at"]?.dateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")
*/
public class Arrow {
public actor Arrow {

internal static var dateFormatter: DateFormatter? = DateFormatter()
internal static var useReferenceDate = false

/// Sets the defaut dateFormat for parsing NSDates.
public class func setDateFormat(_ format: String) {
public static func setDateFormat(_ format: String) {
dateFormatter?.dateFormat = format
}

/// Sets the defaut dateFormatter for parsing NSDates.
public class func setDateFormatter(_ formatter: DateFormatter?) {
public static func setDateFormatter(_ formatter: DateFormatter?) {
dateFormatter = formatter
}

Expand All @@ -79,7 +81,7 @@ public class Arrow {
For more information see `NSDate(timeIntervalSinceReferenceDate`
documentation
*/
public class func setUseTimeIntervalSinceReferenceDate(_ ref: Bool) {
public static func setUseTimeIntervalSinceReferenceDate(_ ref: Bool) {
useReferenceDate = ref
}
}
Expand Down Expand Up @@ -202,7 +204,7 @@ public func <-- (left: inout Date?, right: JSON?) {
df.dateFormat = customFormat
left = df.date(from: s)
} else if let s = right?.data as? String {
if let date = dateFormatter?.date(from: s) {
if let date = Arrow.dateFormatter?.date(from: s) {
left = date
} else if let t = TimeInterval(s) {
left = timeIntervalToDate(t)
Expand Down Expand Up @@ -292,7 +294,7 @@ func parseString<T>(_ left: inout T?, string: String) {
}

func timeIntervalToDate(_ timeInterval: TimeInterval) -> Date {
return useReferenceDate
return Arrow.useReferenceDate
? Date(timeIntervalSinceReferenceDate: timeInterval)
: Date(timeIntervalSince1970: timeInterval)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Arrow/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import CoreGraphics


public protocol ArrowInitializable {
init?(_ json: JSON?)
Expand Down
3 changes: 1 addition & 2 deletions Sources/Arrow/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ open class JSON {
extension JSON: CustomDebugStringConvertible {

/// This is just for supporting default console logs.
open var debugDescription: String {
public var debugDescription: String {
return data.debugDescription
}

}
119 changes: 64 additions & 55 deletions Tests/ArrowTests/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Sacha Durand Saint Omer. All rights reserved.
//

import XCTest
import Testing
import Arrow

struct ArrayContainer {
Expand Down Expand Up @@ -36,104 +36,113 @@ extension ArrayContainer: ArrowParsable {
}
}

class ArrayTests: XCTestCase {
@Suite
struct ArrayTests {

var arrayContainer = ArrayContainer()

override func setUp() {
super.setUp()
init() {
if let json: JSON = mockJSON() {
arrayContainer <-- json
}
}

func testParsingArrayOfCustomModels() {
XCTAssertEqual(arrayContainer.phoneNumbers.count, 3)
@Test
func parsingArrayOfCustomModels() {
#expect(arrayContainer.phoneNumbers.count == 3)
if arrayContainer.phoneNumbers.count >= 3 {
XCTAssertEqual(arrayContainer.phoneNumbers[0].label, "house")
XCTAssertEqual(arrayContainer.phoneNumbers[1].label, "cell")
XCTAssertEqual(arrayContainer.phoneNumbers[2].label, "work")
#expect(arrayContainer.phoneNumbers[0].label == "house")
#expect(arrayContainer.phoneNumbers[1].label == "cell")
#expect(arrayContainer.phoneNumbers[2].label == "work")

XCTAssertEqual(arrayContainer.phoneNumbers[0].number, "9809876545")
XCTAssertEqual(arrayContainer.phoneNumbers[1].number, "0908070656")
XCTAssertEqual(arrayContainer.phoneNumbers[2].number, "0916570656")
#expect(arrayContainer.phoneNumbers[0].number == "9809876545")
#expect(arrayContainer.phoneNumbers[1].number == "0908070656")
#expect(arrayContainer.phoneNumbers[2].number == "0916570656")
} else {
XCTFail("Parsing ArrayOf Custom Models Fails")
Issue.record("Parsing ArrayOf Custom Models Fails")
}
}

func testParsingOptionalArrayOfCustomModels() {
XCTAssertEqual(arrayContainer.optionalPhoneNumbers?.count, 3)
@Test
func parsingOptionalArrayOfCustomModels() {
#expect(arrayContainer.optionalPhoneNumbers?.count == 3)

XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[0].label, "house")
XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[1].label, "cell")
XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[2].label, "work")
#expect(arrayContainer.optionalPhoneNumbers?[0].label == "house")
#expect(arrayContainer.optionalPhoneNumbers?[1].label == "cell")
#expect(arrayContainer.optionalPhoneNumbers?[2].label == "work")

XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[0].number, "9809876545")
XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[1].number, "0908070656")
XCTAssertEqual(arrayContainer.optionalPhoneNumbers?[2].number, "0916570656")
#expect(arrayContainer.optionalPhoneNumbers?[0].number == "9809876545")
#expect(arrayContainer.optionalPhoneNumbers?[1].number == "0908070656")
#expect(arrayContainer.optionalPhoneNumbers?[2].number == "0916570656")
}

func testParsingArrayOfStrings() {
XCTAssertEqual(arrayContainer.strings.count, 3)
@Test
func parsingArrayOfStrings() {
#expect(arrayContainer.strings.count == 3)
if arrayContainer.strings.count >= 3 {
XCTAssertEqual(arrayContainer.strings[0], "one")
XCTAssertEqual(arrayContainer.strings[1], "two")
XCTAssertEqual(arrayContainer.strings[2], "three")
#expect(arrayContainer.strings[0] == "one")
#expect(arrayContainer.strings[1] == "two")
#expect(arrayContainer.strings[2] == "three")
} else {
XCTFail("Parsing an array of strings fails")
Issue.record("Parsing an array of strings fails")
}
}

func testParsingArrayOfInts() {
XCTAssertEqual(arrayContainer.ints.count, 3)
@Test
func parsingArrayOfInts() {
#expect(arrayContainer.ints.count == 3)
if arrayContainer.ints.count >= 3 {
XCTAssertEqual(arrayContainer.ints[0], 1)
XCTAssertEqual(arrayContainer.ints[1], 2)
XCTAssertEqual(arrayContainer.ints[2], 3)
#expect(arrayContainer.ints[0] == 1)
#expect(arrayContainer.ints[1] == 2)
#expect(arrayContainer.ints[2] == 3)
} else {
XCTFail("Parsing an array of ints fails")
Issue.record("Parsing an array of ints fails")
}
}

func testParsingArrayOfBools() {
XCTAssertEqual(arrayContainer.bools.count, 3)
@Test
func parsingArrayOfBools() {
#expect(arrayContainer.bools.count == 3)
if arrayContainer.bools.count >= 3 {
XCTAssertEqual(arrayContainer.bools[0], true)
XCTAssertEqual(arrayContainer.bools[1], false)
XCTAssertEqual(arrayContainer.bools[2], true)
#expect(arrayContainer.bools[0] == true)
#expect(arrayContainer.bools[1] == false)
#expect(arrayContainer.bools[2] == true)
} else {
XCTFail("Parsing an array of bools fails")
Issue.record("Parsing an array of bools fails")
}
}

func testNestedParsing() {
XCTAssertEqual(arrayContainer.meaningOfLife, 42)
@Test
func nestedParsing() {
#expect(arrayContainer.meaningOfLife == 42)
}

func testNestedArrayParsing() {
XCTAssertEqual(arrayContainer.nestedArrayParsing, "Cool")
@Test
func nestedArrayParsing() {
#expect(arrayContainer.nestedArrayParsing == "Cool")
}

func testParsingArrayOfEnums() {
XCTAssertEqual(arrayContainer.weekdays.count, 3)
@Test
func parsingArrayOfEnums() {
#expect(arrayContainer.weekdays.count == 3)
if arrayContainer.weekdays.count >= 3 {
XCTAssertEqual(arrayContainer.weekdays[0], WeekDay.monday)
XCTAssertEqual(arrayContainer.weekdays[1], WeekDay.wednesday)
XCTAssertEqual(arrayContainer.weekdays[2], WeekDay.friday)
#expect(arrayContainer.weekdays[0] == WeekDay.monday)
#expect(arrayContainer.weekdays[1] == WeekDay.wednesday)
#expect(arrayContainer.weekdays[2] == WeekDay.friday)
} else {
XCTFail("Parsing an array of enums fails")
Issue.record("Parsing an array of enums fails")
}
}

func testParsingOptionalArrayOfEnums() {
XCTAssertEqual(arrayContainer.optionalWeekdays?.count, 3)
@Test
func parsingOptionalArrayOfEnums() {
#expect(arrayContainer.optionalWeekdays?.count == 3)
if let w = arrayContainer.optionalWeekdays, w.count >= 3 {
XCTAssertEqual(arrayContainer.optionalWeekdays?[0], WeekDay.monday)
XCTAssertEqual(arrayContainer.optionalWeekdays?[1], WeekDay.wednesday)
XCTAssertEqual(arrayContainer.optionalWeekdays?[2], WeekDay.friday)
#expect(arrayContainer.optionalWeekdays?[0] == WeekDay.monday)
#expect(arrayContainer.optionalWeekdays?[1] == WeekDay.wednesday)
#expect(arrayContainer.optionalWeekdays?[2] == WeekDay.friday)
} else {
XCTFail("Parsing an array of optional enums fails")
Issue.record("Parsing an array of optional enums fails")
}
}
}
25 changes: 15 additions & 10 deletions Tests/ArrowTests/CustomModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
// Copyright © 2016 Sacha Durand Saint Omer. All rights reserved.
//

import XCTest
import Testing
import Foundation
import Arrow

class CustomModelTests: XCTestCase {
@Suite
struct CustomModelTests {

var customModelContainer = CustomModelContainer()

override func setUp() {
super.setUp()
init() {
if let json: JSON = mockJSON() {
customModelContainer <-- json
}
}

@Test
func testParsingCustomModel() {
XCTAssertEqual(customModelContainer.stats.numberOfFriends, 163)
XCTAssertEqual(customModelContainer.stats.numberOfFans, 10987)
#expect(customModelContainer.stats.numberOfFriends == 163)
#expect(customModelContainer.stats.numberOfFans == 10987)
}

@Test
func testParsingOptionalCustomModel() {
XCTAssertEqual(customModelContainer.optionalStats?.numberOfFriends, 163)
XCTAssertEqual(customModelContainer.optionalStats?.numberOfFans, 10987)
#expect(customModelContainer.optionalStats?.numberOfFriends == 163)
#expect(customModelContainer.optionalStats?.numberOfFans == 10987)
}

@Test
func testParsingIssue() {
let myJson = "{ \"homer\": \"simpson\"}"
guard let jsonData = myJson.data(using: .utf8),
Expand All @@ -39,17 +43,18 @@ class CustomModelTests: XCTestCase {
}
var aSimpson = Doh()
aSimpson.deserialize(json)
XCTAssertEqual(aSimpson.homer, "simpson")
#expect(aSimpson.homer == "simpson")
}

@Test
func testParsingIssueWorksWithString() {
let myJson = "{ \"homer\": \"simpson\"}"
guard let json = JSON(myJson) else {
return
}
var aSimpson = Doh()
aSimpson.deserialize(json)
XCTAssertEqual(aSimpson.homer, "simpson")
#expect(aSimpson.homer == "simpson")
}

}
Expand Down
14 changes: 7 additions & 7 deletions Tests/ArrowTests/CustomRawRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
// Copyright © 2016 Sacha Durand Saint Omer. All rights reserved.
//

import XCTest
import Testing
import Arrow

class CustomRawRepresentableTests: XCTestCase {
@Suite
struct CustomRawRepresentableTests {

var customRawRepresentableContainer = CustomRawRepresentableContainer()

override func setUp() {
super.setUp()
init() {
if let json: JSON = mockJSON() {
customRawRepresentableContainer <-- json
}
}

@Test
func testParsingCustomModel() {
XCTAssertEqual(customRawRepresentableContainer.identifier, 15678)
XCTAssertEqual(customRawRepresentableContainer.rawValue, "15678")
#expect(customRawRepresentableContainer.identifier == 15678)
#expect(customRawRepresentableContainer.rawValue == "15678")
}

}
Loading

0 comments on commit 2f087d0

Please sign in to comment.