forked from go-ozzo/ozzo-validation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch_test.go
95 lines (86 loc) · 1.43 KB
/
match_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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package validation
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMatch(t *testing.T) {
var v2 *string
tests := []struct {
tag string
re string
value any
err string
}{
{
"t1",
"[a-z]+",
"abc",
"",
},
{
"t2",
"[a-z]+",
"",
"",
},
{
"t3",
"[a-z]+",
v2,
"",
},
{
"t4",
"[a-z]+",
"123",
"must be in a valid format",
},
{
"t5",
"[a-z]+",
[]byte("abc"),
"",
},
{
"t6",
"[a-z]+",
[]byte("123"),
"must be in a valid format",
},
{
"t7",
"[a-z]+",
[]byte(""),
"",
},
{
"t8",
"[a-z]+",
nil,
"",
},
}
for _, test := range tests {
r := Match(regexp.MustCompile(test.re))
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
}
}
func Test_MatchRule_Error(t *testing.T) {
r := Match(regexp.MustCompile("[a-z]+"))
assert.Equal(t, "must be in a valid format", r.Validate("13").Error())
r = r.Error("123")
assert.Equal(t, "123", r.err.Message())
}
func TestMatchRule_ErrorObject(t *testing.T) {
r := Match(regexp.MustCompile("[a-z]+"))
err := NewError("code", "abc")
r = r.ErrorObject(err)
assert.Equal(t, err, r.err)
assert.Equal(t, err.Code(), r.err.Code())
assert.Equal(t, err.Message(), r.err.Message())
}