1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2package parser2v3 3 4import ( 5 "testing" 6 7 "github.com/spdx/tools-golang/spdx/v2_3" 8) 9 10// ===== Annotation section tests ===== 11func TestParser2_3FailsIfAnnotationNotSet(t *testing.T) { 12 parser := tvParser2_3{ 13 doc: &v2_3.Document{}, 14 st: psCreationInfo2_3, 15 } 16 err := parser.parsePairForAnnotation2_3("Annotator", "Person: John Doe ([email protected])") 17 if err == nil { 18 t.Errorf("expected error when calling parsePairFromAnnotation2_3 without setting ann pointer") 19 } 20} 21 22func TestParser2_3FailsIfAnnotationTagUnknown(t *testing.T) { 23 parser := tvParser2_3{ 24 doc: &v2_3.Document{}, 25 st: psCreationInfo2_3, 26 } 27 // start with valid annotator 28 err := parser.parsePair2_3("Annotator", "Person: John Doe ([email protected])") 29 if err != nil { 30 t.Errorf("expected nil error, got %v", err) 31 } 32 // parse invalid tag, using parsePairForAnnotation2_3( 33 err = parser.parsePairForAnnotation2_3("blah", "oops") 34 if err == nil { 35 t.Errorf("expected non-nil error, got nil") 36 } 37} 38 39func TestParser2_3FailsIfAnnotationFieldsWithoutAnnotation(t *testing.T) { 40 parser := tvParser2_3{ 41 doc: &v2_3.Document{}, 42 st: psCreationInfo2_3, 43 } 44 err := parser.parsePair2_3("AnnotationDate", "2018-09-15T17:25:00Z") 45 if err == nil { 46 t.Errorf("expected error when calling parsePair2_3 for AnnotationDate without Annotator first") 47 } 48 err = parser.parsePair2_3("AnnotationType", "REVIEW") 49 if err == nil { 50 t.Errorf("expected error when calling parsePair2_3 for AnnotationType without Annotator first") 51 } 52 err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") 53 if err == nil { 54 t.Errorf("expected error when calling parsePair2_3 for SPDXREF without Annotator first") 55 } 56 err = parser.parsePair2_3("AnnotationComment", "comment whatever") 57 if err == nil { 58 t.Errorf("expected error when calling parsePair2_3 for AnnotationComment without Annotator first") 59 } 60} 61 62func TestParser2_3CanParseAnnotationTags(t *testing.T) { 63 parser := tvParser2_3{ 64 doc: &v2_3.Document{}, 65 st: psCreationInfo2_3, 66 } 67 68 // Annotator without email address 69 err := parser.parsePair2_3("Annotator", "Person: John Doe") 70 if err != nil { 71 t.Errorf("expected nil error, got %v", err) 72 } 73 if parser.ann.Annotator.Annotator != "John Doe" { 74 t.Errorf("got %+v for Annotator, expected John Doe", parser.ann.Annotator.Annotator) 75 } 76 if parser.ann.Annotator.AnnotatorType != "Person" { 77 t.Errorf("got %v for AnnotatorType, expected Person", parser.ann.Annotator.AnnotatorType) 78 } 79 80 // Annotation Date 81 dt := "2018-09-15T17:32:00Z" 82 err = parser.parsePair2_3("AnnotationDate", dt) 83 if err != nil { 84 t.Errorf("expected nil error, got %v", err) 85 } 86 if parser.ann.AnnotationDate != dt { 87 t.Errorf("got %v for AnnotationDate, expected %v", parser.ann.AnnotationDate, dt) 88 } 89 90 // Annotation type 91 aType := "REVIEW" 92 err = parser.parsePair2_3("AnnotationType", aType) 93 if err != nil { 94 t.Errorf("expected nil error, got %v", err) 95 } 96 if parser.ann.AnnotationType != aType { 97 t.Errorf("got %v for AnnotationType, expected %v", parser.ann.AnnotationType, aType) 98 } 99 100 // SPDX Identifier Reference 101 ref := "SPDXRef-30" 102 err = parser.parsePair2_3("SPDXREF", ref) 103 if err != nil { 104 t.Errorf("expected nil error, got %v", err) 105 } 106 deID := parser.ann.AnnotationSPDXIdentifier 107 if deID.DocumentRefID != "" || deID.ElementRefID != "30" { 108 t.Errorf("got %v for SPDXREF, expected %v", parser.ann.AnnotationSPDXIdentifier, "30") 109 } 110 111 // Annotation Comment 112 cmt := "this is a comment" 113 err = parser.parsePair2_3("AnnotationComment", cmt) 114 if err != nil { 115 t.Errorf("expected nil error, got %v", err) 116 } 117 if parser.ann.AnnotationComment != cmt { 118 t.Errorf("got %v for AnnotationComment, expected %v", parser.ann.AnnotationComment, cmt) 119 } 120} 121 122func TestParser2_3FailsIfAnnotatorInvalid(t *testing.T) { 123 parser := tvParser2_3{ 124 doc: &v2_3.Document{}, 125 st: psCreationInfo2_3, 126 } 127 err := parser.parsePair2_3("Annotator", "John Doe ([email protected])") 128 if err == nil { 129 t.Errorf("expected non-nil error, got nil") 130 } 131} 132 133func TestParser2_3FailsIfAnnotatorTypeInvalid(t *testing.T) { 134 parser := tvParser2_3{ 135 doc: &v2_3.Document{}, 136 st: psCreationInfo2_3, 137 } 138 err := parser.parsePair2_3("Annotator", "Human: John Doe ([email protected])") 139 if err == nil { 140 t.Errorf("expected non-nil error, got nil") 141 } 142} 143 144func TestParser2_3FailsIfAnnotationRefInvalid(t *testing.T) { 145 parser := tvParser2_3{ 146 doc: &v2_3.Document{}, 147 st: psCreationInfo2_3, 148 } 149 // start with valid annotator 150 err := parser.parsePair2_3("Annotator", "Person: John Doe ([email protected])") 151 if err != nil { 152 t.Errorf("expected nil error, got %v", err) 153 } 154 err = parser.parsePair2_3("SPDXREF", "blah:other") 155 if err == nil { 156 t.Errorf("expected non-nil error, got nil") 157 } 158} 159