mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
test: quit/kill model after the first render
This commit is contained in:
55
tea_test.go
55
tea_test.go
@@ -2,16 +2,20 @@ package tea
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testModel struct{}
|
type testModel struct {
|
||||||
|
executed atomic.Value
|
||||||
|
}
|
||||||
|
|
||||||
func (m testModel) Init() Cmd {
|
func (m testModel) Init() Cmd {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m testModel) Update(msg Msg) (Model, Cmd) {
|
func (m *testModel) Update(msg Msg) (Model, Cmd) {
|
||||||
switch msg.(type) {
|
switch msg.(type) {
|
||||||
case KeyMsg:
|
case KeyMsg:
|
||||||
return m, Quit
|
return m, Quit
|
||||||
@@ -19,7 +23,8 @@ func (m testModel) Update(msg Msg) (Model, Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m testModel) View() string {
|
func (m *testModel) View() string {
|
||||||
|
m.executed.Store(true)
|
||||||
return "success\n"
|
return "success\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +33,7 @@ func TestTeaModel(t *testing.T) {
|
|||||||
var in bytes.Buffer
|
var in bytes.Buffer
|
||||||
in.Write([]byte("q"))
|
in.Write([]byte("q"))
|
||||||
|
|
||||||
p := NewProgram(testModel{}, WithInput(&in), WithOutput(&buf))
|
p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf))
|
||||||
if err := p.Start(); err != nil {
|
if err := p.Start(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -37,3 +42,45 @@ func TestTeaModel(t *testing.T) {
|
|||||||
t.Fatal("no output")
|
t.Fatal("no output")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTeaQuit(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var in bytes.Buffer
|
||||||
|
|
||||||
|
m := &testModel{}
|
||||||
|
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
if m.executed.Load() != nil {
|
||||||
|
p.Quit()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := p.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTeaKill(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var in bytes.Buffer
|
||||||
|
|
||||||
|
m := &testModel{}
|
||||||
|
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
if m.executed.Load() != nil {
|
||||||
|
p.Kill()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := p.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
2
tty.go
2
tty.go
@@ -26,7 +26,7 @@ func (p *Program) initTerminal() error {
|
|||||||
|
|
||||||
// restoreTerminalState restores the terminal to the state prior to running the
|
// restoreTerminalState restores the terminal to the state prior to running the
|
||||||
// Bubble Tea program.
|
// Bubble Tea program.
|
||||||
func (p Program) restoreTerminalState() error {
|
func (p *Program) restoreTerminalState() error {
|
||||||
p.output.ShowCursor()
|
p.output.ShowCursor()
|
||||||
|
|
||||||
if p.console != nil {
|
if p.console != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user