-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilmExampleTest.swift
187 lines (166 loc) · 5 KB
/
FilmExampleTest.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
import XCTest
class FilmExampleTest: XCTestCase {
func testFilmFromJson() {
let film: JSON =
[ "title": "The Godfather"
, "genres": ["crime", "drama"]
, "yearOfRelease": 1972
, "actors": ["Al Pacino", "Marlon Brando"]
, "languages": ["en", "it", "lat"]
, "imdbRating": 9.2
, "streamURL": "https://github.com/NunoAlexandre/stork"
]
if let parsedFilm = Film.from(json: film) {
XCTAssertEqual(
parsedFilm,
Film(
title: "The Godfather",
genres: [.crime, .drama],
yearOfRelease: 1972,
actors: [Actor(name: "Al Pacino"), Actor(name: "Marlon Brando")],
languages: [.english, .italian, .latin],
isKidsProof: false,
imdbRating: 9.2,
streamURL: URL(string: "https://github.com/NunoAlexandre/stork")!
)
)
}
else {
XCTFail("The JSON object failed to be decoded to 'Film'")
}
}
func testFilmFromJsonWithMissingField() {
/// Confirm that when a JSON with a missing required
/// fields is given, nil is obtained on decoding that value.
let malformedFilm: JSON = [:]
let maybeFilm = Film.from(json: malformedFilm)
XCTAssertNil(maybeFilm)
}
func testFilmFromJsonWithTypeMismatchOnTitle() {
/// Confirm that when a JSON with a type mismatch
/// is given, nil is obtained on decoding that value.
let malformedFilm: JSON =
[ "title": 1928 // expected to be a String
, "genres": ["crime", "drama"]
, "yearOfRelease": 1972
, "actors": ["Al Pacino", "Marlon Brando"]
, "languages": ["en", "it", "lat"]
, "imdbRating": 9.2
, "streamURL": "https://github.com/NunoAlexandre/stork"
]
let maybeFilm = Film.from(json: malformedFilm)
XCTAssertNil(maybeFilm)
}
func testFilmFromJsonWithTypeMismatchOnArray() {
/// Confirm that when a JSON with a type mismatch
/// on an array, nil is obtained on decoding that value.
let malformedFilm: JSON =
[ "title": "The Godfather"
, "genres": 123 // expected to be [String]
, "yearOfRelease": 1972
, "actors": ["Al Pacino", "Marlon Brando"]
, "languages": ["en", "it", "lat"]
, "imdbRating": 9.2
, "streamURL": "https://github.com/NunoAlexandre/stork"
]
let maybeFilm = Film.from(json: malformedFilm)
XCTAssertNil(maybeFilm)
}
func testFilmFromJsonWithTypeMismatchOnArrayElements() {
/// Confirm that when a JSON with a type mismatch
/// on the array elements type, an empty array is
// obtained instead. This is not ideal but for now
// it works this way and must be tested.
let malformedFilm: JSON =
[ "title": "The Godfather"
, "genres": [1,2,3] // expected to be [String]
, "yearOfRelease": 1972
, "actors": ["Al Pacino", "Marlon Brando"]
, "languages": ["en", "it", "lat"]
, "imdbRating": 9.2
, "streamURL": "https://github.com/NunoAlexandre/stork"
]
if let parsedFilm = Film.from(json: malformedFilm) {
XCTAssertEqual(
parsedFilm,
Film(
title: "The Godfather",
genres: [],
yearOfRelease: 1972,
actors: [Actor(name: "Al Pacino"), Actor(name: "Marlon Brando")],
languages: [.english, .italian, .latin],
isKidsProof: false,
imdbRating: 9.2,
streamURL: URL(string: "https://github.com/NunoAlexandre/stork")!
)
)
}
else {
XCTFail("The JSON object failed to be decoded to 'Film'")
}
}
}
/// Model
struct Film: Equatable {
let title: String
let genres: [Genre]
let yearOfRelease: Int
let actors: [Actor]
let languages: [Language]
let isKidsProof: Bool
let imdbRating: Double?
let streamURL: URL
}
enum Genre: String {
case comedy
case crime
case drama
case horror
}
struct Actor: Equatable {
let name: String
}
enum Language: String {
case english
case italian
case latin
case portuguese
case dutch
}
/// Comply to the 'FromJson' protocol
extension Film: FromJson {
static func from(value: JsonValue) -> Film? {
return value.ifObject { json in
return Film(
title: try json .! "title",
genres: try json ..! "genres",
yearOfRelease: try json .! "yearOfRelease",
actors: try json ..! "actors",
languages: try json ..! "languages",
isKidsProof: (json .? "isKidsProof") ?? false,
imdbRating: json .? "imdbRating",
streamURL: try json .! "streamURL"
)
}
}
}
extension Genre: FromJson {}
extension Actor: FromJson {
static func from(value: JsonValue) -> Actor? {
return value.ifString(Actor.init(name:))
}
}
extension Language: FromJson {
static func from(value: JsonValue) -> Language? {
return value.ifString { str in
switch str {
case "en": return .english
case "it": return .italian
case "lat": return .latin
case "pt": return .portuguese
case "nl": return .dutch
default: return nil
}
}
}
}