github.com/quay/claircore@v1.5.28/version_test.go (about) 1 package claircore 2 3 import ( 4 "testing" 5 6 "github.com/Masterminds/semver" 7 "github.com/google/go-cmp/cmp" 8 ) 9 10 type versionTestcase struct { 11 Name string 12 Version Version 13 Want string 14 } 15 16 func (tc versionTestcase) StringTest(t *testing.T) { 17 t.Logf("%v → %s", tc.Version, tc.Want) 18 if got := tc.Version.String(); !cmp.Equal(tc.Want, got) { 19 t.Error(cmp.Diff(tc.Want, got)) 20 } 21 } 22 23 func (tc versionTestcase) MarshalTest(t *testing.T) { 24 var got Version 25 t.Logf("%v", tc.Version) 26 27 b, err := tc.Version.MarshalText() 28 if err != nil { 29 t.Error(err) 30 } 31 t.Logf("%v → %q", tc.Version, string(b)) 32 if err := got.UnmarshalText(b); err != nil { 33 t.Error(err) 34 } 35 if !cmp.Equal(tc.Version, got) { 36 t.Error(cmp.Diff(tc.Version, got)) 37 } 38 } 39 40 var versiontt = []versionTestcase{ 41 { 42 Name: "Zero", 43 Version: Version{ 44 Kind: "test", 45 V: [...]int32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 46 }, 47 Want: "0", 48 }, 49 { 50 Name: "Simple", 51 Version: Version{ 52 Kind: "test", 53 V: [...]int32{0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 54 }, 55 Want: "1", 56 }, 57 { 58 Name: "Epoch", 59 Version: Version{ 60 Kind: "test", 61 V: [...]int32{1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 62 }, 63 Want: "1!1", 64 }, 65 { 66 Name: "ZeroEpoch", 67 Version: Version{ 68 Kind: "test", 69 V: [...]int32{1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 70 }, 71 Want: "1!0", 72 }, 73 { 74 Name: "Range", 75 Version: Version{ 76 Kind: "test", 77 V: [...]int32{1, 1, 0, 0, 0, 1, 0, 0, 0, 0}, 78 }, 79 Want: "1!1.0.0.0.1", 80 }, 81 { 82 Name: "MidRange", 83 Version: Version{ 84 Kind: "test", 85 V: [...]int32{1, 0, 0, 1, 0, 1, 0, 0, 0, 0}, 86 }, 87 Want: "1!1.0.1", 88 }, 89 } 90 91 func TestVersionString(t *testing.T) { 92 for _, tc := range versiontt { 93 t.Run(tc.Name, tc.StringTest) 94 } 95 } 96 97 func TestVersionMarshal(t *testing.T) { 98 for _, tc := range versiontt { 99 t.Run(tc.Name, tc.MarshalTest) 100 } 101 } 102 103 func TestFromSemver(t *testing.T) { 104 testcases := []struct { 105 name string 106 semver *semver.Version 107 want Version 108 }{ 109 { 110 name: "0.3.0", 111 semver: semver.MustParse("0.3.0"), 112 want: Version{ 113 Kind: `semver`, 114 V: [...]int32{0, 0, 3, 0, 0, 0, 0, 0, 0, 0}, 115 }, 116 }, 117 { 118 name: "1.1.6", 119 semver: semver.MustParse("1.1.6"), 120 want: Version{ 121 Kind: `semver`, 122 V: [...]int32{0, 1, 1, 6, 0, 0, 0, 0, 0, 0}, 123 }, 124 }, 125 } 126 127 for _, tt := range testcases { 128 t.Run(tt.name, func(t *testing.T) { 129 got := FromSemver(tt.semver) 130 if !cmp.Equal(tt.want, got) { 131 t.Error(cmp.Diff(tt.want, got)) 132 } 133 }) 134 } 135 }