Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native UserDefaults types should be decoded via the existing getter #1

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/UserDefaults+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public extension UserDefaults {

func decode<T: Decodable>(_ type: T.Type = T.self, forKey key: String) throws -> T? {
guard let storage = object(forKey: key) else { return nil }
return try KeyValueDecoder.makePlistCompatible().decode(type, from: storage)
switch type {
case is String.Type: return string(forKey: key) as? T
case is Bool.Type: return bool(forKey: key) as? T
case is Int.Type: return integer(forKey: key) as? T
case is Double.Type: return double(forKey: key) as? T
case is Float.Type: return float(forKey: key) as? T
case is URL.Type: return url(forKey: key) as? T
default: return try KeyValueDecoder.makePlistCompatible().decode(type, from: storage)
}
}
}
147 changes: 147 additions & 0 deletions Tests/UserDefaults+CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,153 @@ final class UserDefaultsCodableTests: XCTestCase {
)
)
}

func testDecodes_String() {
let defaults = UserDefaults.makeMock()

defaults.set("YES", forKey: "flag")
XCTAssertEqual(
defaults.string(forKey: "flag"),
"YES"
)
XCTAssertEqual(
try defaults.decode(String.self, forKey: "flag"),
"YES"
)

#if canImport(Darwin)
defaults.set(1, forKey: "flag")
XCTAssertEqual(
defaults.string(forKey: "flag"),
"1"
)
XCTAssertEqual(
try defaults.decode(String.self, forKey: "flag"),
"1"
)
#endif
}

func testDecodes_Bool() {
let defaults = UserDefaults.makeMock()

defaults.set("YES", forKey: "flag")
XCTAssertTrue(defaults.bool(forKey: "flag"))
XCTAssertEqual(
try defaults.decode(Bool.self, forKey: "flag"),
true
)

defaults.set("NO", forKey: "flag")
XCTAssertFalse(defaults.bool(forKey: "flag"))
XCTAssertEqual(
try defaults.decode(Bool.self, forKey: "flag"),
false
)

defaults.set("other", forKey: "flag")
XCTAssertFalse(defaults.bool(forKey: "flag"))
XCTAssertEqual(
try defaults.decode(Bool.self, forKey: "flag"),
false
)
}

func testDecodes_Integer() {
let defaults = UserDefaults.makeMock()

defaults.set(1, forKey: "flag")
XCTAssertEqual(
defaults.integer(forKey: "flag"),
1
)
XCTAssertEqual(
try defaults.decode(Int.self, forKey: "flag"),
1
)

defaults.set("2", forKey: "flag")
XCTAssertEqual(
defaults.integer(forKey: "flag"),
2
)
XCTAssertEqual(
try defaults.decode(Int.self, forKey: "flag"),
2
)
}

func testDecodes_Double() {
let defaults = UserDefaults.makeMock()

defaults.set(Double(1.5), forKey: "flag")
XCTAssertEqual(
defaults.double(forKey: "flag"),
1.5
)
XCTAssertEqual(
try defaults.decode(Double.self, forKey: "flag"),
1.5
)

defaults.set("2.5", forKey: "flag")
XCTAssertEqual(
defaults.double(forKey: "flag"),
2.5
)
XCTAssertEqual(
try defaults.decode(Double.self, forKey: "flag"),
2.5
)
}

func testDecodes_Float() {
let defaults = UserDefaults.makeMock()

defaults.set(Float(1.5), forKey: "flag")
XCTAssertEqual(
defaults.float(forKey: "flag"),
1.5
)
XCTAssertEqual(
try defaults.decode(Float.self, forKey: "flag"),
1.5
)

defaults.set("2.5", forKey: "flag")
XCTAssertEqual(
defaults.float(forKey: "flag"),
2.5
)
XCTAssertEqual(
try defaults.decode(Float.self, forKey: "flag"),
2.5
)
}

func testDecodes_URL() {
let defaults = UserDefaults.makeMock()

defaults.set(URL(fileURLWithPath: "/fish"), forKey: "flag")
XCTAssertEqual(
defaults.url(forKey: "flag"),
URL(fileURLWithPath: "/fish")
)
XCTAssertEqual(
try defaults.decode(URL.self, forKey: "flag"),
URL(fileURLWithPath: "/fish")
)

defaults.set("/chips", forKey: "flag")
XCTAssertEqual(
defaults.url(forKey: "flag"),
URL(fileURLWithPath: "/chips")
)
XCTAssertEqual(
try defaults.decode(URL.self, forKey: "flag"),
URL(fileURLWithPath: "/chips")
)
}
}

private extension UserDefaults {
Expand Down