Skip to content
Myr

test: add batchMsg test

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent 6b77c8f · Browse files · 1 file+44 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−0 removed

  • other:+44 added−0 removed

1 file+44 added−0 removed

modified:tea_test.go+44 added
5 unmodified lines
6 "testing"7 "time"8)910type incrementMsg struct{}1112type testModel struct {13 executed atomic.Value14 counter atomic.Value15}1617func (m testModel) Init() Cmd {18 return nil19}2021func (m *testModel) Update(msg Msg) (Model, Cmd) {22 switch msg.(type) {23 case incrementMsg:24 i := m.counter.Load()25 if i == nil {26 m.counter.Store(1)27 } else {28 m.counter.Store(i.(int) + 1)29 }3031 case KeyMsg:32 return m, Quit33 }3435 return m, nil36}3738func (m *testModel) View() string {
56 unmodified lines
95 if _, err := p.Run(); err != nil {96 t.Fatal(err)97 }98}99100func TestTeaBatchMsg(t *testing.T) {101 var buf bytes.Buffer102 var in bytes.Buffer103104 inc := func() Msg {105 return incrementMsg{}106 }107108 m := &testModel{}109 p := NewProgram(m, WithInput(&in), WithOutput(&buf))110 go func() {111 p.Send(batchMsg{inc, inc})112113 for {114 time.Sleep(time.Millisecond)115 i := m.counter.Load()116 if i != nil && i.(int) >= 2 {117 p.Quit()118 return119 }120 }121 }()122123 if _, err := p.Run(); err != nil {124 t.Fatal(err)125 }126127 if m.counter.Load() != 2 {128 t.Fatalf("counter should be 2, got %d", m.counter)129 }130}