aboutsummaryrefslogtreecommitdiff
path: root/tvloader/parser2v1/parse_relationship_test.go
blob: 17964fce1b972249ed7834667a568efa9e8956cf (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package parser2v1

import (
	"testing"

	"github.com/spdx/tools-golang/spdx"
)

// ===== Relationship section tests =====
func TestParser2_1FailsIfRelationshipNotSet(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}
	err := parser.parsePairForRelationship2_1("Relationship", "something DESCRIBES something-else")
	if err == nil {
		t.Errorf("expected error when calling parsePairFromRelationship2_1 without setting rln pointer")
	}
}

func TestParser2_1FailsIfRelationshipCommentWithoutRelationship(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}
	err := parser.parsePair2_1("RelationshipComment", "comment whatever")
	if err == nil {
		t.Errorf("expected error when calling parsePair2_1 for RelationshipComment without Relationship first")
	}
}

func TestParser2_1CanParseRelationshipTags(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// Relationship
	err := parser.parsePair2_1("Relationship", "something DESCRIBES something-else")
	if err != nil {
		t.Errorf("expected nil error, got %v", err)
	}
	if parser.rln.RefA != "something" {
		t.Errorf("got %v for first part of Relationship, expected something", parser.rln.RefA)
	}
	if parser.rln.RefB != "something-else" {
		t.Errorf("got %v for second part of Relationship, expected something-else", parser.rln.RefB)
	}
	if parser.rln.Relationship != "DESCRIBES" {
		t.Errorf("got %v for Relationship type, expected DESCRIBES", parser.rln.Relationship)
	}

	// Relationship Comment
	cmt := "this is a comment"
	err = parser.parsePair2_1("RelationshipComment", cmt)
	if err != nil {
		t.Errorf("expected nil error, got %v", err)
	}
	if parser.rln.RelationshipComment != cmt {
		t.Errorf("got %v for RelationshipComment, expected %v", parser.rln.RelationshipComment, cmt)
	}
}

func TestParser2_1InvalidRelationshipTagsNoValueFail(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// no items
	parser.rln = nil
	err := parser.parsePair2_1("Relationship", "")
	if err == nil {
		t.Errorf("expected error for empty items in relationship, got nil")
	}
}

func TestParser2_1InvalidRelationshipTagsOneValueFail(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// one item
	parser.rln = nil
	err := parser.parsePair2_1("Relationship", "DESCRIBES")
	if err == nil {
		t.Errorf("expected error for only one item in relationship, got nil")
	}
}

func TestParser2_1InvalidRelationshipTagsTwoValuesFail(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// two items
	parser.rln = nil
	err := parser.parsePair2_1("Relationship", "SPDXRef-DOCUMENT DESCRIBES")
	if err == nil {
		t.Errorf("expected error for only two items in relationship, got nil")
	}
}

func TestParser2_1InvalidRelationshipTagsThreeValuesSucceed(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// three items but with interspersed additional whitespace
	parser.rln = nil
	err := parser.parsePair2_1("Relationship", "  SPDXRef-DOCUMENT \t   DESCRIBES  something-else    ")
	if err != nil {
		t.Errorf("expected pass for three items in relationship w/ extra whitespace, got: %v", err)
	}
}

func TestParser2_1InvalidRelationshipTagsFourValuesFail(t *testing.T) {
	parser := tvParser2_1{
		doc: &spdx.Document2_1{},
		st:  psCreationInfo2_1,
	}

	// four items
	parser.rln = nil
	err := parser.parsePair2_1("Relationship", "a DESCRIBES b c")
	if err == nil {
		t.Errorf("expected error for more than three items in relationship, got nil")
	}
}