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// ===== Snippet section Saver tests ===== 14func TestSaver2_3SnippetSavesText(t *testing.T) { 15 sn := &v2_3.Snippet{ 16 SnippetSPDXIdentifier: common.ElementID("Snippet17"), 17 SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, 18 Ranges: []common.SnippetRange{ 19 { 20 StartPointer: common.SnippetRangePointer{LineNumber: 3}, 21 EndPointer: common.SnippetRangePointer{LineNumber: 8}, 22 }, 23 { 24 StartPointer: common.SnippetRangePointer{Offset: 17}, 25 EndPointer: common.SnippetRangePointer{Offset: 209}, 26 }, 27 }, 28 SnippetLicenseConcluded: "GPL-2.0-or-later", 29 LicenseInfoInSnippet: []string{ 30 "GPL-2.0-or-later", 31 "MIT", 32 }, 33 SnippetLicenseComments: "this is a comment(s) about the snippet license", 34 SnippetCopyrightText: "Copyright (c) John Doe 20x6", 35 SnippetComment: "this is a snippet comment", 36 SnippetName: "from John's program", 37 SnippetAttributionTexts: []string{"some attributions"}, 38 } 39 40 // what we want to get, as a buffer of bytes 41 want := bytes.NewBufferString(`SnippetSPDXID: SPDXRef-Snippet17 42SnippetFromFileSPDXID: SPDXRef-File292 43SnippetLineRange: 3:8 44SnippetByteRange: 17:209 45SnippetLicenseConcluded: GPL-2.0-or-later 46LicenseInfoInSnippet: GPL-2.0-or-later 47LicenseInfoInSnippet: MIT 48SnippetLicenseComments: this is a comment(s) about the snippet license 49SnippetCopyrightText: Copyright (c) John Doe 20x6 50SnippetComment: this is a snippet comment 51SnippetName: from John's program 52SnippetAttributionText: some attributions 53 54`) 55 56 // render as buffer of bytes 57 var got bytes.Buffer 58 err := renderSnippet2_3(sn, &got) 59 if err != nil { 60 t.Errorf("Expected nil error, got %v", err) 61 } 62 63 // check that they match 64 c := bytes.Compare(want.Bytes(), got.Bytes()) 65 if c != 0 { 66 t.Errorf("Expected %v, got %v", want.String(), got.String()) 67 } 68} 69 70func TestSaver2_3SnippetOmitsOptionalFieldsIfEmpty(t *testing.T) { 71 sn := &v2_3.Snippet{ 72 SnippetSPDXIdentifier: common.ElementID("Snippet17"), 73 SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, 74 Ranges: []common.SnippetRange{ 75 { 76 StartPointer: common.SnippetRangePointer{Offset: 17}, 77 EndPointer: common.SnippetRangePointer{Offset: 209}, 78 }, 79 }, 80 SnippetLicenseConcluded: "GPL-2.0-or-later", 81 SnippetCopyrightText: "Copyright (c) John Doe 20x6", 82 } 83 84 // what we want to get, as a buffer of bytes 85 want := bytes.NewBufferString(`SnippetSPDXID: SPDXRef-Snippet17 86SnippetFromFileSPDXID: SPDXRef-File292 87SnippetByteRange: 17:209 88SnippetLicenseConcluded: GPL-2.0-or-later 89SnippetCopyrightText: Copyright (c) John Doe 20x6 90 91`) 92 93 // render as buffer of bytes 94 var got bytes.Buffer 95 err := renderSnippet2_3(sn, &got) 96 if err != nil { 97 t.Errorf("Expected nil error, got %v", err) 98 } 99 100 // check that they match 101 c := bytes.Compare(want.Bytes(), got.Bytes()) 102 if c != 0 { 103 t.Errorf("Expected %v, got %v", want.String(), got.String()) 104 } 105} 106 107func TestSaver2_3SnippetWrapsCopyrightMultiline(t *testing.T) { 108 sn := &v2_3.Snippet{ 109 SnippetSPDXIdentifier: common.ElementID("Snippet17"), 110 SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File292").ElementRefID, 111 Ranges: []common.SnippetRange{ 112 { 113 StartPointer: common.SnippetRangePointer{Offset: 17}, 114 EndPointer: common.SnippetRangePointer{Offset: 209}, 115 }, 116 }, 117 SnippetLicenseConcluded: "GPL-2.0-or-later", 118 SnippetCopyrightText: `Copyright (c) John Doe 20x6 119Copyright (c) John Doe 20x6`, 120 } 121 122 // what we want to get, as a buffer of bytes 123 want := bytes.NewBufferString(`SnippetSPDXID: SPDXRef-Snippet17 124SnippetFromFileSPDXID: SPDXRef-File292 125SnippetByteRange: 17:209 126SnippetLicenseConcluded: GPL-2.0-or-later 127SnippetCopyrightText: <text>Copyright (c) John Doe 20x6 128Copyright (c) John Doe 20x6</text> 129 130`) 131 132 // render as buffer of bytes 133 var got bytes.Buffer 134 err := renderSnippet2_3(sn, &got) 135 if err != nil { 136 t.Errorf("Expected nil error, got %v", err) 137 } 138 139 // check that they match 140 c := bytes.Compare(want.Bytes(), got.Bytes()) 141 if c != 0 { 142 t.Errorf("Expected %v, got %v", want.String(), got.String()) 143 } 144} 145