forked from knakk/rdf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rdf_test.go
100 lines (92 loc) · 2.59 KB
/
rdf_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
package rdf
import (
"fmt"
"testing"
)
func TestIRI(t *testing.T) {
errTests := []struct {
input string
want string
}{
{"", "empty IRI"},
{"http://dott\ncom", "disallowed character: '\\n'"},
{"<a>", "disallowed character: '<'"},
{"here are spaces", "disallowed character: ' '"},
{"myscheme://abc/xyz/伝言/æøå#hei?f=88", "<nil>"},
}
for _, tt := range errTests {
_, err := NewIRI(tt.input)
if fmt.Sprintf("%v", err) != tt.want {
t.Errorf("NewURI(%q) => %v; want %v", tt.input, err, tt.want)
}
}
}
func TestLiteral(t *testing.T) {
inferTypeTests := []struct {
input interface{}
dt IRI
errString string
}{
{1, xsdInteger, ""},
{int64(1), xsdInteger, ""},
{int32(1), xsdInteger, ""},
{3.14, xsdDouble, ""},
{float32(3.14), xsdDouble, ""},
{float64(3.14), xsdDouble, ""},
{true, xsdBoolean, ""},
{false, xsdBoolean, ""},
{"a", xsdString, ""},
{[]byte("123"), xsdByte, ""},
{struct{ a, b string }{"1", "2"}, IRI{}, `cannot infer XSD datatype from struct { a string; b string }{a:"1", b:"2"}`},
}
for _, tt := range inferTypeTests {
l, err := NewLiteral(tt.input)
if err != nil {
if tt.errString == "" {
t.Errorf("NewLiteral(%#v) failed with %v; want no error", tt.input, err)
continue
}
if tt.errString != err.Error() {
t.Errorf("NewLiteral(%#v) failed with %v; want %v", tt.input, err, tt.errString)
continue
}
}
if err == nil && tt.errString != "" {
t.Errorf("NewLiteral(%#v) => <no error>; want error %v", tt.input, tt.errString)
continue
}
if l.DataType != tt.dt {
t.Errorf("NewLiteral(%#v).DataType => %v; want %v", tt.input, l.DataType, tt.dt)
}
}
langTagTests := []struct {
tag string
errWant string
}{
{"en", ""},
{"en-GB", ""},
{"nb-no2", ""},
{"no-no-a", "invalid language tag: only one '-' allowed"},
{"1", "invalid language tag: unexpected character: '1'"},
{"fr-ø", "invalid language tag: unexpected character: 'ø'"},
{"en-", "invalid language tag: trailing '-' disallowed"},
{"-en", "invalid language tag: must start with a letter"},
}
for _, tt := range langTagTests {
_, err := NewLangLiteral("string", tt.tag)
if err != nil {
if tt.errWant == "" {
t.Errorf("NewLangLiteral(\"string\", %#v) failed with %v; want no error", tt.tag, err)
continue
}
if tt.errWant != err.Error() {
t.Errorf("NewLangLiteral(\"string\", %#v) failed with %v; want %v", tt.tag, err, tt.errWant)
continue
}
}
if err == nil && tt.errWant != "" {
t.Errorf("NewLangLiteral(\"string\", %#v) => <no error>; want error %v", tt.tag, tt.errWant)
continue
}
}
}