github.com/quay/claircore@v1.5.28/test/mock.go (about) 1 package test 2 3 import ( 4 "fmt" 5 6 "github.com/quay/claircore" 7 ) 8 9 // Some helpers for using gomock. 10 11 // DigestMatcher is a [gomock.Matcher] for [claircore.Digest]s. 12 type DigestMatcher string 13 14 // Matches implements [gomock.Matcher]. 15 func (d *DigestMatcher) Matches(x any) bool { 16 v := string(*d) 17 switch x := x.(type) { 18 case string: 19 return v == x 20 case *claircore.Digest: 21 return v == x.String() 22 default: 23 if s, ok := x.(fmt.Stringer); ok { 24 return v == s.String() 25 } 26 } 27 return false 28 } 29 30 // String implements [gomock.Matcher]. 31 func (d *DigestMatcher) String() string { 32 return string(*d) 33 } 34 35 // LayerMatcher is a [gomock.Matcher] for [claircore.LayerDescription]s that 36 // allows them to match both [LayerDescription] and [Layer]. 37 type LayerMatcher struct { 38 *claircore.LayerDescription 39 } 40 41 // NewLayerMatcher returns a [LayerMatcher]. 42 func NewLayerMatcher(desc *claircore.LayerDescription) *LayerMatcher { 43 return &LayerMatcher{ 44 LayerDescription: desc, 45 } 46 } 47 48 // DigestMatcher returns a [gomock.Matcher] implementation for the digest of the 49 // layer. 50 func (m *LayerMatcher) DigestMatcher() *DigestMatcher { 51 return (*DigestMatcher)(&m.Digest) 52 } 53 54 // Matches implements [gomock.Matcher]. 55 func (m *LayerMatcher) Matches(x any) bool { 56 switch x := x.(type) { 57 case *claircore.Layer: 58 return x.Hash.String() == m.Digest 59 case claircore.Layer: 60 return x.Hash.String() == m.Digest 61 case *claircore.LayerDescription: 62 return x.Digest == m.Digest 63 case claircore.LayerDescription: 64 return x.Digest == m.Digest 65 } 66 return false 67 } 68 69 // String implements [gomock.Matcher]. 70 func (m *LayerMatcher) String() string { 71 return m.Digest 72 }