1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
package vt100_test
import (
"io"
"strings"
"testing"
. "github.com/jaguilar/vt100"
"github.com/jaguilar/vt100/vttest"
"github.com/stretchr/testify/assert"
)
func splitLines(s string) [][]rune {
ss := strings.Split(s, "\n")
r := make([][]rune, len(ss))
for i, line := range ss {
r[i] = []rune(line)
}
return r
}
func esc(s string) string {
return "\u001b" + s
}
func cmd(s string) Command {
cmd, err := Decode(strings.NewReader(s))
if err != nil {
panic(err)
}
return cmd
}
func cmds(s string) []Command {
var c []Command
r := strings.NewReader(s)
for {
x, err := Decode(r)
if err == io.EOF {
return c
}
if err != nil {
panic(err)
}
c = append(c, x)
}
}
func TestPutRune(t *testing.T) {
v := vttest.FromLines("abc\ndef\nghi")
v.Cursor.Y = 1
v.Cursor.X = 1
assert.Nil(t, v.Process(cmd("z")))
assert.Equal(t, splitLines("abc\ndzf\nghi"), v.Content)
assert.Equal(t, 2, v.Cursor.X)
assert.Equal(t, 1, v.Cursor.Y)
}
func TestMoveCursor(t *testing.T) {
v := vttest.FromLines("abc\ndef\nghi")
assert.Nil(t, v.Process(cmd(esc("[3;1H"))))
assert.Equal(t, 2, v.Cursor.Y)
assert.Equal(t, 0, v.Cursor.X)
}
func TestCursorDirections(t *testing.T) {
v := vttest.FromLines("abc\ndef\nghi")
moves := strings.Join([]string{
esc("[2B"), // down down
esc("[2C"), // right right
esc("[A"), // up (no args = 1)
esc("[1D"), // left
}, "") // End state: +1, +1
s := strings.NewReader(moves)
want := []Cursor{
{Y: 2, X: 0},
{Y: 2, X: 2},
{Y: 1, X: 2},
{Y: 1, X: 1},
}
var got []Cursor
cmd, err := Decode(s)
for err == nil {
assert.Nil(t, v.Process(cmd))
got = append(got, v.Cursor)
cmd, err = Decode(s)
}
if assert.Equal(t, err, io.EOF) {
assert.Equal(t, want, got)
}
}
func TestErase(t *testing.T) {
c := Format{Fg: Yellow, Intensity: Bright}
var d Format
for _, tc := range []struct {
command Command
want *VT100
}{
{cmd(esc("[K")), vttest.FromLinesAndFormats("abcd\nef \nijkl", [][]Format{
{c, c, c, c},
{c, c, d, d},
{c, c, c, c},
})},
{cmd(esc("[1K")), vttest.FromLinesAndFormats("abcd\n h\nijkl", [][]Format{
{c, c, c, c},
{d, d, d, c},
{c, c, c, c},
})},
{cmd(esc("[2K")), vttest.FromLinesAndFormats("abcd\n \nijkl", [][]Format{
{c, c, c, c},
{d, d, d, d},
{c, c, c, c},
})},
{cmd(esc("[J")), vttest.FromLinesAndFormats("abcd\n \n ", [][]Format{
{c, c, c, c},
{d, d, d, d},
{d, d, d, d},
})},
{cmd(esc("[1J")), vttest.FromLinesAndFormats(" \n \nijkl", [][]Format{
{d, d, d, d},
{d, d, d, d},
{c, c, c, c},
})},
{cmd(esc("[2J")), vttest.FromLinesAndFormats(" \n \n ", [][]Format{
{d, d, d, d},
{d, d, d, d},
{d, d, d, d},
})},
} {
v := vttest.FromLinesAndFormats(
"abcd\nefgh\nijkl", [][]Format{
{c, c, c, c},
{c, c, c, c},
{c, c, c, c},
})
v.Cursor = Cursor{Y: 1, X: 2}
beforeCursor := v.Cursor
assert.Nil(t, v.Process(tc.command))
assert.Equal(t, tc.want.Content, v.Content, "while evaluating ", tc.command)
assert.Equal(t, tc.want.Format, v.Format, "while evaluating ", tc.command)
// Check the cursor separately. We don't set it on any of the test cases
// so we cannot expect it to be equal. It's not meant to change.
assert.Equal(t, beforeCursor, v.Cursor)
}
}
var (
bs = "\u0008" // Use strings to contain these runes so they can be concatenated easily.
lf = "\u000a"
cr = "\u000d"
)
func TestBackspace(t *testing.T) {
v := vttest.FromLines("BA..")
v.Cursor.Y, v.Cursor.X = 0, 2
backspace := cmd(bs)
assert.Nil(t, v.Process(backspace))
// Backspace doesn't actually delete text.
assert.Equal(t, vttest.FromLines("BA..").Content, v.Content)
assert.Equal(t, 1, v.Cursor.X)
v.Cursor.X = 0
assert.Nil(t, v.Process(backspace))
assert.Equal(t, 0, v.Cursor.X)
v = vttest.FromLines("..\n..")
v.Cursor.Y, v.Cursor.X = 1, 0
assert.Nil(t, v.Process(backspace))
assert.Equal(t, 0, v.Cursor.Y)
assert.Equal(t, 1, v.Cursor.X)
}
func TestLineFeed(t *testing.T) {
v := vttest.FromLines("AA\n..")
v.Cursor.X = 1
for _, c := range cmds(lf + "b") {
assert.Nil(t, v.Process(c))
}
assert.Equal(t, vttest.FromLines("AA\nb.").Content, v.Content)
}
func TestCarriageReturn(t *testing.T) {
v := vttest.FromLines("AA\n..")
v.Cursor.X = 1
for _, c := range cmds(cr + "b") {
assert.Nil(t, v.Process(c))
}
assert.Equal(t, vttest.FromLines("bA\n..").Content, v.Content)
}
func TestAttributes(t *testing.T) {
v := vttest.FromLines("....")
s := strings.NewReader(
esc("[2ma") + esc("[5;22;31mb") + esc("[0mc") + esc("[4;46md"))
cmd, err := Decode(s)
for err == nil {
assert.Nil(t, v.Process(cmd))
cmd, err = Decode(s)
}
assert.Equal(t, io.EOF, err)
assert.Equal(t, []rune("abcd"), v.Content[0])
assert.Equal(t, []Format{
{Intensity: Dim}, {Blink: true, Fg: Red}, {}, {Underscore: true, Bg: Cyan},
}, v.Format[0])
}
|