| 342 | } |
| 343 | |
| 344 | func TestSerialization(t *testing.T) { |
| 345 | testcases := []struct { |
| 346 | description string |
| 347 | input string |
| 348 | name string |
| 349 | tag string |
| 350 | digest string |
| 351 | err error |
| 352 | }{ |
| 353 | { |
| 354 | description: "empty value", |
| 355 | err: ErrNameEmpty, |
| 356 | }, |
| 357 | { |
| 358 | description: "just a name", |
| 359 | input: "example.com:8000/named", |
| 360 | name: "example.com:8000/named", |
| 361 | }, |
| 362 | { |
| 363 | description: "name with a tag", |
| 364 | input: "example.com:8000/named:tagged", |
| 365 | name: "example.com:8000/named", |
| 366 | tag: "tagged", |
| 367 | }, |
| 368 | { |
| 369 | description: "name with digest", |
| 370 | input: "other.com/named@sha256:1234567890098765432112345667890098765432112345667890098765432112", |
| 371 | name: "other.com/named", |
| 372 | digest: "sha256:1234567890098765432112345667890098765432112345667890098765432112", |
| 373 | }, |
| 374 | } |
| 375 | for _, testcase := range testcases { |
| 376 | failf := func(format string, v ...interface{}) { |
| 377 | t.Logf(strconv.Quote(testcase.input)+": "+format, v...) |
| 378 | t.Fail() |
| 379 | } |
| 380 | |
| 381 | m := map[string]string{ |
| 382 | "Description": testcase.description, |
| 383 | "Field": testcase.input, |
| 384 | } |
| 385 | b, err := json.Marshal(m) |
| 386 | if err != nil { |
| 387 | failf("error marshaling: %v", err) |
| 388 | } |
| 389 | t := serializationType{} |
| 390 | |
| 391 | if err := json.Unmarshal(b, &t); err != nil { |
| 392 | if testcase.err == nil { |
| 393 | failf("error unmarshaling: %v", err) |
| 394 | } |
| 395 | if err != testcase.err { |
| 396 | failf("wrong error, expected %v, got %v", testcase.err, err) |
| 397 | } |
| 398 | |
| 399 | continue |
| 400 | } else if testcase.err != nil { |
| 401 | failf("expected error unmarshaling: %v", testcase.err) |