Add tests for Checked

This commit is contained in:
siemensikkema 2021-07-20 23:22:20 +02:00
parent 659242959b
commit 8dcc2ccdff
2 changed files with 46 additions and 3 deletions

View File

@ -1,5 +1,40 @@
final class DecodedTests: XCTestCase {
func testChecked() throws {
// TODO: implement
import Checked
import Decoded
import XCTest
final class CheckedTests: XCTestCase {
func test_directPropertyAccess() throws {
struct Gadget: Decodable {
let name: Decoded<String>
}
let decoded = try decode(#"{"name":"arduino"}"#, as: Gadget.self)
let checked = try decoded.checked()
XCTAssertEqual(checked.name, "arduino")
}
func test_directPropertyAccess_nested() throws {
struct CarRequest: Decodable {
struct Engine: Decodable {
let cylinderVolume: Decoded<Double>
}
let engine: Decoded<Engine>
}
let decoded = try decode(#"{"engine":{"cylinderVolume": 0.9}}"#, as: CarRequest.self)
let checked = try decoded.checked()
XCTAssertEqual(checked.engine.cylinderVolume, 0.9)
}
func test_checkingFailedDecodingThrowsError() throws {
let decoded = try decode("0", as: String.self)
XCTAssertThrowsError(try decoded.checked()) { error in
guard error is DecodingErrors else {
XCTFail("expected error of type `DecodingErrors`")
return
}
}
}
}

View File

@ -0,0 +1,8 @@
import Decoded
import Foundation
let decoder = JSONDecoder()
func decode<T: Decodable>(_ string: String, as _: T.Type = T.self) throws -> Decoded<T> {
try decoder.decode(Decoded<T>.self, from: string.data(using: .utf8)!)
}