2021-01-17 10:10:35 -05:00
|
|
|
package tea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-10-10 10:44:08 -04:00
|
|
|
"time"
|
2021-01-17 10:10:35 -05:00
|
|
|
)
|
|
|
|
|
2021-10-10 10:44:08 -04:00
|
|
|
func TestEvery(t *testing.T) {
|
|
|
|
expected := "every ms"
|
|
|
|
msg := Every(time.Millisecond, func(t time.Time) Msg {
|
|
|
|
return expected
|
|
|
|
})()
|
|
|
|
if expected != msg {
|
|
|
|
t.Fatalf("expected a msg %v but got %v", expected, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTick(t *testing.T) {
|
|
|
|
expected := "tick"
|
|
|
|
msg := Tick(time.Millisecond, func(t time.Time) Msg {
|
|
|
|
return expected
|
|
|
|
})()
|
|
|
|
if expected != msg {
|
|
|
|
t.Fatalf("expected a msg %v but got %v", expected, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:10:35 -05:00
|
|
|
func TestSequentially(t *testing.T) {
|
|
|
|
var expectedErrMsg = fmt.Errorf("some err")
|
|
|
|
var expectedStrMsg = "some msg"
|
|
|
|
|
|
|
|
var nilReturnCmd = func() Msg {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cmds []Cmd
|
|
|
|
expected Msg
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all nil",
|
|
|
|
cmds: []Cmd{nilReturnCmd, nilReturnCmd},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one error",
|
|
|
|
cmds: []Cmd{
|
|
|
|
nilReturnCmd,
|
|
|
|
func() Msg {
|
|
|
|
return expectedErrMsg
|
|
|
|
},
|
|
|
|
nilReturnCmd,
|
|
|
|
},
|
|
|
|
expected: expectedErrMsg,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "some msg",
|
|
|
|
cmds: []Cmd{
|
|
|
|
nilReturnCmd,
|
|
|
|
func() Msg {
|
|
|
|
return expectedStrMsg
|
|
|
|
},
|
|
|
|
nilReturnCmd,
|
|
|
|
},
|
|
|
|
expected: expectedStrMsg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if msg := Sequentially(test.cmds...)(); msg != test.expected {
|
|
|
|
t.Fatalf("expected a msg %v but got %v", test.expected, msg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|