This introduces a new `CursorModel` interface that can be implemented by the main model to provide a cursor position and style. A nil cursor will hide the cursor. Otherwise, the cursor will be shown at the position and with the style specified. Use `NewCursor` to quickly create a cursor for a given position with default styles. This method is called in-sync with the `Model.View` method. It also removes the previously introduced `Frame` type and `NewFrame` function. The main model can now return a `string` to render the program's UI. The main model can return a `Cursor` to set the cursor position and style. The old `Init` method is now back and it returns a `Cmd` instead of a both a `Model` and a `Cmd`. This is to make the mental model more straight forward, and to keep things backwards compatible.
90 lines
1.5 KiB
Go
90 lines
1.5 KiB
Go
package tea
|
|
|
|
import (
|
|
"bytes"
|
|
"os/exec"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
type execFinishedMsg struct{ err error }
|
|
|
|
type testExecModel struct {
|
|
cmd string
|
|
err error
|
|
}
|
|
|
|
func (m *testExecModel) Init() Cmd {
|
|
c := exec.Command(m.cmd) //nolint:gosec
|
|
return ExecProcess(c, func(err error) Msg {
|
|
return execFinishedMsg{err}
|
|
})
|
|
}
|
|
|
|
func (m *testExecModel) Update(msg Msg) (Model, Cmd) {
|
|
switch msg := msg.(type) {
|
|
case execFinishedMsg:
|
|
if msg.err != nil {
|
|
m.err = msg.err
|
|
}
|
|
return m, Quit
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m *testExecModel) View() string {
|
|
return "\n"
|
|
}
|
|
|
|
func TestTeaExec(t *testing.T) {
|
|
type test struct {
|
|
name string
|
|
cmd string
|
|
expectErr bool
|
|
}
|
|
|
|
// TODO: add more tests for windows
|
|
tests := []test{
|
|
{
|
|
name: "invalid command",
|
|
cmd: "invalid",
|
|
expectErr: true,
|
|
},
|
|
}
|
|
|
|
if runtime.GOOS != "windows" {
|
|
tests = append(tests, []test{
|
|
{
|
|
name: "true",
|
|
cmd: "true",
|
|
expectErr: false,
|
|
},
|
|
{
|
|
name: "false",
|
|
cmd: "false",
|
|
expectErr: true,
|
|
},
|
|
}...)
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
var in bytes.Buffer
|
|
|
|
m := &testExecModel{cmd: test.cmd}
|
|
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
|
if _, err := p.Run(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if m.err != nil && !test.expectErr {
|
|
t.Errorf("expected no error, got %v", m.err)
|
|
}
|
|
if m.err == nil && test.expectErr {
|
|
t.Error("expected error, got nil")
|
|
}
|
|
})
|
|
}
|
|
}
|