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) {
179184type 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.
205211func (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.
212218func (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.
226232func (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+ }
0 commit comments