2019-07-10 00:41:49 +03:00
|
|
|
package m
|
|
|
|
|
2019-07-15 14:34:42 +03:00
|
|
|
import (
|
2021-04-25 21:40:38 +03:00
|
|
|
"fmt"
|
2019-07-15 14:34:42 +03:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-04-25 21:40:38 +03:00
|
|
|
"unicode"
|
2019-07-15 14:34:42 +03:00
|
|
|
"unicode/utf8"
|
2019-10-27 11:15:16 +03:00
|
|
|
|
2020-03-25 10:55:35 +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
|
|
|
)
|
|
|
|
|
2021-04-25 13:25:52 +03:00
|
|
|
// Convert a cells array to a plain string
|
|
|
|
func cellsToPlainString(cells []twin.Cell) string {
|
|
|
|
returnMe := ""
|
|
|
|
for _, cell := range cells {
|
|
|
|
returnMe += string(cell.Rune)
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnMe
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-29 19:08:54 +03:00
|
|
|
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
|
|
|
|
2021-04-25 22:58:31 +03:00
|
|
|
myReader := NewReaderFromStream(fileName, file)
|
|
|
|
<-myReader.done
|
|
|
|
for lineNumber := 1; lineNumber <= myReader.GetLineCount(); lineNumber++ {
|
|
|
|
line := myReader.GetLine(lineNumber)
|
2019-07-15 14:34:42 +03:00
|
|
|
lineNumber++
|
|
|
|
|
|
|
|
var loglines strings.Builder
|
2019-11-27 20:43:46 +03:00
|
|
|
log.SetOutput(&loglines)
|
2019-07-15 14:34:42 +03:00
|
|
|
|
2021-05-02 20:28:51 +03:00
|
|
|
tokens := cellsFromString(line.raw)
|
|
|
|
plainString := withoutFormatting(line.raw)
|
2021-04-24 18:20:09 +03:00
|
|
|
if len(tokens) != utf8.RuneCountInString(plainString) {
|
2019-07-15 14:34:42 +03:00
|
|
|
t.Errorf("%s:%d: len(tokens)=%d, len(plainString)=%d for: <%s>",
|
|
|
|
fileName, lineNumber,
|
2021-05-02 20:28:51 +03:00
|
|
|
len(tokens), utf8.RuneCountInString(plainString), line.raw)
|
2019-07-15 14:34:42 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-25 11:08:19 +03:00
|
|
|
// Tokens and plain have the same lengths, compare contents
|
2021-04-25 14:21:03 +03:00
|
|
|
plainStringChars := []rune(plainString)
|
|
|
|
for index, plainChar := range plainStringChars {
|
2021-04-25 11:08:19 +03:00
|
|
|
cellChar := tokens[index]
|
2021-04-25 13:25:52 +03:00
|
|
|
if cellChar.Rune == plainChar {
|
|
|
|
continue
|
2021-04-25 11:08:19 +03:00
|
|
|
}
|
2021-04-25 13:25:52 +03:00
|
|
|
|
2021-04-25 17:32:00 +03:00
|
|
|
if cellChar.Rune == '•' && plainChar == 'o' {
|
|
|
|
// Pretty bullets on man pages
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:25:52 +03:00
|
|
|
// Chars mismatch!
|
|
|
|
plainStringFromCells := cellsToPlainString(tokens)
|
|
|
|
positionMarker := strings.Repeat(" ", index) + "^"
|
2021-04-25 21:40:38 +03:00
|
|
|
cellCharString := string(cellChar.Rune)
|
|
|
|
if !unicode.IsPrint(cellChar.Rune) {
|
|
|
|
cellCharString = fmt.Sprint(int(cellChar.Rune))
|
|
|
|
}
|
|
|
|
plainCharString := string(plainChar)
|
|
|
|
if !unicode.IsPrint(plainChar) {
|
|
|
|
plainCharString = fmt.Sprint(int(plainChar))
|
|
|
|
}
|
|
|
|
t.Errorf("%s:%d, 0-based column %d: cell char <%s> != plain char <%s>:\nPlain: %s\nCells: %s\n %s",
|
2021-04-25 13:25:52 +03:00
|
|
|
fileName, lineNumber, index,
|
2021-04-25 21:40:38 +03:00
|
|
|
cellCharString, plainCharString,
|
2021-04-25 13:25:52 +03:00
|
|
|
plainString,
|
|
|
|
plainStringFromCells,
|
|
|
|
positionMarker,
|
|
|
|
)
|
|
|
|
break
|
2021-04-25 11:08:19 +03:00
|
|
|
}
|
|
|
|
|
2019-07-15 14:34:42 +03:00
|
|
|
if len(loglines.String()) != 0 {
|
|
|
|
t.Errorf("%s: %s", fileName, loglines.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 11:15:16 +03:00
|
|
|
|
2020-03-25 10:55:35 +03:00
|
|
|
func TestUnderline(t *testing.T) {
|
2021-04-24 18:20:09 +03:00
|
|
|
tokens := cellsFromString("a\x1b[4mb\x1b[24mc")
|
2020-03-25 10:55:35 +03:00
|
|
|
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})
|
2020-03-25 10:55:35 +03:00
|
|
|
}
|
|
|
|
|
2020-03-25 23:42:18 +03:00
|
|
|
func TestManPages(t *testing.T) {
|
2019-10-30 20:47:49 +03:00
|
|
|
// Bold
|
2021-04-24 18:20:09 +03:00
|
|
|
tokens := cellsFromString("ab\bbc")
|
2020-03-25 23:42:18 +03:00
|
|
|
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-24 18:20:09 +03:00
|
|
|
tokens = cellsFromString("a_\bbc")
|
2020-03-25 23:42:18 +03:00
|
|
|
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})
|
2019-11-27 23:20:50 +03:00
|
|
|
|
2020-03-31 10:36:53 +03:00
|
|
|
// 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-24 18:20:09 +03:00
|
|
|
tokens = cellsFromString("a+\b+\bo\bob")
|
2020-03-25 23:42:18 +03:00
|
|
|
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})
|
2020-03-31 10:36:53 +03:00
|
|
|
|
|
|
|
// Bullet point 2, taken from doing this using the "fish" shell on my macOS system:
|
|
|
|
// man printf | hexdump -C | moar
|
2021-04-24 18:20:09 +03:00
|
|
|
tokens = cellsFromString("a+\bob")
|
2020-03-31 10:36:53 +03:00
|
|
|
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
|
|
|
}
|
2020-12-06 18:22:24 +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)))
|
2020-12-06 18:22:24 +03:00
|
|
|
}
|