forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive_confirm_printer_test.go
145 lines (126 loc) · 3.89 KB
/
interactive_confirm_printer_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
package pterm_test
import (
"reflect"
"testing"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"
"github.com/pterm/pterm"
)
func TestInteractiveConfirmPrinter_Show_yes(t *testing.T) {
go func() {
keyboard.SimulateKeyPress('y')
}()
result, _ := pterm.DefaultInteractiveConfirm.Show()
testza.AssertTrue(t, result)
}
func TestInteractiveConfirmPrinter_Show_no(t *testing.T) {
go func() {
keyboard.SimulateKeyPress('n')
}()
result, _ := pterm.DefaultInteractiveConfirm.Show()
testza.AssertFalse(t, result)
}
func TestInteractiveConfirmPrinter_WithDefaultValue(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithDefaultValue(true)
testza.AssertTrue(t, p.DefaultValue)
}
func TestInteractiveConfirmPrinter_WithDefaultValue_false(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
p := pterm.DefaultInteractiveConfirm.WithDefaultValue(false)
result, _ := p.Show()
testza.AssertFalse(t, result)
}
func TestInteractiveConfirmPrinter_WithDefaultValue_true(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
p := pterm.DefaultInteractiveConfirm.WithDefaultValue(true)
result, _ := p.Show()
testza.AssertTrue(t, result)
}
func TestInteractiveConfirmPrinter_WithConfirmStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithConfirmStyle(style)
testza.AssertEqual(t, p.ConfirmStyle, style)
}
func TestInteractiveConfirmPrinter_WithConfirmText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithConfirmText("confirm")
testza.AssertEqual(t, p.ConfirmText, "confirm")
}
func TestInteractiveConfirmPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}
func TestInteractiveConfirmPrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}
func TestInteractiveConfirmPrinter_WithRejectStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithRejectStyle(style)
testza.AssertEqual(t, p.RejectStyle, style)
}
func TestInteractiveConfirmPrinter_WithRejectText(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithRejectText("reject")
testza.AssertEqual(t, p.RejectText, "reject")
}
func TestInteractiveConfirmPrinter_CustomAnswers(t *testing.T) {
p := pterm.DefaultInteractiveConfirm.WithRejectText("reject").WithConfirmText("accept")
tests := []struct {
name string
key rune
expected bool
}{
{
name: "Accept_upper_case",
key: 'A',
expected: true,
},
{
name: "Accept_lower",
key: 'a',
expected: true,
},
{
name: "Reject_upper_case",
key: 'R',
expected: false,
},
{
name: "Reject_lower_case",
key: 'r',
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(tc.key)
}()
result, _ := p.Show()
testza.AssertEqual(t, result, tc.expected)
})
}
}
func TestInteractiveConfirmPrinter_WithSuffixStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithSuffixStyle(style)
testza.AssertEqual(t, p.SuffixStyle, style)
}
func TestInteractiveConfirmPrinter_WithTextStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveConfirm.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}
func TestInteractiveConfirmPrinter_WithOnInterruptFunc(t *testing.T) {
// OnInterrupt function defaults to nil
pd := pterm.InteractiveConfirmPrinter{}
testza.AssertNil(t, pd.OnInterruptFunc)
// Verify OnInterrupt is set
exitfunc := func() {}
p := pterm.DefaultInteractiveConfirm.WithOnInterruptFunc(exitfunc)
testza.AssertEqual(t, reflect.ValueOf(p.OnInterruptFunc).Pointer(), reflect.ValueOf(exitfunc).Pointer())
}