1
1
mirror of https://github.com/walles/moar.git synced 2024-11-26 13:46:16 +03:00
moar/m/ansiTokenizer_test.go

188 lines
6.8 KiB
Go
Raw Normal View History

package m
2019-07-15 14:34:42 +03:00
import (
"bufio"
"os"
"strings"
"testing"
"unicode/utf8"
2019-10-27 11:15:16 +03:00
log "github.com/sirupsen/logrus"
2021-04-15 16:16:06 +03:00
"github.com/walles/moar/twin"
2019-10-27 11:15:16 +03:00
"gotest.tools/assert"
2019-07-15 14:34:42 +03:00
)
// Verify that we can tokenize all lines in ../sample-files/*
// without logging any errors
func TestTokenize(t *testing.T) {
for _, fileName := range getTestFiles() {
2019-07-15 14:34:42 +03:00
file, err := os.Open(fileName)
if err != nil {
t.Errorf("Error opening file <%s>: %s", fileName, err.Error())
continue
}
2021-04-20 09:43:37 +03:00
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
2019-07-15 14:34:42 +03:00
scanner := bufio.NewScanner(file)
lineNumber := 0
for scanner.Scan() {
line := scanner.Text()
lineNumber++
var loglines strings.Builder
log.SetOutput(&loglines)
2019-07-15 14:34:42 +03:00
2021-04-15 16:16:06 +03:00
tokens, plainString := cellsFromString(line)
2019-07-15 14:34:42 +03:00
if len(tokens) != utf8.RuneCountInString(*plainString) {
t.Errorf("%s:%d: len(tokens)=%d, len(plainString)=%d for: <%s>",
fileName, lineNumber,
len(tokens), utf8.RuneCountInString(*plainString), line)
continue
}
if len(loglines.String()) != 0 {
t.Errorf("%s: %s", fileName, loglines.String())
continue
}
}
}
}
2019-10-27 11:15:16 +03:00
func TestUnderline(t *testing.T) {
2021-04-15 16:16:06 +03:00
tokens, _ := cellsFromString("a\x1b[4mb\x1b[24mc")
assert.Equal(t, len(tokens), 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, tokens[0], twin.Cell{Rune: 'a', Style: twin.StyleDefault})
assert.Equal(t, tokens[1], twin.Cell{Rune: 'b', Style: twin.StyleDefault.WithAttr(twin.AttrUnderline)})
assert.Equal(t, tokens[2], twin.Cell{Rune: 'c', Style: twin.StyleDefault})
}
func TestManPages(t *testing.T) {
2019-10-30 20:47:49 +03:00
// Bold
2021-04-15 16:16:06 +03:00
tokens, _ := cellsFromString("ab\bbc")
assert.Equal(t, len(tokens), 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, tokens[0], twin.Cell{Rune: 'a', Style: twin.StyleDefault})
assert.Equal(t, tokens[1], twin.Cell{Rune: 'b', Style: twin.StyleDefault.WithAttr(twin.AttrBold)})
assert.Equal(t, tokens[2], twin.Cell{Rune: 'c', Style: twin.StyleDefault})
2019-10-30 20:47:49 +03:00
// Underline
2021-04-15 16:16:06 +03:00
tokens, _ = cellsFromString("a_\bbc")
assert.Equal(t, len(tokens), 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, tokens[0], twin.Cell{Rune: 'a', Style: twin.StyleDefault})
assert.Equal(t, tokens[1], twin.Cell{Rune: 'b', Style: twin.StyleDefault.WithAttr(twin.AttrUnderline)})
assert.Equal(t, tokens[2], twin.Cell{Rune: 'c', Style: twin.StyleDefault})
// Bullet point 1, taken from doing this on my macOS system:
2020-03-31 10:24:29 +03:00
// env PAGER="hexdump -C" man printf | moar
2021-04-15 16:16:06 +03:00
tokens, _ = cellsFromString("a+\b+\bo\bob")
assert.Equal(t, len(tokens), 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, tokens[0], twin.Cell{Rune: 'a', Style: twin.StyleDefault})
assert.Equal(t, tokens[1], twin.Cell{Rune: '•', Style: twin.StyleDefault})
assert.Equal(t, tokens[2], twin.Cell{Rune: 'b', Style: twin.StyleDefault})
// Bullet point 2, taken from doing this using the "fish" shell on my macOS system:
// man printf | hexdump -C | moar
2021-04-15 16:16:06 +03:00
tokens, _ = cellsFromString("a+\bob")
assert.Equal(t, len(tokens), 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, tokens[0], twin.Cell{Rune: 'a', Style: twin.StyleDefault})
assert.Equal(t, tokens[1], twin.Cell{Rune: '•', Style: twin.StyleDefault})
assert.Equal(t, tokens[2], twin.Cell{Rune: 'b', Style: twin.StyleDefault})
2019-10-30 20:47:49 +03:00
}
2019-10-27 11:15:16 +03:00
func TestConsumeCompositeColorHappy(t *testing.T) {
// 8 bit color
// Example from: https://github.com/walles/moar/issues/14
2019-10-27 23:40:30 +03:00
newIndex, color, err := consumeCompositeColor([]string{"38", "5", "74"}, 0)
2019-10-27 11:15:16 +03:00
assert.NilError(t, err)
assert.Equal(t, newIndex, 3)
2021-04-15 16:16:06 +03:00
assert.Equal(t, *color, twin.NewColor256(74))
2019-10-27 11:15:16 +03:00
// 24 bit color
2019-10-27 23:40:30 +03:00
newIndex, color, err = consumeCompositeColor([]string{"38", "2", "10", "20", "30"}, 0)
2019-10-27 11:15:16 +03:00
assert.NilError(t, err)
2019-10-28 23:46:18 +03:00
assert.Equal(t, newIndex, 5)
2021-04-15 16:16:06 +03:00
assert.Equal(t, *color, twin.NewColor24Bit(10, 20, 30))
2019-10-27 11:15:16 +03:00
}
2019-10-27 11:58:26 +03:00
func TestConsumeCompositeColorHappyMidSequence(t *testing.T) {
// 8 bit color
// Example from: https://github.com/walles/moar/issues/14
2019-10-27 23:40:30 +03:00
newIndex, color, err := consumeCompositeColor([]string{"whatever", "38", "5", "74"}, 1)
2019-10-27 11:58:26 +03:00
assert.NilError(t, err)
assert.Equal(t, newIndex, 4)
2021-04-15 16:16:06 +03:00
assert.Equal(t, *color, twin.NewColor256(74))
2019-10-27 11:58:26 +03:00
// 24 bit color
2019-10-27 23:40:30 +03:00
newIndex, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20", "30"}, 1)
2019-10-27 11:58:26 +03:00
assert.NilError(t, err)
2019-10-28 23:46:18 +03:00
assert.Equal(t, newIndex, 6)
2021-04-15 16:16:06 +03:00
assert.Equal(t, *color, twin.NewColor24Bit(10, 20, 30))
2019-10-27 11:58:26 +03:00
}
2019-10-27 11:15:16 +03:00
func TestConsumeCompositeColorBadPrefix(t *testing.T) {
// 8 bit color
// Example from: https://github.com/walles/moar/issues/14
2019-10-27 23:40:30 +03:00
_, color, err := consumeCompositeColor([]string{"29"}, 0)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 11:15:16 +03:00
2019-10-27 12:03:19 +03:00
// Same test but mid-sequence, with initial index > 0
2019-10-27 23:40:30 +03:00
_, color, err = consumeCompositeColor([]string{"whatever", "29"}, 1)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 11:15:16 +03:00
}
func TestConsumeCompositeColorBadType(t *testing.T) {
2019-10-27 23:40:30 +03:00
_, color, err := consumeCompositeColor([]string{"38", "4"}, 0)
2019-10-27 11:15:16 +03:00
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 11:15:16 +03:00
2019-10-27 12:03:19 +03:00
// Same test but mid-sequence, with initial index > 0
2019-10-27 23:40:30 +03:00
_, color, err = consumeCompositeColor([]string{"whatever", "38", "4"}, 1)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
2019-10-27 23:45:26 +03:00
assert.Assert(t, color == nil)
}
func TestConsumeCompositeColorIncomplete(t *testing.T) {
_, color, err := consumeCompositeColor([]string{"38"}, 0)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete color sequence: <CSI 38m>")
2019-10-27 23:45:26 +03:00
assert.Assert(t, color == nil)
// Same test, mid-sequence
_, color, err = consumeCompositeColor([]string{"whatever", "38"}, 1)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete color sequence: <CSI 38m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 11:15:16 +03:00
}
2019-10-27 12:07:34 +03:00
func TestConsumeCompositeColorIncomplete8Bit(t *testing.T) {
2019-10-27 23:40:30 +03:00
_, color, err := consumeCompositeColor([]string{"38", "5"}, 0)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete 8 bit color sequence: <CSI 38;5m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 12:07:34 +03:00
// Same test, mid-sequence
2019-10-27 23:40:30 +03:00
_, color, err = consumeCompositeColor([]string{"whatever", "38", "5"}, 1)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete 8 bit color sequence: <CSI 38;5m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 12:07:34 +03:00
}
2019-10-27 12:11:25 +03:00
func TestConsumeCompositeColorIncomplete24Bit(t *testing.T) {
2019-10-27 23:40:30 +03:00
_, color, err := consumeCompositeColor([]string{"38", "2", "10", "20"}, 0)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 12:11:25 +03:00
// Same test, mid-sequence
2019-10-27 23:40:30 +03:00
_, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20"}, 1)
2021-04-19 21:29:05 +03:00
assert.Equal(t, err.Error(), "incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
2019-10-27 23:43:33 +03:00
assert.Assert(t, color == nil)
2019-10-27 12:11:25 +03:00
}
func TestUpdateStyle(t *testing.T) {
2021-04-15 16:16:06 +03:00
numberColored := updateStyle(twin.StyleDefault, "\x1b[33m")
assert.Equal(t, numberColored, twin.StyleDefault.Foreground(twin.NewColor16(3)))
}