From b7ee77a5a989ab9ef3aa0a047acd28e44c2fe629 Mon Sep 17 00:00:00 2001 From: Lucas dos Santos Abreu Date: Mon, 28 Jun 2021 23:00:02 -0300 Subject: [PATCH] (feat): tests input navagation --- input.go | 6 +----- input_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/input.go b/input.go index 478ff65a..fe08d1f6 100644 --- a/input.go +++ b/input.go @@ -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 @@ -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) @@ -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 @@ -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 }) } diff --git a/input_test.go b/input_test.go index 8ff31727..38b0c645 100644 --- a/input_test.go +++ b/input_test.go @@ -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 {