Skip to content
Myr

Add a tea.Sequentially command

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

authored by Carlos Alexandro Becker · committed by Christian Rocha · parent b65205a · Browse files · 2 files+80 added−0 removed
This commit

State

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

Trailers

  • Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

Diff stats

2 files+80 added−0 removed

  • other:+80 added−0 removed

2 files+80 added−0 removed

added:commands_test.go+56 added
1package tea23import (4 "fmt"5 "testing"6)78func TestSequentially(t *testing.T) {9 var expectedErrMsg = fmt.Errorf("some err")10 var expectedStrMsg = "some msg"1112 var nilReturnCmd = func() Msg {13 return nil14 }1516 tests := []struct {17 name string18 cmds []Cmd19 expected Msg20 }{21 {22 name: "all nil",23 cmds: []Cmd{nilReturnCmd, nilReturnCmd},24 expected: nil,25 },26 {27 name: "one error",28 cmds: []Cmd{29 nilReturnCmd,30 func() Msg {31 return expectedErrMsg32 },33 nilReturnCmd,34 },35 expected: expectedErrMsg,36 },37 {38 name: "some msg",39 cmds: []Cmd{40 nilReturnCmd,41 func() Msg {42 return expectedStrMsg43 },44 nilReturnCmd,45 },46 expected: expectedStrMsg,47 },48 }49 for _, test := range tests {50 t.Run(test.name, func(t *testing.T) {51 if msg := Sequentially(test.cmds...)(); msg != test.expected {52 t.Fatalf("expected a msg %v but got %v", test.expected, msg)53 }54 })55 }56}
modified:commands.go+24 added
49 unmodified lines
50 t := time.NewTimer(d)51 return fn(<-t.C)52 }53}5455// Sequentially produces a command that sequentially executes the given56// commands.57// The Msg returned is the first non-nil message returned by a Cmd.58//59// func saveStateCmd() Msg {60// if err := save(); err != nil {61// return errMsg{err}62// }63// return nil64// }65//66// cmd := Sequentially(saveStateCmd, Quit)67//68func Sequentially(cmds ...Cmd) Cmd {69 return func() Msg {70 for _, cmd := range cmds {71 if msg := cmd(); msg != nil {72 return msg73 }74 }75 return nil76 }77}