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
|
package survey
import (
"fmt"
"testing"
"github.com/AlecAivazis/survey/v2/core"
expect "github.com/Netflix/go-expect"
"github.com/stretchr/testify/assert"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestPasswordRender(t *testing.T) {
tests := []struct {
title string
prompt Password
data PasswordTemplateData
expected string
}{
{
"Test Password question output",
Password{Message: "Tell me your secret:"},
PasswordTemplateData{},
fmt.Sprintf("%s Tell me your secret: ", defaultIcons().Question.Text),
},
{
"Test Password question output with help hidden",
Password{Message: "Tell me your secret:", Help: "This is helpful"},
PasswordTemplateData{},
fmt.Sprintf("%s Tell me your secret: [%s for help] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Password question output with help shown",
Password{Message: "Tell me your secret:", Help: "This is helpful"},
PasswordTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s Tell me your secret: ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
}
for _, test := range tests {
test.data.Password = test.prompt
// set the icon set
test.data.Config = defaultPromptConfig()
actual, _, err := core.RunTemplate(
PasswordQuestionTemplate,
&test.data,
)
assert.Nil(t, err, test.title)
assert.Equal(t, test.expected, actual, test.title)
}
}
func TestPasswordPrompt(t *testing.T) {
tests := []PromptTest{
{
"Test Password prompt interaction",
&Password{
Message: "Please type your password",
},
func(c *expect.Console) {
c.ExpectString("Please type your password")
c.Send("secret")
c.SendLine("")
c.ExpectEOF()
},
"secret",
},
{
"Test Password prompt interaction with help",
&Password{
Message: "Please type your password",
Help: "It's a secret",
},
func(c *expect.Console) {
c.ExpectString("Please type your password")
c.SendLine("?")
c.ExpectString("It's a secret")
c.Send("secret")
c.SendLine("")
c.ExpectEOF()
},
"secret",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
RunPromptTest(t, test)
})
}
}
|