1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package saver2v3 4 5import ( 6 "bytes" 7 "testing" 8 9 "github.com/spdx/tools-golang/spdx/common" 10 "github.com/spdx/tools-golang/spdx/v2_3" 11) 12 13// ===== Relationship section Saver tests ===== 14func TestSaver2_3RelationshipSavesText(t *testing.T) { 15 rln := &v2_3.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_3(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_3RelationshipOmitsOptionalFieldsIfEmpty(t *testing.T) { 43 rln := &v2_3.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_3(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_3RelationshipCanHaveNONEOnRight(t *testing.T) { 68 rln := &v2_3.Relationship{ 69 RefA: common.MakeDocElementID("", "PackageA"), 70 RefB: common.MakeDocElementSpecial("NONE"), 71 Relationship: "DEPENDS_ON", 72 } 73 74 // what we want to get, as a buffer of bytes 75 // no trailing blank newline 76 want := bytes.NewBufferString("Relationship: SPDXRef-PackageA DEPENDS_ON NONE\n") 77 78 // render as buffer of bytes 79 var got bytes.Buffer 80 err := renderRelationship2_3(rln, &got) 81 if err != nil { 82 t.Errorf("Expected nil error, got %v", err) 83 } 84 85 // check that they match 86 c := bytes.Compare(want.Bytes(), got.Bytes()) 87 if c != 0 { 88 t.Errorf("Expected %v, got %v", want.String(), got.String()) 89 } 90} 91 92func TestSaver2_3RelationshipCanHaveNOASSERTIONOnRight(t *testing.T) { 93 rln := &v2_3.Relationship{ 94 RefA: common.MakeDocElementID("", "PackageA"), 95 RefB: common.MakeDocElementSpecial("NOASSERTION"), 96 Relationship: "DEPENDS_ON", 97 } 98 99 // what we want to get, as a buffer of bytes 100 // no trailing blank newline 101 want := bytes.NewBufferString("Relationship: SPDXRef-PackageA DEPENDS_ON NOASSERTION\n") 102 103 // render as buffer of bytes 104 var got bytes.Buffer 105 err := renderRelationship2_3(rln, &got) 106 if err != nil { 107 t.Errorf("Expected nil error, got %v", err) 108 } 109 110 // check that they match 111 c := bytes.Compare(want.Bytes(), got.Bytes()) 112 if c != 0 { 113 t.Errorf("Expected %v, got %v", want.String(), got.String()) 114 } 115} 116 117func TestSaver2_3RelationshipWrapsCommentMultiLine(t *testing.T) { 118 rln := &v2_3.Relationship{ 119 RefA: common.MakeDocElementID("", "DOCUMENT"), 120 RefB: common.MakeDocElementID("", "2"), 121 Relationship: "DESCRIBES", 122 RelationshipComment: `this is a 123multi-line comment`, 124 } 125 126 // what we want to get, as a buffer of bytes 127 // no trailing blank newline 128 want := bytes.NewBufferString(`Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-2 129RelationshipComment: <text>this is a 130multi-line comment</text> 131`) 132 133 // render as buffer of bytes 134 var got bytes.Buffer 135 err := renderRelationship2_3(rln, &got) 136 if err != nil { 137 t.Errorf("Expected nil error, got %v", err) 138 } 139 140 // check that they match 141 c := bytes.Compare(want.Bytes(), got.Bytes()) 142 if c != 0 { 143 t.Errorf("Expected %v, got %v", want.String(), got.String()) 144 } 145} 146