-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConfiguratorTests.swift
193 lines (162 loc) · 5.02 KB
/
ConfiguratorTests.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import XCTest
@testable import FunctionalConfigurator
final class ConfiguratorTests: XCTestCase {
func testConfiguration() {
struct TestConfigurable: Equatable {
struct Wrapped: Equatable {
var value = 0
}
var value = false
var wrapped = Wrapped()
}
let wrappedConfiguator = Configurator<TestConfigurable>()
.wrapped.value(1)
let valueConfigurator = Configurator<TestConfigurable>()
.value(true)
let configurator =
wrappedConfiguator
.combined(with: valueConfigurator)
let initial = TestConfigurable()
let expected = TestConfigurable(value: true, wrapped: .init(value: 1))
let actual = configurator.configured(initial)
XCTAssertNotEqual(actual, initial)
XCTAssertEqual(actual, expected)
}
func testConfigInitializable() {
final class TestConfigurable: NSObject {
override init() {
super.init()
}
init(value: Bool, wrapped: TestConfigurable.Wrapped) {
self.value = value
self.wrapped = wrapped
}
struct Wrapped: Equatable {
var value = 0
}
var value = false
var wrapped = Wrapped()
}
let initial = TestConfigurable()
let expected = TestConfigurable(value: true, wrapped: .init(value: 1))
let actual1 = TestConfigurable {
$0
.value(true)
.wrapped(.init(value: 1))
}
let actual2 = TestConfigurable(
config: TestConfigurable.Config
.value(true)
.wrapped(.init(value: 1))
)
XCTAssertNotEqual(actual1.value, initial.value)
XCTAssertNotEqual(actual1.wrapped, initial.wrapped)
XCTAssertEqual(actual1.value, actual2.value)
XCTAssertEqual(actual1.wrapped, actual2.wrapped)
XCTAssertEqual(actual1.value, expected.value)
XCTAssertEqual(actual1.wrapped, expected.wrapped)
}
func testCustomConfigurable() {
struct TestConfigurable: CustomConfigurable {
struct Wrapped: Equatable {
var value = 0
}
var value = false
var wrapped = Wrapped()
}
let initial = TestConfigurable()
let expected = TestConfigurable(value: true, wrapped: .init(value: 1))
let actual = TestConfigurable().configured {
$0
.value(true)
.wrapped(.init(value: 1))
}
XCTAssertNotEqual(actual.value, initial.value)
XCTAssertNotEqual(actual.wrapped, initial.wrapped)
XCTAssertEqual(actual.value, expected.value)
XCTAssertEqual(actual.wrapped, expected.wrapped)
}
func testOptional() {
struct TestConfigurable: CustomConfigurable {
internal init(value: Bool = false, wrappedValue: Int = 0) {
self.value = value
wrapped?.value = wrappedValue
}
class Wrapped: NSObject {
var value: Int? = 0
override init() { self.value = 0 }
}
var value = false
let _wrapped: Wrapped = .init()
var wrapped: Wrapped? { _wrapped }
}
let initial = TestConfigurable()
let expected = TestConfigurable(value: true, wrappedValue: 1)
let actual = TestConfigurable().configured {
$0
.value(true)
.wrapped.value(1)
}
XCTAssertNotEqual(actual.value, initial.value)
XCTAssertNotEqual(actual.wrapped?.value, initial.wrapped?.value)
XCTAssertEqual(actual.value, expected.value)
XCTAssertEqual(actual._wrapped.value, expected._wrapped.value)
}
func testScope() {
struct Container: ConfigInitializable {
class Content {
class InnerClass {
var value: Int = 0
}
struct InnerStruct {
var value: Int = 0
}
var a: Int = 0
var b: Int = 0
var c: Int = 0
let innerClass: InnerClass? = nil
var innerStruct: InnerStruct?
init() {}
}
let content: Content = .init()
}
let expected = Container {
$0
.content.a(1)
.content.b(2)
.content.c(3)
.content.innerClass.value(1)
.content.innerStruct.value(1)
}
let initial = Container()
let actual = Container {
$0
.content.scope {
$0
.a(1)
.b(2)
.c(3)
.innerClass
.ifLetScope {
$0
.value(1)
}
.innerStruct
.ifLetScope {
$0
.value(1)
}
}
}
XCTAssertNotEqual(actual.content.a, initial.content.a)
XCTAssertNotEqual(actual.content.b, initial.content.b)
XCTAssertNotEqual(actual.content.c, initial.content.c)
XCTAssertEqual(actual.content.innerClass?.value, initial.content.innerClass?.value)
XCTAssertEqual(actual.content.innerStruct?.value, initial.content.innerStruct?.value)
XCTAssertEqual(actual.content.a, expected.content.a)
XCTAssertEqual(actual.content.b, expected.content.b)
XCTAssertEqual(actual.content.c, expected.content.c)
XCTAssertEqual(actual.content.innerClass?.value, expected.content.innerClass?.value)
XCTAssertEqual(actual.content.innerStruct?.value, expected.content.innerStruct?.value)
}
}