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
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package reader
import (
"testing"
spdx "github.com/spdx/tools-golang/spdx/v2/v2_2"
)
func Test_setCreator(t *testing.T) {
// TestCase 1: invalid creator (empty)
input := ""
err := setCreator(input, &spdx.CreationInfo{})
if err == nil {
t.Errorf("shoud've raised an error due to invalid input")
}
// TestCase 2: invalid entity type
input = "Company: some company"
err = setCreator(input, &spdx.CreationInfo{})
if err == nil {
t.Errorf("shoud've raised an error due to unknown entity type")
}
// TestCase 3: valid input
input = "Person: Jane Doe"
ci := &spdx.CreationInfo{}
err = setCreator(input, ci)
if err != nil {
t.Errorf("error parsing a valid input: %v", err)
}
if len(ci.Creators) != 1 {
t.Errorf("creationInfo should've had 1 creatorPersons, found %d", len(ci.Creators))
}
expectedPerson := "Jane Doe"
if ci.Creators[0].Creator != expectedPerson {
t.Errorf("expected %s, found %s", expectedPerson, ci.Creators[0])
}
}
func Test_rdfParser2_2_parseCreationInfoFromNode(t *testing.T) {
// TestCase 1: invalid creator must raise an error
parser, _ := parserFromBodyContent(`
<spdx:CreationInfo>
<spdx:licenseListVersion>2.6</spdx:licenseListVersion>
<spdx:creator>Person Unknown</spdx:creator>
<spdx:created>2018-08-24T19:55:34Z</spdx:created>
</spdx:CreationInfo>
`)
ciNode := parser.gordfParserObj.Triples[0].Subject
err := parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode)
if err == nil {
t.Errorf("invalid creator must raise an error")
}
// TestCase 2: unknown predicate must also raise an error
parser, _ = parserFromBodyContent(`
<spdx:CreationInfo>
<spdx:licenseListVersion>2.6</spdx:licenseListVersion>
<spdx:creator>Person: fossy (y)</spdx:creator>
<spdx:creator>Organization: </spdx:creator>
<spdx:creator>Tool: spdx2</spdx:creator>
<spdx:created>2018-08-24T19:55:34Z</spdx:created>
<spdx:unknownPredicate />
</spdx:CreationInfo>
`)
ciNode = parser.gordfParserObj.Triples[0].Subject
err = parser.parseCreationInfoFromNode(&spdx.CreationInfo{}, ciNode)
if err == nil {
t.Errorf("unknown predicate must raise an error")
}
// TestCase 2: unknown predicate must also raise an error
parser, _ = parserFromBodyContent(`
<spdx:CreationInfo>
<spdx:licenseListVersion>2.6</spdx:licenseListVersion>
<spdx:creator>Person: fossy</spdx:creator>
<spdx:created>2018-08-24T19:55:34Z</spdx:created>
<rdfs:comment>comment</rdfs:comment>
</spdx:CreationInfo>
`)
ciNode = parser.gordfParserObj.Triples[0].Subject
ci := &spdx.CreationInfo{}
err = parser.parseCreationInfoFromNode(ci, ciNode)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if ci.LicenseListVersion != "2.6" {
t.Errorf(`expected %s, found %s`, "2.6", ci.LicenseListVersion)
}
n := len(ci.Creators)
if n != 1 {
t.Errorf("expected 1 creatorPersons, found %d", n)
}
if ci.Creators[0].Creator != "fossy" {
t.Errorf("expected %s, found %s", "fossy", ci.Creators[0].Creator)
}
expectedCreated := "2018-08-24T19:55:34Z"
if ci.Created != expectedCreated {
t.Errorf("expected %s, found %s", expectedCreated, ci.Created)
}
expectedComment := "comment"
if ci.CreatorComment != expectedComment {
t.Errorf("expected %s, found %s", expectedComment, ci.CreatorComment)
}
}
|