File: uri_test.go

package info (click to toggle)
golang-github-spdx-gordf 0.0~git20221230.b735bd5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: makefile: 4
file content (75 lines) | stat: -rw-r--r-- 2,006 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
package uri

import (
	"testing"
)

func TestNewURIRef(t *testing.T) {
	// TestCase 1: input is an empty string
	_, err := NewURIRef("")
	if err == nil {
		t.Errorf("blank uri should've raised an error")
	}

	// case when the input is not empty and is an invalid uri.
	_, err = NewURIRef("%%")
	if err == nil {
		t.Errorf("expected function to raise an error for invalid input uri")
	}

	// case when the uri is valid and ends with a #
	uri := "https://www.spdx.org/rdf/terms#"
	uriref, err := NewURIRef(uri)
	if err != nil {
		t.Errorf(err.Error())
	}
	if uriref.String() != uri {
		t.Errorf("expected %v, found %v", uri, uriref)
	}

	// case when the uri is valid and doesn't end with a hash
	uri = "https://www.spdx.org/rdf/terms"
	uriref, err = NewURIRef(uri)
	if err != nil {
		t.Errorf(err.Error())
	}
	if exp := uri + "#"; uriref.String() != exp {
		t.Errorf("expected %v, found %v", exp, uriref)
	}
}

func TestURIRef_AddFragment(t *testing.T) {
	uriString := "https://www.someuri.com/valid/uri"
	uriref, _ := NewURIRef(uriString)

	// TestCase 1: Invalid Fragment must return an empty uri
	fragment := "%%%%"
	newUri := uriref.AddFragment(fragment)
	if newUri.String() != "" {
		t.Errorf("invalid fragment must result in an empty uri. Found %v", newUri.String())
	}

	// TestCase 2: valid fragment
	fragment = "someFrag"
	newUri = uriref.AddFragment(fragment)
	expectedURI := uriString + "#" + fragment
	if newUri.String() != expectedURI {
		t.Errorf("expected: %v, found: %v", expectedURI, newUri)
	}

	// TestCase 3: valid fragmnet starting with a hash char.
	fragment = "#" + fragment
	newUri = uriref.AddFragment(fragment)
	if newUri.String() != expectedURI {
		t.Errorf("expected %v, found %v", expectedURI, newUri)
	}
}

func TestURIRef_String(t *testing.T) {
	// again, nothing much to test
	uriString := "https://www.someuri.com/valid/uri"
	uriref, _ := NewURIRef(uriString)
	if uriref.String() != uriString+"#" {
		t.Errorf("expected: %v, found: %v", uriString+"#", uriref.String())
	}
}