Skip to content
Myr

main_test.go

This file

Path

examples/simple/main_test.go

88 lines · 2.2 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23/*4func TestApp(t *testing.T) {5	// TODO: Enable this test again6	// Since we added colorprofile.Writer to standard_renderer.go, this test7	// keeps failing. This is because the output is colored and has escape8	// sequences but the test runs against a buffer output and not a terminal,9	// tty, or pty. One way to fix this is to pass a color profile to the test10	// program using [tea.WithColorProfile(Ascii)].11	t.Skip("this test is currently disabled")1213	m := model(10)14	tm := teatest.NewTestModel(15		t, m,16		teatest.WithInitialTermSize(70, 30),17	)18	t.Cleanup(func() {19		if err := tm.Quit(); err != nil {20			t.Fatal(err)21		}22	})2324	time.Sleep(time.Second + time.Millisecond*200)25	tm.Type("I'm typing things, but it'll be ignored by my program")26	tm.Send("ignored msg")27	tm.Send(tea.KeyPressMsg{28		Code: tea.KeyEnter,29	})3031	if err := tm.Quit(); err != nil {32		t.Fatal(err)33	}3435	out := readBts(t, tm.FinalOutput(t))36	if !regexp.MustCompile(`This program will exit in \d+ seconds`).Match(out) {37		t.Fatalf("output does not match the given regular expression: %s", string(out))38	}39	teatest.RequireEqualOutput(t, out)4041	if tm.FinalModel(t).(model) != 9 {42		t.Errorf("expected model to be 10, was %d", m)43	}44}4546func TestAppInteractive(t *testing.T) {47	t.Skip("This test is flaky and needs to be fixed.\n" +48		"We need a more concrete way to set the initial terminal size")4950	m := model(10)51	tm := teatest.NewTestModel(52		t, m,53		teatest.WithInitialTermSize(70, 30),54	)5556	time.Sleep(time.Second + time.Millisecond*200)57	tm.Send("ignored msg")5859	if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("This program will exit in 9 seconds")) {60		t.Fatalf("output does not match: expected %q", string(bts))61	}6263	teatest.WaitFor(t, tm.Output(), func(out []byte) bool {64		return bytes.Contains(out, []byte("This program will exit in 7 seconds"))65	}, teatest.WithDuration(5*time.Second))6667	tm.Send(tea.KeyPressMsg{68		Code: tea.KeyEnter,69	})7071	if err := tm.Quit(); err != nil {72		t.Fatal(err)73	}7475	if tm.FinalModel(t).(model) != 7 {76		t.Errorf("expected model to be 7, was %d", m)77	}78}7980func readBts(tb testing.TB, r io.Reader) []byte {81	tb.Helper()82	bts, err := io.ReadAll(r)83	if err != nil {84		tb.Fatal(err)85	}86	return bts87}88*/