File: writer_test.go

package info (click to toggle)
golang-github-spdx-tools-golang 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,252 kB
  • sloc: xml: 428; makefile: 22; ansic: 5; python: 2
file content (84 lines) | stat: -rw-r--r-- 2,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package json_test

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/spdx/tools-golang/json"
	"github.com/spdx/tools-golang/spdx/common"
	spdx "github.com/spdx/tools-golang/spdx/v2/v2_3"
)

func Test_Write(t *testing.T) {
	tests := []struct {
		name   string
		doc    common.AnyDocument
		option []json.WriteOption
		want   string
	}{
		{
			name: "happy path",
			doc: spdx.Document{
				SPDXVersion:  "2.3",
				DocumentName: "test_doc",
			},
			want: `{"spdxVersion":"2.3","dataLicense":"","SPDXID":"SPDXRef-","name":"test_doc","documentNamespace":"","creationInfo":null}
`,
		},
		{
			name: "happy path with Indent option",
			doc: spdx.Document{
				SPDXVersion:  "2.3",
				DocumentName: "test_doc",
			},
			option: []json.WriteOption{json.Indent(" ")},
			want: `{
 "spdxVersion": "2.3",
 "dataLicense": "",
 "SPDXID": "SPDXRef-",
 "name": "test_doc",
 "documentNamespace": "",
 "creationInfo": null
}
`,
		},
		{
			name: "happy path with EscapeHTML==true option",
			doc: spdx.Document{
				SPDXVersion:  "2.3",
				DocumentName: "test_doc_>",
			},
			option: []json.WriteOption{json.EscapeHTML(true)},
			want:   "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_\\u003e\",\"documentNamespace\":\"\",\"creationInfo\":null}\n",
		},
		{
			name: "happy path with EscapeHTML==false option",
			doc: spdx.Document{
				SPDXVersion:  "2.3",
				DocumentName: "test_doc_>",
			},
			option: []json.WriteOption{json.EscapeHTML(false)},
			want:   "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_>\",\"documentNamespace\":\"\",\"creationInfo\":null}\n",
		},
		{
			name: "happy path with EscapeHTML==false option",
			doc: spdx.Document{
				SPDXVersion:  "2.3",
				DocumentName: "test_doc_>",
			},
			option: []json.WriteOption{json.EscapeHTML(false)},
			want:   "{\"spdxVersion\":\"2.3\",\"dataLicense\":\"\",\"SPDXID\":\"SPDXRef-\",\"name\":\"test_doc_>\",\"documentNamespace\":\"\",\"creationInfo\":null}\n",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			buf := new(bytes.Buffer)
			err := json.Write(tt.doc, buf, tt.option...)
			assert.NoError(t, err)
			assert.Equal(t, tt.want, buf.String())
		})
	}
}