bubbletea/examples/simple/main_test.go

87 lines
1.8 KiB
Go
Raw Normal View History

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>
2023-05-05 15:14:26 -04:00
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
}