Skip to content

Commit

Permalink
test: add screen manipulation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Oct 9, 2022
1 parent 13cdfd2 commit ebfb091
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions screen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tea

import (
"bytes"
"testing"
)

func TestClearMsg(t *testing.T) {
tests := []struct {
name string
cmds sequenceMsg
expected string
}{
{
name: "clear_screen",
cmds: []Cmd{ClearScreen},
expected: "\x1b[?25l\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
{
name: "altscreen",
cmds: []Cmd{EnterAltScreen, ExitAltScreen},
expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?1049lsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
{
name: "altscreen_autoexit",
cmds: []Cmd{EnterAltScreen},
expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?1049l\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
{
name: "mouse_cellmotion",
cmds: []Cmd{EnableMouseCellMotion},
expected: "\x1b[?25l\x1b[?1002hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
{
name: "mouse_allmotion",
cmds: []Cmd{EnableMouseAllMotion},
expected: "\x1b[?25l\x1b[?1003hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
{
name: "cursor_hide",
cmds: []Cmd{HideCursor},
expected: "\x1b[?25l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer

m := &testModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))

test.cmds = append(test.cmds, Quit)
go p.Send(test.cmds)

if _, err := p.Run(); err != nil {
t.Fatal(err)
}

if buf.String() != test.expected {
t.Errorf("expected embedded sequence, got %q", buf.String())
}
})
}
}

0 comments on commit ebfb091

Please sign in to comment.