| 1 | package teatest |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "testing/iotest" |
| 8 | "time" |
| 9 | |
| 10 | tea "github.com/charmbracelet/bubbletea/v2" |
| 11 | ) |
| 12 | |
| 13 | func TestWaitForErrorReader(t *testing.T) { |
| 14 | err := doWaitFor(iotest.ErrReader(fmt.Errorf("fake")), func(bts []byte) bool { |
| 15 | return true |
| 16 | }, WithDuration(time.Millisecond), WithCheckInterval(10*time.Microsecond)) |
| 17 | if err == nil { |
| 18 | t.Fatal("expected an error, got nil") |
| 19 | } |
| 20 | if err.Error() != "WaitFor: fake" { |
| 21 | t.Fatalf("unexpected error: %s", err.Error()) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestWaitForTimeout(t *testing.T) { |
| 26 | err := doWaitFor(strings.NewReader("nope"), func(bts []byte) bool { |
| 27 | return false |
| 28 | }, WithDuration(time.Millisecond), WithCheckInterval(10*time.Microsecond)) |
| 29 | if err == nil { |
| 30 | t.Fatal("expected an error, got nil") |
| 31 | } |
| 32 | if err.Error() != "WaitFor: condition not met after 1ms. Last output:\nnope" { |
| 33 | t.Fatalf("unexpected error: %s", err.Error()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | type m string |
| 38 | |
| 39 | func (m m) Init() tea.Cmd { return nil } |
| 40 | func (m m) Update(tea.Msg) (tea.Model, tea.Cmd) { return m, nil } |
| 41 | func (m m) View() string { return string(m) } |
| 42 | |
| 43 | func TestWaitFinishedWithTimeoutFn(t *testing.T) { |
| 44 | tm := NewTestModel(t, m("a")) |
| 45 | var timedOut bool |
| 46 | tm.WaitFinished(t, WithFinalTimeout(time.Nanosecond), WithTimeoutFn(func(testing.TB) { |
| 47 | timedOut = true |
| 48 | })) |
| 49 | if !timedOut { |
| 50 | t.Fatal("expected timedOut to be set") |
| 51 | } |
| 52 | } |