-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_test.go
65 lines (48 loc) · 1.64 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
package hvalid
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStringContains(t *testing.T) {
var s1 string = "hello,world"
substr := "llo"
err := Validate[string](s1, ContainsStr(substr))
assert.NoError(t, err)
var s2 string = "iamlyon.ne"
err = Validate[string](s2, ContainsStr("nee", "不包含指定字符串"))
assert.Error(t, err)
}
func TestIsEmail(t *testing.T) {
var e1 string = "lyon.nee@outlook.com"
err := Validate[string](e1, IsEmail("无效的邮箱地址"))
assert.NoError(t, err)
var e2 string = "lyon.neeoutlook.com"
err = Validate[string](e2, IsEmail("无效的邮箱地址"))
assert.Error(t, err)
}
func TestIsUrl(t *testing.T) {
err := Validate[string]("testURL", IsUrl("无效的url"))
assert.Error(t, err)
err = Validate[string]("lyon.nee/", IsUrl())
assert.Error(t, err)
err = Validate[string]("http://github.com", IsUrl())
assert.NoError(t, err)
err = Validate[string]("https://github.com/lyonnee/hvalid", IsUrl())
assert.NoError(t, err)
}
func TestIsIP(t *testing.T) {
err := Validate[string]("192.168.0.1", IsIPv4())
assert.NoError(t, err)
err = Validate[string]("0.0.0.0", IsIPv4())
assert.NoError(t, err)
err = Validate[string]("266.0.0.-1", IsIPv4("无效的IPv4地址"))
assert.Error(t, err)
err = Validate[string]("ac.0.a.-1", IsIPv4("无效的IPv4地址"))
assert.Error(t, err)
err = Validate[string]("FC00:0000:130F:0000:0000:09C0:876A:130B", IsIPv6())
assert.NoError(t, err)
err = Validate[string]("XXXX:0000:130F:0000:0000:09C0:876A:130B", IsIPv6())
assert.Error(t, err)
err = Validate[string]("abcf:0000:130F:efgd:0000:09C0:876A:130B", IsIPv6())
assert.Error(t, err)
}