-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
131 lines (114 loc) · 5.88 KB
/
unmarshal_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
package turtle_test
import (
"testing"
"github.com/nvkp/turtle"
"github.com/nvkp/turtle/assert"
)
func ptr[T any](v T) *T {
return &v
}
func TestUnmarshalStruct(t *testing.T) {
var target triple
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .`)
expected := triple{
Subject: "http://example.org/person/Mark_Twain",
Predicate: "http://example.org/relation/author",
Object: "http://example.org/books/Huckleberry_Finn",
}
err := turtle.Unmarshal(data, &target)
assert.NoError(t, err, "function Unmarshal should have returned no error")
assert.Equal(t, expected, target, "function Unmarshal should have assigned correct values to the target triple")
}
func TestUnmarshalSlice(t *testing.T) {
target := make([]triple, 0)
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .
<http://example.org/person/Mark_Twain> <http://example.org/relation/author2> <http://example.org/books/Huckleberry_Finn2> .`)
expected := []triple{
{
Subject: "http://example.org/person/Mark_Twain",
Predicate: "http://example.org/relation/author",
Object: "http://example.org/books/Huckleberry_Finn",
},
{
Subject: "http://example.org/person/Mark_Twain",
Predicate: "http://example.org/relation/author2",
Object: "http://example.org/books/Huckleberry_Finn2",
},
}
err := turtle.Unmarshal(data, &target)
assert.NoError(t, err, "function Unmarshal should have returned no error")
assert.Equal(t, expected, target, "function Unmarshal should have assigned correct values to the target slice")
}
func TestUnmarshalCompact(t *testing.T) {
target := make([]triple, 0)
data := []byte(`<http://example.org/green-goblin>
<http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/spiderman> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> ;
<http://xmlns.com/foaf/0.1/name> "Green Goblin".<http://example.org/spiderman>
<http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/green-goblin>;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>;
<http://xmlns.com/foaf/0.1/name> "Spiderman", "Человек-паук" .`)
expected := []triple{
{"http://example.org/green-goblin", "http://www.perceive.net/schemas/relationship/enemyOf", "http://example.org/spiderman"},
{"http://example.org/green-goblin", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://xmlns.com/foaf/0.1/Person"},
{"http://example.org/green-goblin", "http://xmlns.com/foaf/0.1/name", "Green Goblin"},
{"http://example.org/spiderman", "http://www.perceive.net/schemas/relationship/enemyOf", "http://example.org/green-goblin"},
{"http://example.org/spiderman", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://xmlns.com/foaf/0.1/Person"},
{"http://example.org/spiderman", "http://xmlns.com/foaf/0.1/name", "Spiderman"},
{"http://example.org/spiderman", "http://xmlns.com/foaf/0.1/name", "Человек-паук"},
}
err := turtle.Unmarshal(data, &target)
assert.NoError(t, err, "function Unmarshal should have returned no error")
assert.Equal(t, expected, target, "function Unmarshal should have assigned correct values to the target slice")
}
func TestUnmarshalSliceOfPointers(t *testing.T) {
target := make([]*triple, 0)
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .
<http://example.org/person/Mark_Twain> <http://example.org/relation/author2> <http://example.org/books/Huckleberry_Finn2> .`)
expected := []*triple{
{
Subject: "http://example.org/person/Mark_Twain",
Predicate: "http://example.org/relation/author",
Object: "http://example.org/books/Huckleberry_Finn",
},
{
Subject: "http://example.org/person/Mark_Twain",
Predicate: "http://example.org/relation/author2",
Object: "http://example.org/books/Huckleberry_Finn2",
},
}
err := turtle.Unmarshal(data, &target)
assert.NoError(t, err, "function Unmarshal should have returned no error")
assert.Equal(t, expected, target, "function Unmarshal should have assigned correct values to the target slice")
}
func TestUnmarshalSliceStructsWithPointers(t *testing.T) {
target := make([]tripleWithPointers, 0)
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .
<http://example.org/person/Mark_Twain> <http://example.org/relation/author2> <http://example.org/books/Huckleberry_Finn2> .`)
expected := []tripleWithPointers{
{
Subject: ptr("http://example.org/person/Mark_Twain"),
Predicate: ptr("http://example.org/relation/author"),
Object: ptr("http://example.org/books/Huckleberry_Finn"),
},
{
Subject: ptr("http://example.org/person/Mark_Twain"),
Predicate: ptr("http://example.org/relation/author2"),
Object: ptr("http://example.org/books/Huckleberry_Finn2"),
},
}
err := turtle.Unmarshal(data, &target)
assert.NoError(t, err, "function Unmarshal should have returned no error")
assert.Equal(t, expected, target, "function Unmarshal should have assigned correct values to the target slice")
}
func TestUnmarshalNil(t *testing.T) {
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .`)
err := turtle.Unmarshal(data, nil)
assert.ErrorIs(t, err, turtle.ErrNilValue, "function Unmarshal should have returned correct error")
}
func TestUnmarshalNotAPointer(t *testing.T) {
var target triple
data := []byte(`<http://example.org/person/Mark_Twain> <http://example.org/relation/author> <http://example.org/books/Huckleberry_Finn> .`)
err := turtle.Unmarshal(data, target)
assert.ErrorIs(t, err, turtle.ErrNoPointerValue, "function Unmarshal should have returned correct error")
}