1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package validate
16
17 import (
18 "math"
19 "reflect"
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23 "k8s.io/kube-openapi/pkg/validation/spec"
24 )
25
26 func TestNumberValidator_EdgeCases(t *testing.T) {
27
28 var min = float64(math.MinInt32 - 1)
29 var max = float64(math.MaxInt32 + 1)
30
31 v := numberValidator{
32 Path: "path",
33 In: "in",
34
35
36 Maximum: &max,
37 ExclusiveMaximum: false,
38 Minimum: &min,
39 ExclusiveMinimum: false,
40
41 Type: "integer",
42 Format: "int32",
43 }
44
45
46
47 sources := []interface{}{
48 new(spec.Schema),
49 }
50
51 testNumberApply(t, &v, sources)
52
53 assert.False(t, v.Applies(float64(32), reflect.Float64))
54
55
56
57
58 res := v.Validate(int64(math.MaxInt32 + 2))
59 assert.True(t, res.HasErrors())
60
61
62 res = v.Validate(int64(math.MinInt32 - 2))
63 assert.True(t, res.HasErrors())
64 }
65
66 func testNumberApply(t *testing.T, v *numberValidator, sources []interface{}) {
67 for _, source := range sources {
68
69 assert.False(t, v.Applies(source, reflect.String))
70 assert.False(t, v.Applies(source, reflect.Struct))
71
72 assert.True(t, v.Applies(source, reflect.Int))
73 assert.True(t, v.Applies(source, reflect.Int8))
74 assert.True(t, v.Applies(source, reflect.Uint16))
75 assert.True(t, v.Applies(source, reflect.Uint32))
76 assert.True(t, v.Applies(source, reflect.Uint64))
77 assert.True(t, v.Applies(source, reflect.Uint))
78 assert.True(t, v.Applies(source, reflect.Uint8))
79 assert.True(t, v.Applies(source, reflect.Uint16))
80 assert.True(t, v.Applies(source, reflect.Uint32))
81 assert.True(t, v.Applies(source, reflect.Uint64))
82 assert.True(t, v.Applies(source, reflect.Float32))
83 assert.True(t, v.Applies(source, reflect.Float64))
84 }
85 }
86
87 func TestStringValidator_EdgeCases(t *testing.T) {
88
89
90 v := stringValidator{}
91
92
93
94 sources := []interface{}{
95 new(spec.Schema),
96 }
97
98 testStringApply(t, &v, sources)
99
100 assert.False(t, v.Applies("A string", reflect.String))
101
102 }
103
104 func testStringApply(t *testing.T, v *stringValidator, sources []interface{}) {
105 for _, source := range sources {
106
107 assert.False(t, v.Applies(source, reflect.Struct))
108 assert.False(t, v.Applies(source, reflect.Int))
109
110 assert.True(t, v.Applies(source, reflect.String))
111 }
112 }
113
114 func TestBasicCommonValidator_EdgeCases(t *testing.T) {
115
116
117 v := basicCommonValidator{}
118
119
120
121 sources := []interface{}{
122 new(spec.Schema),
123 }
124
125 testCommonApply(t, &v, sources)
126
127 assert.False(t, v.Applies("A string", reflect.String))
128
129 }
130
131 func testCommonApply(t *testing.T, v *basicCommonValidator, sources []interface{}) {
132 for _, source := range sources {
133 assert.True(t, v.Applies(source, reflect.String))
134 }
135 }
136
View as plain text