-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForcibleNumberTests.swift
86 lines (78 loc) · 2.08 KB
/
ForcibleNumberTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
import Testing
@testable import ForcibleValue
@Suite
struct ForcibleNumberTests {
private struct Target: Decodable {
@ForcibleNumber var value: Int
}
private struct OptionTarget: Decodable {
@ForcibleNumber.Option var value: Int?
}
private struct DefaultTarget: Decodable {
@ForcibleDefault.Zero var value: Int
}
@Test(arguments: [
TestCase(input: "\"1\"", output: 1)
])
func decodeSuccess(testCase: TestCase<String, Int>) throws {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)
let target = try JSONDecoder().decode(Target.self, from: json!)
#expect(target.value == testCase.output)
}
@Test(arguments: [
TestCase(input: "\"abc\"", output: ())
])
func decodeError(testCase: TestCase<String, Void>) throws {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)
#expect(throws: (any Error).self) {
try JSONDecoder().decode(Target.self, from: json!)
}
}
@Test(arguments: [
TestCase(input: nil, output: nil),
TestCase(input: "\"1\"", output: 1),
])
func decodeOptionSuccess(testCase: TestCase<String?, Int?>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()
let target = try JSONDecoder().decode(OptionTarget.self, from: json!)
#expect(target.value == testCase.output)
}
@Test(arguments: [
TestCase(input: nil, output: 0),
TestCase(input: "\"1\"", output: 1),
])
func decodeDefaultSuccess(testCase: TestCase<String?, Int>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()
let target = try JSONDecoder().decode(DefaultTarget.self, from: json!)
#expect(target.value == testCase.output)
}
}