forked from jesseduffield/gocui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape_test.go
117 lines (102 loc) · 4.12 KB
/
escape_test.go
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
package gocui
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseOne(t *testing.T) {
var ei *escapeInterpreter
ei = newEscapeInterpreter(OutputNormal)
isEscape, err := ei.parseOne('a')
assert.Equal(t, false, isEscape)
assert.NoError(t, err)
ei = newEscapeInterpreter(OutputNormal)
parseEscRunes(t, ei, []rune{'\x1b', '[', '0', 'K'})
_, ok := ei.instruction.(eraseInLineFromCursor)
assert.Equal(t, true, ok)
ei = newEscapeInterpreter(OutputNormal)
parseEscRunes(t, ei, []rune{'\x1b', '[', 'K'})
_, ok = ei.instruction.(eraseInLineFromCursor)
assert.Equal(t, true, ok)
ei = newEscapeInterpreter(OutputNormal)
parseEscRunes(t, ei, []rune{'\x1b', '[', '1', 'K'})
_, ok = ei.instruction.(noInstruction)
assert.Equal(t, true, ok)
}
func TestParseOneColours(t *testing.T) {
scenarios := []struct {
outputMode OutputMode
runes []rune
expectedFg Attribute
expectedBg Attribute
}{
{OutputNormal, []rune{'\x1b', '[', '3', '0', 'm'}, ColorBlack, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '1', 'm'}, ColorRed, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '2', 'm'}, ColorGreen, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '3', 'm'}, ColorYellow, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '4', 'm'}, ColorBlue, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '5', 'm'}, ColorMagenta, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '6', 'm'}, ColorCyan, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '3', '7', 'm'}, ColorWhite, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '4', '0', 'm'}, ColorDefault, ColorBlack},
{OutputNormal, []rune{'\x1b', '[', '4', '1', 'm'}, ColorDefault, ColorRed},
{OutputNormal, []rune{'\x1b', '[', '4', '2', 'm'}, ColorDefault, ColorGreen},
{OutputNormal, []rune{'\x1b', '[', '4', '3', 'm'}, ColorDefault, ColorYellow},
{OutputNormal, []rune{'\x1b', '[', '4', '4', 'm'}, ColorDefault, ColorBlue},
{OutputNormal, []rune{'\x1b', '[', '4', '5', 'm'}, ColorDefault, ColorMagenta},
{OutputNormal, []rune{'\x1b', '[', '4', '6', 'm'}, ColorDefault, ColorCyan},
{OutputNormal, []rune{'\x1b', '[', '4', '7', 'm'}, ColorDefault, ColorWhite},
{OutputNormal, []rune{'\x1b', '[', '4', '7', ';', '3', '1', 'm'}, ColorRed, ColorWhite},
}
for _, scenario := range scenarios {
ei := newEscapeInterpreter(scenario.outputMode)
parseEscRunes(t, ei, scenario.runes)
assert.Equal(t, scenario.expectedFg, ei.curFgColor)
assert.Equal(t, scenario.expectedBg, ei.curBgColor)
}
// resetting colours
scenarios = []struct {
outputMode OutputMode
runes []rune
expectedFg Attribute
expectedBg Attribute
}{
{OutputNormal, []rune{'\x1b', '[', '3', '9', 'm'}, ColorDefault, ColorRed},
{OutputNormal, []rune{'\x1b', '[', '4', '9', 'm'}, ColorRed, ColorDefault},
{OutputNormal, []rune{'\x1b', '[', '0', 'm'}, ColorDefault, ColorDefault},
}
for _, scenario := range scenarios {
ei := newEscapeInterpreter(scenario.outputMode)
ei.curFgColor = ColorRed
ei.curBgColor = ColorRed
parseEscRunes(t, ei, scenario.runes)
assert.Equal(t, scenario.expectedFg, ei.curFgColor)
assert.Equal(t, scenario.expectedBg, ei.curBgColor)
}
// setting attributes
attrScenarios := []struct {
outputMode OutputMode
runes []rune
expectedAttr Attribute
}{
{OutputNormal, []rune{'\x1b', '[', '1', 'm'}, AttrBold},
{OutputNormal, []rune{'\x1b', '[', '2', 'm'}, AttrDim},
{OutputNormal, []rune{'\x1b', '[', '3', 'm'}, AttrItalic},
{OutputNormal, []rune{'\x1b', '[', '4', 'm'}, AttrUnderline},
{OutputNormal, []rune{'\x1b', '[', '5', 'm'}, AttrBlink},
{OutputNormal, []rune{'\x1b', '[', '7', 'm'}, AttrReverse},
{OutputNormal, []rune{'\x1b', '[', '9', 'm'}, AttrStrikeThrough},
}
for _, scenario := range attrScenarios {
ei := newEscapeInterpreter(scenario.outputMode)
parseEscRunes(t, ei, scenario.runes)
isBold := ei.curFgColor&scenario.expectedAttr == scenario.expectedAttr
assert.Equal(t, true, isBold)
}
}
func parseEscRunes(t *testing.T, ei *escapeInterpreter, runes []rune) {
for _, r := range runes {
isEscape, err := ei.parseOne(r)
assert.Equal(t, true, isEscape)
assert.NoError(t, err)
}
}