cat / 012
clone

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

main
012 / internal / ui / lineedit / word_test.go
word_test.go 1.8K
1package lineedit23import (4 "testing"56 tea "charm.land/bubbletea/v2"7)89var (10 ctrlLeft = tea.KeyPressMsg{Code: tea.KeyLeft, Mod: tea.ModCtrl}11 altRight = tea.KeyPressMsg{Code: tea.KeyRight, Mod: tea.ModAlt}12 ctrlBackspace = tea.KeyPressMsg{Code: tea.KeyBackspace, Mod: tea.ModCtrl}13 legacyCtrlBS = tea.KeyPressMsg{Code: 'h', Mod: tea.ModCtrl}14 optBackspace = tea.KeyPressMsg{Code: tea.KeyBackspace, Mod: tea.ModAlt}15 altD = tea.KeyPressMsg{Code: 'd', Mod: tea.ModAlt}16 ctrlU = tea.KeyPressMsg{Code: 'u', Mod: tea.ModCtrl}17)1819func TestWordMoves(t *testing.T) {20 var l Line21 l.Set("=SUM(A1:B2) + total")22 for _, want := range []int{14, 12, 10, 8, 7, 5, 4, 1, 0, 0} {23 l.Key(ctrlLeft)24 if l.Pos != want {25 t.Fatalf("ctrl+left to %d, want %d", l.Pos, want)26 }27 }28 for _, want := range []int{1, 4, 5, 7, 8, 10, 11, 13, 19, 19} {29 l.Key(altRight)30 if l.Pos != want {31 t.Fatalf("alt+right to %d, want %d", l.Pos, want)32 }33 }34}3536func TestWordDeletes(t *testing.T) {37 var l Line38 l.Set("=SUM(A1:B2")39 for _, step := range []struct {40 k tea.KeyPressMsg41 want string42 }{43 {ctrlBackspace, "=SUM(A1:"},44 {legacyCtrlBS, "=SUM(A1"},45 {optBackspace, "=SUM("},46 } {47 l.Key(step.k)48 if l.Text() != step.want {49 t.Fatalf("%s left %q, want %q", step.k, l.Text(), step.want)50 }51 }52 l.Set("hello big world")53 l.Pos = 654 l.Key(altD)55 if l.Text() != "hello world" || l.Pos != 6 {56 t.Errorf("alt+d left %q at %d", l.Text(), l.Pos)57 }58 l.Key(ctrlU)59 if l.Text() != " world" || l.Pos != 0 {60 t.Errorf("ctrl+u left %q at %d", l.Text(), l.Pos)61 }62}6364func TestAreaWordKeys(t *testing.T) {65 a := &Area{}66 a.Set("ls\n| where size > 1kb")67 a.Key(ctrlBackspace, 40)68 if a.Text() != "ls\n| where size > " {69 t.Fatalf("ctrl+backspace left %q", a.Text())70 }71 a.Key(ctrlU, 40)72 if a.Text() != "ls\n" {73 t.Errorf("ctrl+u took more than its line: %q", a.Text())74 }75}