-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_test.go
333 lines (274 loc) · 9 KB
/
table_test.go
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package termui
import (
"bytes"
"fmt"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Table", func() {
var (
outputs *bytes.Buffer
table *Table
)
BeforeEach(func() {
outputs = &bytes.Buffer{}
table = NewTable("watashi", "no", "atama!")
})
It("prints the header", func() {
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi", "no", "atama!"},
))
})
Describe("REGRESSION: #117404629, having a space in one of the middle headers", func() {
BeforeEach(func() {
outputs = &bytes.Buffer{}
table = NewTable("watashi", "no ", "atama!")
})
It("prints the table without panicking", func() {
Expect(func() {
table.PrintTo(outputs)
}).NotTo(Panic())
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi", "no", "atama!"},
))
})
It("prints the table with the extra whitespace from the header stripped", func() {
Expect(func() {
table.PrintTo(outputs)
}).NotTo(Panic())
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
))
})
})
It("prints format string literals as strings", func() {
table.Add("cloak %s", "and", "dagger")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"cloak %s", "and", "dagger"},
))
})
It("prints all the rows you give it", func() {
table.Add("something", "and", "nothing")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"something", "and", "nothing"},
))
})
Describe("adding rows to be printed later", func() {
It("prints them when you call Print()", func() {
table.Add("a", "b", "c")
table.Add("passed", "to", "print")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"a", "b", "c"},
))
})
It("flushes previously added rows and then outputs passed rows", func() {
table.Add("a", "b", "c")
table.Add("passed", "to", "print")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi", "no", "atama!"},
[]string{"a", "b", "c"},
[]string{"passed", "to", "print"},
))
})
})
It("prints a newline for the headers, and nothing for rows", func() {
table = NewTable()
table.PrintTo(outputs)
Expect(outputs.String()).To(Equal("\n"))
})
It("prints nothing with suppressed headers and no rows", func() {
table.NoHeaders()
table.PrintTo(outputs)
Expect(outputs.String()).To(BeEmpty())
})
It("does not print the header when suppressed", func() {
table.NoHeaders()
table.Add("cloak", "and", "dagger")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(Not(ContainSubstrings(
[]string{"watashi", "no", "atama!"},
)))
Expect(s).To(ContainSubstrings(
[]string{"cloak", "and", "dagger"},
))
})
It("prints cell strings as specified by column transformers", func() {
table.Add("cloak", "and", "dagger")
table.SetTransformer(0, func(s string) string {
return "<<" + s + ">>"
})
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"<<cloak>>", "and", "dagger"},
))
Expect(s).To(Not(ContainSubstrings(
[]string{"<<watashi>>"},
)))
})
It("prints no more columns than headers", func() {
table.Add("something", "and", "nothing", "ignored")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(Not(ContainSubstrings(
[]string{"ignored"},
)))
})
It("avoids printing trailing whitespace for empty columns", func() {
table.Add("something", "and")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"something and"},
))
})
It("avoids printing trailing whitespace for whitespace columns", func() {
table.Add("something", " ")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"something"},
))
})
It("even avoids printing trailing whitespace for multi-line cells", func() {
table.Add("a", "b\nd", "\nc")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"a b"},
[]string{" d c"},
))
})
It("prints multi-line cells on separate physical lines", func() {
table.Add("a", "b\nd", "c")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"a b c"},
[]string{" d"},
))
})
Describe("aligning columns", func() {
It("aligns rows to the header when the header is longest", func() {
table.Add("a", "b", "c")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"a b c"},
))
})
It("aligns rows to the longest row provided", func() {
table.Add("x", "y", "z")
table.Add("something", "something", "darkside")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"x y z"},
[]string{"something something darkside"},
))
})
It("aligns rows to the longest row provided when there are multibyte characters present", func() {
table.Add("x", "ÿ", "z")
table.Add("something", "something", "darkside")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"watashi no atama!"},
[]string{"x ÿ z"},
[]string{"something something darkside"},
))
})
It("supports multi-byte Japanese runes", func() {
table = NewTable("", "", "", "", "", "")
table.Add("名前", "要求された状態", "インスタンス", "メモリー", "ディスク", "URL")
table.Add("app-name", "stopped", "0/1", "1G", "1G", "app-name.example.com")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"名前 要求された状態 インスタンス メモリー ディスク URL"},
[]string{"app-name stopped 0/1 1G 1G app-name.example.com"},
))
})
It("supports multi-byte French runes", func() {
table = NewTable("", "", "", "", "", "")
table.Add("nom", "état demandé", "instances", "mémoire", "disque", "adresses URL")
table.Add("app-name", "stopped", "0/1", "1G", "1G", "app-name.example.com")
table.PrintTo(outputs)
s := strings.Split(outputs.String(), "\n")
Expect(s).To(ContainSubstrings(
[]string{"nom état demandé instances mémoire disque adresses URL"},
[]string{"app-name stopped 0/1 1G 1G app-name.example.com"},
))
})
})
})
// SliceMatcher looks for all substrings passed to it in the given string.
type SliceMatcher struct {
expected [][]string
failedAtIndex int
}
// ContainSubstrings returns an OmegaMatcher that looks for all substrings
// passed to it in whatever it's being matched against.
func ContainSubstrings(substrings ...[]string) OmegaMatcher {
return &SliceMatcher{expected: substrings}
}
func (matcher *SliceMatcher) Match(actual interface{}) (success bool, err error) {
actualStrings, ok := actual.([]string)
if !ok {
return false, nil
}
allStringsMatched := make([]bool, len(matcher.expected))
for index, expectedArray := range matcher.expected {
for _, actualValue := range actualStrings {
allStringsFound := true
for _, expectedValue := range expectedArray {
allStringsFound = allStringsFound && strings.Contains(Decolorize(actualValue), expectedValue)
}
if allStringsFound {
allStringsMatched[index] = true
break
}
}
}
for index, value := range allStringsMatched {
if !value {
matcher.failedAtIndex = index
return false, nil
}
}
return true, nil
}
func (matcher *SliceMatcher) FailureMessage(actual interface{}) string {
actualStrings, ok := actual.([]string)
if !ok {
return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
}
return fmt.Sprintf("expected to find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
}
func (matcher *SliceMatcher) NegatedFailureMessage(actual interface{}) string {
actualStrings, ok := actual.([]string)
if !ok {
return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
}
return fmt.Sprintf("expected to not find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
}