Skip to content

Commit c0895f9

Browse files
committed
feat: updates
Signed-off-by: Carlos Alexandro Becker <[email protected]>
1 parent bc6939f commit c0895f9

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

exp/teatest/v2/app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestApp(t *testing.T) {
3535
t.Fatal(err)
3636
}
3737

38-
out := tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
38+
out := teatest.TrimEmptyLines(tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
3939
if !regexp.MustCompile(`This program will exit in \d+ seconds`).MatchString(out) {
4040
t.Fatalf("output does not match the given regular expression: %q", out)
4141
}

exp/teatest/v2/send_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestAppSendToOtherProgram(t *testing.T) {
3939
tm1.Type("q")
4040
tm2.Type("q")
4141

42-
out1 := tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
43-
out2 := tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second))
42+
out1 := teatest.TrimEmptyLines(tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
43+
out2 := teatest.TrimEmptyLines(tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
4444

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

exp/teatest/v2/teatest.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"strings"
89
"sync"
910
"syscall"
1011
"testing"
@@ -154,19 +155,23 @@ func NewTestModel(tb testing.TB, m tea.Model, options ...TestOption) *TestModel
154155
return tm
155156
}
156157

157-
func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) {
158+
func mergeOpts(opts []FinalOpt) FinalOpts {
159+
r := FinalOpts{}
160+
for _, opt := range opts {
161+
opt(&r)
162+
}
163+
return r
164+
}
165+
166+
func (tm *TestModel) waitDone(tb testing.TB, opts FinalOpts) {
158167
tm.done.Do(func() {
159-
fopts := FinalOpts{}
160-
for _, opt := range opts {
161-
opt(&fopts)
162-
}
163-
if fopts.timeout > 0 {
168+
if opts.timeout > 0 {
164169
select {
165-
case <-time.After(fopts.timeout):
166-
if fopts.onTimeout == nil {
167-
tb.Fatalf("timeout after %s", fopts.timeout)
170+
case <-time.After(opts.timeout):
171+
if opts.onTimeout == nil {
172+
tb.Fatalf("timeout after %s", opts.timeout)
168173
}
169-
fopts.onTimeout(tb)
174+
opts.onTimeout(tb)
170175
case <-tm.doneCh:
171176
}
172177
} else {
@@ -179,6 +184,7 @@ func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) {
179184
type FinalOpts struct {
180185
timeout time.Duration
181186
onTimeout func(tb testing.TB)
187+
trim bool
182188
}
183189

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

209215
// FinalModel returns the resulting model, resulting from program.Run().
210216
// This method only returns once the program has finished running or when it
211217
// times out.
212218
func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model {
213-
tm.waitDone(tb, opts)
219+
tm.waitDone(tb, mergeOpts(opts))
214220
select {
215221
case m := <-tm.modelCh:
216222
tm.model = m
@@ -224,7 +230,8 @@ func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model {
224230
// This method only returns once the program has finished running or when it
225231
// times out.
226232
func (tm *TestModel) FinalOutput(tb testing.TB, opts ...FinalOpt) string {
227-
tm.waitDone(tb, opts)
233+
opt := mergeOpts(opts)
234+
tm.waitDone(tb, opt)
228235
return tm.Output()
229236
}
230237

@@ -269,3 +276,15 @@ func RequireEqualOutput(tb testing.TB, out string) {
269276
tb.Helper()
270277
golden.RequireEqualEscape(tb, []byte(out), true)
271278
}
279+
280+
// TrimEmptyLines removes trailing empty lines from the given output.
281+
func TrimEmptyLines(out string) string {
282+
// trim empty trailing lines from the output
283+
lines := strings.Split(out, "\n")
284+
for i := len(lines) - 1; i >= 0; i-- {
285+
if strings.TrimSpace(lines[i]) != "" {
286+
return strings.Join(lines[:i], "\n")
287+
}
288+
}
289+
return out
290+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
[?25l[?2004h[?2027h[?2027$pHi. This program will exit in 10 seconds. To quit sooner press any key
2-
Hi. This program will exit in 9 seconds. To quit sooner press any key.
3-
[?2004l[?25h
1+
Hi. This program will exit in 10 seconds. To quit sooner press any key
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
[?25l[?2004h[?2027h[?2027$pAll pings:
1+
All pings:
22
from m1
33
from m1
44
from m2
55
from m2
6-
from m2
7-
from m2[?2004l[?25h
6+
from m2

0 commit comments

Comments
 (0)