cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / cmdhelp / cmdhelp_test.go
cmdhelp_test.go 2.4K
1package cmdhelp23import (4 "strconv"5 "strings"6 "testing"78 tea "charm.land/bubbletea/v2"9 "github.com/charmbracelet/x/ansi"1011 "src.rest/cat/012/internal/nushell"12 "src.rest/cat/012/internal/ui/theme"13)1415type fakeHost struct {16 th theme.Theme17 w, h int18 closed bool19}2021func (f *fakeHost) Theme() *theme.Theme { return &f.th }22func (f *fakeHost) Size() (int, int) { return f.w, f.h }23func (f *fakeHost) Close() { f.closed = true }24func (f *fakeHost) Code(src string) string { return src }2526var sortBy = nushell.Help{27 Name: "sort-by", Desc: "Sort by the given cell path or closure.", Usage: "sort-by {flags} <...comparator>",28 Flags: []nushell.Flag{{Short: "-r", Long: "--reverse", Desc: "Sort in reverse order."},29 {Short: "-i", Long: "--ignore-case", Desc: "Sort string-based data case-insensitively."}},30 Params: []nushell.Param{{Name: "...comparator", Type: "oneof<cell-path, closure>", Desc: "The cell path(s) or closure(s) to compare elements by."}},31 Types: []string{"table | table"},32 Examples: []nushell.Example{{Desc: "Sort files by modified date.", Code: "ls | sort-by modified"}, {Desc: "Reversed.", Code: "ls | sort-by name -r"}},33}3435func boxText(v *View) []string {36 lines := v.Layout()[0].Lines37 out := make([]string, len(lines))38 for i, l := range lines {39 out[i] = ansi.Strip(l)40 }41 return out42}4344// The help says what the command does, links its docs, and lists its45// usage, flags, parameters, types and examples, scrolling in a box46// that fits the screen.47func TestHelpShowsTheCommand(t *testing.T) {48 f := &fakeHost{th: theme.New(true), w: 80, h: 16}49 v := New(f, sortBy)50 lines := boxText(v)51 box := strings.Join(lines, "\n")52 for _, want := range []string{"┌─ sort-by ", "Sort by the given cell path", "Docs: https://www.nushell.sh/commands/docs/sort-by.html", "Usage", "-r, --reverse"} {53 if !strings.Contains(box, want) {54 t.Errorf("no %q in\n%s", want, box)55 }56 }57 for _, l := range lines {58 if w := ansi.StringWidth(l); w > f.w {59 t.Errorf("a line %d wide: %q", w, l)60 }61 }62 if total := len(v.Lines()) + 1; !strings.Contains(lines[len(lines)-1], "of "+strconv.Itoa(total)) {63 t.Errorf("the count: %q", lines[len(lines)-1])64 }65 v.Key(tea.KeyPressMsg{Code: tea.KeyEnd})66 if box := strings.Join(boxText(v), "\n"); !strings.Contains(box, "ls | sort-by name -r") {67 t.Errorf("scrolled to the end\n%s", box)68 }69 v.Key(tea.KeyPressMsg{Code: tea.KeyEscape})70 if !f.closed {71 t.Error("Esc didn't close")72 }73}