update yaml tests to ignore order of relationships
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
diff --git a/spdx/v2/v2_2/yaml/yaml_test.go b/spdx/v2/v2_2/yaml/yaml_test.go
index bacfc80..404a261 100644
--- a/spdx/v2/v2_2/yaml/yaml_test.go
+++ b/spdx/v2/v2_2/yaml/yaml_test.go
@@ -4,6 +4,7 @@
import (
"bytes"
+ jsonenc "encoding/json"
"fmt"
"os"
"testing"
@@ -11,7 +12,6 @@
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
- "github.com/spdx/tools-golang/spdx/v2/common"
spdx "github.com/spdx/tools-golang/spdx/v2/v2_2"
"github.com/spdx/tools-golang/spdx/v2/v2_2/example"
"github.com/spdx/tools-golang/yaml"
@@ -20,34 +20,6 @@
func Test_Read(t *testing.T) {
want := example.Copy()
- want.Relationships = append(want.Relationships, []*spdx.Relationship{
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "File"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "DOCUMENT"},
- RefB: common.DocElementID{ElementRefID: "Package"},
- Relationship: "DESCRIBES",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "CommonsLangSrc"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "JenaLib"},
- Relationship: "CONTAINS",
- },
- {
- RefA: common.DocElementID{ElementRefID: "Package"},
- RefB: common.DocElementID{ElementRefID: "DoapSource"},
- Relationship: "CONTAINS",
- },
- }...)
-
file, err := os.Open("../../../../examples/sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml")
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
@@ -60,8 +32,8 @@
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
@@ -88,8 +60,14 @@
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", diff)
return
}
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}
diff --git a/spdx/v2/v2_3/yaml/yaml_test.go b/spdx/v2/v2_3/yaml/yaml_test.go
index 766a7c3..c0f787d 100644
--- a/spdx/v2/v2_3/yaml/yaml_test.go
+++ b/spdx/v2/v2_3/yaml/yaml_test.go
@@ -4,6 +4,7 @@
import (
"bytes"
+ jsonenc "encoding/json"
"flag"
"fmt"
"os"
@@ -47,8 +48,8 @@
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
@@ -76,8 +77,14 @@
return
}
- if !cmp.Equal(want, got, cmpopts.IgnoreUnexported(spdx.Package{})) {
- t.Errorf("got incorrect struct after writing and re-parsing YAML example: %s", cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{})))
+ if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(spdx.Package{}), cmpopts.SortSlices(relationshipLess)); len(diff) > 0 {
+ t.Errorf("got incorrect struct after parsing YAML example: %s", diff)
return
}
}
+
+func relationshipLess(a, b *spdx.Relationship) bool {
+ aStr, _ := jsonenc.Marshal(a)
+ bStr, _ := jsonenc.Marshal(b)
+ return string(aStr) < string(bStr)
+}