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
|
package pterm_test
import (
"testing"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"
"github.com/pterm/pterm"
)
func TestInteractiveContinuePrinter_Show_yes(t *testing.T) {
go func() {
keyboard.SimulateKeyPress('y')
}()
result, _ := pterm.DefaultInteractiveContinue.Show()
testza.AssertEqual(t, result, "yes")
go func() {
keyboard.SimulateKeyPress('Y')
}()
result, _ = pterm.DefaultInteractiveContinue.Show()
testza.AssertEqual(t, result, "yes")
}
func TestInteractiveContinuePrinter_Show_no(t *testing.T) {
go func() {
keyboard.SimulateKeyPress('n')
}()
result, _ := pterm.DefaultInteractiveContinue.Show()
testza.AssertEqual(t, result, "no")
}
func TestInteractiveContinuePrinter_WithDefaultValueIndes(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithDefaultValueIndex(1)
testza.AssertEqual(t, p.DefaultValueIndex, 1)
}
func TestInteractiveContinuePrinter_WithDefaultValue_yes(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
p := pterm.DefaultInteractiveContinue.WithDefaultValue("yes")
result, _ := p.Show()
testza.AssertEqual(t, result, "yes")
}
func TestInteractiveContinuePrinter_WithDefaultValue_no(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithDefaultValue("no")
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := p.Show()
testza.AssertEqual(t, result, "no")
go func() {
keyboard.SimulateKeyPress('n')
}()
result, _ = p.Show()
testza.AssertEqual(t, result, "no")
go func() {
keyboard.SimulateKeyPress('N')
}()
result, _ = p.Show()
testza.AssertEqual(t, result, "no")
}
func TestInteractiveContinuePrinter_WithShowShortHandles(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithShowShortHandles()
testza.AssertTrue(t, p.ShowShortHandles)
go func() {
keyboard.SimulateKeyPress('n')
}()
result, _ := p.Show()
testza.AssertEqual(t, result, "no")
}
func TestInteractiveContinuePrinter_WithOptionsStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveContinue.WithOptionsStyle(style)
testza.AssertEqual(t, p.OptionsStyle, style)
}
func TestInteractiveContinuePrinter_WithOptions(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithOptions([]string{"next", "stop", "continue"})
testza.AssertEqual(t, p.Options, []string{"next", "stop", "continue"})
}
func TestInteractiveContinuePrinter_WithHandles(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithOptions([]string{"yes", "no", "always", "never"}).WithHandles([]string{"y", "n", "a", "N"})
testza.AssertEqual(t, p.Handles, []string{"y", "n", "a", "N"})
tests := []struct {
name string
key rune
expected string
}{
{
name: "Yes",
key: 'y',
expected: "yes",
},
{
name: "No",
key: 'n',
expected: "no",
},
{
name: "Always",
key: 'a',
expected: "always",
},
{
name: "Never",
key: 'N',
expected: "never",
},
}
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)
})
}
p.DefaultValueIndex = 1
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := p.Show()
testza.AssertEqual(t, result, "no")
}
func TestInteractiveContinuePrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}
func TestInteractiveContinuePrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}
func TestInteractiveContinuePrinter_CustomAnswers(t *testing.T) {
p := pterm.DefaultInteractiveContinue.WithOptions([]string{"next", "stop", "continue"})
tests := []struct {
name string
key rune
expected string
}{
{
name: "Next",
key: 'n',
expected: "next",
},
{
name: "Stop",
key: 's',
expected: "stop",
},
{
name: "Continue",
key: 'c',
expected: "continue",
},
}
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 TestInteractiveContinuePrinter_WithSuffixStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveContinue.WithSuffixStyle(style)
testza.AssertEqual(t, p.SuffixStyle, style)
}
func TestInteractiveContinuePrinter_WithTextStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveContinue.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}
|