-
Notifications
You must be signed in to change notification settings - Fork 38
/
string_test.go
102 lines (89 loc) · 2.14 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
package dry
import (
"reflect"
"strings"
"testing"
)
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 Test_TwoSlicesSubtraction(t *testing.T) {
A := []string{"apple", "orange", "banana", "peach", "plum"}
B := []string{"melon", "banana", "guava", "plum"}
wanted := []string{"apple", "orange", "peach"}
result := TwoSlicesSubtraction(A, B)
if !reflect.DeepEqual(wanted, result) {
t.Errorf("wanted: %v, but got: %v", wanted, result)
}
}