Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
(feat): tests input navagation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 29, 2021
1 parent dac17cc commit e50e421
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
6 changes: 1 addition & 5 deletions input.go
Expand Up @@ -21,7 +21,6 @@ type Input struct {
Default string
Help string
Suggest func(toComplete string) []string
typedAnswer string
answer string
options []core.OptionAnswer
selectedIndex int
Expand Down Expand Up @@ -84,14 +83,12 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
i.answer = string(line)
options := i.Suggest(i.answer)
i.selectedIndex = 0
i.typedAnswer = i.answer
if len(options) == 0 {
return line, false, nil
}

i.answer = options[0]
if len(options) == 1 {
i.typedAnswer = i.answer
i.options = nil
} else {
i.options = core.OptionAnswerList(options)
Expand All @@ -103,7 +100,6 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {

if key >= terminal.KeySpace {
i.answer += string(key)
i.typedAnswer = i.answer
}

i.options = nil
Expand All @@ -127,7 +123,7 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
err = readLineAgain
}

return []rune(i.typedAnswer), true, err
return []rune(i.answer), true, err
})
}

Expand Down
48 changes: 48 additions & 0 deletions input_test.go
Expand Up @@ -368,6 +368,54 @@ func TestInputPrompt(t *testing.T) {
},
"special answer",
},
{
"Test Input prompt must allow moving cursor using right and left arrows",
&Input{Message: "Filename to save:"},
func(c *expect.Console) {
c.ExpectString("Filename to save:")
c.Send("essay.txt")
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send("_final")
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyBackspace))
c.Send(string(terminal.KeyBackspace))
c.Send(string(terminal.KeyBackspace))
c.Send("md")
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.SendLine("2")
c.ExpectEOF()
},
"essay_final2.md",
},
{
"Test Input prompt must allow moving cursor using right and left arrows, even after suggestions",
&Input{Message: "Filename to save:", Suggest: func(string) []string { return []string{".txt", ".csv", ".go"} }},
func(c *expect.Console) {
c.ExpectString("Filename to save:")
c.Send(string(terminal.KeyTab))
c.ExpectString(".txt")
c.ExpectString(".csv")
c.ExpectString(".go")
c.Send(string(terminal.KeyTab))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send("newtable")
c.SendLine("")
c.ExpectEOF()
},
"newtable.csv",
},
}

for _, test := range tests {
Expand Down

0 comments on commit e50e421

Please sign in to comment.