-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommon_test.go
110 lines (97 loc) · 2.32 KB
/
common_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
package sparkypmtatracking_test
import (
"errors"
"log"
"os"
"testing"
spmta "github.com/tuck1s/sparkypmtatracking"
)
// Can't test ConsoleAndLogFatal() as it exits
func TestMyLogger(t *testing.T) {
f := "common_test.log"
spmta.MyLogger(f)
s := errors.New("Something to log")
log.Println(s)
}
func Test_GetenvDefault(t *testing.T) {
vname := "some_weird_env_variable42"
blank := "---"
vval := "ta ta, a a value"
v := spmta.GetenvDefault(vname, blank)
if v != blank {
t.Errorf("Unexpected value")
}
os.Setenv(vname, vval)
v = spmta.GetenvDefault(vname, blank)
if v != vval {
t.Errorf("Unexpected value")
}
os.Unsetenv(vname)
}
func TestHostCleanup(t *testing.T) {
u := "example.com"
clean := "https://" + u
if spmta.HostCleanup(u) != clean {
t.Errorf("Unexpected value")
}
if spmta.HostCleanup(u+"/") != clean {
t.Errorf("Unexpected value")
}
if spmta.HostCleanup(u+"/api/v1") != clean {
t.Errorf("Unexpected value")
}
if spmta.HostCleanup(u+"/api/v1/") != clean {
t.Errorf("Unexpected value")
}
}
var haystack = []string{"the", "rain", "in", "spain", "falls", "mainly", "on", "the", "plain"}
const needle = "spain"
const miss = "snow"
func TestPositionIn(t *testing.T) {
p, found := spmta.PositionIn(haystack, needle)
if p != 3 || !found {
t.Errorf("Unexpected value")
}
p, found = spmta.PositionIn([]string{}, needle)
if p != 0 || found {
t.Errorf("Unexpected value")
}
p, found = spmta.PositionIn([]string{}, miss)
if p != 0 || found {
t.Errorf("Unexpected value")
}
}
func TestContains(t *testing.T) {
found := spmta.Contains(haystack, needle)
if !found {
t.Errorf("Unexpected value")
}
found = spmta.Contains(haystack, miss)
if found {
t.Errorf("Unexpected value")
}
}
func TestSafeStringToInt(t *testing.T) {
if v := spmta.SafeStringToInt("0123456"); v != 123456 {
t.Errorf("Unexpected value %d", v)
}
if v := spmta.SafeStringToInt(""); v != 0 {
t.Errorf("Unexpected value %d", v)
}
if v := spmta.SafeStringToInt(" "); v != 0 {
t.Errorf("Unexpected value %d", v)
}
if v := spmta.SafeStringToInt("-9876"); v != -9876 {
t.Errorf("Unexpected value %d", v)
}
if v := spmta.SafeStringToInt("kittens"); v != 0 {
t.Errorf("Unexpected value %d", v)
}
}
func TestMyRedis(t *testing.T) {
r := spmta.MyRedis()
if r == nil {
t.Errorf("Unexpected value")
}
r.Close()
}