xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v2/save_other_license_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v2
4
5import (
6	"bytes"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/v2_2"
10)
11
12// ===== Other License section Saver tests =====
13func TestSaver2_2OtherLicenseSavesText(t *testing.T) {
14	ol := &v2_2.OtherLicense{
15		LicenseIdentifier: "LicenseRef-1",
16		ExtractedText: `License 1 text
17blah blah blah
18blah blah blah blah`,
19		LicenseName: "License 1",
20		LicenseCrossReferences: []string{
21			"http://example.com/License1/",
22			"http://example.com/License1AnotherURL/",
23		},
24		LicenseComment: "this is a license comment",
25	}
26
27	// what we want to get, as a buffer of bytes
28	want := bytes.NewBufferString(`LicenseID: LicenseRef-1
29ExtractedText: <text>License 1 text
30blah blah blah
31blah blah blah blah</text>
32LicenseName: License 1
33LicenseCrossReference: http://example.com/License1/
34LicenseCrossReference: http://example.com/License1AnotherURL/
35LicenseComment: this is a license comment
36
37`)
38
39	// render as buffer of bytes
40	var got bytes.Buffer
41	err := renderOtherLicense2_2(ol, &got)
42	if err != nil {
43		t.Errorf("Expected nil error, got %v", err)
44	}
45
46	// check that they match
47	c := bytes.Compare(want.Bytes(), got.Bytes())
48	if c != 0 {
49		t.Errorf("Expected %v, got %v", want.String(), got.String())
50	}
51}
52
53func TestSaver2_2OtherLicenseOmitsOptionalFieldsIfEmpty(t *testing.T) {
54	ol := &v2_2.OtherLicense{
55		LicenseIdentifier: "LicenseRef-1",
56		ExtractedText: `License 1 text
57blah blah blah
58blah blah blah blah`,
59		LicenseName: "License 1",
60	}
61
62	// what we want to get, as a buffer of bytes
63	want := bytes.NewBufferString(`LicenseID: LicenseRef-1
64ExtractedText: <text>License 1 text
65blah blah blah
66blah blah blah blah</text>
67LicenseName: License 1
68
69`)
70
71	// render as buffer of bytes
72	var got bytes.Buffer
73	err := renderOtherLicense2_2(ol, &got)
74	if err != nil {
75		t.Errorf("Expected nil error, got %v", err)
76	}
77
78	// check that they match
79	c := bytes.Compare(want.Bytes(), got.Bytes())
80	if c != 0 {
81		t.Errorf("Expected %v, got %v", want.String(), got.String())
82	}
83}
84