-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmarker_test.go
More file actions
127 lines (113 loc) · 3.01 KB
/
marker_test.go
File metadata and controls
127 lines (113 loc) · 3.01 KB
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
package marker
import (
"fmt"
"regexp"
"testing"
"github.com/fatih/color"
"github.com/stretchr/testify/assert"
)
func Test_Mark(t *testing.T) {
blueFg := color.New(color.FgBlue)
blueFg.EnableColor()
blue := blueFg.SprintFunc()
redFg := color.New(color.FgRed)
redFg.EnableColor()
red := redFg.SprintFunc()
tests := []struct {
text string
matcher MatcherFunc
expected string
color *color.Color
}{
{
text: "Skydome is a data company.",
color: blueFg,
matcher: func(str string) Match {
return Match{Template: "%s is a data company.", Patterns: []string{"Skydome"}}
},
expected: fmt.Sprintf("%s is a data company.", blue("Skydome")),
},
{
text: "Skydome is Skydome. Give yourself freedom.",
color: redFg,
matcher: func(str string) Match {
return Match{Template: "%s is %s. Give yourself freedom.", Patterns: []string{"Skydome", "Skydome"}}
},
expected: fmt.Sprintf("%s is %s. Give yourself freedom.", red("Skydome"), red("Skydome")),
},
}
for _, testCase := range tests {
actual := Mark(testCase.text, testCase.matcher, testCase.color)
assert.Equal(t, testCase.expected, actual)
}
}
func Test_MarkMany(t *testing.T) {
blueFg := color.New(color.FgBlue)
blueFg.EnableColor()
blue := blueFg.SprintFunc()
redFg := color.New(color.FgRed)
redFg.EnableColor()
red := redFg.SprintFunc()
r, _ := regexp.Compile("i")
tests := []struct {
text string
matchers []MatcherFunc
expected string
color *color.Color
}{
{
text: "Skydome is a data company.",
color: blueFg,
matchers: []MatcherFunc{MatchN("Skydome", 1), MatchRegexp(r)},
expected: fmt.Sprintf("%s %ss a data company.", blue("Skydome"), blue("i")),
},
{
text: "Skydome is Skydome. Give yourself freedom.",
color: redFg,
matchers: []MatcherFunc{MatchAll("Skydome"), MatchAll("yourself")},
expected: fmt.Sprintf("%s is %s. Give %s freedom.", red("Skydome"), red("Skydome"), red("yourself")),
},
}
for _, testCase := range tests {
actual := MarkMany(testCase.text, testCase.color, testCase.matchers...)
assert.Equal(t, testCase.expected, actual)
}
}
func Benchmark_Mark(b *testing.B) {
blueFg := color.New(color.FgBlue)
blueFg.EnableColor()
b.ReportAllocs()
data := struct {
text string
matcher MatcherFunc
expected string
color *color.Color
}{
text: "Skydome is a data company.",
color: blueFg,
matcher: func(str string) Match {
return Match{Template: "%s is a data company.", Patterns: []string{"Skydome"}}
},
}
for i := 0; i < b.N; i++ {
Mark(data.text, data.matcher, data.color)
}
}
func Benchmark_MarkMany(b *testing.B) {
blueFg := color.New(color.FgBlue)
blueFg.EnableColor()
b.ReportAllocs()
data := struct {
text string
matchers []MatcherFunc
expected []string
color *color.Color
}{
text: "Skydome is a data company.",
color: blueFg,
matchers: []MatcherFunc{MatchAll("Skydome"), MatchAll("yourself")},
}
for i := 0; i < b.N; i++ {
MarkMany(data.text, data.color, data.matchers...)
}
}