1package main
2
3import (
4	"testing"
5
6	"github.com/gdamore/tcell/v2"
7)
8
9func TestApplyAnsiCodes(t *testing.T) {
10	none := tcell.StyleDefault
11
12	tests := []struct {
13		s     string
14		st    tcell.Style
15		stExp tcell.Style
16	}{
17		{"", none, none},
18		{"", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
19		{"", none.Bold(true), none},
20		{"", none.Foreground(tcell.ColorMaroon).Bold(true), none},
21
22		{"0", none, none},
23		{"0", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
24		{"0", none.Bold(true), none},
25		{"0", none.Foreground(tcell.ColorMaroon).Bold(true), none},
26
27		{"1", none, none.Bold(true)},
28		{"4", none, none.Underline(true)},
29		{"7", none, none.Reverse(true)},
30
31		{"1", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Bold(true)},
32		{"4", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Underline(true)},
33		{"7", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Reverse(true)},
34
35		{"4", none.Bold(true), none.Bold(true).Underline(true)},
36		{"4", none.Foreground(tcell.ColorMaroon).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true).Underline(true)},
37
38		{"31", none, none.Foreground(tcell.ColorMaroon)},
39		{"31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
40		{"31", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
41
42		{"41", none, none.Background(tcell.ColorMaroon)},
43		{"41", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
44
45		{"1;31", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
46		{"1;31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
47
48		{"38;5;0", none, none.Foreground(tcell.ColorBlack)},
49		{"38;5;1", none, none.Foreground(tcell.ColorMaroon)},
50		{"38;5;8", none, none.Foreground(tcell.ColorGray)},
51		{"38;5;16", none, none.Foreground(tcell.Color16)},
52		{"38;5;232", none, none.Foreground(tcell.Color232)},
53
54		{"38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
55		{"38;5;1", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
56
57		{"48;5;0", none, none.Background(tcell.ColorBlack)},
58		{"48;5;1", none, none.Background(tcell.ColorMaroon)},
59		{"48;5;8", none, none.Background(tcell.ColorGray)},
60		{"48;5;16", none, none.Background(tcell.Color16)},
61		{"48;5;232", none, none.Background(tcell.Color232)},
62
63		{"48;5;1", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
64
65		{"1;38;5;1", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
66		{"1;38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
67
68		{"38;2;5;102;8", none, none.Foreground(tcell.NewRGBColor(5, 102, 8))},
69		{"48;2;0;48;143", none, none.Background(tcell.NewRGBColor(0, 48, 143))},
70
71		// Fixes color construction issue: https://github.com/gokcehan/lf/pull/439#issuecomment-674409446
72		{"38;5;34;1", none, none.Foreground(tcell.Color34).Bold(true)},
73	}
74
75	for _, test := range tests {
76		if stGot := applyAnsiCodes(test.s, test.st); stGot != test.stExp {
77			t.Errorf("at input '%s' with '%d' expected '%d' but got '%d'",
78				test.s, test.st, test.stExp, stGot)
79		}
80	}
81}
82