(t *testing.T)
| 247 | } |
| 248 | |
| 249 | func Test_assignValidationMapping(t *testing.T) { |
| 250 | fieldNames := []string{"name0", "name1"} |
| 251 | fieldByName := func(n string) (FieldIndex, bool) { |
| 252 | for idx, name := range fieldNames { |
| 253 | if n == name { |
| 254 | return FieldIndex(idx), true |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return 0, false |
| 259 | } |
| 260 | |
| 261 | cfgValidation := ConfigValidation{"name0": "^val$", "name1": "^val$"} |
| 262 | validate := func(r Record) (bool, FieldIndex) { |
| 263 | if string(r.Get(0)) != "val" { |
| 264 | return false, 0 |
| 265 | } |
| 266 | if string(r.Get(1)) != "val" { |
| 267 | return false, 1 |
| 268 | } |
| 269 | return true, 0 |
| 270 | } |
| 271 | |
| 272 | tests := []struct { |
| 273 | name string |
| 274 | cfg *Config |
| 275 | comp Components |
| 276 | wantErr bool |
| 277 | skipCheck bool |
| 278 | }{ |
| 279 | { |
| 280 | name: "only in Config", |
| 281 | cfg: &Config{ |
| 282 | Validation: cfgValidation, |
| 283 | fieldByName: fieldByName, // needed by func assignValidationMapping |
| 284 | }, |
| 285 | comp: Components{}, |
| 286 | }, |
| 287 | { |
| 288 | name: "only in Components", |
| 289 | cfg: &Config{ |
| 290 | fieldByName: fieldByName, // needed by func assignValidationMapping |
| 291 | }, |
| 292 | comp: Components{ |
| 293 | Validate: validate, |
| 294 | }, |
| 295 | }, |
| 296 | { |
| 297 | name: "nothing set", |
| 298 | cfg: &Config{}, |
| 299 | comp: Components{}, |
| 300 | skipCheck: true, // check only that cfg.validate is not nil |
| 301 | }, |
| 302 | |
| 303 | // error cases |
| 304 | { |
| 305 | name: "validation set both in Config and Components", |
| 306 | cfg: &Config{ |
nothing calls this directly
no test coverage detected