From 05ae6917a436db31a11a61cc68b17400bad1e75b Mon Sep 17 00:00:00 2001 From: Jason Connery Date: Fri, 1 Apr 2016 17:05:44 +0100 Subject: [PATCH] Added NSDate support --- Pantry/Storable.swift | 3 ++- PantryTests/MemoryTests.swift | 16 ++++++++++++++++ PantryTests/PantryTests.swift | 10 +++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Pantry/Storable.swift b/Pantry/Storable.swift index 4a47e23..9ba874e 100644 --- a/Pantry/Storable.swift +++ b/Pantry/Storable.swift @@ -91,7 +91,7 @@ public enum StorageExpiry { /** Default storable types -Default types are `Bool`, `String`, `Int`, `Float`, `Double` +Default types are `Bool`, `String`, `Int`, `Float`, `Double`, `NSDate` */ public protocol StorableDefaultType { } @@ -101,6 +101,7 @@ extension String: StorableDefaultType { } extension Int: StorableDefaultType { } extension Float: StorableDefaultType { } extension Double: StorableDefaultType { } +extension NSDate: StorableDefaultType { } // MARK: Enums with Raw Values diff --git a/PantryTests/MemoryTests.swift b/PantryTests/MemoryTests.swift index 1c498a2..aed64a0 100644 --- a/PantryTests/MemoryTests.swift +++ b/PantryTests/MemoryTests.swift @@ -25,10 +25,14 @@ class MemoryTests: XCTestCase { let string: String = "Hello" let int: Int = 4 let float: Float = 10.2 + let double: Double = 12.7 + let date: NSDate = NSDate(timeIntervalSince1970: 1459355217) Pantry.pack(string, key: "ourTestString") Pantry.pack(int, key: "ourTestInt") Pantry.pack(float, key: "ourTestFloat") + Pantry.pack(double,key: "ourTestDouble") + Pantry.pack(date, key: "ourTestDate") if let unpackedString: String = Pantry.unpack("ourTestString") { XCTAssert(unpackedString == "Hello", "default string was incorrect") @@ -45,6 +49,18 @@ class MemoryTests: XCTestCase { } else { XCTFail("no default float could be unpacked") } + if let unpackedDouble: Double = Pantry.unpack("ourTestDouble") { + XCTAssert(unpackedDouble == 12.7 , "default double was incorrect") + } + else { + XCTFail("no default double could be unpacked") + } + if let unpackedDate: NSDate = Pantry.unpack("ourTestDate") { + XCTAssert(unpackedDate.timeIntervalSince1970 == 1459355217 , "default date was incorrect") + } + else { + XCTFail("no default double could be unpacked") + } } // nested storable types diff --git a/PantryTests/PantryTests.swift b/PantryTests/PantryTests.swift index c15c4b7..5d8ce1a 100644 --- a/PantryTests/PantryTests.swift +++ b/PantryTests/PantryTests.swift @@ -38,11 +38,13 @@ class PantryTests: XCTestCase { let int: Int = 4 let float: Float = 10.2 let double: Double = 20.6 - + let date: NSDate = NSDate(timeIntervalSince1970: 1459355217) + Pantry.pack(string, key: "ourTestString") Pantry.pack(int, key: "ourTestInt") Pantry.pack(float, key: "ourTestFloat") Pantry.pack(double, key: "ourTestDouble") + Pantry.pack(date, key: "ourTestDate") if let unpackedString: String = Pantry.unpack("ourTestString") { XCTAssert(unpackedString == "Hello", "default string was incorrect") @@ -64,6 +66,12 @@ class PantryTests: XCTestCase { } else { XCTFail("no default double could be unpacked") } + if let unpackedDate: NSDate = Pantry.unpack("ourTestDate") { + XCTAssert(unpackedDate.timeIntervalSince1970 == 1459355217 , "default date was incorrect") + } + else { + XCTFail("no default double could be unpacked") + } } func testDefaultArray() {