Skip to content
Myr

example: using the x/exp/teatest package (#352)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

teatest was originally designed in this PR, and was later moved into `github.com/charmbracelet/x`. * docs: example test Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: teatest Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: improve api Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: improve api Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: goldenfiles Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * chore: minor improvements Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: type text, diff Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: release terminal Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: lint Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: update cancelreader Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: make it safe Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: functional options Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: IsQuit and IsQuitMsg * fix: save file Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: do not use deprecate func Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: make diff not complain about trailing whitespaces Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: with term size Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: RequireRegexOutput Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: update Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * chore: rename Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: improve reliability * fix: use returned model * fix: making it more predictable, avoid sleeps * fix: remove WithRequiredRegexpOutput Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: allow to assert within interactions * feat: added wait for Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: optional * feat: improve usage Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: use udiff Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * feat: tea.Wait wait for the underlying context to finish. extract from #352 * fix: merge Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: wait til the end of shutdown Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: final output * feat: use x/exp/teatest Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * chore: go mod tidy Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> ---------

authored by Carlos Alexandro Becker · committed by GitHub · parent 5326d76 · Browse files · 4 files+99 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Diff stats

4 files+99 added−2 removed

  • other:+99 added−2 removed

4 files+99 added−2 removed

added:examples/simple/testdata/TestApp.golden+3 added
1[?25lHi. This program will exit in 10 seconds. To quit sooner press any key2Hi. This program will exit in 9 seconds. To quit sooner press any key.3[?25h[?1002l[?1003l
added:examples/simple/main_test.go+86 added
1package main23import (4 "bytes"5 "io"6 "regexp"7 "testing"8 "time"910 tea "github.com/charmbracelet/bubbletea"11 "github.com/charmbracelet/x/exp/teatest"12)1314func TestApp(t *testing.T) {15 m := model(10)16 tm := teatest.NewTestModel(17 t, m,18 teatest.WithInitialTermSize(70, 30),19 )20 t.Cleanup(func() {21 if err := tm.Quit(); err != nil {22 t.Fatal(err)23 }24 })2526 time.Sleep(time.Second + time.Millisecond*200)27 tm.Type("I'm typing things, but it'll be ignored by my program")28 tm.Send("ignored msg")29 tm.Send(tea.KeyMsg{30 Type: tea.KeyEnter,31 })3233 if err := tm.Quit(); err != nil {34 t.Fatal(err)35 }3637 out := readBts(t, tm.FinalOutput())38 if !regexp.MustCompile(`This program will exit in \d+ seconds`).Match(out) {39 t.Fatalf("output does not match the given regular expression: %s", string(out))40 }41 teatest.RequireEqualOutput(t, out)4243 if tm.FinalModel().(model) != 9 {44 t.Errorf("expected model to be 10, was %d", m)45 }46}4748func TestAppInteractive(t *testing.T) {49 m := model(10)50 tm := teatest.NewTestModel(51 t, m,52 teatest.WithInitialTermSize(70, 30),53 )5455 time.Sleep(time.Second + time.Millisecond*200)56 tm.Send("ignored msg")5758 if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("This program will exit in 9 seconds")) {59 t.Fatalf("output does not match: expected %q", string(bts))60 }6162 teatest.WaitFor(t, tm.Output(), func(out []byte) bool {63 return bytes.Contains(out, []byte("This program will exit in 7 seconds"))64 }, teatest.WithDuration(5*time.Second))6566 tm.Send(tea.KeyMsg{67 Type: tea.KeyEnter,68 })6970 if err := tm.Quit(); err != nil {71 t.Fatal(err)72 }7374 if tm.FinalModel().(model) != 7 {75 t.Errorf("expected model to be 7, was %d", m)76 }77}7879func readBts(tb testing.TB, r io.Reader) []byte {80 tb.Helper()81 bts, err := io.ReadAll(r)82 if err != nil {83 tb.Fatal(err)84 }85 return bts86}
modified:examples/go.mod+4 added-2 removed
2 unmodified lines
3go 1.1745require (6 github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b7 github.com/charmbracelet/bubbletea v0.23.27 github.com/charmbracelet/bubbletea v0.23.3-0.20230504193325-5326d76c402a8 github.com/charmbracelet/glamour v0.6.09 github.com/charmbracelet/harmonica v0.2.010 github.com/charmbracelet/lipgloss v0.7.111 github.com/dustin/go-humanize v1.0.111 github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b71312 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177613 github.com/lucasb-eyer/go-colorful v1.2.014 github.com/mattn/go-isatty v0.0.1815 github.com/muesli/reflow v0.3.0
3 unmodified lines
19require (20 github.com/alecthomas/chroma v0.10.0 // indirect21 github.com/atotto/clipboard v0.1.4 // indirect22 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect23 github.com/aymanbagabas/go-udiff v0.1.0 // indirect24 github.com/aymerick/douceur v0.2.0 // indirect25 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect26 github.com/dlclark/regexp2 v1.4.0 // indirect27 github.com/dustin/go-humanize v1.0.1 // indirect28 github.com/gorilla/css v1.0.0 // indirect29 github.com/mattn/go-localereader v0.0.1 // indirect30 github.com/mattn/go-runewidth v0.0.14 // indirect31 github.com/microcosm-cc/bluemonday v1.0.21 // indirect
15 unmodified lines
modified:examples/go.sum+6 added
5 unmodified lines
6github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTxab9L4E=7github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=8github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=9github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=10github.com/aymanbagabas/go-udiff v0.1.0 h1:9Dpklm2oBBhMxIFbMffmPvDaF7vOYfv9B5HXVr42KMU=11github.com/aymanbagabas/go-udiff v0.1.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=12github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=13github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=14github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b h1:K9dWJ2spDhDhIrqnchjG867djPxWWe3mwdk6RdLMfhg=15github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=
3 unmodified lines
19github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=20github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=21github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=22github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=23github.com/charmbracelet/x/exp/teatest v0.0.0-20230504195420-fee909365d1c h1:3Dha70yq12co31grOoSMOaBIo8POU4ARhvp9W8RsOsw=24github.com/charmbracelet/x/exp/teatest v0.0.0-20230504195420-fee909365d1c/go.mod h1:dpMo6PfGlhavn+ofggWhfFQmK9sZB2Ewljiz9bZtKVI=25github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b713 h1:aRUlesfHrzj9NECuQ1O2h1yBK5M4LFKwLYTQVoEkaQ8=26github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b713/go.mod h1:dpMo6PfGlhavn+ofggWhfFQmK9sZB2Ewljiz9bZtKVI=27github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=28github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=29github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=30github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
101 unmodified lines