-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorexp_test.go
130 lines (125 loc) · 3.54 KB
/
colorexp_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
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"reflect"
"testing"
)
func TestAddRange(t *testing.T) {
tests := []struct {
name string
existing []rangeWithID
newRange rangeWithID
expected []rangeWithID
}{
{
name: "Before 1",
existing: []rangeWithID{{5, 8, 1}},
newRange: rangeWithID{3, 5, 2},
expected: []rangeWithID{{3, 5, 2}, {5, 8, 1}},
},
{
name: "Before 2",
existing: []rangeWithID{{5, 8, 1}},
newRange: rangeWithID{3, 4, 2},
expected: []rangeWithID{{3, 4, 2}, {5, 8, 1}},
},
{
name: "After 1",
existing: []rangeWithID{{1, 3, 0}},
newRange: rangeWithID{3, 5, 2},
expected: []rangeWithID{{1, 3, 0}, {3, 5, 2}},
},
{
name: "After 2",
existing: []rangeWithID{{1, 3, 0}},
newRange: rangeWithID{4, 5, 2},
expected: []rangeWithID{{1, 3, 0}, {4, 5, 2}},
},
{
name: "In-between 1",
existing: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
newRange: rangeWithID{3, 5, 2},
expected: []rangeWithID{{1, 3, 0}, {3, 5, 2}, {5, 8, 1}},
},
{
name: "In-between 2",
existing: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
newRange: rangeWithID{3, 4, 2},
expected: []rangeWithID{{1, 3, 0}, {3, 4, 2}, {5, 8, 1}},
},
{
name: "In-between 3",
existing: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
newRange: rangeWithID{4, 5, 2},
expected: []rangeWithID{{1, 3, 0}, {4, 5, 2}, {5, 8, 1}},
},
{
name: "Partial overlap",
existing: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
newRange: rangeWithID{2, 6, 2},
expected: []rangeWithID{{1, 3, 0}, {3, 5, 2}, {5, 8, 1}},
},
{
name: "Full overlap",
existing: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
newRange: rangeWithID{6, 7, 2},
expected: []rangeWithID{{1, 3, 0}, {5, 8, 1}},
},
{
name: "Overlap and extend",
existing: []rangeWithID{{1, 5, 0}, {10, 15, 1}},
newRange: rangeWithID{3, 12, 2},
expected: []rangeWithID{{1, 5, 0}, {5, 10, 2}, {10, 15, 1}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := addRange(tt.existing, tt.newRange)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("addRange() got = %v, want %v", got, tt.expected)
}
})
}
}
func TestColorize(t *testing.T) {
tests := []struct {
name string
s string
reversedColors [][]string
patternColorCount int
ranges []rangeWithID
want string
}{
{
name: "single colorization",
s: "Hello, world!",
reversedColors: [][]string{{"\033[31m", "\033[0m"}},
patternColorCount: 1,
ranges: []rangeWithID{{7, 12, 0}},
want: "Hello, \033[31mworld\033[0m!",
},
{
name: "multiple colorization",
s: "Hello, beautiful world!",
reversedColors: [][]string{{"\033[32m", "\033[0m"}, {"\033[31m", "\033[0m"}},
patternColorCount: 2,
ranges: []rangeWithID{{7, 16, 0}, {17, 22, 1}},
want: "Hello, \033[31mbeautiful\033[0m \033[32mworld\033[0m!",
},
{
name: "cycle colors",
s: "Hello, beautiful world!",
reversedColors: [][]string{{"\033[32m", "\033[0m"}, {"\033[31m", "\033[0m"}},
patternColorCount: 2,
ranges: []rangeWithID{{7, 16, 0}, {17, 22, 2}},
want: "Hello, \033[31mbeautiful\033[0m \033[31mworld\033[0m!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := colorize(tt.s, tt.reversedColors, tt.ranges, tt.patternColorCount)
if got != tt.want {
t.Errorf("colorize() = %v, want %v", got, tt.want)
}
})
}
}