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