Skip to content

Commit 3202fe3

Browse files
authoredFeb 7, 2018
Merge pull request #3 from appunite/tests
Tests
2 parents 77ee30f + dbbe843 commit 3202fe3

9 files changed

+746
-4
lines changed
 

‎Stefan.xcodeproj/project.pbxproj

+20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
AD06B8EE202758BF000A1736 /* ItemsLoadableStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */; };
1011
AD0C13962012B6A800CEA0D0 /* ReloadingType.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0C13952012B6A800CEA0D0 /* ReloadingType.swift */; };
12+
AD127C35202719FE00486A0A /* StefanDifferTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C34202719FE00486A0A /* StefanDifferTests.swift */; };
13+
AD127C3720271A4100486A0A /* Fruit.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C3620271A4100486A0A /* Fruit.swift */; };
14+
AD127C3920271A5700486A0A /* FruitStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C3820271A5700486A0A /* FruitStorage.swift */; };
1115
AD46DDF3200E015A00D0A5B8 /* Stefan_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD46DDE9200E015A00D0A5B8 /* Stefan_iOS.framework */; };
1216
AD46DDF8200E015A00D0A5B8 /* StefanTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DDF7200E015A00D0A5B8 /* StefanTests.swift */; };
1317
AD46DDFA200E015A00D0A5B8 /* Stefan.h in Headers */ = {isa = PBXBuildFile; fileRef = AD46DDEC200E015A00D0A5B8 /* Stefan.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -41,6 +45,7 @@
4145
AD46DE4B200E06D500D0A5B8 /* FruitStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE4A200E06D500D0A5B8 /* FruitStorage.swift */; };
4246
AD46DE4E200E06F700D0A5B8 /* Fruit.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE4D200E06F700D0A5B8 /* Fruit.swift */; };
4347
AD46DE53200E143100D0A5B8 /* FruitTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE52200E143100D0A5B8 /* FruitTableViewCell.swift */; };
48+
AD5E9D55202B0AE300D0AD2B /* StefanTestController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */; };
4449
AD8ACAF020109B1B00CBDD0A /* Differ.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD8ACAEF20109B1B00CBDD0A /* Differ.framework */; };
4550
ADB93944202466D80085590A /* SectionatedStefanDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB93943202466D80085590A /* SectionatedStefanDelegate.swift */; };
4651
ADFB2E05202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADFB2E04202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift */; };
@@ -65,7 +70,11 @@
6570
/* End PBXContainerItemProxy section */
6671

6772
/* Begin PBXFileReference section */
73+
AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsLoadableStateTests.swift; sourceTree = "<group>"; };
6874
AD0C13952012B6A800CEA0D0 /* ReloadingType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReloadingType.swift; sourceTree = "<group>"; };
75+
AD127C34202719FE00486A0A /* StefanDifferTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StefanDifferTests.swift; sourceTree = "<group>"; };
76+
AD127C3620271A4100486A0A /* Fruit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fruit.swift; sourceTree = "<group>"; };
77+
AD127C3820271A5700486A0A /* FruitStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStorage.swift; sourceTree = "<group>"; };
6978
AD46DDE9200E015A00D0A5B8 /* Stefan_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Stefan_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7079
AD46DDEC200E015A00D0A5B8 /* Stefan.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Stefan.h; sourceTree = "<group>"; };
7180
AD46DDED200E015A00D0A5B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -104,6 +113,7 @@
104113
AD46DE4A200E06D500D0A5B8 /* FruitStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStorage.swift; sourceTree = "<group>"; };
105114
AD46DE4D200E06F700D0A5B8 /* Fruit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fruit.swift; sourceTree = "<group>"; };
106115
AD46DE52200E143100D0A5B8 /* FruitTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitTableViewCell.swift; sourceTree = "<group>"; };
116+
AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StefanTestController.swift; sourceTree = "<group>"; };
107117
AD8ACAEF20109B1B00CBDD0A /* Differ.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Differ.framework; path = Carthage/Build/iOS/Differ.framework; sourceTree = "<group>"; };
108118
ADB93943202466D80085590A /* SectionatedStefanDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionatedStefanDelegate.swift; sourceTree = "<group>"; };
109119
ADFB2E04202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView + LoadableStatePlaceholderPresentable.swift"; sourceTree = "<group>"; };
@@ -177,8 +187,13 @@
177187
AD46DDF6200E015A00D0A5B8 /* StefanTests */ = {
178188
isa = PBXGroup;
179189
children = (
190+
AD127C3620271A4100486A0A /* Fruit.swift */,
191+
AD127C3820271A5700486A0A /* FruitStorage.swift */,
180192
AD46DDF7200E015A00D0A5B8 /* StefanTests.swift */,
181193
AD46DDF9200E015A00D0A5B8 /* Info.plist */,
194+
AD127C34202719FE00486A0A /* StefanDifferTests.swift */,
195+
AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */,
196+
AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */,
182197
);
183198
path = StefanTests;
184199
sourceTree = "<group>";
@@ -460,6 +475,11 @@
460475
isa = PBXSourcesBuildPhase;
461476
buildActionMask = 2147483647;
462477
files = (
478+
AD5E9D55202B0AE300D0AD2B /* StefanTestController.swift in Sources */,
479+
AD06B8EE202758BF000A1736 /* ItemsLoadableStateTests.swift in Sources */,
480+
AD127C3920271A5700486A0A /* FruitStorage.swift in Sources */,
481+
AD127C3720271A4100486A0A /* Fruit.swift in Sources */,
482+
AD127C35202719FE00486A0A /* StefanDifferTests.swift in Sources */,
463483
AD46DDF8200E015A00D0A5B8 /* StefanTests.swift in Sources */,
464484
);
465485
runOnlyForDeploymentPostprocessing = 0;

‎Stefan.xcodeproj/xcshareddata/xcschemes/Stefan-iOS.xcscheme

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
language = ""
30-
shouldUseLaunchSchemeArgsEnv = "YES">
30+
shouldUseLaunchSchemeArgsEnv = "YES"
31+
codeCoverageEnabled = "YES">
3132
<Testables>
3233
<TestableReference
3334
skipped = "NO">

‎Stefan/Enums/ItemReloadingResult.swift

+23
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,26 @@ public enum ItemReloadingResult<ItemType: Equatable> {
2626
case itemsAndPlaceholder(oldItems: [ItemType], newItems: [ItemType])
2727

2828
}
29+
30+
extension ItemReloadingResult: Equatable {
31+
32+
public static func == (lhs: ItemReloadingResult<ItemType>, rhs: ItemReloadingResult<ItemType>) -> Bool {
33+
34+
switch(lhs, rhs) {
35+
36+
case (.none, .none), (.placeholder, .placeholder):
37+
return true
38+
case (.items(let lOldItems, let lNewItems), .items(let rOldItems, let rNewItems)):
39+
return lOldItems == rOldItems && lNewItems == rNewItems
40+
case (.placeholderAndItems(let lOldItems, let lNewItems), .placeholderAndItems(let rOldItems, let rNewItems)):
41+
return lOldItems == rOldItems && lNewItems == rNewItems
42+
case (.itemsAndPlaceholder(let lOldItems, let lNewItems), .itemsAndPlaceholder(let rOldItems, let rNewItems)):
43+
return lOldItems == rOldItems && lNewItems == rNewItems
44+
default:
45+
return false
46+
}
47+
}
48+
49+
50+
51+
}

‎StefanTests/Fruit.swift

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Fruit.swift
3+
// StefanTests
4+
//
5+
// Created by Szymon Mrozek on 04.02.2018.
6+
// Copyright © 2018 AppUnite. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
enum FruitSize {
12+
13+
case small
14+
case medium
15+
case big
16+
}
17+
18+
struct Fruit {
19+
20+
let name: String
21+
let size: FruitSize
22+
23+
init(name: String, size: FruitSize) {
24+
self.name = name
25+
self.size = size
26+
}
27+
28+
}
29+
30+
extension Fruit: Equatable {
31+
32+
static func == (lhs: Fruit, rhs: Fruit) -> Bool {
33+
return lhs.name == rhs.name &&
34+
lhs.size == rhs.size
35+
}
36+
37+
}

‎StefanTests/FruitStorage.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// FruitStorage.swift
3+
// StefanTests
4+
//
5+
// Created by Szymon Mrozek on 04.02.2018.
6+
// Copyright © 2018 AppUnite. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct FruitStorage {
12+
13+
static var smallFruits: [Fruit] {
14+
let fruitSize: FruitSize = .small
15+
16+
return [
17+
Fruit(name: "Blackberry", size: fruitSize),
18+
Fruit(name: "Grape", size: fruitSize),
19+
Fruit(name: "Blackcurrant", size: fruitSize),
20+
Fruit(name: "Gooseberry", size: fruitSize),
21+
Fruit(name: "Raspberry", size: fruitSize),
22+
Fruit(name: "Strawberry", size: fruitSize),
23+
Fruit(name: "Cherry", size: fruitSize),
24+
Fruit(name: "Plum", size: fruitSize)
25+
]
26+
}
27+
28+
static var mediumFruits: [Fruit] {
29+
let fruitSize: FruitSize = .medium
30+
31+
return [
32+
Fruit(name: "Apple", size: fruitSize),
33+
Fruit(name: "Peach", size: fruitSize),
34+
Fruit(name: "Pear", size: fruitSize),
35+
Fruit(name: "Kiwi", size: fruitSize),
36+
Fruit(name: "Maracuja", size: fruitSize),
37+
Fruit(name: "Guava", size: fruitSize)
38+
]
39+
}
40+
41+
static var bigFruits: [Fruit] {
42+
let fruitSize: FruitSize = .big
43+
44+
return [
45+
Fruit(name: "Banana", size: fruitSize),
46+
Fruit(name: "Mango", size: fruitSize),
47+
Fruit(name: "Pineapple", size: fruitSize),
48+
Fruit(name: "Grapefruit", size: fruitSize),
49+
Fruit(name: "Watermelon", size: fruitSize),
50+
Fruit(name: "Coconut", size: fruitSize),
51+
Fruit(name: "Papaya", size: fruitSize)
52+
]
53+
}
54+
55+
}
+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//
2+
// ItemsLoadableStateTests.swift
3+
// StefanTests
4+
//
5+
// Created by Szymon Mrozek on 04.02.2018.
6+
// Copyright © 2018 AppUnite. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
@testable import Stefan_iOS
12+
13+
class ItemsLoadableStateTests: XCTestCase {
14+
15+
enum TestError: Error {
16+
case someError
17+
}
18+
19+
override func setUp() {
20+
super.setUp()
21+
}
22+
23+
public func testItemsCount() {
24+
25+
let items = FruitStorage.smallFruits
26+
27+
var state = ItemsLoadableState<Fruit>.loaded(items: items)
28+
29+
let notEmptyItemsCount = items.count
30+
31+
XCTAssertEqual(state.itemsCount, notEmptyItemsCount)
32+
33+
state = .noContent
34+
35+
XCTAssertEqual(state.itemsCount, 0)
36+
}
37+
38+
public func testGrabbingItemsInLoadedStateCorrectly() {
39+
40+
let state = ItemsLoadableState<Fruit>.loaded(items: FruitStorage.bigFruits)
41+
42+
do {
43+
_ = try state.items()
44+
} catch {
45+
XCTFail("Should not throw error")
46+
}
47+
}
48+
49+
public func testGrabbingItemsInLoadedStateWith0Items() {
50+
51+
let state = ItemsLoadableState<Fruit>.loaded(items: [])
52+
53+
do {
54+
_ = try state.items()
55+
XCTFail("Should not return items without error")
56+
} catch let error {
57+
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.zeroItemsInLoadedState else {
58+
XCTFail("Wrong error thrown")
59+
return
60+
}
61+
}
62+
}
63+
64+
public func testGrabbingItemsInRefreshingStateCorrectly() {
65+
66+
let state = ItemsLoadableState<Fruit>.refreshing(silent: true, items: FruitStorage.mediumFruits)
67+
68+
do {
69+
_ = try state.items()
70+
} catch {
71+
XCTFail("Should not throw error")
72+
}
73+
}
74+
75+
public func testGrabbingItemsInSilentRefreshState() {
76+
77+
let state = ItemsLoadableState<Fruit>.refreshing(silent: false, items: FruitStorage.bigFruits)
78+
79+
do {
80+
_ = try state.items()
81+
XCTFail("Should not return items without error")
82+
} catch let error {
83+
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.wrongStateForReadingItems else {
84+
XCTFail("Wrong error thrown")
85+
return
86+
}
87+
}
88+
}
89+
90+
public func testGrabbingItemsInWrongStates() {
91+
92+
let idleState = ItemsLoadableState<Fruit>.idle
93+
let noContentState = ItemsLoadableState<Fruit>.noContent
94+
let loadingState = ItemsLoadableState<Fruit>.loading
95+
96+
do {
97+
_ = try idleState.items()
98+
_ = try noContentState.items()
99+
_ = try loadingState.items()
100+
XCTFail("Should not return items without error")
101+
} catch let error {
102+
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.wrongStateForReadingItems else {
103+
XCTFail("Wrong error thrown")
104+
return
105+
}
106+
}
107+
}
108+
109+
public func testInitWithItemsExplicity() {
110+
111+
let items = FruitStorage.mediumFruits
112+
113+
let state = ItemsLoadableState<Fruit>(items)
114+
115+
XCTAssertEqual(state, ItemsLoadableState<Fruit>.loaded(items: items))
116+
}
117+
118+
public func testInitWithNoItems() {
119+
120+
let state = ItemsLoadableState<Fruit>([])
121+
XCTAssertEqual(state, ItemsLoadableState<Fruit>.noContent)
122+
}
123+
124+
public func testStatesComparingNoItems() {
125+
126+
var lState = ItemsLoadableState<Fruit>.idle
127+
var rState = ItemsLoadableState<Fruit>.idle
128+
XCTAssertEqual(lState, rState)
129+
130+
131+
lState = ItemsLoadableState<Fruit>.loading
132+
rState = ItemsLoadableState<Fruit>.loading
133+
XCTAssertEqual(lState, rState)
134+
135+
136+
lState = ItemsLoadableState<Fruit>.noContent
137+
rState = ItemsLoadableState<Fruit>.noContent
138+
XCTAssertEqual(lState, rState)
139+
140+
141+
lState = ItemsLoadableState<Fruit>.error(TestError.someError)
142+
rState = ItemsLoadableState<Fruit>.error(TestError.someError)
143+
XCTAssertEqual(lState, rState)
144+
145+
146+
lState = ItemsLoadableState<Fruit>.idle
147+
rState = ItemsLoadableState<Fruit>.loading
148+
XCTAssertNotEqual(lState, rState)
149+
150+
}
151+
152+
public func testStatesComparingRefreshing() {
153+
154+
let items = FruitStorage.smallFruits
155+
156+
var lState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
157+
var rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
158+
XCTAssertEqual(lState, rState)
159+
160+
161+
lState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
162+
rState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
163+
XCTAssertEqual(lState, rState)
164+
165+
166+
lState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
167+
rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
168+
XCTAssertNotEqual(lState, rState)
169+
170+
171+
lState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: [])
172+
rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
173+
XCTAssertNotEqual(lState, rState)
174+
175+
}
176+
177+
public func testStatesComparingLoaded() {
178+
179+
let items = FruitStorage.smallFruits
180+
181+
var lState = ItemsLoadableState<Fruit>.loaded(items: items)
182+
var rState = ItemsLoadableState<Fruit>.loaded(items: items)
183+
XCTAssertEqual(lState, rState)
184+
185+
186+
lState = ItemsLoadableState<Fruit>.loaded(items: [])
187+
rState = ItemsLoadableState<Fruit>.loaded(items: items)
188+
XCTAssertNotEqual(lState, rState)
189+
190+
191+
lState = ItemsLoadableState<Fruit>.loaded(items: items)
192+
rState = ItemsLoadableState<Fruit>.loaded(items: items + FruitStorage.mediumFruits)
193+
XCTAssertNotEqual(lState, rState)
194+
195+
}
196+
197+
}

0 commit comments

Comments
 (0)
Failed to load comments.