-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathToDoListTests.swift
243 lines (169 loc) · 8.12 KB
/
ToDoListTests.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
//
// ToDoListTests.swift
// ReSwift-Todo
//
// Created by Christian Tietze on 05/02/16.
// Copyright © 2016 ReSwift. All rights reserved.
//
import XCTest
@testable import ReSwiftTodo
class ToDoListTests: XCTestCase {
func testEqualContent_WithDifferentTitles_ReturnsFalse() {
let items = [ToDo(title: "irrelevant", completion: .finished(when: nil))]
XCTAssertFalse(ToDoList(title: "a", items: items).hasEqualContent(ToDoList(title: "b", items: items)))
}
func testEqualContent_WithDifferentItems_ReturnsFalse() {
let oneItems = [ToDo(title: "one", completion: .finished(when: nil))]
let otherItems = [ToDo(title: "other", completion: .finished(when: nil))]
XCTAssertFalse(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
func testEqualContent_With1ContentEqualItem_ReturnsTrue() {
let oneItems = [ToDo(title: "same", completion: .finished(when: nil))]
let otherItems = [ToDo(title: "same", completion: .finished(when: nil))]
XCTAssertTrue(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
func testEqualContent_With1ContentEqualItemButDifferentCountsLeft_ReturnsFalse() {
let oneItems = [
ToDo(title: "same", completion: .finished(when: nil))
]
let otherItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "other", completion: .finished(when: nil))
]
XCTAssertFalse(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
func testEqualContent_With1ContentEqualItemButDifferentCountsRight_ReturnsFalse() {
let oneItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "other", completion: .finished(when: nil))
]
let otherItems = [
ToDo(title: "same", completion: .finished(when: nil))
]
XCTAssertFalse(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
func testEqualContent_WithContentEqualItemsPlusDifference_ReturnsFalse() {
let oneItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "different", completion: .finished(when: nil))
]
let otherItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "unlike", completion: .finished(when: nil))
]
XCTAssertFalse(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
func testEqualContent_WithContentEqualDuplicateItems_ReturnsFalse() {
let oneItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "same", completion: .finished(when: nil))
]
let otherItems = [
ToDo(title: "same", completion: .finished(when: nil)),
ToDo(title: "unlike", completion: .finished(when: nil))
]
XCTAssertFalse(ToDoList(title: "a", items: oneItems).hasEqualContent(ToDoList(title: "a", items: otherItems)))
}
// MARK: Adding items
func testAppendItem_EmptyList_AddsItem() {
let toDo = ToDo(title: "an item")
var list = ToDoList.empty
list.appendItem(toDo)
let expectedList = ToDoList(title: nil, items: [toDo])
XCTAssertEqual(list, expectedList)
}
func testAppendItem_ListWithItem_AddsItemToEnd() {
let newToDo = ToDo(title: "new item")
let existingToDo = ToDo(title: "existing item")
var list = ToDoList(title: nil, items: [existingToDo])
list.appendItem(newToDo)
let expectedList = ToDoList(title: nil, items: [existingToDo, newToDo])
XCTAssertEqual(list, expectedList)
let wronglySortedList = ToDoList(title: nil, items: [newToDo, existingToDo])
XCTAssertNotEqual(list, wronglySortedList)
}
func testInsertItem_EmptyList_At0_AddsItem() {
let toDo = ToDo(title: "an item")
var list = ToDoList.empty
list.insertItem(toDo, atIndex: 0)
let expectedList = ToDoList(title: nil, items: [toDo])
XCTAssertEqual(list, expectedList)
}
func testInsertItem_EmptyList_AtNegativeValue_AddsItem() {
let toDo = ToDo(title: "an item")
var list = ToDoList.empty
list.insertItem(toDo, atIndex: -123)
let expectedList = ToDoList(title: nil, items: [toDo])
XCTAssertEqual(list, expectedList)
}
func testInsertItem_EmptyList_At1000_AddsItem() {
let toDo = ToDo(title: "an item")
var list = ToDoList.empty
list.insertItem(toDo, atIndex: 1000)
let expectedList = ToDoList(title: nil, items: [toDo])
XCTAssertEqual(list, expectedList)
}
func testInsertItem_ListWithItem_AtNegativeValue_AddsItemToBeginning() {
let newToDo = ToDo(title: "new item")
let existingToDo = ToDo(title: "existing item")
var list = ToDoList(title: nil, items: [existingToDo])
list.insertItem(newToDo, atIndex: -999)
let expectedList = ToDoList(title: nil, items: [newToDo, existingToDo])
XCTAssertEqual(list, expectedList)
let wronglySortedList = ToDoList(title: nil, items: [existingToDo, newToDo])
XCTAssertNotEqual(list, wronglySortedList)
}
func testInsertItem_ListWithItem_At0_AddsItemToBeginning() {
let newToDo = ToDo(title: "new item")
let existingToDo = ToDo(title: "existing item")
var list = ToDoList(title: nil, items: [existingToDo])
list.insertItem(newToDo, atIndex: 0)
let expectedList = ToDoList(title: nil, items: [newToDo, existingToDo])
XCTAssertEqual(list, expectedList)
let wronglySortedList = ToDoList(title: nil, items: [existingToDo, newToDo])
XCTAssertNotEqual(list, wronglySortedList)
}
func testInsertItem_ListWithItem_AtHighValue_AddsItemToEnd() {
let newToDo = ToDo(title: "new item")
let existingToDo = ToDo(title: "existing item")
var list = ToDoList(title: nil, items: [existingToDo])
list.insertItem(newToDo, atIndex: 123)
let expectedList = ToDoList(title: nil, items: [existingToDo, newToDo])
XCTAssertEqual(list, expectedList)
let wronglySortedList = ToDoList(title: nil, items: [newToDo, existingToDo])
XCTAssertNotEqual(list, wronglySortedList)
}
func testInsertItem_ListWith2Items_At1_AddsItemInBetween() {
let newToDo = ToDo(title: "new item")
let firstToDo = ToDo(title: "existing item")
let secondToDo = ToDo(title: "another existing item")
var list = ToDoList(title: nil, items: [firstToDo, secondToDo])
list.insertItem(newToDo, atIndex: 1)
let expectedList = ToDoList(title: nil, items: [firstToDo, newToDo, secondToDo])
XCTAssertEqual(list, expectedList)
}
// MARK: Removing items
func testRemoveItem_ItemWithDifferentID_ReturnsUnchangedList() {
let existingToDo = ToDo(toDoID: ToDoID(), title: "irrelevant")
var list = ToDoList(title: nil, items: [existingToDo])
list.removeItem(toDoID: ToDoID())
let expectedList = ToDoList(title: nil, items: [existingToDo])
XCTAssertEqual(list, expectedList)
}
func testRemoveItem_ItemWithSameID_ReturnsEmptyList() {
let toDoID = ToDoID()
let existingToDo = ToDo(toDoID: toDoID, title: "irrelevant")
var list = ToDoList(title: nil, items: [existingToDo])
list.removeItem(toDoID: toDoID)
XCTAssertEqual(list, ToDoList.empty)
}
func testRemoveItem_OneOf2ItemsWithSameID_ReturnsListWithOtherItemLeft() {
let toDoID = ToDoID()
let matchingToDo = ToDo(toDoID: toDoID, title: "irrelevant")
let unmatchingToDo = ToDo(toDoID: ToDoID(), title: "also irrelevant")
var list = ToDoList(title: nil, items: [matchingToDo, unmatchingToDo])
list.removeItem(toDoID: toDoID)
let expectedList = ToDoList(title: nil, items: [unmatchingToDo])
XCTAssertEqual(list, expectedList)
}
}