test: add batchMsg test

This commit is contained in:
Christian Muehlhaeuser 2022-10-08 18:22:29 +02:00
parent 6b77c8fc10
commit 9117bbc137
1 changed files with 44 additions and 0 deletions

View File

@ -7,8 +7,11 @@ import (
"time" "time"
) )
type incrementMsg struct{}
type testModel struct { type testModel struct {
executed atomic.Value executed atomic.Value
counter atomic.Value
} }
func (m testModel) Init() Cmd { func (m testModel) Init() Cmd {
@ -17,9 +20,18 @@ func (m testModel) Init() Cmd {
func (m *testModel) Update(msg Msg) (Model, Cmd) { func (m *testModel) Update(msg Msg) (Model, Cmd) {
switch msg.(type) { switch msg.(type) {
case incrementMsg:
i := m.counter.Load()
if i == nil {
m.counter.Store(1)
} else {
m.counter.Store(i.(int) + 1)
}
case KeyMsg: case KeyMsg:
return m, Quit return m, Quit
} }
return m, nil return m, nil
} }
@ -84,3 +96,35 @@ func TestTeaKill(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestTeaBatchMsg(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer
inc := func() Msg {
return incrementMsg{}
}
m := &testModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
go func() {
p.Send(batchMsg{inc, inc})
for {
time.Sleep(time.Millisecond)
i := m.counter.Load()
if i != nil && i.(int) >= 2 {
p.Quit()
return
}
}
}()
if _, err := p.Run(); err != nil {
t.Fatal(err)
}
if m.counter.Load() != 2 {
t.Fatalf("counter should be 2, got %d", m.counter)
}
}