mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-16 18:15:03 -07:00
example: using the x/exp/teatest package (#352)
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> --------- Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
5326d76c40
commit
25022e9789
86
examples/simple/main_test.go
Normal file
86
examples/simple/main_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/x/exp/teatest"
|
||||
)
|
||||
|
||||
func TestApp(t *testing.T) {
|
||||
m := model(10)
|
||||
tm := teatest.NewTestModel(
|
||||
t, m,
|
||||
teatest.WithInitialTermSize(70, 30),
|
||||
)
|
||||
t.Cleanup(func() {
|
||||
if err := tm.Quit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
time.Sleep(time.Second + time.Millisecond*200)
|
||||
tm.Type("I'm typing things, but it'll be ignored by my program")
|
||||
tm.Send("ignored msg")
|
||||
tm.Send(tea.KeyMsg{
|
||||
Type: tea.KeyEnter,
|
||||
})
|
||||
|
||||
if err := tm.Quit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
out := readBts(t, tm.FinalOutput())
|
||||
if !regexp.MustCompile(`This program will exit in \d+ seconds`).Match(out) {
|
||||
t.Fatalf("output does not match the given regular expression: %s", string(out))
|
||||
}
|
||||
teatest.RequireEqualOutput(t, out)
|
||||
|
||||
if tm.FinalModel().(model) != 9 {
|
||||
t.Errorf("expected model to be 10, was %d", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppInteractive(t *testing.T) {
|
||||
m := model(10)
|
||||
tm := teatest.NewTestModel(
|
||||
t, m,
|
||||
teatest.WithInitialTermSize(70, 30),
|
||||
)
|
||||
|
||||
time.Sleep(time.Second + time.Millisecond*200)
|
||||
tm.Send("ignored msg")
|
||||
|
||||
if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("This program will exit in 9 seconds")) {
|
||||
t.Fatalf("output does not match: expected %q", string(bts))
|
||||
}
|
||||
|
||||
teatest.WaitFor(t, tm.Output(), func(out []byte) bool {
|
||||
return bytes.Contains(out, []byte("This program will exit in 7 seconds"))
|
||||
}, teatest.WithDuration(5*time.Second))
|
||||
|
||||
tm.Send(tea.KeyMsg{
|
||||
Type: tea.KeyEnter,
|
||||
})
|
||||
|
||||
if err := tm.Quit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if tm.FinalModel().(model) != 7 {
|
||||
t.Errorf("expected model to be 7, was %d", m)
|
||||
}
|
||||
}
|
||||
|
||||
func readBts(tb testing.TB, r io.Reader) []byte {
|
||||
tb.Helper()
|
||||
bts, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
return bts
|
||||
}
|
||||
3
examples/simple/testdata/TestApp.golden
vendored
Normal file
3
examples/simple/testdata/TestApp.golden
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[?25lHi. This program will exit in 10 seconds. To quit sooner press any key
|
||||
[70D[1A[70D[2KHi. This program will exit in 9 seconds. To quit sooner press any key.
|
||||
[70D[2K[?25h[?1002l[?1003l
|
||||
Reference in New Issue
Block a user