-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
103 lines (78 loc) · 1.93 KB
/
util_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
package dbshiftcore
import (
"io/ioutil"
"os"
"testing"
)
func TestPrintSuccess(t *testing.T) {
// Formatted string => args
tests := map[string][]interface{}{
"Success": nil,
"Success: %s": {"flott!"},
"Success: %d %s!": {0, "errors"},
}
expectedResult := map[string]string{
"Success": "✔ Success\n",
"Success: %s": "✔ Success: flott!\n",
"Success: %d %s!": "✔ Success: 0 errors!\n",
}
for k, v := range tests {
rescueStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
os.Stdout = w
PrintSuccess(k, v...)
w.Close()
out, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
os.Stdout = rescueStdout
output := string(out)
runes := []rune(output)
if runes[0] != successCharacter {
t.Errorf("unexpected character %c instead of %c", runes[0], successCharacter)
}
if output != expectedResult[k] {
t.Errorf("unexpected success message %s instead of %s", output, expectedResult[k])
}
}
}
func TestPrintFailure(t *testing.T) {
// Formatted string => args
tests := map[string][]interface{}{
"Failure": nil,
"Failure: %s": {"uffda!"},
"Failure: %d %s!": {5, "errors"},
}
expectedResult := map[string]string{
"Failure": "✘ Failure\n",
"Failure: %s": "✘ Failure: uffda!\n",
"Failure: %d %s!": "✘ Failure: 5 errors!\n",
}
for k, v := range tests {
rescueStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
os.Stdout = w
PrintFailure(k, v...)
w.Close()
out, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
os.Stdout = rescueStdout
output := string(out)
runes := []rune(output)
if runes[0] != failureCharacter {
t.Errorf("unexpected character %c instead of %c", runes[0], successCharacter)
}
if output != expectedResult[k] {
t.Errorf("unexpected failure message %s instead of %s", output, expectedResult[k])
}
}
}