-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathTableSelectionTests.swift
More file actions
161 lines (145 loc) · 5.01 KB
/
TableSelectionTests.swift
File metadata and controls
161 lines (145 loc) · 5.01 KB
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
//
// TableSelectionTests.swift
// TableProTests
//
import Foundation
@testable import TablePro
import Testing
@Suite("TableSelection")
struct TableSelectionTests {
@Test("Default selection is empty")
func defaultIsEmpty() {
let selection = TableSelection()
#expect(selection.focusedRow == -1)
#expect(selection.focusedColumn == -1)
#expect(selection.anchor == -1)
#expect(selection.pivot == -1)
#expect(selection.hasFocus == false)
}
@Test("hasFocus requires both row and column")
func hasFocusRequiresBoth() {
var selection = TableSelection()
selection.focusedRow = 5
#expect(selection.hasFocus == false)
selection.focusedColumn = 2
#expect(selection.hasFocus == true)
selection.focusedRow = -1
#expect(selection.hasFocus == false)
}
@Test("clearFocus resets focus only")
func clearFocusKeepsAnchor() {
var selection = TableSelection()
selection.setFocus(row: 5, column: 2)
selection.resetAnchor(at: 5)
selection.clearFocus()
#expect(selection.focusedRow == -1)
#expect(selection.focusedColumn == -1)
#expect(selection.anchor == 5)
#expect(selection.pivot == 5)
}
@Test("setFocus assigns row and column")
func setFocus() {
var selection = TableSelection()
selection.setFocus(row: 7, column: 3)
#expect(selection.focusedRow == 7)
#expect(selection.focusedColumn == 3)
}
@Test("resetAnchor sets both anchor and pivot")
func resetAnchor() {
var selection = TableSelection()
selection.resetAnchor(at: 4)
#expect(selection.anchor == 4)
#expect(selection.pivot == 4)
}
@Test("clearAnchor resets anchor and pivot")
func clearAnchor() {
var selection = TableSelection()
selection.resetAnchor(at: 4)
selection.clearAnchor()
#expect(selection.anchor == -1)
#expect(selection.pivot == -1)
}
@Test("Equatable compares all four fields")
func equatable() {
var a = TableSelection()
a.setFocus(row: 1, column: 2)
a.resetAnchor(at: 1)
var b = a
#expect(a == b)
b.focusedRow = 2
#expect(a != b)
}
}
@Suite("TableSelection.reloadIndexes")
struct TableSelectionReloadIndexesTests {
@Test("No change returns nil")
func noChange() {
var selection = TableSelection()
selection.setFocus(row: 5, column: 2)
let same = selection
#expect(selection.reloadIndexes(from: same) == nil)
}
@Test("Anchor change without focus change returns nil")
func anchorOnlyChange() {
var previous = TableSelection()
previous.setFocus(row: 5, column: 2)
var current = previous
current.resetAnchor(at: 8)
#expect(current.reloadIndexes(from: previous) == nil)
}
@Test("Initial focus from empty includes new cell only")
func initialFocus() {
let previous = TableSelection()
var current = previous
current.setFocus(row: 3, column: 1)
let result = current.reloadIndexes(from: previous)
#expect(result?.rows == IndexSet([3]))
#expect(result?.columns == IndexSet([1]))
}
@Test("Clearing focus includes old cell only")
func clearFocusFromActive() {
var previous = TableSelection()
previous.setFocus(row: 3, column: 1)
var current = previous
current.clearFocus()
let result = current.reloadIndexes(from: previous)
#expect(result?.rows == IndexSet([3]))
#expect(result?.columns == IndexSet([1]))
}
@Test("Row change at same column reloads both rows")
func rowChange() {
var previous = TableSelection()
previous.setFocus(row: 3, column: 2)
var current = previous
current.focusedRow = 4
let result = current.reloadIndexes(from: previous)
#expect(result?.rows == IndexSet([3, 4]))
#expect(result?.columns == IndexSet([2]))
}
@Test("Column change at same row reloads both columns")
func columnChange() {
var previous = TableSelection()
previous.setFocus(row: 3, column: 2)
var current = previous
current.focusedColumn = 5
let result = current.reloadIndexes(from: previous)
#expect(result?.rows == IndexSet([3]))
#expect(result?.columns == IndexSet([2, 5]))
}
@Test("Both change reloads both rows and both columns")
func bothChange() {
var previous = TableSelection()
previous.setFocus(row: 3, column: 2)
var current = previous
current.setFocus(row: 7, column: 5)
let result = current.reloadIndexes(from: previous)
#expect(result?.rows == IndexSet([3, 7]))
#expect(result?.columns == IndexSet([2, 5]))
}
@Test("Clearing focus from no-focus state returns nil")
func clearFromEmpty() {
let previous = TableSelection()
let current = previous
#expect(current.reloadIndexes(from: previous) == nil)
}
}