github.com/google/osv-scalibr@v0.4.1/testing/extracttest/compare.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package extracttest 16 17 import ( 18 "cmp" 19 "fmt" 20 "strings" 21 22 "github.com/google/osv-scalibr/extractor" 23 ) 24 25 // PackageCmpLess is a comparator function for Packages, to be used in 26 // tests with cmp.Diff to disregard the order in which the Packages 27 // are reported. 28 func PackageCmpLess(a, b *extractor.Package) bool { 29 aLoc := fmt.Sprintf("%v", a.Locations) 30 bLoc := fmt.Sprintf("%v", b.Locations) 31 32 aSourceCode := fmt.Sprintf("%v", a.SourceCode) 33 bSourceCode := fmt.Sprintf("%v", b.SourceCode) 34 35 return cmp.Or( 36 cmp.Compare(aLoc, bLoc), 37 cmp.Compare(a.Name, b.Name), 38 cmp.Compare(a.Version, b.Version), 39 cmp.Compare(aSourceCode, bSourceCode), 40 cmp.Compare(strings.Join(a.Plugins, ","), strings.Join(b.Plugins, ",")), 41 ) < 0 42 }