From e9fe9426757ee2f032d0f6f5043787f21ec571c8 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 22 Sep 2022 12:57:54 +0200 Subject: [PATCH] test: run model with provided input and check output --- tea_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tea_test.go diff --git a/tea_test.go b/tea_test.go new file mode 100644 index 0000000..3c35074 --- /dev/null +++ b/tea_test.go @@ -0,0 +1,39 @@ +package tea + +import ( + "bytes" + "testing" +) + +type testModel struct{} + +func (m testModel) Init() Cmd { + return nil +} + +func (m testModel) Update(msg Msg) (Model, Cmd) { + switch msg.(type) { + case KeyMsg: + return m, Quit + } + return m, nil +} + +func (m testModel) View() string { + return "success\n" +} + +func TestTeaModel(t *testing.T) { + var buf bytes.Buffer + var in bytes.Buffer + in.Write([]byte("q")) + + p := NewProgram(testModel{}, WithInput(&in), WithOutput(&buf)) + if err := p.Start(); err != nil { + t.Fatal(err) + } + + if buf.Len() == 0 { + t.Fatal("no output") + } +}