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

teatest: use one program within another, signal handler, ansi compressor #39

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
87 changes: 87 additions & 0 deletions exp/teatest/send_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package teatest_test

import (
"fmt"
"strings"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
)

func TestAppSendToOtherProgram(t *testing.T) {
m1 := &connectedModel{
name: "m1",
}
m2 := &connectedModel{
name: "m2",
}

tm1 := teatest.NewTestModel(t, m1, teatest.WithInitialTermSize(70, 30))
t.Cleanup(func() {
if err := tm1.Quit(); err != nil {
t.Fatal(err)
}
})
tm2 := teatest.NewTestModel(t, m2, teatest.WithInitialTermSize(70, 30))
t.Cleanup(func() {
if err := tm2.Quit(); err != nil {
t.Fatal(err)
}
})
m1.programs = append(m1.programs, tm2)
m2.programs = append(m2.programs, tm1)

tm1.Type("pp")
tm2.Type("pppp")

tm1.Type("q")
tm2.Type("q")

out1 := readBts(t, tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
out2 := readBts(t, tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))

if string(out1) != string(out2) {
t.Errorf("output of both models should be the same, got:\n%v\nand:\n%v\n", string(out1), string(out2))
}

teatest.RequireEqualOutput(t, out1)
}

type connectedModel struct {
name string
programs []interface{ Send(tea.Msg) }
msgs []string
}

type ping string

func (m *connectedModel) Init() tea.Cmd {
return nil
}

func (m *connectedModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "p":
send := ping("from " + m.name)
m.msgs = append(m.msgs, string(send))
for _, p := range m.programs {
p.Send(send)
}
fmt.Printf("sent ping %q to others\n", send)
case "q":
return m, tea.Quit
}
case ping:
fmt.Printf("rcvd ping %q on %s\n", msg, m.name)
m.msgs = append(m.msgs, string(msg))
}
return m, nil
}

func (m *connectedModel) View() string {
return "All pings:\n" + strings.Join(m.msgs, "\n")
}
5 changes: 3 additions & 2 deletions exp/teatest/teatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func NewTestModel(tb testing.TB, m tea.Model, options ...TestOption) *TestModel
tea.WithInput(tm.in),
tea.WithOutput(tm.out),
tea.WithoutSignals(),
tea.WithANSICompressor(), // this helps a bit to reduce drift between runs
)

interruptions := make(chan os.Signal, 1)
Expand All @@ -148,7 +149,7 @@ func NewTestModel(tb testing.TB, m tea.Model, options ...TestOption) *TestModel
<-interruptions
signal.Stop(interruptions)
tb.Log("interrupted")
tm.program.Quit()
tm.program.Kill()
}()

var opts TestModelOptions
Expand Down Expand Up @@ -244,7 +245,7 @@ func (tm *TestModel) Quit() error {
// Type types the given text into the given program.
func (tm *TestModel) Type(s string) {
for _, c := range []byte(s) {
tm.program.Send(tea.KeyMsg{
tm.Send(tea.KeyMsg{
Runes: []rune{rune(c)},
Type: tea.KeyRunes,
})
Expand Down
7 changes: 7 additions & 0 deletions exp/teatest/testdata/TestAppSendToOtherProgram.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[?25lAll pings:
from m1
from m1
from m2
from m2
from m2
from m2[?25h[?1002l[?1003l[?1006l
Loading