-
Notifications
You must be signed in to change notification settings - Fork 14
/
term_test.go
149 lines (121 loc) · 3.93 KB
/
term_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package rdf2go
import (
"testing"
"github.com/stretchr/testify/assert"
)
type fakeTerm struct {
URI string
}
func (tt *fakeTerm) Equal(term Term) bool {
return false
}
func (tt *fakeTerm) String() string {
return ""
}
func (tt *fakeTerm) RawValue() string {
return ""
}
func TestTermResourceEqual(t *testing.T) {
t1 := NewResource(testUri)
assert.True(t, t1.Equal(NewResource(testUri)))
assert.False(t, t1.Equal(NewLiteral("test1")))
}
func TestTermResourceWithHash(t *testing.T) {
rURI := "http://example.com/foo#i"
r1 := NewResource(rURI)
assert.Equal(t, rURI, r1.RawValue())
}
func TestTermLiteral(t *testing.T) {
str := "value"
t1 := NewLiteral(str)
assert.Equal(t, "\""+str+"\"", t1.String())
assert.Equal(t, str, t1.RawValue())
}
func TestTermLiteralEqual(t *testing.T) {
t1 := NewLiteralWithLanguage("test1", "en")
assert.False(t, t1.Equal(NewResource(testUri)))
assert.True(t, t1.Equal(NewLiteralWithLanguage("test1", "en")))
assert.False(t, t1.Equal(NewLiteralWithLanguage("test2", "en")))
assert.True(t, t1.Equal(NewLiteralWithLanguage("test1", "en")))
assert.False(t, t1.Equal(NewLiteralWithLanguage("test1", "fr")))
t1 = NewLiteralWithDatatype("test1", NewResource("http://www.w3.org/2001/XMLSchema#string"))
assert.False(t, t1.Equal(NewLiteral("test1")))
assert.True(t, t1.Equal(NewLiteralWithDatatype("test1", NewResource("http://www.w3.org/2001/XMLSchema#string"))))
assert.False(t, t1.Equal(NewLiteralWithDatatype("test1", NewResource("http://www.w3.org/2001/XMLSchema#int"))))
}
func TestTermNewLiteralWithLanguage(t *testing.T) {
s := NewLiteralWithLanguage("test", "en")
assert.Equal(t, "\"test\"@en", s.String())
}
func TestTermNewLiteralWithDatatype(t *testing.T) {
s := NewLiteralWithDatatype("test", NewResource("http://www.w3.org/2001/XMLSchema#string"))
assert.Equal(t, "\"test\"^^<http://www.w3.org/2001/XMLSchema#string>", s.String())
}
func TestTermNewBlankNode(t *testing.T) {
id := NewBlankNode("n1")
assert.Equal(t, "_:n1", id.String())
assert.Equal(t, "n1", id.RawValue())
}
func TestTermNewAnonNode(t *testing.T) {
id := NewAnonNode()
assert.True(t, len(id.String()) > 3)
assert.True(t, id.String()[:3] == "_:n")
}
func TestTermBNodeEqual(t *testing.T) {
id1 := NewBlankNode("n1")
id2 := NewBlankNode("n1")
assert.True(t, id1.Equal(id2))
id3 := NewBlankNode("n2")
assert.False(t, id1.Equal(id3))
assert.False(t, id1.Equal(NewResource(testUri)))
}
func TestTermNils(t *testing.T) {
t1 := Term(&fakeTerm{URI: testUri})
assert.Nil(t, term2rdf(t1))
assert.Nil(t, term2jterm(t1))
}
func TestAtLang(t *testing.T) {
assert.Equal(t, "@en", atLang("en"))
assert.Equal(t, "@en", atLang("@en"))
assert.Equal(t, "@e", atLang("e"))
assert.Equal(t, "", atLang(""))
}
func TestEncodeTerm(t *testing.T) {
iterm := NewResource(testUri)
assert.Equal(t, iterm.String(), encodeTerm(iterm))
iterm = NewBlankNode("n1")
assert.Equal(t, iterm.String(), encodeTerm(iterm))
iterm = NewLiteral("value")
assert.Equal(t, iterm.String(), encodeTerm(iterm))
iterm = Term(&fakeTerm{URI: testUri})
assert.Equal(t, "", encodeTerm(iterm))
}
func TestSplitPrefix(t *testing.T) {
hashUri := testUri + "#me"
base, name := splitPrefix(hashUri)
assert.Equal(t, testUri+"#", base)
assert.Equal(t, "me", name)
slashUri := testUri + "/foaf"
base, name = splitPrefix(slashUri)
assert.Equal(t, testUri+"/", base)
assert.Equal(t, "foaf", name)
badUri := "test"
base, name = splitPrefix(badUri)
assert.Equal(t, "", base)
assert.Equal(t, badUri, name)
}
func TestRDFBrack(t *testing.T) {
assert.Equal(t, "<test>", brack("test"))
assert.Equal(t, "<test", brack("<test"))
assert.Equal(t, "test>", brack("test>"))
}
func TestRDFDebrack(t *testing.T) {
assert.Equal(t, "a", debrack("a"))
assert.Equal(t, "test", debrack("<test>"))
assert.Equal(t, "<test", debrack("<test"))
assert.Equal(t, "test>", debrack("test>"))
}
func TestDefrag(t *testing.T) {
assert.Equal(t, "test", defrag("test"))
assert.Equal(t, "test", defrag("test#me"))
}