go-prompt/render_test.go

114 lines
2.8 KiB
Go
Raw Normal View History

2017-07-15 09:51:33 +00:00
package prompt
import (
"reflect"
"syscall"
2017-07-15 13:23:47 +00:00
"testing"
2017-07-15 09:51:33 +00:00
)
func TestFormatCompletion(t *testing.T) {
2017-07-17 17:01:24 +00:00
scenarioTable := []struct {
scenario string
2017-08-04 11:30:50 +00:00
completions []Suggest
2017-07-17 17:01:24 +00:00
prefix string
suffix string
2017-08-04 11:30:50 +00:00
expected []Suggest
2017-07-17 17:01:24 +00:00
maxWidth int
2017-07-17 14:17:08 +00:00
expectedWidth int
2017-07-17 17:01:24 +00:00
}{
2017-07-17 14:17:08 +00:00
{
scenario: "",
2017-08-04 11:30:50 +00:00
completions: []Suggest{
2017-07-18 00:53:59 +00:00
{Text: "select"},
{Text: "from"},
{Text: "insert"},
{Text: "where"},
2017-07-17 14:17:08 +00:00
},
prefix: " ",
suffix: " ",
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 15:36:16 +00:00
{Text: " select "},
{Text: " from "},
{Text: " insert "},
{Text: " where "},
2017-07-17 14:17:08 +00:00
},
2017-07-17 17:01:24 +00:00
maxWidth: 20,
2017-07-17 14:17:08 +00:00
expectedWidth: 8,
},
2017-07-18 15:36:16 +00:00
{
scenario: "",
2017-08-04 11:30:50 +00:00
completions: []Suggest{
2017-07-18 15:36:16 +00:00
{Text: "select", Description: "select description"},
{Text: "from", Description: "from description"},
{Text: "insert", Description: "insert description"},
{Text: "where", Description: "where description"},
},
prefix: " ",
suffix: " ",
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 15:36:16 +00:00
{Text: " select ", Description: " select description "},
{Text: " from ", Description: " from description "},
{Text: " insert ", Description: " insert description "},
{Text: " where ", Description: " where description "},
},
maxWidth: 40,
expectedWidth: 28,
},
2017-07-15 09:51:33 +00:00
}
2017-07-17 14:17:08 +00:00
for _, s := range scenarioTable {
2017-08-12 09:59:10 +00:00
ac, width := formatSuggestions(s.completions, s.maxWidth)
2017-07-17 14:17:08 +00:00
if !reflect.DeepEqual(ac, s.expected) {
t.Errorf("Should be %#v, but got %#v", s.expected, ac)
}
if width != s.expectedWidth {
t.Errorf("Should be %#v, but got %#v", s.expectedWidth, width)
}
2017-07-15 09:51:33 +00:00
}
}
func TestBreakLineCallback(t *testing.T) {
var i int
r := &Render{
prefix: "> ",
out: &PosixWriter{
fd: syscall.Stdin, // "write" to stdin just so we don't mess with the output of the tests
},
livePrefixCallback: func() (string, bool) { return "", false },
prefixTextColor: Blue,
prefixBGColor: DefaultColor,
inputTextColor: DefaultColor,
inputBGColor: DefaultColor,
previewSuggestionTextColor: Green,
previewSuggestionBGColor: DefaultColor,
suggestionTextColor: White,
suggestionBGColor: Cyan,
selectedSuggestionTextColor: Black,
selectedSuggestionBGColor: Turquoise,
descriptionTextColor: Black,
descriptionBGColor: Turquoise,
selectedDescriptionTextColor: White,
selectedDescriptionBGColor: Cyan,
scrollbarThumbColor: DarkGray,
scrollbarBGColor: Cyan,
col: 1,
}
b := NewBuffer()
r.BreakLine(b)
if i != 0 {
t.Errorf("i should initially be 0, before applying a break line callback")
}
r.breakLineCallback = func(doc *Document) {
i++
}
r.BreakLine(b)
r.BreakLine(b)
r.BreakLine(b)
if i != 3 {
t.Errorf("BreakLine callback not called, i should be 3")
}
}