-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring_test.go
167 lines (151 loc) · 2.79 KB
/
string_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_StringMap(t *testing.T) {
result := StringMap(strings.TrimSpace, []string{" a ", " b ", "c", " d", "e "})
correct := []string{"a", "b", "c", "d", "e"}
if len(result) != len(correct) {
t.Fail()
}
for i := range result {
if result[i] != correct[i] {
t.Fail()
}
}
}
func Test_StringFilter(t *testing.T) {
hFunc := func(s string) bool {
return strings.HasPrefix(s, "h")
}
result := StringFilter(hFunc, []string{"cheese", "mouse", "hi", "there", "horse"})
correct := []string{"hi", "horse"}
if len(result) != len(correct) {
t.Fail()
}
for i := range result {
if result[i] != correct[i] {
t.Fail()
}
}
}
func Test_StringFindBetween(t *testing.T) {
s := "Hello <em>World</em>!"
between, remainder, found := StringFindBetween(s, "<em>", "</em>")
if between != "World" {
t.Fail()
}
if remainder != "!" {
t.Fail()
}
if !found {
t.Fail()
}
between, remainder, found = StringFindBetween(s, "l", "l")
if between != "" {
t.Fail()
}
if remainder != "o <em>World</em>!" {
t.Fail()
}
if !found {
t.Fail()
}
between, remainder, found = StringFindBetween(s, "<i>", "</i>")
if between != "" {
t.Fail()
}
if remainder != "Hello <em>World</em>!" {
t.Fail()
}
if found {
t.Fail()
}
}
func Test_StringStripHTMLTags(t *testing.T) {
withHTML := "<div>Hello > World <br/> <im src='xxx'/>"
skippedHTML := "Hello > World "
if StringStripHTMLTags(withHTML) != skippedHTML {
t.Fail()
}
}
func Test_StringReplaceHTMLTags(t *testing.T) {
withHTML := "<div>Hello > World <br/> <im src='xxx'/>"
replacedHTML := "xxHello > World xx xx"
if StringReplaceHTMLTags(withHTML, "xx") != replacedHTML {
t.Fail()
}
}
func TestStringsCommonPrefix(t *testing.T) {
tests := []struct {
a, b, want string
}{
{
"abc",
"abcdef",
"abc",
},
{
"abc",
"abc",
"abc",
},
{
"one",
"two",
"",
},
{
"проверыа",
"проверка",
"провер",
},
{
"п😹",
"п😸",
"п",
},
}
for i, tt := range tests {
t.Run("StringsCommonPrefix_"+strconv.Itoa(i), func(t *testing.T) {
if !assert.Equal(t, tt.want, StringsCommonPrefix(tt.a, tt.b)) {
t.Fail()
}
})
}
}
func TestStringsCRLF(t *testing.T) {
type args struct {
}
tests := []struct {
in string
want string
}{
{
"abcd\r\nefgh",
"abcd\nefgh",
},
{
"abcd\refgh\n",
"abcd\refgh\n",
},
{
"\r\n\r\r\n",
"\n\r\n",
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) {
got := StringsCRLF(tt.in)
assert.Equal(t, tt.want, got)
})
}
}