-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwlog_test.go
183 lines (168 loc) · 4.88 KB
/
wlog_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
package wlog
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var newCases = []struct {
in io.Reader
out io.Writer
err io.Writer
}{
{strings.NewReader("Hello Worlds\r\n"), os.Stdout, nil},
{nil, nil, nil},
{os.Stdin, nil, os.Stderr},
}
var addColorCases = []struct {
logColor Color
outputColor Color
successColor Color
infoColor Color
errorColor Color
warnColor Color
runningColor Color
askColor Color
responseColor Color
}{
{None, Blue, Green, Red, Yellow, Cyan, Magenta, White, Black},
{None, None, None, None, None, None, None, None, None},
{BrightBlue, BrightGreen, BrightRed, BrightYellow, BrightCyan, BrightMagenta, BrightWhite, BrightBlack, None},
}
var addPrefixCases = []struct {
ask string
err string
inf string
log string
out string
suc string
run string
war string
}{
{"", "", "", "", "", "", "", ""},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{Cross, Check, "!", "~", "@", "#", "+", "="},
{"%", "^", "&", "*", "@", ":", ",", "?"},
}
var trimCases = []struct {
input string
trim string
}{
{" 123 \r\n", " "},
{" 123 \r\n", " "},
{"!!123!!\r\n", "!"},
{" !!123!! \r\n", "! "},
}
func Example() {
var ui UI
reader := strings.NewReader("User Input\r\n") // Simulate user typing "User Input" then pressing [enter] when reading from os.Stdin
ui = New(reader, os.Stdout, os.Stdout)
ui = AddPrefix("?", Cross, " ", "", "", "~", Check, "!", ui)
ui = AddConcurrent(ui)
_, err := ui.Ask("Ask question", " ")
if err != nil {
fmt.Println(err)
}
ui.Error("Error message")
ui.Info("Info message")
ui.Output("Output message")
ui.Running("Running message")
ui.Success("Success message")
ui.Warn("Warning message")
// Output:
// ? Ask question
// ✗ Error message
// Info message
// Output message
// ~ Running message
// ✓ Success message
// ! Warning message
}
func TestNew(t *testing.T) {
assert := assert.New(t)
for _, c := range newCases {
basic := New(c.in, c.out, c.err)
assert.Equal(c.in, basic.Reader)
assert.Equal(c.out, basic.Writer)
assert.Equal(c.err, basic.ErrorWriter)
}
}
func TestAskTrim(t *testing.T) {
assert := assert.New(t)
for _, c := range trimCases {
writer, errWriter, in := initTest(c.input)
basic := New(in, writer, errWriter)
res, err := basic.Ask("Awesome string", c.trim)
if err != nil {
assert.Fail(err.Error())
}
out, err := writer.ReadString((byte)('\n'))
if err != nil {
assert.Fail(err.Error())
}
_, err = errWriter.ReadString((byte)('\n'))
expectedString := "Awesome string\n"
assert.Equal("EOF", err.Error())
assert.Equal(expectedString, out)
expectedAns := strings.Replace(c.input, "\r", "", -1)
expectedAns = strings.Replace(expectedAns, "\n", "", -1)
expectedAns = strings.Trim(expectedAns, c.trim)
assert.Equal(expectedAns, res)
}
}
func TestAddColor(t *testing.T) {
assert := assert.New(t)
basic := New(os.Stdin, os.Stdout, os.Stderr)
for _, c := range addColorCases {
color := AddColor(c.askColor, c.errorColor, c.infoColor, c.logColor, c.outputColor, c.responseColor, c.runningColor, c.successColor, c.warnColor, basic)
assert.Equal(None, color.AskBGColor)
assert.Equal(None, color.ErrorBGColor)
assert.Equal(None, color.InfoBGColor)
assert.Equal(None, color.LogBGColor)
assert.Equal(None, color.OutputBGColor)
assert.Equal(None, color.ResponseBGColor)
assert.Equal(None, color.RunningBGColor)
assert.Equal(None, color.SuccessBGColor)
assert.Equal(None, color.WarnBGColor)
assert.Equal(c.askColor, color.AskFGColor)
assert.Equal(c.errorColor, color.ErrorFGColor)
assert.Equal(c.infoColor, color.InfoFGColor)
assert.Equal(c.logColor, color.LogFGColor)
assert.Equal(c.outputColor, color.OutputFGColor)
assert.Equal(c.responseColor, color.ResponseFGColor)
assert.Equal(c.runningColor, color.RunningFGColor)
assert.Equal(c.successColor, color.SuccessFGColor)
assert.Equal(c.warnColor, color.WarnFGColor)
assert.Equal(basic, color.UI)
}
}
func TestAddPrefix(t *testing.T) {
assert := assert.New(t)
basic := New(os.Stdin, os.Stdout, os.Stderr)
for _, c := range addPrefixCases {
prefix := AddPrefix(c.ask, c.err, c.inf, c.log, c.out, c.run, c.suc, c.war, basic)
assert.Equal(c.ask, prefix.AskPrefix)
assert.Equal(c.err, prefix.ErrorPrefix)
assert.Equal(c.inf, prefix.InfoPrefix)
assert.Equal(c.log, prefix.LogPrefix)
assert.Equal(c.out, prefix.OutputPrefix)
assert.Equal(c.run, prefix.RunningPrefix)
assert.Equal(c.suc, prefix.SuccessPrefix)
assert.Equal(c.war, prefix.WarnPrefix)
assert.Equal(basic, prefix.UI)
}
}
func TestAddConcurrent(t *testing.T) {
basic := New(os.Stdin, os.Stdout, os.Stderr)
con := AddConcurrent(basic)
assert.Equal(t, basic, con.UI)
}
func initTest(input string) (*bytes.Buffer, *bytes.Buffer, io.Reader) {
var b []byte
var e []byte
return bytes.NewBuffer(b), bytes.NewBuffer(e), strings.NewReader(input)
}