-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate_value_test.go
90 lines (80 loc) · 1.95 KB
/
validate_value_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
package openapi_test
import (
"errors"
"reflect"
"strconv"
"testing"
openapi "github.com/nasa9084/go-openapi"
)
const (
exampleCom = "https://example.com"
exampleMail = "[email protected]"
)
type candidate struct {
label string
in openapi.Validater
err error
}
func testValidater(t *testing.T, candidates []candidate) {
t.Helper()
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
if err := c.in.Validate(); !reflect.DeepEqual(err, c.err) {
t.Errorf("error should be %s, but %s", c.err, err)
}
})
}
}
type mockValidater struct {
err error
}
func (v mockValidater) Validate() error {
return v.err
}
func TestValidateAll(t *testing.T) {
valid := mockValidater{}
invalid := mockValidater{errors.New("err")}
candidates := []struct {
label string
in []openapi.Validater
hasErr bool
}{
{"nil", nil, false},
{"empty", []openapi.Validater{}, false},
{"all valid", []openapi.Validater{valid, valid, valid}, false},
{"have invalid", []openapi.Validater{valid, invalid, valid}, true},
{"have nil", []openapi.Validater{valid, nil, valid, valid}, false},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
if err := openapi.ValidateAll(c.in); (err != nil) != c.hasErr {
t.Errorf("error: %s", err)
}
})
}
}
func TestMustURL(t *testing.T) {
candidates := []struct {
label string
in string
hasErr bool
}{
{"empty", "", true},
{"valid HTTP url", "http://example.com", false},
{"allowed relative path", "foo/bar/baz", true},
{"absolute path", "/foo/bar/baz", false},
{"plain string", "foobarbaz", true},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
if err := openapi.MustURL(c.label, c.in); (err != nil) != c.hasErr {
if c.hasErr {
t.Error("error should occured, but not")
return
}
t.Error("error should not occurred, but occurred")
return
}
})
}
}