package ansi import ( "bytes" "reflect" "strings" "testing" "github.com/stretchr/testify/assert" ) func TestUTF8(t *testing.T) { tests := []struct { name string input string expected []Sequence }{ { name: "UTF-8", input: "🔥", expected: []Sequence{ Print{"🔥", 2}, }, }, { name: "UTF-8", input: "👩‍🚀", expected: []Sequence{ Print{"👩‍🚀", 2}, }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { r := strings.NewReader(test.input) parse := NewParser(r) i := 0 for { seq := <-parse.Next() if _, ok := seq.(EOF); ok { assert.Equal(t, len(test.expected), i, "wrong amount of sequences") break } if i < len(test.expected) { assert.Equal(t, test.expected[i], seq) } i += 1 } }) } } func TestIn(t *testing.T) { tests := []struct { name string inRange []rune input rune expected bool }{ { name: "endpoint min", inRange: []rune{0x00, 0x20}, input: 0x00, expected: true, }, { name: "endpoint max", inRange: []rune{0x00, 0x20}, input: 0x20, expected: true, }, { name: "within", inRange: []rune{0x00, 0x20}, input: 0x19, expected: true, }, { name: "outside", inRange: []rune{0x00, 0x20}, input: 0x21, expected: false, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { actual := in(test.input, test.inRange[0], test.inRange[1]) assert.Equal(t, test.expected, actual) }) } } func TestAnywhere(t *testing.T) { tests := []struct { expected stateFn name string input rune }{ { name: "0x18", input: 0x18, expected: ground, }, { name: "0x1A", input: 0x1A, expected: ground, }, { name: "0x1B", input: 0x1B, expected: escape, }, { name: "eof", input: eof, expected: nil, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { r := bytes.NewBuffer(nil) parse := NewParser(r) called := false parse.exit = func() { called = true } actual := anywhere(test.input, parse) act := reflect.ValueOf(actual).Pointer() exp := reflect.ValueOf(test.expected).Pointer() assert.Equal(t, exp, act, "wrong return function") if test.expected != nil { assert.True(t, called, "exit function not called") } }) } } func TestCSI(t *testing.T) { tests := []struct { name string input string expected []Sequence }{ { name: "CSI Entry + C0", input: "a\x1b[\x00", expected: []Sequence{ Print{"a", 1}, C0(0x00), }, }, { name: "CSI Entry + escape", input: "a\x1b[\x1b", expected: []Sequence{ Print{"a", 1}, }, }, { name: "CSI Entry + ignore", input: "a\x1b[\x7F", expected: []Sequence{ Print{"a", 1}, }, }, { name: "CSI Entry + dispatch", input: "a\x1b[c", expected: []Sequence{ Print{"a", 1}, CSI{ Final: 'c', }, }, }, { name: "CSI Param with collect first", input: "a\x1b[