-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspinner_test.go
199 lines (166 loc) · 5.08 KB
/
spinner_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
package ysmrr_test
import (
"bytes"
"fmt"
"testing"
"github.com/chelnak/ysmrr"
"github.com/chelnak/ysmrr/pkg/animations"
"github.com/chelnak/ysmrr/pkg/colors"
"github.com/stretchr/testify/assert"
)
var initialMessage = "test"
var initialOpts = ysmrr.SpinnerOptions{
Message: initialMessage,
SpinnerColor: colors.NoColor,
CompleteColor: colors.NoColor,
ErrorColor: colors.NoColor,
MessageColor: colors.NoColor,
CompleteCharacter: "✓",
ErrorCharacter: "✗",
HasUpdate: make(chan bool),
}
func TestNewSpinner(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
assert.NotNil(t, spinner)
}
func TestSpinnerGetMessage(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
assert.Equal(t, initialMessage, spinner.GetMessage())
}
func TestSpinnerGetPrefix(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
assert.Equal(t, "", spinner.GetPrefix())
}
func TestSpinnerIsError(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
assert.Equal(t, false, spinner.IsError())
}
func TestSpinnerIsComplete(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
assert.Equal(t, false, spinner.IsComplete())
}
func TestSpinnerUpdateMessage(t *testing.T) {
updatedMessage := "updated message"
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.UpdateMessage(updatedMessage)
assert.Equal(t, updatedMessage, spinner.GetMessage())
}
func TestSpinnerUpdateMessagef(t *testing.T) {
expectedMessage := "updated message test"
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.UpdateMessagef("updated message %s", "test")
assert.Equal(t, expectedMessage, spinner.GetMessage())
}
func TestSpinnerUpdatePrefix(t *testing.T) {
expectedPrefix := "prefix"
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.UpdatePrefix(expectedPrefix)
assert.Equal(t, expectedPrefix, spinner.GetPrefix())
}
func TestSpinnerUpdatePrefixf(t *testing.T) {
expectedPrefix := "prefix test"
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.UpdatePrefixf("prefix %s", "test")
assert.Equal(t, expectedPrefix, spinner.GetPrefix())
}
func TestSpinnerCompleteWithMessage(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.CompleteWithMessage("complete")
assert.Equal(t, true, spinner.IsComplete())
assert.Equal(t, "complete", spinner.GetMessage())
}
func TestSpinnerCompleteWithMessagef(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.CompleteWithMessagef("complete %s", "test")
assert.Equal(t, true, spinner.IsComplete())
assert.Equal(t, "complete test", spinner.GetMessage())
}
func TestSpinnerComplete(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.Complete()
assert.Equal(t, true, spinner.IsComplete())
}
func TestCompleteCharacter(t *testing.T) {
opts := initialOpts
expectedCharacter := "*"
spinner := ysmrr.NewSpinner(opts)
spinner.CompleteCharacter(expectedCharacter)
assert.Equal(t, expectedCharacter, spinner.GetCompleteCharacter())
}
func TestErrorCharacter(t *testing.T) {
opts := initialOpts
expectedCharacter := "*"
spinner := ysmrr.NewSpinner(opts)
spinner.ErrorCharacter(expectedCharacter)
assert.Equal(t, expectedCharacter, spinner.GetErrorCharacter())
}
func TestSpinnerErrorWithMessage(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.ErrorWithMessage("error")
assert.Equal(t, true, spinner.IsError())
assert.Equal(t, "error", spinner.GetMessage())
}
func TestSpinnerErrorWithMessagef(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.ErrorWithMessagef("error %s", "test")
assert.Equal(t, true, spinner.IsError())
assert.Equal(t, "error test", spinner.GetMessage())
}
func TestSpinnerError(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.Error()
assert.Equal(t, true, spinner.IsError())
}
func TestPrint(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
var buf bytes.Buffer
_, dots := animations.GetAnimation(animations.Dots)
spinner.Print(&buf, dots[0])
want := fmt.Sprintf("%s %s\r\n", dots[0], initialMessage)
assert.Equal(t, want, buf.String())
}
func TestPrintWithPrefix(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
prefix := "prefix"
spinner.UpdatePrefix(prefix)
var buf bytes.Buffer
_, dots := animations.GetAnimation(animations.Dots)
spinner.Print(&buf, dots[0])
want := fmt.Sprintf("%s%s %s\r\n", prefix, dots[0], initialMessage)
assert.Equal(t, want, buf.String())
}
func TestPrintWithComplete(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.Complete()
var buf bytes.Buffer
spinner.Print(&buf, "✓")
want := fmt.Sprintf("%s %s\r\n", "✓", initialMessage)
assert.Equal(t, want, buf.String())
}
func TestPrintWithError(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.Error()
var buf bytes.Buffer
spinner.Print(&buf, "✗")
want := fmt.Sprintf("%s %s\r\n", "✗", initialMessage)
assert.Equal(t, want, buf.String())
}