Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 54 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1345296
docs: example test
caarlos0 Jun 2, 2022
f3decbd
feat: teatest
caarlos0 Jun 2, 2022
0eac2a3
fix: improve api
caarlos0 Jun 2, 2022
c0129d8
fix: improve api
caarlos0 Jun 2, 2022
cb7aa73
feat: goldenfiles
caarlos0 Jun 3, 2022
5b6d430
chore: minor improvements
caarlos0 Jun 21, 2022
9769735
Merge remote-tracking branch 'origin/master' into test
caarlos0 Jun 21, 2022
6ddcd77
feat: type text, diff
caarlos0 Jun 21, 2022
5a7379b
fix: release terminal
caarlos0 Jun 21, 2022
26d32cd
fix: lint
caarlos0 Jun 21, 2022
023ecf8
fix: update cancelreader
caarlos0 Jul 21, 2022
a5f0944
Merge remote-tracking branch 'origin/master' into test
caarlos0 Jul 21, 2022
0646406
Merge remote-tracking branch 'origin/master' into test
caarlos0 Jul 21, 2022
b52e8ac
fix: make it safe
caarlos0 Jul 21, 2022
bf4c39d
Merge branch 'master' into test
caarlos0 Sep 17, 2022
880d6fc
feat: functional options
caarlos0 Sep 17, 2022
8bde1b3
Merge remote-tracking branch 'origin/master' into test
caarlos0 Sep 27, 2022
98d6d93
feat: IsQuit and IsQuitMsg
caarlos0 Sep 27, 2022
2299590
fix: save file
caarlos0 Sep 27, 2022
d063434
Merge branch 'master' into test
caarlos0 Oct 17, 2022
7a26677
fix: do not use deprecate func
caarlos0 Oct 17, 2022
51c5ce7
fix: make diff not complain about trailing whitespaces
caarlos0 Oct 17, 2022
0b9bb8f
feat: with term size
caarlos0 Oct 17, 2022
c4cf039
feat: RequireRegexOutput
caarlos0 Oct 17, 2022
cb0a82a
fix: update
caarlos0 Oct 17, 2022
3fda49f
chore: rename
caarlos0 Oct 17, 2022
b457db4
fix: improve reliability
caarlos0 Oct 17, 2022
4056572
Merge remote-tracking branch 'origin/master' into test
caarlos0 Oct 18, 2022
02901a0
Merge branch 'master' into test
caarlos0 Oct 21, 2022
1509254
fix: use returned model
caarlos0 Oct 21, 2022
d7074e4
fix: making it more predictable, avoid sleeps
caarlos0 Oct 21, 2022
d145319
Merge branch 'master' into test
caarlos0 Oct 25, 2022
1537f76
fix: remove WithRequiredRegexpOutput
caarlos0 Oct 26, 2022
7b269d8
Merge remote-tracking branch 'origin/master' into test
caarlos0 Nov 1, 2022
544baca
fix: allow to assert within interactions
caarlos0 Nov 1, 2022
42af5c0
Merge remote-tracking branch 'origin/master' into test
caarlos0 Nov 4, 2022
2566c94
Merge branch 'master' into test
caarlos0 Nov 10, 2022
e99fec5
feat: added wait for
caarlos0 Nov 10, 2022
1683325
fix: optional
caarlos0 Nov 15, 2022
c29ad51
Merge remote-tracking branch 'origin/master' into test
caarlos0 Dec 1, 2022
d5e43f1
feat: improve usage
caarlos0 Dec 1, 2022
689d778
Merge remote-tracking branch 'origin/master' into test
caarlos0 Feb 27, 2023
c54010f
Merge remote-tracking branch 'origin/master' into test
caarlos0 Apr 17, 2023
4d5d7f6
fix: use udiff
caarlos0 Apr 17, 2023
09c36fc
feat: tea.Wait
caarlos0 Apr 17, 2023
3f5bfce
Merge remote-tracking branch 'origin/master' into test
caarlos0 Apr 18, 2023
acd44ed
fix: merge
caarlos0 Apr 18, 2023
5b74dfe
Merge remote-tracking branch 'origin/master' into wait
caarlos0 Apr 21, 2023
01c4fea
fix: wait til the end of shutdown
caarlos0 Apr 21, 2023
974f6ee
Merge branch 'wait' into test
caarlos0 Apr 21, 2023
2b506e3
fix: final output
caarlos0 May 4, 2023
e5a4e37
feat: use x/exp/teatest
caarlos0 May 5, 2023
d95e4ef
Merge remote-tracking branch 'origin/master' into test
caarlos0 May 5, 2023
1e73e94
chore: go mod tidy
caarlos0 May 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.17

require (
github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/bubbletea v0.23.3-0.20230504193325-5326d76c402a
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/harmonica v0.2.0
github.com/charmbracelet/lipgloss v0.7.1
github.com/dustin/go-humanize v1.0.1
github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b713
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.18
Expand All @@ -20,9 +20,11 @@ require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.1.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand Down
6 changes: 6 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTx
github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.1.0 h1:9Dpklm2oBBhMxIFbMffmPvDaF7vOYfv9B5HXVr42KMU=
github.com/aymanbagabas/go-udiff v0.1.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b h1:K9dWJ2spDhDhIrqnchjG867djPxWWe3mwdk6RdLMfhg=
Expand All @@ -18,6 +20,10 @@ github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJ
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/x/exp/teatest v0.0.0-20230504195420-fee909365d1c h1:3Dha70yq12co31grOoSMOaBIo8POU4ARhvp9W8RsOsw=
github.com/charmbracelet/x/exp/teatest v0.0.0-20230504195420-fee909365d1c/go.mod h1:dpMo6PfGlhavn+ofggWhfFQmK9sZB2Ewljiz9bZtKVI=
github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b713 h1:aRUlesfHrzj9NECuQ1O2h1yBK5M4LFKwLYTQVoEkaQ8=
github.com/charmbracelet/x/exp/teatest v0.0.0-20230505175841-25a09ea9b713/go.mod h1:dpMo6PfGlhavn+ofggWhfFQmK9sZB2Ewljiz9bZtKVI=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
86 changes: 86 additions & 0 deletions examples/simple/main_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions examples/simple/testdata/TestApp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[?25lHi. This program will exit in 10 seconds. To quit sooner press any key
Hi. This program will exit in 9 seconds. To quit sooner press any key.
[?25h[?1002l[?1003l