Skip to content

Commit f7f7f76

Browse files
committed
Convert model specs to XCTestCases
1 parent df8c8db commit f7f7f76

File tree

6 files changed

+98
-115
lines changed

6 files changed

+98
-115
lines changed

iCookTV.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
B5EFE6C21DC600FF00236D1A /* VideosGridLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EFE6C11DC600FF00236D1A /* VideosGridLayout.swift */; };
6666
B5F7BF4C1BA9C95F00A75099 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F7BF4B1BA9C95F00A75099 /* AppDelegate.swift */; };
6767
B5F7BF531BA9C95F00A75099 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B5F7BF521BA9C95F00A75099 /* Assets.xcassets */; };
68-
B5F7BF5E1BA9C95F00A75099 /* iCookTVTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F7BF5D1BA9C95F00A75099 /* iCookTVTests.swift */; };
6968
E0CC9B4F972579066B42862A /* Pods_iCookTV.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1068EAEDE861A2EEA0CF2228 /* Pods_iCookTV.framework */; };
7069
/* End PBXBuildFile section */
7170

@@ -149,7 +148,6 @@
149148
B5F7BF521BA9C95F00A75099 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
150149
B5F7BF541BA9C95F00A75099 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
151150
B5F7BF591BA9C95F00A75099 /* iCookTV.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = iCookTV.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
152-
B5F7BF5D1BA9C95F00A75099 /* iCookTVTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iCookTVTests.swift; sourceTree = "<group>"; };
153151
B5F7BF5F1BA9C95F00A75099 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
154152
CCC5F61AC4253D5EB2FF6C5F /* Pods_iCookTV_iCookTVTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iCookTV_iCookTVTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
155153
F631C183B75D6A45DF934799 /* Pods-iCookTV-iCookTVTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iCookTV-iCookTVTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-iCookTV-iCookTVTests/Pods-iCookTV-iCookTVTests.release.xcconfig"; sourceTree = "<group>"; };
@@ -347,7 +345,6 @@
347345
B5761A521CCF4D3D008CCC08 /* CategorySpec.swift */,
348346
B52BD6801D60B62E0023D4E9 /* DataCollectionSpec.swift */,
349347
B52BD67E1D6034A10023D4E9 /* DataSourceSpec.swift */,
350-
B5F7BF5D1BA9C95F00A75099 /* iCookTVTests.swift */,
351348
B5761A571CCF5752008CCC08 /* ResourceHelper.swift */,
352349
B5761A591CCF6CCD008CCC08 /* VideoSpec.swift */,
353350
);
@@ -696,7 +693,6 @@
696693
B5761A531CCF4D3D008CCC08 /* CategorySpec.swift in Sources */,
697694
B52BD6811D60B62E0023D4E9 /* DataCollectionSpec.swift in Sources */,
698695
B52BD67F1D6034A10023D4E9 /* DataSourceSpec.swift in Sources */,
699-
B5F7BF5E1BA9C95F00A75099 /* iCookTVTests.swift in Sources */,
700696
B5761A581CCF5752008CCC08 /* ResourceHelper.swift in Sources */,
701697
B5761A5A1CCF6CCD008CCC08 /* VideoSpec.swift in Sources */,
702698
);

iCookTV/Models/Category.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import Foundation
2828

29-
struct Category: Codable {
29+
struct Category {
3030

3131
let id: String
3232
let name: String
3333
let coverURLs: [String]
3434

35-
// MARK: - Codable
35+
}
36+
37+
38+
extension Category: Codable {
3639

3740
private enum CodingKeys: String, CodingKey {
3841
case id
@@ -44,6 +47,8 @@ struct Category: Codable {
4447
case coverURLs = "cover-urls"
4548
}
4649

50+
// MARK: - Decodable
51+
4752
init(from decoder: Decoder) throws {
4853
let container = try decoder.container(keyedBy: CodingKeys.self)
4954
id = try container.decode(String.self, forKey: .id)
@@ -53,6 +58,8 @@ struct Category: Codable {
5358
coverURLs = try attributes.decode([String].self, forKey: .coverURLs)
5459
}
5560

61+
// MARK: - Encodable
62+
5663
func encode(to encoder: Encoder) throws {
5764
var container = encoder.container(keyedBy: CodingKeys.self)
5865
try container.encode(id, forKey: .id)

iCookTV/Models/Video.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import Foundation
2828

29-
struct Video: Codable {
29+
struct Video {
3030

3131
let id: String
3232
let title: String
@@ -48,7 +48,10 @@ struct Video: Codable {
4848
return (hours > 0 ? "\(hours):" : "") + String(format: "%d:%02d", minutes, seconds)
4949
}
5050

51-
// MARK: - Codable
51+
}
52+
53+
54+
extension Video: Codable {
5255

5356
private enum CodingKeys: String, CodingKey {
5457
case id
@@ -65,6 +68,8 @@ struct Video: Codable {
6568
case cover = "cover-url"
6669
}
6770

71+
// MARK: - Decodable
72+
6873
init(from decoder: Decoder) throws {
6974
let container = try decoder.container(keyedBy: CodingKeys.self)
7075
id = try container.decode(String.self, forKey: .id)
@@ -79,6 +84,8 @@ struct Video: Codable {
7984
cover = try attributes.decode(String.self, forKey: .cover)
8085
}
8186

87+
// MARK: - Encodable
88+
8289
func encode(to encoder: Encoder) throws {
8390
var container = encoder.container(keyedBy: CodingKeys.self)
8491
try container.encode(id, forKey: .id)

iCookTVTests/CategorySpec.swift

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,50 @@
2525
//
2626

2727
@testable import iCookTV
28-
import Nimble
29-
import Quick
28+
import XCTest
3029

31-
class CategorySpec: QuickSpec {
32-
33-
override func spec() {
30+
final class CategorySpec: XCTestCase {
3431

32+
func testDecoding() throws {
33+
// Given Category.json
3534
let data: Data = Resources.testData(named: "Category.json")!
3635

37-
describe("decoding") {
38-
it("should parse JSON as Category") {
39-
let decoder = JSONDecoder()
40-
let category = try! decoder.decode(Category.self, from: data)
41-
42-
expect(category.id).to(equal("9527"))
43-
expect(category.name).to(equal("愛料理廚房"))
44-
expect(category.coverURLs).to(equal([
45-
"https://imag.es/1.jpg",
46-
"https://imag.es/2.jpg",
47-
"https://imag.es/3.jpg",
48-
"https://imag.es/4.jpg"
49-
]))
50-
}
51-
}
36+
// When decoding
37+
let decoder = JSONDecoder()
38+
let category = try decoder.decode(Category.self, from: data)
39+
40+
// It should parse JSON as Category
41+
XCTAssertEqual(category.id, "9527")
42+
XCTAssertEqual(category.name, "愛料理廚房")
43+
XCTAssertEqual(category.coverURLs, [
44+
"https://imag.es/1.jpg",
45+
"https://imag.es/2.jpg",
46+
"https://imag.es/3.jpg",
47+
"https://imag.es/4.jpg"
48+
])
49+
}
50+
51+
func testEncoding() throws {
52+
// Given a Category object
53+
let category = Category(
54+
id: "9527",
55+
name: "愛料理廚房",
56+
coverURLs: [
57+
"https://imag.es/1.jpg",
58+
"https://imag.es/2.jpg"
59+
]
60+
)
61+
62+
// When encoding
63+
let encoder = JSONEncoder()
64+
let json = try encoder.encode(category)
65+
let jsonString = String(data: json, encoding: .utf8)
5266

67+
// It should encode Category to JSON
68+
XCTAssert(jsonString!.contains("\"id\":\"9527\""))
69+
XCTAssert(jsonString!.contains("\"attributes\":{"))
70+
XCTAssert(jsonString!.contains("\"name\":\"愛料理廚房\""))
71+
XCTAssert(jsonString!.contains("\"cover-urls\":[\"https:\\/\\/imag.es\\/1.jpg\",\"https:\\/\\/imag.es\\/2.jpg\"]"))
5372
}
5473

5574
}

iCookTVTests/VideoSpec.swift

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,56 @@
2525
//
2626

2727
@testable import iCookTV
28-
import Nimble
29-
import Quick
28+
import XCTest
3029

31-
class VideoSpec: QuickSpec {
32-
33-
override func spec() {
30+
final class VideoSpec: XCTestCase {
3431

32+
func testDecoding() throws {
33+
// Given Video.json
3534
let data: Data = Resources.testData(named: "Video.json")!
35+
36+
// When decoding
3637
let decoder = JSONDecoder()
3738
let video = try! decoder.decode(Video.self, from: data)
3839

39-
describe("decoding") {
40-
it("should parse JSON as Video") {
41-
expect(video.id).to(equal("42"))
42-
expect(video.title).to(equal("Lorem"))
43-
expect(video.subtitle).to(equal("ipsum"))
44-
expect(video.description).to(equal("dolor sit amet"))
45-
expect(video.length).to(equal(123))
46-
expect(video.youtube).to(equal("https://www.youtube.com/watch?v=3345678"))
47-
expect(video.source).to(equal("https://vide.os/source.m3u8"))
48-
expect(video.cover).to(equal("https://imag.es/cover.jpg"))
49-
}
50-
}
40+
// It should parse JSON as Video
41+
XCTAssertEqual(video.id, "42")
42+
XCTAssertEqual(video.title, "Lorem")
43+
XCTAssertEqual(video.subtitle, "ipsum")
44+
XCTAssertEqual(video.description, "dolor sit amet")
45+
XCTAssertEqual(video.length, 123)
46+
XCTAssertEqual(video.youtube, "https://www.youtube.com/watch?v=3345678")
47+
XCTAssertEqual(video.source, "https://vide.os/source.m3u8")
48+
XCTAssertEqual(video.cover, "https://imag.es/cover.jpg")
49+
}
5150

52-
describe("encoding") {
53-
let encoder = JSONEncoder()
54-
let data = try! encoder.encode(video)
55-
let jsonString = String(data: data, encoding: .utf8)
51+
func testEncoding() throws {
52+
// Given a Video object
53+
let video = Video(
54+
id: "42",
55+
title: "Lorem",
56+
subtitle: "ipsum",
57+
description: "dolor sit amet",
58+
length: 123,
59+
youtube: "https://www.youtube.com/watch?v=3345678",
60+
source: "https://vide.os/source.m3u8",
61+
cover: "https://imag.es/cover.jpg"
62+
)
5663

57-
it("should encode Video to JSON") {
58-
expect(jsonString).to(contain("\"id\":\"42\""))
59-
expect(jsonString).to(contain("\"title\":\"Lorem\""))
60-
expect(jsonString).to(contain("\"subtitle\":\"ipsum\""))
61-
expect(jsonString).to(contain("\"description\":\"dolor sit amet\""))
62-
expect(jsonString).to(contain("\"length\":123"))
63-
expect(jsonString).to(contain("\"embed-url\":\"https:\\/\\/www.youtube.com\\/watch?v=3345678\""))
64-
expect(jsonString).to(contain("\"video-url\":\"https:\\/\\/vide.os\\/source.m3u8\""))
65-
expect(jsonString).to(contain("\"cover-url\":\"https:\\/\\/imag.es\\/cover.jpg\""))
66-
}
67-
}
64+
// When encoding
65+
let encoder = JSONEncoder()
66+
let json = try encoder.encode(video)
67+
let jsonString = String(data: json, encoding: .utf8)
6868

69+
// It should encode Video to JSON
70+
XCTAssert(jsonString!.contains("\"id\":\"42\""))
71+
XCTAssert(jsonString!.contains("\"title\":\"Lorem\""))
72+
XCTAssert(jsonString!.contains("\"subtitle\":\"ipsum\""))
73+
XCTAssert(jsonString!.contains("\"description\":\"dolor sit amet\""))
74+
XCTAssert(jsonString!.contains("\"length\":123"))
75+
XCTAssert(jsonString!.contains("\"embed-url\":\"https:\\/\\/www.youtube.com\\/watch?v=3345678\""))
76+
XCTAssert(jsonString!.contains("\"video-url\":\"https:\\/\\/vide.os\\/source.m3u8\""))
77+
XCTAssert(jsonString!.contains("\"cover-url\":\"https:\\/\\/imag.es\\/cover.jpg\""))
6978
}
7079

7180
}

iCookTVTests/iCookTVTests.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)