Repository navigation
Expand file tree
/
Copy pathalias_test.go
More file actions
267 lines (229 loc) · 7.79 KB
/
Copy pathalias_test.go
File metadata and controls
267 lines (229 loc) · 7.79 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package simplecsv
import (
"reflect"
"testing"
)
// Methods that return a SimpleCsv never modify the receiver; the returned
// csv shares no mutable state with the receiver. Getters return copies.
func TestDeleteRowDoesNotModifyReceiver(t *testing.T) {
orig := SimpleCsv{{"A"}, {"1"}, {"2"}, {"3"}}
want := SimpleCsv{{"A"}, {"1"}, {"2"}, {"3"}}
deleted, ok := orig.DeleteRow(1)
if !ok {
t.Fatalf("DeleteRow() ok = false, want true")
}
if !reflect.DeepEqual(orig, want) {
t.Errorf("DeleteRow() modified the receiver: orig = %v, want %v", orig, want)
}
wantDeleted := SimpleCsv{{"A"}, {"2"}, {"3"}}
if !reflect.DeepEqual(deleted, wantDeleted) {
t.Errorf("DeleteRow() = %v, want %v", deleted, wantDeleted)
}
}
func TestWritersDoNotModifyReceiver(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}}
orig.SetCell(0, 1, "x")
orig.SetCellByField("A", 1, "x")
orig.SetRow(1, []string{"x", "y"})
orig.AddRow([]string{"x", "y"})
orig.DeleteRow(1)
orig.RenameHeader("A", "x")
orig.AddEmptyColumn("C")
orig.RemoveColumn(0)
if !reflect.DeepEqual(orig, want) {
t.Errorf("write methods modified the receiver: orig = %v, want %v", orig, want)
}
}
func TestSetCellOnSortedCopyDoesNotChangeSource(t *testing.T) {
orig := SimpleCsv{{"b"}, {"a"}, {"c"}}
want := SimpleCsv{{"b"}, {"a"}, {"c"}}
sorted, ok := orig.SortByColumn(0, true)
if !ok {
t.Fatalf("SortByColumn() ok = false, want true")
}
sorted, _ = sorted.SetCell(0, 0, "ZZZ")
if sorted[0][0] != "ZZZ" {
t.Errorf("SetCell() did not change the sorted copy: %v", sorted)
}
if !reflect.DeepEqual(orig, want) {
t.Errorf("SetCell() on a sorted copy changed the source: orig = %v, want %v", orig, want)
}
}
func TestSetCellOnFilteredCopyDoesNotChangeSource(t *testing.T) {
orig := SimpleCsv{{"Name", "Age"}, {"Ana", "30"}, {"Bob", "7"}}
want := SimpleCsv{{"Name", "Age"}, {"Ana", "30"}, {"Bob", "7"}}
filtered := orig.FilterRows(func(row []string) bool { return row[1] != "7" }, true)
filtered, _ = filtered.SetCell(1, 1, "99")
if filtered[1][1] != "99" {
t.Errorf("SetCell() did not change the filtered copy: %v", filtered)
}
if !reflect.DeepEqual(orig, want) {
t.Errorf("SetCell() on a filtered copy changed the source: orig = %v, want %v", orig, want)
}
}
func TestSetCellOnOnlyThisRowsCopyDoesNotChangeSource(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
only, ok := orig.OnlyThisRows([]int{2}, true)
if !ok {
t.Fatalf("OnlyThisRows() ok = false, want true")
}
only, _ = only.SetCell(0, 1, "ZZZ")
if only[1][0] != "ZZZ" {
t.Errorf("SetCell() did not change the OnlyThisRows copy: %v", only)
}
if !reflect.DeepEqual(orig, want) {
t.Errorf("SetCell() on an OnlyThisRows copy changed the source: orig = %v, want %v", orig, want)
}
}
func TestGetRowReturnsCopy(t *testing.T) {
s := SimpleCsv{{"A", "B"}, {"1", "2"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}}
row, ok := s.GetRow(1)
if !ok {
t.Fatalf("GetRow() ok = false, want true")
}
row[0] = "CHANGED"
if !reflect.DeepEqual(s, want) {
t.Errorf("mutating the GetRow() result changed the csv: %v", s)
}
}
func TestGetHeadersReturnsCopy(t *testing.T) {
s := SimpleCsv{{"A", "B"}, {"1", "2"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}}
headers := s.GetHeaders()
headers[0] = "CHANGED"
if !reflect.DeepEqual(s, want) {
t.Errorf("mutating the GetHeaders() result changed the csv: %v", s)
}
}
func TestAddRowDoesNotShareBackingArray(t *testing.T) {
base := make(SimpleCsv, 1, 4)
base[0] = []string{"A", "B"}
s1, _ := base.AddRow([]string{"1", "2"})
s2, _ := base.AddRow([]string{"3", "4"})
if s1[1][0] != "1" {
t.Errorf("AddRow() result was overwritten by a later AddRow(): s1 = %v", s1)
}
if s2[1][0] != "3" {
t.Errorf("AddRow() = %v, want row [3 4]", s2)
}
if len(base) != 1 {
t.Errorf("AddRow() modified the receiver: base = %v", base)
}
}
func TestCreateEmptyCsvDoesNotAliasHeaders(t *testing.T) {
columnNames := []string{"A", "B"}
s, err := CreateEmptyCsv(columnNames)
if err != nil {
t.Fatalf("CreateEmptyCsv() unexpected error = %v", err)
}
columnNames[0] = "MUTATED"
if s[0][0] != "A" {
t.Errorf("mutating the input slice changed the csv headers: %v", s[0])
}
}
func TestOnlyThisFieldsDoesNotAliasFields(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}}
fields := []string{"A"}
only, ok := orig.OnlyThisFields(fields)
if !ok {
t.Fatalf("OnlyThisFields() ok = false, want true")
}
fields[0] = "MUTATED"
if only[0][0] != "A" {
t.Errorf("mutating the fields slice changed the csv headers: %v", only[0])
}
}
// Mutating any row of a returned csv must not change the csv it came from.
func TestDeleteRowResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
next, ok := orig.DeleteRow(2)
if !ok {
t.Fatalf("DeleteRow() ok = false, want true")
}
next[1][0] = "MUTATED"
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the DeleteRow() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestAddRowResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}}
next, ok := orig.AddRow([]string{"3", "4"})
if !ok {
t.Fatalf("AddRow() ok = false, want true")
}
next[0][0] = "MUTATED"
next[1][1] = "MUTATED"
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the AddRow() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestSetRowResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
next, ok := orig.SetRow(1, []string{"x", "y"})
if !ok {
t.Fatalf("SetRow() ok = false, want true")
}
next[0][0] = "MUTATED" // header row, not set
next[2][0] = "MUTATED" // untouched row
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the SetRow() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestSetCellResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}, {"3", "4"}}
next, ok := orig.SetCell(0, 1, "x")
if !ok {
t.Fatalf("SetCell() ok = false, want true")
}
next[0][0] = "MUTATED" // other row: header
next[2][0] = "MUTATED" // other row: body
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the SetCell() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestRenameHeaderResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"A", "B"}, {"1", "2"}}
want := SimpleCsv{{"A", "B"}, {"1", "2"}}
next, ok := orig.RenameHeader("A", "C")
if !ok {
t.Fatalf("RenameHeader() ok = false, want true")
}
next[1][0] = "MUTATED" // non-header row
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the RenameHeader() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestSortByColumnResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"b", "2"}, {"a", "1"}, {"c", "3"}}
want := SimpleCsv{{"b", "2"}, {"a", "1"}, {"c", "3"}}
sorted, ok := orig.SortByColumn(0, true)
if !ok {
t.Fatalf("SortByColumn() ok = false, want true")
}
for i := range sorted {
sorted[i][0] = "MUTATED"
}
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the SortByColumn() result changed the source: orig = %v, want %v", orig, want)
}
}
func TestSortByFieldResultSharesNoData(t *testing.T) {
orig := SimpleCsv{{"K", "V"}, {"b", "2"}, {"a", "1"}}
want := SimpleCsv{{"K", "V"}, {"b", "2"}, {"a", "1"}}
sorted, ok := orig.SortByField("K", true)
if !ok {
t.Fatalf("SortByField() ok = false, want true")
}
sorted[0][0] = "MUTATED" // header row
sorted[1][0] = "MUTATED" // body row
if !reflect.DeepEqual(orig, want) {
t.Errorf("mutating the SortByField() result changed the source: orig = %v, want %v", orig, want)
}
}