xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v1/save_relationship_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v1
4
5import (
6	"bytes"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_1"
11)
12
13// ===== Relationship section Saver tests =====
14func TestSaver2_1RelationshipSavesText(t *testing.T) {
15	rln := &v2_1.Relationship{
16		RefA:                common.MakeDocElementID("", "DOCUMENT"),
17		RefB:                common.MakeDocElementID("", "2"),
18		Relationship:        "DESCRIBES",
19		RelationshipComment: "this is a comment",
20	}
21
22	// what we want to get, as a buffer of bytes
23	// no trailing blank newline
24	want := bytes.NewBufferString(`Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-2
25RelationshipComment: this is a comment
26`)
27
28	// render as buffer of bytes
29	var got bytes.Buffer
30	err := renderRelationship2_1(rln, &got)
31	if err != nil {
32		t.Errorf("Expected nil error, got %v", err)
33	}
34
35	// check that they match
36	c := bytes.Compare(want.Bytes(), got.Bytes())
37	if c != 0 {
38		t.Errorf("Expected %v, got %v", want.String(), got.String())
39	}
40}
41
42func TestSaver2_1RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) {
43	rln := &v2_1.Relationship{
44		RefA:         common.MakeDocElementID("", "DOCUMENT"),
45		RefB:         common.MakeDocElementID("", "2"),
46		Relationship: "DESCRIBES",
47	}
48
49	// what we want to get, as a buffer of bytes
50	// no trailing blank newline
51	want := bytes.NewBufferString("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-2\n")
52
53	// render as buffer of bytes
54	var got bytes.Buffer
55	err := renderRelationship2_1(rln, &got)
56	if err != nil {
57		t.Errorf("Expected nil error, got %v", err)
58	}
59
60	// check that they match
61	c := bytes.Compare(want.Bytes(), got.Bytes())
62	if c != 0 {
63		t.Errorf("Expected %v, got %v", want.String(), got.String())
64	}
65}
66
67func TestSaver2_1RelationshipWrapsCommentMultiLine(t *testing.T) {
68	rln := &v2_1.Relationship{
69		RefA:         common.MakeDocElementID("", "DOCUMENT"),
70		RefB:         common.MakeDocElementID("", "2"),
71		Relationship: "DESCRIBES",
72		RelationshipComment: `this is a
73multi-line comment`,
74	}
75
76	// what we want to get, as a buffer of bytes
77	// no trailing blank newline
78	want := bytes.NewBufferString(`Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-2
79RelationshipComment: <text>this is a
80multi-line comment</text>
81`)
82
83	// render as buffer of bytes
84	var got bytes.Buffer
85	err := renderRelationship2_1(rln, &got)
86	if err != nil {
87		t.Errorf("Expected nil error, got %v", err)
88	}
89
90	// check that they match
91	c := bytes.Compare(want.Bytes(), got.Bytes())
92	if c != 0 {
93		t.Errorf("Expected %v, got %v", want.String(), got.String())
94	}
95}
96