Debian Codegraph @

uri/uri_test.go

raw symbols SCIP
1 package uri
2
3 import (
4 "testing"
5 )
6
7 func TestNewURIRef(t *testing.T) {
8 // TestCase 1: input is an empty string
9 _, err := NewURIRef("")
10 if err == nil {
11 t.Errorf("blank uri should've raised an error")
12 }
13
14 // case when the input is not empty and is an invalid uri.
15 _, err = NewURIRef("%%")
16 if err == nil {
17 t.Errorf("expected function to raise an error for invalid input uri")
18 }
19
20 // case when the uri is valid and ends with a #
21 uri := "https://www.spdx.org/rdf/terms#"
22 uriref, err := NewURIRef(uri)
23 if err != nil {
24 t.Errorf(err.Error())
25 }
26 if uriref.String() != uri {
27 t.Errorf("expected %v, found %v", uri, uriref)
28 }
29
30 // case when the uri is valid and doesn't end with a hash
31 uri = "https://www.spdx.org/rdf/terms"
32 uriref, err = NewURIRef(uri)
33 if err != nil {
34 t.Errorf(err.Error())
35 }
36 if exp := uri + "#"; uriref.String() != exp {
37 t.Errorf("expected %v, found %v", exp, uriref)
38 }
39 }
40
41 func TestURIRef_AddFragment(t *testing.T) {
42 uriString := "https://www.someuri.com/valid/uri"
43 uriref, _ := NewURIRef(uriString)
44
45 // TestCase 1: Invalid Fragment must return an empty uri
46 fragment := "%%%%"
47 newUri := uriref.AddFragment(fragment)
48 if newUri.String() != "" {
49 t.Errorf("invalid fragment must result in an empty uri. Found %v", newUri.String())
50 }
51
52 // TestCase 2: valid fragment
53 fragment = "someFrag"
54 newUri = uriref.AddFragment(fragment)
55 expectedURI := uriString + "#" + fragment
56 if newUri.String() != expectedURI {
57 t.Errorf("expected: %v, found: %v", expectedURI, newUri)
58 }
59
60 // TestCase 3: valid fragmnet starting with a hash char.
61 fragment = "#" + fragment
62 newUri = uriref.AddFragment(fragment)
63 if newUri.String() != expectedURI {
64 t.Errorf("expected %v, found %v", expectedURI, newUri)
65 }
66 }
67
68 func TestURIRef_String(t *testing.T) {
69 // again, nothing much to test
70 uriString := "https://www.someuri.com/valid/uri"
71 uriref, _ := NewURIRef(uriString)
72 if uriref.String() != uriString+"#" {
73 t.Errorf("expected: %v, found: %v", uriString+"#", uriref.String())
74 }
75 }