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// ===== Creation Info section Saver tests ===== 14func TestSaver2_3CISavesText(t *testing.T) { 15 ci := &v2_3.CreationInfo{ 16 LicenseListVersion: "3.9", 17 Creators: []common.Creator{ 18 {Creator: "John Doe", CreatorType: "Person"}, 19 {Creator: "Jane Doe ([email protected])", CreatorType: "Person"}, 20 {Creator: "John Doe, Inc.", CreatorType: "Organization"}, 21 {Creator: "Jane Doe LLC", CreatorType: "Organization"}, 22 {Creator: "magictool1-1.0", CreatorType: "Tool"}, 23 {Creator: "magictool2-1.0", CreatorType: "Tool"}, 24 {Creator: "magictool3-1.0", CreatorType: "Tool"}, 25 }, 26 Created: "2018-10-10T06:20:00Z", 27 CreatorComment: "this is a creator comment", 28 } 29 30 // what we want to get, as a buffer of bytes 31 want := bytes.NewBufferString(`LicenseListVersion: 3.9 32Creator: Person: John Doe 33Creator: Person: Jane Doe (janedoe@example.com) 34Creator: Organization: John Doe, Inc. 35Creator: Organization: Jane Doe LLC 36Creator: Tool: magictool1-1.0 37Creator: Tool: magictool2-1.0 38Creator: Tool: magictool3-1.0 39Created: 2018-10-10T06:20:00Z 40CreatorComment: this is a creator comment 41 42`) 43 44 // render as buffer of bytes 45 var got bytes.Buffer 46 err := renderCreationInfo2_3(ci, &got) 47 if err != nil { 48 t.Errorf("Expected nil error, got %v", err) 49 } 50 51 // check that they match 52 c := bytes.Compare(want.Bytes(), got.Bytes()) 53 if c != 0 { 54 t.Errorf("Expected %v, got %v", want.String(), got.String()) 55 } 56} 57 58func TestSaver2_3CIOmitsOptionalFieldsIfEmpty(t *testing.T) { 59 // --- need at least one creator; do first for Persons --- 60 ci1 := &v2_3.CreationInfo{ 61 Creators: []common.Creator{ 62 {Creator: "John Doe", CreatorType: "Person"}, 63 }, 64 Created: "2018-10-10T06:20:00Z", 65 } 66 67 // what we want to get, as a buffer of bytes 68 want1 := bytes.NewBufferString(`Creator: Person: John Doe 69Created: 2018-10-10T06:20:00Z 70 71`) 72 73 // render as buffer of bytes 74 var got1 bytes.Buffer 75 err := renderCreationInfo2_3(ci1, &got1) 76 if err != nil { 77 t.Errorf("Expected nil error, got %v", err) 78 } 79 80 // check that they match 81 c1 := bytes.Compare(want1.Bytes(), got1.Bytes()) 82 if c1 != 0 { 83 t.Errorf("Expected %v, got %v", want1.String(), got1.String()) 84 } 85 86 // --- need at least one creator; now switch to organization --- 87 ci2 := &v2_3.CreationInfo{ 88 Creators: []common.Creator{ 89 {Creator: "John Doe, Inc.", CreatorType: "Organization"}, 90 }, 91 Created: "2018-10-10T06:20:00Z", 92 } 93 94 // what we want to get, as a buffer of bytes 95 want2 := bytes.NewBufferString(`Creator: Organization: John Doe, Inc. 96Created: 2018-10-10T06:20:00Z 97 98`) 99 100 // render as buffer of bytes 101 var got2 bytes.Buffer 102 err = renderCreationInfo2_3(ci2, &got2) 103 if err != nil { 104 t.Errorf("Expected nil error, got %v", err) 105 } 106 107 // check that they match 108 c2 := bytes.Compare(want2.Bytes(), got2.Bytes()) 109 if c2 != 0 { 110 t.Errorf("Expected %v, got %v", want2.String(), got2.String()) 111 } 112} 113