-
Notifications
You must be signed in to change notification settings - Fork 2
/
oas_test.go
57 lines (49 loc) · 1.68 KB
/
oas_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
package valix
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestValidatorForWithOasTag(t *testing.T) {
type myStruct struct {
Foo string `json:"foo" oas:"description:'THIS IS ''DESC''!',title:'THIS IS TITLE',format:'THIS IS FORMAT',example:'THIS IS EXAMPLE',deprecated"`
}
v, err := ValidatorFor(myStruct{}, nil)
require.NoError(t, err)
require.NotNil(t, v)
pv, ok := v.Properties["foo"]
require.True(t, ok)
require.True(t, pv.OasInfo.Deprecated)
require.Equal(t, "THIS IS 'DESC'!", pv.OasInfo.Description)
require.Equal(t, "THIS IS TITLE", pv.OasInfo.Title)
require.Equal(t, "THIS IS FORMAT", pv.OasInfo.Format)
require.Equal(t, "THIS IS EXAMPLE", pv.OasInfo.Example)
}
func TestValidatorForWithOasTagUnknownTokenFails(t *testing.T) {
type myStruct struct {
Foo string `json:"foo" oas:"UNKNOWN_TOKEN"`
}
_, err := ValidatorFor(myStruct{}, nil)
require.Error(t, err)
require.Equal(t, fmt.Sprintf(msgOasUnknownTokenInTag, "UNKNOWN_TOKEN"), err.Error())
}
func TestValidatorForWithBadOasTag(t *testing.T) {
type myStruct struct {
Foo string `json:"foo" oas:"title:'<unclosed single quote"`
}
_, err := ValidatorFor(myStruct{}, nil)
require.Error(t, err)
require.Equal(t, "unclosed ''' at position 6", err.Error())
}
func TestOasTagItemExpectedString(t *testing.T) {
pv := &PropertyValidator{}
err := pv.addOasTagItem("desc:xxx")
require.Error(t, err)
require.Equal(t, fmt.Sprintf(msgOasExpectedString, "desc"), err.Error())
}
func TestOasTagItemUnexpectedColon(t *testing.T) {
pv := &PropertyValidator{}
err := pv.addOasTagItem("deprecated:xxx")
require.Error(t, err)
require.Equal(t, fmt.Sprintf(msgOasUnexpectedColon, "deprecated"), err.Error())
}