command and help ux pass (#21)
Reviewed-on: #21 Co-authored-by: vickydotbat <vickydotbat@tutamail.com> Co-committed-by: vickydotbat <vickydotbat@tutamail.com>
This commit was merged in pull request #21.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package menu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -45,3 +47,58 @@ func TestSelectInvalidErrors(t *testing.T) {
|
||||
t.Fatal("out-of-range selection should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectAlignsDescriptionsAfterLongestLabel(t *testing.T) {
|
||||
items := []Item{
|
||||
{Label: "hak build", Desc: "build haks", Args: []string{"hak", "build"}},
|
||||
{Label: "topdata exceptionally-long-command", Desc: "long command", Args: []string{"topdata", "long"}},
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if _, err := Select(&out, strings.NewReader("q\n"), items); err != nil {
|
||||
t.Fatalf("select: %v", err)
|
||||
}
|
||||
text := stripANSI(out.String())
|
||||
var positions []int
|
||||
for _, line := range strings.Split(text, "\n") {
|
||||
for _, desc := range []string{"build haks", "long command"} {
|
||||
if index := strings.Index(line, desc); index >= 0 {
|
||||
positions = append(positions, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(positions) != 2 {
|
||||
t.Fatalf("expected two menu descriptions, got %d:\n%s", len(positions), text)
|
||||
}
|
||||
if positions[0] != positions[1] {
|
||||
t.Fatalf("descriptions are not aligned: columns %v\n%s", positions, text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectShowsCommandGuidanceBeforeArgumentsPrompt(t *testing.T) {
|
||||
items := []Item{{
|
||||
Label: "topdata build",
|
||||
Desc: "compile topdata",
|
||||
Args: []string{"topdata", "build"},
|
||||
Usage: "crucible topdata build [--force]",
|
||||
Options: []string{"--force rebuild outputs"},
|
||||
}}
|
||||
var out bytes.Buffer
|
||||
if _, err := Select(&out, strings.NewReader("1\n\n"), items); err != nil {
|
||||
t.Fatalf("select: %v", err)
|
||||
}
|
||||
text := stripANSI(out.String())
|
||||
for _, want := range []string{
|
||||
"usage: crucible topdata build [--force]",
|
||||
"options:",
|
||||
"--force rebuild outputs",
|
||||
"arguments (press Enter to use defaults):",
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Errorf("selected command guidance missing %q:\n%s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stripANSI(value string) string {
|
||||
return regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(value, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user