Debian Codegraph @

validate/test/validate_test.go

raw symbols SCIP
1 // Copyright 2019 Red Hat, Inc
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.)
14
15 package validate
16
17 import (
18 "errors"
19 "reflect"
20 "testing"
21
22 "github.com/coreos/vcontext/path"
23 "github.com/coreos/vcontext/report"
24 "github.com/coreos/vcontext/validate"
25 )
26
27 // Why are these tests in their own package?
28 // Go doesn't let you call reflect.Value.Interface() on non-public types but we
29 // need to test that. This package prevents those types from needing to be
30 // included in the exported package that people use.
31
32 var (
33 dummy = errors.New("dummy")
34 )
35
36 func fromSingleError(c []interface{}, err error) (r report.Report) {
37 ctx := path.ContextPath{
38 Path: c,
39 }
40 r.AddOnError(ctx, err)
41 return
42 }
43
44 type Test1 struct{}
45
46 func (t Test1) Validate(c path.ContextPath) (r report.Report) {
47 return
48 }
49
50 type Test2 struct{}
51
52 func (t Test2) Validate(c path.ContextPath) (r report.Report) {
53 r.AddOnError(c, dummy)
54 return
55 }
56
57 type Test3 struct {
58 Foo Test2 `yaml:"yaml,garbage" json:"json,garbage"`
59 }
60
61 type Test4 struct {
62 Test2 `yaml:",inline"`
63 }
64
65 type Test5 struct {
66 Bar Test3 `yaml:"yaml2,garbage" json:"json2,garbage"`
67 }
68
69 type Test6 struct {
70 Bar *Test3 `yaml:"yaml2,garbage" json:"json2,garbage"`
71 }
72
73 type Test7 struct {
74 Bar []*Test2 `yaml:"yaml" json:"json"`
75 }
76
77 func TestValidate(t *testing.T) {
78 type test struct {
79 in interface{}
80 src string
81 out report.Report
82 }
83 tests := []test{
84 {
85 in: nil,
86 },
87 {
88 in: struct{}{},
89 },
90 {
91 in: []int{},
92 },
93 {
94 in: Test1{},
95 },
96 {
97 in: Test2{},
98 out: fromSingleError(nil, dummy),
99 },
100 {
101 in: Test3{},
102 out: fromSingleError([]interface{}{"Foo"}, dummy),
103 },
104 {
105 in: Test3{},
106 src: "json",
107 out: fromSingleError([]interface{}{"json"}, dummy),
108 },
109 {
110 in: Test3{},
111 src: "yaml",
112 out: fromSingleError([]interface{}{"yaml"}, dummy),
113 },
114 {
115 in: Test4{},
116 out: fromSingleError(nil, dummy),
117 },
118 {
119 in: Test4{},
120 src: "json",
121 out: fromSingleError(nil, dummy),
122 },
123 {
124 in: Test4{},
125 src: "yaml",
126 out: fromSingleError(nil, dummy),
127 },
128 {
129 in: Test5{},
130 out: fromSingleError([]interface{}{"Bar", "Foo"}, dummy),
131 },
132 {
133 in: Test5{},
134 src: "json",
135 out: fromSingleError([]interface{}{"json2", "json"}, dummy),
136 },
137 {
138 in: Test5{},
139 src: "yaml",
140 out: fromSingleError([]interface{}{"yaml2", "yaml"}, dummy),
141 },
142 {
143 in: Test6{},
144 },
145 {
146 in: Test6{Bar: new(Test3)},
147 out: fromSingleError([]interface{}{"Bar", "Foo"}, dummy),
148 },
149 {
150 in: Test6{Bar: new(Test3)},
151 src: "json",
152 out: fromSingleError([]interface{}{"json2", "json"}, dummy),
153 },
154 {
155 in: Test6{Bar: new(Test3)},
156 src: "yaml",
157 out: fromSingleError([]interface{}{"yaml2", "yaml"}, dummy),
158 },
159 {
160 in: Test7{},
161 },
162 {
163 in: Test7{Bar: []*Test2{new(Test2)}},
164 out: fromSingleError([]interface{}{"Bar", 0}, dummy),
165 },
166 {
167 in: Test7{Bar: []*Test2{new(Test2)}},
168 src: "json",
169 out: fromSingleError([]interface{}{"json", 0}, dummy),
170 },
171 {
172 in: Test7{Bar: []*Test2{new(Test2)}},
173 src: "yaml",
174 out: fromSingleError([]interface{}{"yaml", 0}, dummy),
175 },
176 }
177 for i, test := range tests {
178 expected := test.out
179 for i := range test.out.Entries {
180 test.out.Entries[i].Context.Tag = test.src
181 }
182
183 actual := validate.Validate(test.in, test.src)
184 if !reflect.DeepEqual(expected, actual) {
185 t.Errorf("Fail %da: expected %+v got %+v", i, expected, actual)
186 }
187
188 // now test on the pointer version for good measure
189 actual = validate.Validate(&test.in, test.src)
190 if !reflect.DeepEqual(expected, actual) {
191 t.Errorf("Fail %db: expected %+v got %+v", i, expected, actual)
192 }
193 }
194 }