github.com/quay/claircore@v1.5.28/pkg/pep440/range_test.go (about) 1 package pep440 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 ) 8 9 type rangeTestcase struct { 10 Name string 11 In string 12 Want Range 13 Match []matchTestcase 14 } 15 16 type matchTestcase struct { 17 In string 18 Match bool 19 } 20 21 func (tc rangeTestcase) RunParse(t *testing.T) { 22 t.Logf("%s → %s", tc.In, tc.Want.String()) 23 r, err := ParseRange(tc.In) 24 if err != nil { 25 t.Error(err) 26 } 27 if !cmp.Equal(tc.Want, r) { 28 t.Error(cmp.Diff(tc.Want, r)) 29 } 30 } 31 32 func (tc rangeTestcase) RunMatch(t *testing.T) { 33 t.Log(tc.In) 34 r, err := ParseRange(tc.In) 35 if err != nil { 36 t.Fatal(err) 37 } 38 if len(tc.Match) == 0 { 39 t.SkipNow() 40 } 41 for _, pair := range tc.Match { 42 t.Run(pair.In, func(t *testing.T) { 43 v, err := Parse(pair.In) 44 if err != nil { 45 t.Fatal(err) 46 } 47 got, want := r.Match(&v), pair.Match 48 if got != want { 49 t.Errorf("got: %v, want: %v", got, want) 50 } 51 }) 52 } 53 if !cmp.Equal(tc.Want, r) { 54 t.Error(cmp.Diff(tc.Want, r)) 55 } 56 } 57 58 var rangeCases = []rangeTestcase{ 59 { 60 Name: "Simple", 61 In: ">1.0", 62 Want: Range{ 63 criterion{Op: opGT, V: Version{Release: []int{1, 0}}}, 64 }, 65 Match: []matchTestcase{ 66 {In: "1.0", Match: false}, 67 {In: "1.0.0.pre1", Match: false}, 68 {In: "1.0.0.1", Match: true}, 69 {In: "2.0", Match: true}, 70 }, 71 }, 72 { 73 Name: "SimpleLT", 74 In: "<2022.12.07", 75 Want: Range{ 76 criterion{Op: opLT, V: Version{Release: []int{2022, 12, 7}}}, 77 }, 78 Match: []matchTestcase{ 79 {In: "2022.12.07", Match: false}, 80 {In: "2022.12.7", Match: false}, 81 {In: "2022.12.08", Match: false}, 82 {In: "2022.12.06", Match: true}, 83 }, 84 }, 85 { 86 Name: "SimpleLTE", 87 In: "<=2022.12.07", 88 Want: Range{ 89 criterion{Op: opLTE, V: Version{Release: []int{2022, 12, 7}}}, 90 }, 91 Match: []matchTestcase{ 92 {In: "2022.12.07", Match: true}, 93 {In: "2022.12.7", Match: true}, 94 {In: "2022.12.08", Match: false}, 95 {In: "2022.12.8", Match: false}, 96 }, 97 }, 98 { 99 Name: "Compatible", 100 In: "~=1.1", 101 Want: Range{ 102 criterion{Op: opGTE, V: Version{Release: []int{1, 1}}}, 103 criterion{Op: opLT, V: Version{Release: []int{2}}}, 104 }, 105 Match: []matchTestcase{ 106 {In: "1.1", Match: true}, 107 {In: "1.1.0.pre1", Match: false}, 108 {In: "1.1.0.1", Match: true}, 109 {In: "2.0", Match: false}, 110 }, 111 }, 112 { 113 Name: "CompatiblePatch", 114 In: "~=1.1.10", 115 Want: Range{ 116 criterion{Op: opGTE, V: Version{Release: []int{1, 1, 10}}}, 117 criterion{Op: opLT, V: Version{Release: []int{1, 2}}}, 118 }, 119 Match: []matchTestcase{ 120 {In: "1.1", Match: false}, 121 {In: "1.1.10.pre1", Match: false}, 122 {In: "1.1.10.1", Match: true}, 123 {In: "2.0", Match: false}, 124 }, 125 }, 126 { 127 Name: "CompatiblePost", 128 In: "~= 2.2.post3", 129 Want: Range{ 130 criterion{Op: opGTE, V: Version{Release: []int{2, 2}, Post: 3}}, 131 criterion{Op: opLT, V: Version{Release: []int{3}}}, 132 }, 133 Match: []matchTestcase{ 134 {In: "2.2", Match: false}, 135 {In: "2.2.0.pre1", Match: false}, 136 {In: "2.2.0.1", Match: true}, 137 {In: "3.0", Match: false}, 138 }, 139 }, 140 { 141 Name: "CompatibleSpecific", 142 In: "~= 2.2.0", 143 Want: Range{ 144 criterion{Op: opGTE, V: Version{Release: []int{2, 2, 0}}}, 145 criterion{Op: opLT, V: Version{Release: []int{2, 3}}}, 146 }, 147 Match: []matchTestcase{ 148 {In: "2.2", Match: true}, 149 {In: "2.2.0.pre1", Match: false}, 150 {In: "2.2.0.1", Match: true}, 151 {In: "3.0", Match: false}, 152 }, 153 }, 154 { 155 Name: "CompatibleSpecificLong", 156 In: "~= 1.4.5.0", 157 Want: Range{ 158 criterion{Op: opGTE, V: Version{Release: []int{1, 4, 5, 0}}}, 159 criterion{Op: opLT, V: Version{Release: []int{1, 4, 6}}}, 160 }, 161 Match: []matchTestcase{ 162 {In: "1.4.4", Match: false}, 163 {In: "1.4.5.0.pre1", Match: false}, 164 {In: "1.4.5.0.1", Match: true}, 165 {In: "2.0", Match: false}, 166 {In: "1.4", Match: false}, 167 {In: "1.4.0.0.0.0", Match: false}, 168 }, 169 }, 170 { 171 Name: "Weird", 172 In: "~=1.1, !=1.4", 173 Want: Range{ 174 criterion{Op: opGTE, V: Version{Release: []int{1, 1}}}, 175 criterion{Op: opLT, V: Version{Release: []int{2}}}, 176 criterion{Op: opExclusion, V: Version{Release: []int{1, 4}}}, 177 }, 178 Match: []matchTestcase{ 179 {In: "1.1", Match: true}, 180 {In: "1.1.0.pre1", Match: false}, 181 {In: "1.1.0.1", Match: true}, 182 {In: "2.0", Match: false}, 183 {In: "1.4", Match: false}, 184 {In: "1.4.0.0.0.0", Match: false}, 185 }, 186 }, 187 } 188 189 func TestRange(t *testing.T) { 190 for _, tc := range rangeCases { 191 t.Run(tc.Name, tc.RunParse) 192 } 193 } 194 195 func TestMatch(t *testing.T) { 196 for _, tc := range rangeCases { 197 t.Run(tc.Name, tc.RunMatch) 198 } 199 }