-
Notifications
You must be signed in to change notification settings - Fork 1
/
part_string_test.go
141 lines (125 loc) · 4.97 KB
/
part_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
package mc
import (
"fmt"
"github.com/stretchr/testify/require"
"math"
"testing"
)
func TestStringEnsurePrefix(t *testing.T) {
assertions := require.New(t)
assertions.Equal("https://test", StringEnsurePrefix("https://test", "https"))
assertions.Equal("https://test", StringEnsurePrefix("test", "https://"))
}
func TestStringEnsureSuffix(t *testing.T) {
assertions := require.New(t)
assertions.Equal("a.log", StringEnsureSuffix("a.log", ".log"))
assertions.Equal("a.log", StringEnsureSuffix("a", ".log"))
}
func TestStringToInt(t *testing.T) {
assertions := require.New(t)
assertions.Equal(1, StringToInt("1"))
assertions.Equal(0, StringToInt("0"))
assertions.Equal(-1, StringToInt("-1"))
assertions.Equal(0, StringToInt("-0"))
assertions.Equal(0, StringToInt("a"))
}
func TestStringToInt8(t *testing.T) {
assertions := require.New(t)
assertions.Equal(int8(1), StringToInt8("1"))
assertions.Equal(int8(0), StringToInt8("0"))
assertions.Equal(int8(-1), StringToInt8("-1"))
assertions.Equal(int8(0), StringToInt8("-0"))
assertions.Equal(int8(0), StringToInt8("a"))
assertions.Equal(int8(math.MaxInt8), StringToInt8(fmt.Sprintf("%d", math.MaxInt8+1)))
}
func TestStringToInt16(t *testing.T) {
assertions := require.New(t)
assertions.Equal(int16(1), StringToInt16("1"))
assertions.Equal(int16(0), StringToInt16("0"))
assertions.Equal(int16(-1), StringToInt16("-1"))
assertions.Equal(int16(0), StringToInt16("-0"))
assertions.Equal(int16(0), StringToInt16("a"))
assertions.Equal(int16(math.MaxInt16), StringToInt16(fmt.Sprintf("%d", math.MaxInt16+1)))
}
func TestStringToInt32(t *testing.T) {
assertions := require.New(t)
assertions.Equal(int32(1), StringToInt32("1"))
assertions.Equal(int32(0), StringToInt32("0"))
assertions.Equal(int32(-1), StringToInt32("-1"))
assertions.Equal(int32(0), StringToInt32("-0"))
assertions.Equal(int32(0), StringToInt32("a"))
assertions.Equal(int32(math.MaxInt32), StringToInt32(fmt.Sprintf("%d", math.MaxInt32+1)))
}
func TestStringToInt64(t *testing.T) {
assertions := require.New(t)
assertions.Equal(int64(1), StringToInt64("1"))
assertions.Equal(int64(0), StringToInt64("0"))
assertions.Equal(int64(-1), StringToInt64("-1"))
assertions.Equal(int64(0), StringToInt64("-0"))
assertions.Equal(int64(0), StringToInt64("a"))
// math.MaxInt64 == 9223372036854775807
assertions.Equal(int64(math.MaxInt64), StringToInt64("9223372036854775807"))
assertions.Equal(int64(math.MaxInt64), StringToInt64("9223372036854775808"))
}
func TestStringToUint(t *testing.T) {
assertions := require.New(t)
assertions.Equal(uint(1), StringToUint("1"))
assertions.Equal(uint(0), StringToUint("0"))
assertions.Equal(uint(0), StringToUint("-1"))
assertions.Equal(uint(0), StringToUint("-0"))
assertions.Equal(uint(0), StringToUint("a"))
}
func TestStringToUint8(t *testing.T) {
assertions := require.New(t)
assertions.Equal(uint8(1), StringToUint8("1"))
assertions.Equal(uint8(0), StringToUint8("0"))
assertions.Equal(uint8(0), StringToUint8("-1"))
assertions.Equal(uint8(0), StringToUint8("-0"))
assertions.Equal(uint8(0), StringToUint8("a"))
assertions.Equal(uint8(math.MaxUint8), StringToUint8(fmt.Sprintf("%d", math.MaxUint8+1)))
}
func TestStringToUint16(t *testing.T) {
assertions := require.New(t)
assertions.Equal(uint16(1), StringToUint16("1"))
assertions.Equal(uint16(0), StringToUint16("0"))
assertions.Equal(uint16(0), StringToUint16("-1"))
assertions.Equal(uint16(0), StringToUint16("-0"))
assertions.Equal(uint16(0), StringToUint16("a"))
assertions.Equal(uint16(math.MaxUint16), StringToUint16(fmt.Sprintf("%d", math.MaxUint16+1)))
}
func TestStringToUint32(t *testing.T) {
assertions := require.New(t)
assertions.Equal(uint32(1), StringToUint32("1"))
assertions.Equal(uint32(0), StringToUint32("0"))
assertions.Equal(uint32(0), StringToUint32("-1"))
assertions.Equal(uint32(0), StringToUint32("-0"))
assertions.Equal(uint32(0), StringToUint32("a"))
assertions.Equal(uint32(math.MaxUint32), StringToUint32(fmt.Sprintf("%d", math.MaxUint32+1)))
}
func TestStringToUint64(t *testing.T) {
assertions := require.New(t)
assertions.Equal(uint64(1), StringToUint64("1"))
assertions.Equal(uint64(0), StringToUint64("0"))
assertions.Equal(uint64(0), StringToUint64("-1"))
assertions.Equal(uint64(0), StringToUint64("-0"))
assertions.Equal(uint64(0), StringToUint64("a"))
// math.MaxUint64 == 18446744073709551615
assertions.Equal(uint64(math.MaxUint64), StringToUint64("18446744073709551615"))
assertions.Equal(uint64(math.MaxUint64), StringToUint64("18446744073709551616"))
}
func TestStringRand(t *testing.T) {
assertions := require.New(t)
s := StringRand(8)
assertions.Len(s, 8)
}
func TestStringRandWithCharset(t *testing.T) {
assertions := require.New(t)
s := StringRandWithCharset(8, "1234567890")
assertions.Len(s, 8)
}
func TestStringRandWithCharsetAndPrefix(t *testing.T) {
assertions := require.New(t)
assertions.False(StringIsValidEmail("invalid"))
assertions.False(StringIsValidEmail("Me <me@gmail.com>"))
assertions.True(StringIsValidEmail("me@gmail.com"))
}