Skip to content

Commit

Permalink
feat: updates
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Nov 13, 2024
1 parent bc6939f commit c0895f9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion exp/teatest/v2/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestApp(t *testing.T) {
t.Fatal(err)
}

out := tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
out := teatest.TrimEmptyLines(tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
if !regexp.MustCompile(`This program will exit in \d+ seconds`).MatchString(out) {
t.Fatalf("output does not match the given regular expression: %q", out)
}
Expand Down
4 changes: 2 additions & 2 deletions exp/teatest/v2/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestAppSendToOtherProgram(t *testing.T) {
tm1.Type("q")
tm2.Type("q")

out1 := tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
out2 := tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
out1 := teatest.TrimEmptyLines(tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
out2 := teatest.TrimEmptyLines(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))
Expand Down
45 changes: 32 additions & 13 deletions exp/teatest/v2/teatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -154,19 +155,23 @@ func NewTestModel(tb testing.TB, m tea.Model, options ...TestOption) *TestModel
return tm
}

func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) {
func mergeOpts(opts []FinalOpt) FinalOpts {
r := FinalOpts{}
for _, opt := range opts {
opt(&r)
}
return r
}

func (tm *TestModel) waitDone(tb testing.TB, opts FinalOpts) {
tm.done.Do(func() {
fopts := FinalOpts{}
for _, opt := range opts {
opt(&fopts)
}
if fopts.timeout > 0 {
if opts.timeout > 0 {
select {
case <-time.After(fopts.timeout):
if fopts.onTimeout == nil {
tb.Fatalf("timeout after %s", fopts.timeout)
case <-time.After(opts.timeout):
if opts.onTimeout == nil {
tb.Fatalf("timeout after %s", opts.timeout)
}
fopts.onTimeout(tb)
opts.onTimeout(tb)
case <-tm.doneCh:
}
} else {
Expand All @@ -179,6 +184,7 @@ func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) {
type FinalOpts struct {
timeout time.Duration
onTimeout func(tb testing.TB)
trim bool
}

// FinalOpt changes FinalOpts.
Expand All @@ -203,14 +209,14 @@ func WithFinalTimeout(d time.Duration) FinalOpt {
// This method only returns once the program has finished running or when it
// times out.
func (tm *TestModel) WaitFinished(tb testing.TB, opts ...FinalOpt) {
tm.waitDone(tb, opts)
tm.waitDone(tb, mergeOpts(opts))
}

// FinalModel returns the resulting model, resulting from program.Run().
// This method only returns once the program has finished running or when it
// times out.
func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model {
tm.waitDone(tb, opts)
tm.waitDone(tb, mergeOpts(opts))
select {
case m := <-tm.modelCh:
tm.model = m
Expand All @@ -224,7 +230,8 @@ func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model {
// This method only returns once the program has finished running or when it
// times out.
func (tm *TestModel) FinalOutput(tb testing.TB, opts ...FinalOpt) string {
tm.waitDone(tb, opts)
opt := mergeOpts(opts)
tm.waitDone(tb, opt)
return tm.Output()
}

Expand Down Expand Up @@ -269,3 +276,15 @@ func RequireEqualOutput(tb testing.TB, out string) {
tb.Helper()
golden.RequireEqualEscape(tb, []byte(out), true)
}

// TrimEmptyLines removes trailing empty lines from the given output.
func TrimEmptyLines(out string) string {
// trim empty trailing lines from the output
lines := strings.Split(out, "\n")
for i := len(lines) - 1; i >= 0; i-- {
if strings.TrimSpace(lines[i]) != "" {
return strings.Join(lines[:i], "\n")
}
}
return out
}
4 changes: 1 addition & 3 deletions exp/teatest/v2/testdata/TestApp.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[?25l[?2004h[?2027h[?2027$pHi. 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.
[?2004l[?25h
Hi. This program will exit in 10 seconds. To quit sooner press any key
5 changes: 2 additions & 3 deletions exp/teatest/v2/testdata/TestAppSendToOtherProgram.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[?25l[?2004h[?2027h[?2027$pAll pings:
All pings:
from m1
from m1
from m2
from m2
from m2
from m2[?2004l[?25h
from m2

0 comments on commit c0895f9

Please sign in to comment.