-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutofixturesTests.swift
165 lines (135 loc) · 4.05 KB
/
AutofixturesTests.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Autofixtures
import XCTest
import ExampleModels
enum Enum: String, Decodable, CaseIterable {
case one
case two
}
struct Simple: Decodable, Hashable, Equatable {
var id: Int
var double: Double
var string: String
var enumeration: Enum
}
struct Nested: Decodable {
var nested: Simple
}
extension Simple: Override {
static var fixOverride: Simple = .fixDecoded.set(\.string, "NAME")
}
extension ExampleType: Override {}
extension Enum: Override {}
final class AutofixturesTests: XCTestCase {
func testDecimal() {
XCTAssertEqual(Decimal.fix, Decimal(Double.fix))
}
func test3rdPartyModels() {
let model = Model.fix
XCTAssertEqual(model.type, .one)
XCTAssertEqual(model.string, "FIX")
}
func testSingleValues() throws {
XCTAssertEqual(String.fix, "FIX")
XCTAssertEqual(Bool.fix, false)
XCTAssertEqual(Int.fix, 333)
XCTAssertEqual(Double.fix, 1.1)
XCTAssertEqual(Float.fix, 1.2)
XCTAssertEqual(Int.fix, 333)
XCTAssertEqual(Int8.fix, 8)
XCTAssertEqual(Int16.fix, 16)
XCTAssertEqual(Int32.fix, 32)
XCTAssertEqual(Int64.fix, 64)
XCTAssertEqual(UInt.fix, 333)
XCTAssertEqual(UInt8.fix, 111)
XCTAssertEqual(UInt16.fix, 333)
XCTAssertEqual(UInt32.fix, 333)
XCTAssertEqual(UInt64.fix, 333)
}
func testStruct() {
let fix = Simple.fixDecoded
XCTAssertNotEqual(fix.id, 0)
XCTAssertEqual(fix.id, 333)
XCTAssertEqual(fix.string, "FIX")
}
func testNested() {
let fix = Nested.fix
XCTAssertEqual(fix.nested.id, Int.fix)
XCTAssertEqual(fix.nested.string, "NAME")
}
func testArrayOfStrings() {
let decoded = [String].fix
XCTAssertEqual(decoded.count, 1)
XCTAssertEqual(decoded[0], String.fix)
}
func testArrayOfSimples() {
let decoded = [Simple].fix
XCTAssertEqual(decoded.count, 1)
XCTAssertEqual(decoded.first, Simple.fixOverride)
}
func testSetOfSimples() {
let decoded = Set<Simple>.fix
XCTAssertEqual(decoded.count, 1)
XCTAssertEqual(decoded.first, Simple.fixOverride)
}
func testGeneric() {
struct Generic<A: Decodable>: Decodable {
let a: A
}
XCTAssertEqual(Generic<Simple>.fix.a, Simple.fix)
}
func testEnum() {
XCTAssertEqual(Enum.fix, .one)
XCTAssertEqual(Simple.fix.enumeration, .one)
}
func testEnumWithAssociatedValue() {
enum EnumWithValue: Decodable, Equatable, Override {
case value(Simple)
static let fixOverride: EnumWithValue = .value(.fix)
}
XCTAssertEqual(EnumWithValue.fix, .value(.fix))
}
func testOptional() {
XCTAssertEqual(Simple?.fix, nil)
}
func testStringDictionary() {
XCTAssertEqual([String: String].fix, [.fix: .fix])
XCTAssertEqual([String: Simple].fix, [.fix: .fix])
}
func testSet() {
struct Auto: Decodable {
var name: String
}
let fix = Auto.fix
.set(\.name, "updated")
XCTAssertEqual(fix.name, "updated")
}
func testClosure() {
struct WithClosure: Decodable {
let closure: Closure<Void, Void>
}
XCTAssertNotNil(WithClosure.fix)
}
func testClosureWithOverridenValue() {
struct WithClosure: Decodable {
let closure: Closure<Void, Int>
}
XCTAssertEqual(WithClosure.fix.closure(()), 9)
}
func testThatJSONDecoderWorksAsExpected() {
}
}
/// Closure wrapper for decodable support
struct Closure<A, B>: Decodable {
var closure: (A) -> B
init(from decoder: Decoder) throws {
self.closure = { _ in fatalError() }
}
func callAsFunction(_ a: A) -> B {
closure(a)
}
}
/// Example of setting up default for closure
extension Closure: Override where A == Void, B == Int {
static var fixOverride: Closure<(), Int> = .fixDecoded
.set(\.closure, { return 9 })
}