Skip to content
4 changes: 4 additions & 0 deletions Marshal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
AFBED2761C7E1E5100622331 /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFBED2751C7E1E5100622331 /* JSON.swift */; };
AFBED27C1C7F65BB00622331 /* Unmarshaling.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFBED27B1C7F65BB00622331 /* Unmarshaling.swift */; };
AFBED27E1C7F699600622331 /* UnmarshalUpdating.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFBED27D1C7F699600622331 /* UnmarshalUpdating.swift */; };
C638F8C02982569100851751 /* DateAndUUIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C638F8BF2982569100851751 /* DateAndUUIDTests.swift */; };
CC7504601DA6B27500643B7A /* Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC75045F1DA6B27500643B7A /* Migration.swift */; };
CCAE549F1CFDF9D30069AC65 /* UnmarshalingWithContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCAE549E1CFDF9D30069AC65 /* UnmarshalingWithContext.swift */; };
CCB6D6C21CF90E7F00422F4C /* UnmarshalingWithContextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCB6D6C01CF8F2F500422F4C /* UnmarshalingWithContextTests.swift */; };
Expand Down Expand Up @@ -73,6 +74,7 @@
AFBED2751C7E1E5100622331 /* JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSON.swift; path = ../Sources/JSON.swift; sourceTree = "<group>"; };
AFBED27B1C7F65BB00622331 /* Unmarshaling.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Unmarshaling.swift; path = ../Sources/Unmarshaling.swift; sourceTree = "<group>"; };
AFBED27D1C7F699600622331 /* UnmarshalUpdating.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UnmarshalUpdating.swift; path = ../Sources/UnmarshalUpdating.swift; sourceTree = "<group>"; };
C638F8BF2982569100851751 /* DateAndUUIDTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateAndUUIDTests.swift; sourceTree = "<group>"; };
CC75045F1DA6B27500643B7A /* Migration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Migration.swift; path = Sources/Migration.swift; sourceTree = SOURCE_ROOT; };
CCAE549E1CFDF9D30069AC65 /* UnmarshalingWithContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UnmarshalingWithContext.swift; path = Sources/UnmarshalingWithContext.swift; sourceTree = SOURCE_ROOT; };
CCB6D6C01CF8F2F500422F4C /* UnmarshalingWithContextTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnmarshalingWithContextTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -154,6 +156,7 @@
71EFC3AB1CCB513300394E57 /* PerformanceTests.swift */,
CCB6D6C01CF8F2F500422F4C /* UnmarshalingWithContextTests.swift */,
E4605F131EE94EF4000147B7 /* FloatArray.json */,
C638F8BF2982569100851751 /* DateAndUUIDTests.swift */,
);
path = MarshalTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -299,6 +302,7 @@
files = (
71EFC3A81CCB4EAF00394E57 /* PerformanceTestObjects.swift in Sources */,
AFBED25F1C7E1AAB00622331 /* MarshalTests.swift in Sources */,
C638F8C02982569100851751 /* DateAndUUIDTests.swift in Sources */,
CCB6D6C21CF90E7F00422F4C /* UnmarshalingWithContextTests.swift in Sources */,
71EFC3AC1CCB513300394E57 /* PerformanceTests.swift in Sources */,
);
Expand Down
92 changes: 92 additions & 0 deletions MarshalTests/DateAndUUIDTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// DateAndUUIDTests.swift
//
// Created by Rick Mann on 2021-05-18.
//

import Foundation

import XCTest
import Marshal



class
TestMarshalAdditions : XCTestCase
{
func
testDateFromMilliseconds()
throws
{
let json: [String:Any] =
[
"dateInt" : 1621391590892,
"dateDouble" : 1621391590892.0,
"dateString" : "1621391590892",
"notADate" : "pizza",
]
let dateInt: Date = try json.value(for: "dateInt", dateEncoding: .millisecondsSince1970)
let dateDouble: Date = try json.value(for: "dateDouble", dateEncoding: .millisecondsSince1970)
let dateString: Date = try json.value(for: "dateString", dateEncoding: .millisecondsSince1970)
XCTAssertEqual(dateInt, Date(timeIntervalSince1970: 1621391590.892))
XCTAssertEqual(dateDouble, Date(timeIntervalSince1970: 1621391590.892))
XCTAssertEqual(dateString, Date(timeIntervalSince1970: 1621391590.892))

let notADate: Date? = try? json.value(for: "notADate", dateEncoding: .millisecondsSince1970)
XCTAssertNil(notADate)

XCTAssertThrowsError(try json.value(for: "notADate", dateEncoding: .millisecondsSince1970))
}

func
testDateFromSeconds()
throws
{
let json: [String:Any] =
[
"dateInt" : 1621391590,
"dateDouble" : 1621391590.0,
"dateString" : "1621391590",
"notADate" : "pizza",
]
let dateInt: Date = try json.value(for: "dateInt", dateEncoding: .secondsSince1970)
let dateDouble: Date = try json.value(for: "dateDouble", dateEncoding: .secondsSince1970)
let dateString: Date = try json.value(for: "dateString", dateEncoding: .secondsSince1970)
XCTAssertEqual(dateInt, Date(timeIntervalSince1970: 1621391590))
XCTAssertEqual(dateDouble, Date(timeIntervalSince1970: 1621391590))
XCTAssertEqual(dateString, Date(timeIntervalSince1970: 1621391590))

let notADate: Date? = try? json.value(for: "notADate", dateEncoding: .secondsSince1970)
XCTAssertNil(notADate)

XCTAssertThrowsError(try json.value(for: "notADate", dateEncoding: .secondsSince1970))
}

func
testDateFromString()
throws
{
let json: [String:Any] =
[
"createdAt" : "Tue, 25 May 2021 05:21:16 GMT",
]
let createdAt: Date = try json.value(for: "createdAt", dateEncoding: .formatted("EEE, dd MMM yyyy HH:mm:ss ZZZ"))
XCTAssertEqual(createdAt, Date(timeIntervalSinceReferenceDate: 643612876.0))
}

func
testDateFromDateFormatter()
throws
{
let json: [String:Any] =
[
"createdAt" : "Tue, 25 May 2021 05:21:16 GMT",
]

let df = DateFormatter()
df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss ZZZ"

let createdAt: Date = try json.value(for: "createdAt", dateEncoding: .formatter(df))
XCTAssertEqual(createdAt, Date(timeIntervalSinceReferenceDate: 643612876.0))
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
17 changes: 0 additions & 17 deletions [email protected]

This file was deleted.

6 changes: 6 additions & 0 deletions Sources/Marshaling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ public protocol Marshaling {

func marshaled() -> Self.MarshalType
}

extension Array where Element: Marshaling {
public func marshaled() -> [Any] {
return self.map { $0.marshaled() }
}
}
Loading