-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring_test.go
74 lines (64 loc) · 1.44 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
package slicer
import (
"strings"
"testing"
)
func TestStringFilter(t *testing.T) {
s := String()
s.Add("one")
s.Add("two")
s.Add("one")
s.Add("three")
s.Add("one")
expected := "one one one"
filtered := s.Filter(func(v string) bool {
return v == "one"
})
actual := strings.Join(filtered.AsSlice(), " ")
if expected != actual {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestStringEach(t *testing.T) {
s := String()
s.Add("one")
s.Add("two")
s.Add("three")
result := String()
s.Each(func(elem string) {
result.Add(elem + "!")
})
expected := "one! two! three!"
actual := strings.Join(result.AsSlice(), " ")
if expected != actual {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
// TestOptionalStringSlice tests when you construct a String with
// an existing slice
func TestOptionalStringSlice(t *testing.T) {
data := []string{"one", "two", "three"}
s := String(data)
result := ""
s.Each(func(elem string) {
result += elem
})
expected := "onetwothree"
if expected != result {
t.Errorf("Expected '%s', but got '%s'", expected, result)
}
}
// TestSort tests that the slicer can be sorted
func TestSort(t *testing.T) {
data := []string{"cat", "bat", "dog", "arachnid"}
s := String(data)
s.Sort()
result := ""
s.Each(func(elem string) {
result += elem
})
expected := "arachnidbatcatdog"
if expected != result {
t.Errorf("Expected '%s', but got '%s'", expected, result)
}
}