-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_ld_reader_test.go
227 lines (199 loc) · 7.51 KB
/
json_ld_reader_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package jsonld_helper_test
import (
"testing"
jsonld_helper "github.com/cloudmatelabs/go-jsonld-helper"
)
func Test_Get_WhenGivenKeyCanNotFind_ShouldReturnNil(t *testing.T) {
givenKey := "notFoundKey"
if result := jsonld.ReadKey(givenKey).Get(); result != nil {
t.Fatal("should return nil:", result)
}
if result := jsonld.ReadIndex(0).ReadKey(givenKey).Get(); result != nil {
t.Fatal("should return nil:", result)
}
}
func Test_Get_WhenGivenIndexResultIsObject_ShouldReturnNil(t *testing.T) {
if result := jsonld.ReadIndex(0).ReadIndex(0).Get(); result != nil {
t.Fatal("should return nil:", result)
}
}
func Test_Get_WhenGivenKeyCanFind_ShouldReturnValue(t *testing.T) {
givenKey := "name"
if result := jsonld.ReadKey(givenKey).Get(); result != "지상 최강의 개발자 쥬니니" {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey(givenKey).Get(); result != "지상 최강의 개발자 쥬니니" {
t.Fatal("failed", result)
}
}
func Test_Get_WhenGivenKeyIsPreDefinedKey_ShouldReturnValue(t *testing.T) {
if result := jsonld.ReadKey("id").Get(); result != "acct:[email protected]" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@id").Get(); result != "acct:[email protected]" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("type").Get(); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@type").Get(); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey("id").Get(); result != "acct:[email protected]" {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey("@id").Get(); result != "acct:[email protected]" {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey("type").Get(); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey("@type").Get(); result != "Person" {
t.Fatal("failed", result)
}
}
func Test_Get_WhenGivenIndexAndKeyCanFind_ShouldReturnValue(t *testing.T) {
if result := jsonld.ReadKey("attachment").ReadIndex(0).ReadKey("value").Get(); result != "juunini" {
t.Fatal("failed", result)
}
}
func Test_GetOrElse_WhenGivenKeyCanNotFind_ShouldReturnDefaultValue(t *testing.T) {
givenKey := "notFoundKey"
givenDefaultValue := "defaultValue"
if result := jsonld.ReadKey(givenKey).GetOrElse(givenDefaultValue); result != givenDefaultValue {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey(givenKey).GetOrElse(givenDefaultValue); result != givenDefaultValue {
t.Fatal("failed", result)
}
}
func Test_GetOrElse_WhenGivenKeyCanFind_ShouldReturnValue(t *testing.T) {
givenKey := "name"
givenDefaultValue := "defaultValue"
if result := jsonld.ReadKey(givenKey).GetOrElse("지상 최강의 개발자 쥬니니"); result == givenDefaultValue {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadKey(givenKey).GetOrElse("지상 최강의 개발자 쥬니니"); result == givenDefaultValue {
t.Fatal("failed", result)
}
}
func Test_GetOrElse_WhenGivenIndexCanNotFind_ShouldReturnDefaultValue(t *testing.T) {
givenIndex := 1
givenDefaultValue := "defaultValue"
if result := jsonld.ReadIndex(givenIndex).GetOrElse(givenDefaultValue); result != givenDefaultValue {
t.Fatal("failed", result)
}
if result := jsonld.ReadIndex(0).ReadIndex(0).GetOrElse(givenDefaultValue); result != givenDefaultValue {
t.Fatal("failed", result)
}
}
func Test_GetOrThrow_WhenGivenKeyCanNotFind_ShouldThrowError(t *testing.T) {
givenKey := "notFoundKey"
if result, err := jsonld.ReadKey(givenKey).GetOrThrow(nil); err == nil || err.Error() != "not found key: notFoundKey" {
t.Fatal("failed", result, err)
}
if result, err := jsonld.ReadIndex(0).ReadKey(givenKey).GetOrThrow(nil); err == nil || err.Error() != "not found key: notFoundKey" {
t.Fatal("failed", result, err)
}
}
func Test_GetOrThrow_WhenGivenKeyCanFind_ShouldReturnValue(t *testing.T) {
givenKey := "name"
if result, err := jsonld.ReadKey(givenKey).GetOrThrow(nil); err != nil || result != "지상 최강의 개발자 쥬니니" {
t.Fatal("failed", result, err)
}
if result, err := jsonld.ReadIndex(0).ReadKey(givenKey).GetOrThrow(nil); err != nil || result != "지상 최강의 개발자 쥬니니" {
t.Fatal("failed", result, err)
}
}
func Test_READMEJsonLDTest(t *testing.T) {
jsonld, _ := jsonld_helper.ParseJsonLD(readmeJsonLD, nil)
if result := jsonld.ReadKey("id").Get(); result != "https://mastodon.social/users/juunini" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@id").Get(); result != "https://mastodon.social/users/juunini" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@id").StringOrElse(""); result != "https://mastodon.social/users/juunini" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("type").Get(); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@type").Get(); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("@type").StringOrElse(""); result != "Person" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("url").Get(); result != "https://mastodon.social/@juunini" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("url").StringOrElse(""); result != "https://mastodon.social/@juunini" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("type").Get(); result != "Image" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("@type").Get(); result != "Image" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("@type").StringOrElse(""); result != "Image" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("mediaType").Get(); result != "image/png" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("mediaType").StringOrElse(""); result != "image/png" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("url").Get(); result != "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("image").ReadKey("url").StringOrElse(""); result != "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("manuallyApprovesFollowers").Get(); result != "true" {
t.Fatal("failed", result)
}
if result := jsonld.ReadKey("manuallyApprovesFollowers").BoolOrElse(false); result != true {
t.Fatal("failed", result)
}
}
var jsonld, _ = jsonld_helper.ParseJsonLD(givenJsonLD, nil)
var givenJsonLD = `{
"@context": [
"activitystreams.json",
{
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value"
}
],
"as:type": "Person",
"@id": "acct:[email protected]",
"name": "지상 최강의 개발자 쥬니니",
"attachment": [
{
"type": "PropertyValue",
"name": "GitHub",
"value": "juunini"
}
]
}`
var readmeJsonLD = map[string]any{
"@context": []any{
"activitystreams.json",
map[string]any{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
},
},
"@id": "https://mastodon.social/users/juunini",
"as:type": "Person",
"url": "https://mastodon.social/@juunini",
"as:image": map[string]any{
"@type": "Image",
"as:mediaType": "image/png",
"url": "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png",
},
"manuallyApprovesFollowers": "true",
}