-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_test.go
More file actions
67 lines (61 loc) · 1.37 KB
/
strings_test.go
File metadata and controls
67 lines (61 loc) · 1.37 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
package strutils
import (
"math/rand"
"regexp"
"testing"
"time"
)
func TestConcat(t *testing.T) {
testcases := map[string]struct {
input []string
expected string
}{
"empty": {
input: []string{},
expected: "",
},
"single": {
input: []string{"a"},
expected: "a",
},
"multi": {
input: []string{"a", "bb", "ccc"},
expected: "abbccc",
},
}
for name, example := range testcases {
t.Run(name, func(t *testing.T) {
var value string
if len(example.input) == 0 {
value = Concat()
} else {
value = Concat(example.input...)
}
if value != example.expected {
t.Fatalf("Expected Concat on %v to return %s, got %s", example.input, example.expected, value)
}
})
}
}
func TestGenRandomString(t *testing.T) {
rand.Seed(time.Now().UnixNano())
r, err := regexp.Compile("^[a-zA-Z0-9]*$")
if err != nil {
panic(err)
}
for i := -1; i <= 64; i++ {
result := GenRandomString(i)
if i < 1 {
if result != "" {
t.Fatalf("expected GenRandomString(%d) to return empty string, got %s", i, result)
}
} else {
if len(result) != i {
t.Fatalf("expected GenRandomString(%d) to return a string of length %d, got %s of length %d", i, i, result, len(result))
}
if !r.MatchString(result) {
t.Fatalf("expected GenRandomString(%d) to return an alphanumeric string, got %s instead", i, result)
}
}
}
}