mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 11:09:17 -07:00
test: add batchMsg test
This commit is contained in:
44
tea_test.go
44
tea_test.go
@@ -7,8 +7,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type incrementMsg struct{}
|
||||
|
||||
type testModel struct {
|
||||
executed atomic.Value
|
||||
counter atomic.Value
|
||||
}
|
||||
|
||||
func (m testModel) Init() Cmd {
|
||||
@@ -17,9 +20,18 @@ func (m testModel) Init() Cmd {
|
||||
|
||||
func (m *testModel) Update(msg Msg) (Model, Cmd) {
|
||||
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:
|
||||
return m, Quit
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -84,3 +96,35 @@ func TestTeaKill(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user