reference: use subtests for reference tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-10 15:14:53 +01:00
parent 21622ca699
commit f481f877f1
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -4,7 +4,6 @@ import (
_ "crypto/sha256" _ "crypto/sha256"
_ "crypto/sha512" _ "crypto/sha512"
"encoding/json" "encoding/json"
"strconv"
"strings" "strings"
"testing" "testing"
@ -268,65 +267,62 @@ func TestReferenceParse(t *testing.T) {
}, },
} }
for _, testcase := range referenceTestcases { for _, testcase := range referenceTestcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.input)+": "+format, v...) t.Run(testcase.input, func(t *testing.T) {
t.Fail()
}
repo, err := Parse(testcase.input) repo, err := Parse(testcase.input)
if testcase.err != nil { if testcase.err != nil {
if err == nil { if err == nil {
failf("missing expected error: %v", testcase.err) t.Errorf("missing expected error: %v", testcase.err)
} else if testcase.err != err { } else if testcase.err != err {
failf("mismatched error: got %v, expected %v", err, testcase.err) t.Errorf("mismatched error: got %v, expected %v", err, testcase.err)
} }
continue return
} else if err != nil { } else if err != nil {
failf("unexpected parse error: %v", err) t.Errorf("unexpected parse error: %v", err)
continue return
} }
if repo.String() != testcase.input { if repo.String() != testcase.input {
failf("mismatched repo: got %q, expected %q", repo.String(), testcase.input) t.Errorf("mismatched repo: got %q, expected %q", repo.String(), testcase.input)
} }
if named, ok := repo.(Named); ok { if named, ok := repo.(Named); ok {
if named.Name() != testcase.repository { if named.Name() != testcase.repository {
failf("unexpected repository: got %q, expected %q", named.Name(), testcase.repository) t.Errorf("unexpected repository: got %q, expected %q", named.Name(), testcase.repository)
} }
domain, _ := SplitHostname(named) domain, _ := SplitHostname(named)
if domain != testcase.domain { if domain != testcase.domain {
failf("unexpected domain: got %q, expected %q", domain, testcase.domain) t.Errorf("unexpected domain: got %q, expected %q", domain, testcase.domain)
} }
} else if testcase.repository != "" || testcase.domain != "" { } else if testcase.repository != "" || testcase.domain != "" {
failf("expected named type, got %T", repo) t.Errorf("expected named type, got %T", repo)
} }
tagged, ok := repo.(Tagged) tagged, ok := repo.(Tagged)
if testcase.tag != "" { if testcase.tag != "" {
if ok { if ok {
if tagged.Tag() != testcase.tag { if tagged.Tag() != testcase.tag {
failf("unexpected tag: got %q, expected %q", tagged.Tag(), testcase.tag) t.Errorf("unexpected tag: got %q, expected %q", tagged.Tag(), testcase.tag)
} }
} else { } else {
failf("expected tagged type, got %T", repo) t.Errorf("expected tagged type, got %T", repo)
} }
} else if ok { } else if ok {
failf("unexpected tagged type") t.Errorf("unexpected tagged type")
} }
digested, ok := repo.(Digested) digested, ok := repo.(Digested)
if testcase.digest != "" { if testcase.digest != "" {
if ok { if ok {
if digested.Digest().String() != testcase.digest { if digested.Digest().String() != testcase.digest {
failf("unexpected digest: got %q, expected %q", digested.Digest().String(), testcase.digest) t.Errorf("unexpected digest: got %q, expected %q", digested.Digest().String(), testcase.digest)
} }
} else { } else {
failf("expected digested type, got %T", repo) t.Errorf("expected digested type, got %T", repo)
} }
} else if ok { } else if ok {
failf("unexpected digested type") t.Errorf("unexpected digested type")
} }
})
} }
} }
@ -363,15 +359,13 @@ func TestWithNameFailure(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.input)+": "+format, v...) t.Run(testcase.input, func(t *testing.T) {
t.Fail()
}
_, err := WithName(testcase.input) _, err := WithName(testcase.input)
if err == nil { if err == nil {
failf("no error parsing name. expected: %s", testcase.err) t.Errorf("no error parsing name. expected: %s", testcase.err)
} }
})
} }
} }
@ -413,22 +407,20 @@ func TestSplitHostname(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.input)+": "+format, v...) t.Run(testcase.input, func(t *testing.T) {
t.Fail()
}
named, err := WithName(testcase.input) named, err := WithName(testcase.input)
if err != nil { if err != nil {
failf("error parsing name: %s", err) t.Errorf("error parsing name: %s", err)
} }
domain, name := SplitHostname(named) domain, name := SplitHostname(named)
if domain != testcase.domain { if domain != testcase.domain {
failf("unexpected domain: got %q, expected %q", domain, testcase.domain) t.Errorf("unexpected domain: got %q, expected %q", domain, testcase.domain)
} }
if name != testcase.name { if name != testcase.name {
failf("unexpected name: got %q, expected %q", name, testcase.name) t.Errorf("unexpected name: got %q, expected %q", name, testcase.name)
} }
})
} }
} }
@ -469,95 +461,92 @@ func TestSerialization(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.input)+": "+format, v...) t.Run(testcase.description, func(t *testing.T) {
t.Fail()
}
m := map[string]string{ m := map[string]string{
"Description": testcase.description, "Description": testcase.description,
"Field": testcase.input, "Field": testcase.input,
} }
b, err := json.Marshal(m) b, err := json.Marshal(m)
if err != nil { if err != nil {
failf("error marshalling: %v", err) t.Errorf("error marshalling: %v", err)
} }
t := serializationType{} st := serializationType{}
if err := json.Unmarshal(b, &t); err != nil { if err := json.Unmarshal(b, &st); err != nil {
if testcase.err == nil { if testcase.err == nil {
failf("error unmarshalling: %v", err) t.Errorf("error unmarshalling: %v", err)
} }
if err != testcase.err { if err != testcase.err {
failf("wrong error, expected %v, got %v", testcase.err, err) t.Errorf("wrong error, expected %v, got %v", testcase.err, err)
} }
continue return
} else if testcase.err != nil { } else if testcase.err != nil {
failf("expected error unmarshalling: %v", testcase.err) t.Errorf("expected error unmarshalling: %v", testcase.err)
} }
if t.Description != testcase.description { if st.Description != testcase.description {
failf("wrong description, expected %q, got %q", testcase.description, t.Description) t.Errorf("wrong description, expected %q, got %q", testcase.description, st.Description)
} }
ref := t.Field.Reference() ref := st.Field.Reference()
if named, ok := ref.(Named); ok { if named, ok := ref.(Named); ok {
if named.Name() != testcase.name { if named.Name() != testcase.name {
failf("unexpected repository: got %q, expected %q", named.Name(), testcase.name) t.Errorf("unexpected repository: got %q, expected %q", named.Name(), testcase.name)
} }
} else if testcase.name != "" { } else if testcase.name != "" {
failf("expected named type, got %T", ref) t.Errorf("expected named type, got %T", ref)
} }
tagged, ok := ref.(Tagged) tagged, ok := ref.(Tagged)
if testcase.tag != "" { if testcase.tag != "" {
if ok { if ok {
if tagged.Tag() != testcase.tag { if tagged.Tag() != testcase.tag {
failf("unexpected tag: got %q, expected %q", tagged.Tag(), testcase.tag) t.Errorf("unexpected tag: got %q, expected %q", tagged.Tag(), testcase.tag)
} }
} else { } else {
failf("expected tagged type, got %T", ref) t.Errorf("expected tagged type, got %T", ref)
} }
} else if ok { } else if ok {
failf("unexpected tagged type") t.Errorf("unexpected tagged type")
} }
digested, ok := ref.(Digested) digested, ok := ref.(Digested)
if testcase.digest != "" { if testcase.digest != "" {
if ok { if ok {
if digested.Digest().String() != testcase.digest { if digested.Digest().String() != testcase.digest {
failf("unexpected digest: got %q, expected %q", digested.Digest().String(), testcase.digest) t.Errorf("unexpected digest: got %q, expected %q", digested.Digest().String(), testcase.digest)
} }
} else { } else {
failf("expected digested type, got %T", ref) t.Errorf("expected digested type, got %T", ref)
} }
} else if ok { } else if ok {
failf("unexpected digested type") t.Errorf("unexpected digested type")
} }
t = serializationType{ st = serializationType{
Description: testcase.description, Description: testcase.description,
Field: AsField(ref), Field: AsField(ref),
} }
b2, err := json.Marshal(t) b2, err := json.Marshal(st)
if err != nil { if err != nil {
failf("error marshing serialization type: %v", err) t.Errorf("error marshing serialization type: %v", err)
} }
if string(b) != string(b2) { if string(b) != string(b2) {
failf("unexpected serialized value: expected %q, got %q", string(b), string(b2)) t.Errorf("unexpected serialized value: expected %q, got %q", string(b), string(b2))
} }
// Ensure t.Field is not implementing "Reference" directly, getting // Ensure st.Field is not implementing "Reference" directly, getting
// around the Reference type system // around the Reference type system
var fieldInterface interface{} = t.Field var fieldInterface interface{} = st.Field
if _, ok := fieldInterface.(Reference); ok { if _, ok := fieldInterface.(Reference); ok {
failf("field should not implement Reference interface") t.Errorf("field should not implement Reference interface")
} }
})
} }
} }
@ -596,30 +585,28 @@ func TestWithTag(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.name)+": "+format, v...) t.Run(testcase.combined, func(t *testing.T) {
t.Fail()
}
named, err := WithName(testcase.name) named, err := WithName(testcase.name)
if err != nil { if err != nil {
failf("error parsing name: %s", err) t.Errorf("error parsing name: %s", err)
} }
if testcase.digest != "" { if testcase.digest != "" {
canonical, err := WithDigest(named, testcase.digest) canonical, err := WithDigest(named, testcase.digest)
if err != nil { if err != nil {
failf("error adding digest") t.Errorf("error adding digest")
} }
named = canonical named = canonical
} }
tagged, err := WithTag(named, testcase.tag) tagged, err := WithTag(named, testcase.tag)
if err != nil { if err != nil {
failf("WithTag failed: %s", err) t.Errorf("WithTag failed: %s", err)
} }
if tagged.String() != testcase.combined { if tagged.String() != testcase.combined {
failf("unexpected: got %q, expected %q", tagged.String(), testcase.combined) t.Errorf("unexpected: got %q, expected %q", tagged.String(), testcase.combined)
} }
})
} }
} }
@ -653,29 +640,27 @@ func TestWithDigest(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.name)+": "+format, v...) t.Run(testcase.combined, func(t *testing.T) {
t.Fail()
}
named, err := WithName(testcase.name) named, err := WithName(testcase.name)
if err != nil { if err != nil {
failf("error parsing name: %s", err) t.Errorf("error parsing name: %s", err)
} }
if testcase.tag != "" { if testcase.tag != "" {
tagged, err := WithTag(named, testcase.tag) tagged, err := WithTag(named, testcase.tag)
if err != nil { if err != nil {
failf("error adding tag") t.Errorf("error adding tag")
} }
named = tagged named = tagged
} }
digested, err := WithDigest(named, testcase.digest) digested, err := WithDigest(named, testcase.digest)
if err != nil { if err != nil {
failf("WithDigest failed: %s", err) t.Errorf("WithDigest failed: %s", err)
} }
if digested.String() != testcase.combined { if digested.String() != testcase.combined {
failf("unexpected: got %q, expected %q", digested.String(), testcase.combined) t.Errorf("unexpected: got %q, expected %q", digested.String(), testcase.combined)
} }
})
} }
} }
@ -724,31 +709,29 @@ func TestParseNamed(t *testing.T) {
}, },
} }
for _, testcase := range testcases { for _, testcase := range testcases {
failf := func(format string, v ...interface{}) { testcase := testcase
t.Logf(strconv.Quote(testcase.input)+": "+format, v...) t.Run(testcase.input, func(t *testing.T) {
t.Fail()
}
named, err := ParseNamed(testcase.input) named, err := ParseNamed(testcase.input)
if err != nil && testcase.err == nil { if err != nil && testcase.err == nil {
failf("error parsing name: %s", err) t.Errorf("error parsing name: %s", err)
continue return
} else if err == nil && testcase.err != nil { } else if err == nil && testcase.err != nil {
failf("parsing succeeded: expected error %v", testcase.err) t.Errorf("parsing succeeded: expected error %v", testcase.err)
continue return
} else if err != testcase.err { } else if err != testcase.err {
failf("unexpected error %v, expected %v", err, testcase.err) t.Errorf("unexpected error %v, expected %v", err, testcase.err)
continue return
} else if err != nil { } else if err != nil {
continue return
} }
domain, name := SplitHostname(named) domain, name := SplitHostname(named)
if domain != testcase.domain { if domain != testcase.domain {
failf("unexpected domain: got %q, expected %q", domain, testcase.domain) t.Errorf("unexpected domain: got %q, expected %q", domain, testcase.domain)
} }
if name != testcase.name { if name != testcase.name {
failf("unexpected name: got %q, expected %q", name, testcase.name) t.Errorf("unexpected name: got %q, expected %q", name, testcase.name)
} }
})
} }
} }