forked from tristanhimmelman/ObjectMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappableTypesWithTransformsTests.swift
323 lines (262 loc) · 9.81 KB
/
MappableTypesWithTransformsTests.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// MappableTypesWithTransformsTests.swift
// ObjectMapper
//
// Created by Paddy O'Brien on 2015-12-04.
// Copyright © 2015 hearst. All rights reserved.
//
import XCTest
import ObjectMapper
class MappableTypesWithTransformsTests: XCTestCase {
// This is a func so that it can be collapsed
func JSONPayload() -> [String : AnyObject] {
return [
"teams": [[
"api_uri": "/teams/8",
"full_name": "Team Secret",
"short_name": "Secret",
"players": ["/players/1", "/players/2", "/players/3", "/players/4", "/players/5"]
], [
"api_uri": "/teams/43",
"full_name": "Mineski",
"short_name": "Mski",
"players": ["/players/6", "/players/7", "/players/8", "/players/9", "/players/10"]
]],
"game": [
"api_uri": "/games/2723",
"game_time": "33:49",
"players": [
["/players/1", "/players/2", "/players/3", "/players/4", "/players/5"],
["/players/6", "/players/7", "/players/8", "/players/9", "/players/10"]
],
"team1_lineup": [
"top": "/players/1",
"mid": "/players/2",
"bottom": "/players/3",
"support": "/players/4",
"carry": "/players/5"
],
"team2_lineup": [
"top": "/players/6",
"mid": "/players/7",
"bottom": "/players/8",
"support": "/players/9",
"carry": "/players/10"
],
"head_to_head": [
"top": ["/players/1", "/players/6"],
"mid": ["/players/2", "/players/7"],
"bottom": ["/players/3", "/players/8"],
"support": ["/players/4", "/players/9"],
"carry": ["/players/5", "/players/10"]
],
"teams": ["/teams/43", "/teams/8"],
"winning_team_url": "/teams/8"
]
]
}
// MARK: - Non-Optional Tests
func testParsingSingleInstanceWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotEqual(game!.winner.URI, "FAKE")
}
func testParsingArrayOfObjectsWithTransform() {
let teams = Mapper<Team>().mapArray(JSONPayload()["teams"])
XCTAssertNotNil(teams)
XCTAssertNotEqual(teams!.count, 0)
XCTAssertNotEqual(teams!.first!.players.count, 0)
}
func testParsing2DimensionalArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotEqual(game!.players.count, 0)
XCTAssertNotEqual(game!.players.first!.count, 0)
XCTAssertNotEqual(game!.players.last!.count, 0)
}
func testParsingDictionaryOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotEqual(game!.team1Lineup.count, 0)
XCTAssertNotEqual(game!.team2Lineup.count, 0)
}
func testParsingDictionaryOfArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
for (position, players) in game!.headToHead {
XCTAssertNotEqual(players.count, 0, "No players were mapped for \(position)")
}
}
func testParsingSetOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotEqual(game!.teams.count, 0)
}
// MARK: - Optional Tests
func testParsingOptionalSingleInstanceWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.O_winner)
}
func testParsingOptionalArrayOfObjectsWithTransform() {
let teams = Mapper<Team>().mapArray(JSONPayload()["teams"])
XCTAssertNotNil(teams)
XCTAssertNotEqual(teams!.count, 0)
XCTAssertNotNil(teams!.first!.O_players)
XCTAssertNotEqual(teams!.first!.O_players!.count, 0)
}
func testParsingOptional2DimensionalArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.O_players)
XCTAssertNotEqual(game!.O_players!.count, 0)
XCTAssertNotEqual(game!.O_players!.first!.count, 0)
XCTAssertNotEqual(game!.O_players!.last!.count, 0)
}
func testParsingOptionalDictionaryOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.O_team1Lineup)
XCTAssertNotEqual(game!.O_team1Lineup!.count, 0)
XCTAssertNotNil(game!.O_team2Lineup)
XCTAssertNotEqual(game!.O_team2Lineup!.count, 0)
}
func testParsingOptionalDictionaryOfArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.O_headToHead)
for (position, players) in game!.O_headToHead! {
XCTAssertNotEqual(players.count, 0, "No players were mapped for \(position)")
}
}
func testParsingOptionalSetOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.O_teams)
XCTAssertNotEqual(game!.O_teams!.count, 0)
}
// MARK: - Implicitly Unwrapped Optional Tests
func testParsingImplicitlyUnwrappedSingleInstanceWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.I_winner)
}
func testParsingImplicitlyUnwrappedArrayOfObjectsWithTransform() {
let teams = Mapper<Team>().mapArray(JSONPayload()["teams"])
XCTAssertNotNil(teams)
XCTAssertNotEqual(teams!.count, 0)
XCTAssertNotNil(teams!.first!.I_players)
XCTAssertNotEqual(teams!.first!.I_players!.count, 0)
}
func testParsingImplicitlyUnwrapped2DimensionalArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.I_players)
XCTAssertNotEqual(game!.I_players!.count, 0)
XCTAssertNotEqual(game!.I_players!.first!.count, 0)
XCTAssertNotEqual(game!.I_players!.last!.count, 0)
}
func testParsingImplicitlyUnwrappedDictionaryOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.I_team1Lineup)
XCTAssertNotEqual(game!.I_team1Lineup!.count, 0)
XCTAssertNotNil(game!.I_team2Lineup)
XCTAssertNotEqual(game!.I_team2Lineup!.count, 0)
}
func testParsingImplicitlyUnwrappedDictionaryOfArrayOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.I_headToHead)
for (position, players) in game!.I_headToHead! {
XCTAssertNotEqual(players.count, 0, "No players were mapped for \(position)")
}
}
func testParsingImplicitlyUnwrappedSetOfObjectsWithTransform() {
let game = Mapper<Game>().map(JSONPayload()["game"])
XCTAssertNotNil(game)
XCTAssertNotNil(game!.I_teams)
XCTAssertNotEqual(game!.I_teams!.count, 0)
}
// MARK: - Internal classes for testing
class Game: Mappable, URIInitiable {
var URI: String?
var players: [[Player]] = [[]]
var team1Lineup: [String : Player] = [:]
var team2Lineup: [String : Player] = [:]
var headToHead: [String : [Player]] = [:]
var teams: Set<Team> = []
var winner: Team = Team(URI: "FAKE")
// Optional
var O_players: [[Player]]?
var O_team1Lineup: [String : Player]?
var O_team2Lineup: [String : Player]?
var O_headToHead: [String : [Player]]?
var O_teams: Set<Team>?
var O_winner: Team?
// Implicitly Unwrapped
var I_players: [[Player]]!
var I_team1Lineup: [String : Player]!
var I_team2Lineup: [String : Player]!
var I_headToHead: [String : [Player]]!
var I_teams: Set<Team>!
var I_winner: Team!
required init(URI: String) { self.URI = URI }
required init?(_ map: Map) {}
func mapping(map: Map) {
players <- (map["players"], RelationshipTransform<Player>()) // 2D Array with transform
team1Lineup <- (map["team1_lineup"], RelationshipTransform<Player>()) // Dictionary with transform
team2Lineup <- (map["team1_lineup"], RelationshipTransform<Player>())
headToHead <- (map["head_to_head"], RelationshipTransform<Player>()) // Dictionary of arrays with transform
teams <- (map["teams"], RelationshipTransform<Team>()) // Set with transform
winner <- (map["winning_team_url"], RelationshipTransform<Team>()) // Single instance with transform
// Optional
O_players <- (map["players"], RelationshipTransform<Player>())
O_team1Lineup <- (map["team1_lineup"], RelationshipTransform<Player>())
O_team2Lineup <- (map["team1_lineup"], RelationshipTransform<Player>())
O_headToHead <- (map["head_to_head"], RelationshipTransform<Player>())
O_teams <- (map["teams"], RelationshipTransform<Team>())
O_winner <- (map["winning_team_url"], RelationshipTransform<Team>())
// Implicitly Unwrapped
I_players <- (map["players"], RelationshipTransform<Player>())
I_team1Lineup <- (map["team1_lineup"], RelationshipTransform<Player>())
I_team2Lineup <- (map["team1_lineup"], RelationshipTransform<Player>())
I_headToHead <- (map["head_to_head"], RelationshipTransform<Player>())
I_teams <- (map["teams"], RelationshipTransform<Team>())
I_winner <- (map["winning_team_url"], RelationshipTransform<Team>())
}
}
class Team: NSObject, Mappable, URIInitiable {
var URI: String?
var players: [Player] = []
var O_players: [Player]?
var I_players: [Player]?
required init(URI: String) { self.URI = URI }
required init?(_ map: Map) {}
func mapping(map: Map) {
players <- (map["players"], RelationshipTransform<Player>())
O_players <- (map["players"], RelationshipTransform<Player>())
I_players <- (map["players"], RelationshipTransform<Player>())
}
}
class Player: Mappable, URIInitiable {
required init(URI: String) {}
required init?(_ map: Map) {}
func mapping(map: Map) {}
}
}
protocol URIInitiable {
init(URI: String)
}
class RelationshipTransform<ObjectType where ObjectType: protocol<Mappable, URIInitiable>>: TransformType {
typealias Object = ObjectType
typealias JSON = [String: AnyObject]
func transformFromJSON(value: AnyObject?) -> Object? {
guard let URI = value as? String else { return nil }
let relation = ObjectType(URI: URI)
return relation
}
func transformToJSON(value: Object?) -> JSON? {
return nil
}
}