yaml-ld 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/AUTHORS +1 -0
  3. data/README.md +150 -0
  4. data/UNLICENSE +24 -0
  5. data/VERSION +1 -0
  6. data/lib/yaml_ld/api.rb +295 -0
  7. data/lib/yaml_ld/format.rb +56 -0
  8. data/lib/yaml_ld/reader.rb +40 -0
  9. data/lib/yaml_ld/version.rb +20 -0
  10. data/lib/yaml_ld/writer.rb +42 -0
  11. data/lib/yaml_ld.rb +37 -0
  12. data/spec/api_spec.rb +92 -0
  13. data/spec/compact_spec.rb +358 -0
  14. data/spec/expand_spec.rb +565 -0
  15. data/spec/flatten_spec.rb +225 -0
  16. data/spec/format_spec.rb +54 -0
  17. data/spec/frame_spec.rb +662 -0
  18. data/spec/from_rdf_spec.rb +730 -0
  19. data/spec/matchers.rb +22 -0
  20. data/spec/reader_spec.rb +138 -0
  21. data/spec/spec_helper.rb +265 -0
  22. data/spec/support/extensions.rb +44 -0
  23. data/spec/test-files/test-1-compacted.jsonld +10 -0
  24. data/spec/test-files/test-1-context.jsonld +7 -0
  25. data/spec/test-files/test-1-expanded.jsonld +5 -0
  26. data/spec/test-files/test-1-input.yamlld +8 -0
  27. data/spec/test-files/test-1-rdf.ttl +8 -0
  28. data/spec/test-files/test-2-compacted.jsonld +20 -0
  29. data/spec/test-files/test-2-context.jsonld +7 -0
  30. data/spec/test-files/test-2-expanded.jsonld +16 -0
  31. data/spec/test-files/test-2-input.yamlld +16 -0
  32. data/spec/test-files/test-2-rdf.ttl +14 -0
  33. data/spec/test-files/test-3-compacted.jsonld +11 -0
  34. data/spec/test-files/test-3-context.jsonld +8 -0
  35. data/spec/test-files/test-3-expanded.jsonld +10 -0
  36. data/spec/test-files/test-3-input.yamlld +13 -0
  37. data/spec/test-files/test-3-rdf.ttl +8 -0
  38. data/spec/test-files/test-4-compacted.jsonld +10 -0
  39. data/spec/test-files/test-4-context.jsonld +7 -0
  40. data/spec/test-files/test-4-expanded.jsonld +6 -0
  41. data/spec/test-files/test-4-input.yamlld +9 -0
  42. data/spec/test-files/test-4-rdf.ttl +5 -0
  43. data/spec/test-files/test-5-compacted.jsonld +13 -0
  44. data/spec/test-files/test-5-context.jsonld +7 -0
  45. data/spec/test-files/test-5-expanded.jsonld +9 -0
  46. data/spec/test-files/test-5-input.yamlld +10 -0
  47. data/spec/test-files/test-5-rdf.ttl +7 -0
  48. data/spec/test-files/test-6-compacted.jsonld +10 -0
  49. data/spec/test-files/test-6-context.jsonld +7 -0
  50. data/spec/test-files/test-6-expanded.jsonld +10 -0
  51. data/spec/test-files/test-6-input.yamlld +12 -0
  52. data/spec/test-files/test-6-rdf.ttl +6 -0
  53. data/spec/test-files/test-7-compacted.jsonld +23 -0
  54. data/spec/test-files/test-7-context.jsonld +4 -0
  55. data/spec/test-files/test-7-expanded.jsonld +20 -0
  56. data/spec/test-files/test-7-input.yamlld +16 -0
  57. data/spec/test-files/test-7-rdf.ttl +14 -0
  58. data/spec/test-files/test-8-compacted.jsonld +34 -0
  59. data/spec/test-files/test-8-context.jsonld +11 -0
  60. data/spec/test-files/test-8-expanded.jsonld +24 -0
  61. data/spec/test-files/test-8-frame.jsonld +18 -0
  62. data/spec/test-files/test-8-framed.jsonld +25 -0
  63. data/spec/test-files/test-8-input.yamlld +24 -0
  64. data/spec/test-files/test-8-rdf.ttl +15 -0
  65. data/spec/test-files/test-9-compacted.jsonld +20 -0
  66. data/spec/test-files/test-9-context.jsonld +13 -0
  67. data/spec/test-files/test-9-expanded.jsonld +14 -0
  68. data/spec/test-files/test-9-input.yamlld +16 -0
  69. data/spec/to_rdf_spec.rb +556 -0
  70. data/spec/writer_spec.rb +441 -0
  71. metadata +350 -0
@@ -0,0 +1,565 @@
1
+ # coding: utf-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe JSON::LD::API do
5
+ let(:logger) {RDF::Spec.logger}
6
+
7
+ describe ".expand" do
8
+ {
9
+ "empty doc": {
10
+ input: {},
11
+ output: []
12
+ },
13
+ "@list coercion": {
14
+ input: %(
15
+ "@context":
16
+ foo:
17
+ "@id": http://example.com/foo
18
+ "@container": "@list"
19
+ foo:
20
+ - "@value": bar
21
+ ),
22
+ output: %(
23
+ - http://example.com/foo:
24
+ - "@list":
25
+ - "@value": bar
26
+ )
27
+ },
28
+ "native values in list": {
29
+ input: %(
30
+ http://example.com/foo:
31
+ "@list":
32
+ - 1
33
+ - 2
34
+ ),
35
+ output: %(
36
+ - http://example.com/foo:
37
+ - "@list":
38
+ - "@value": 1
39
+ - "@value": 2
40
+ )
41
+ },
42
+ "@graph": {
43
+ input: %(
44
+ "@context":
45
+ ex: http://example.com/
46
+ "@graph":
47
+ - ex:foo:
48
+ "@value": foo
49
+ - ex:bar:
50
+ "@value": bar
51
+ ),
52
+ output: %(
53
+ - http://example.com/foo:
54
+ - "@value": foo
55
+ - http://example.com/bar:
56
+ - "@value": bar
57
+ )
58
+ },
59
+ "@graph value (expands to array form)": {
60
+ input: %(
61
+ "@context":
62
+ ex: http://example.com/
63
+ ex:p:
64
+ "@id": ex:Sub1
65
+ "@graph":
66
+ ex:q: foo
67
+ ),
68
+ output: %(
69
+ - http://example.com/p:
70
+ - "@id": http://example.com/Sub1
71
+ "@graph":
72
+ - http://example.com/q:
73
+ - "@value": foo
74
+ )
75
+ },
76
+ "@type with CURIE": {
77
+ input: %(
78
+ "@context":
79
+ ex: http://example.com/
80
+ "@type": ex:type
81
+ ),
82
+ output: %(
83
+ - "@type":
84
+ - http://example.com/type
85
+ )
86
+ },
87
+ "@type with CURIE and muliple values": {
88
+ input: %(
89
+ "@context":
90
+ ex: http://example.com/
91
+ "@type":
92
+ - ex:type1
93
+ - ex:type2
94
+ ),
95
+ output: %(
96
+ - "@type":
97
+ - http://example.com/type1
98
+ - http://example.com/type2
99
+ )
100
+ },
101
+ "@value with false": {
102
+ input: %(
103
+ http://example.com/ex:
104
+ "@value": false
105
+ ),
106
+ output: %(
107
+ - http://example.com/ex:
108
+ - "@value": false
109
+ )
110
+ },
111
+ "compact IRI": {
112
+ input: %(
113
+ "@context":
114
+ ex: http://example.com/
115
+ ex:p:
116
+ "@id": ex:Sub1
117
+ ),
118
+ output: %(
119
+ - http://example.com/p:
120
+ - "@id": http://example.com/Sub1
121
+ }])
122
+ },
123
+ }.each_pair do |title, params|
124
+ it(title) {run_expand params}
125
+ end
126
+
127
+ context "keyword aliasing" do
128
+ {
129
+ "@id": {
130
+ input: %(
131
+ "@context":
132
+ id: "@id"
133
+ id: ""
134
+ "@type": http://www.w3.org/2000/01/rdf-schema#Resource
135
+ ),
136
+ output: %(
137
+ - "@id": ""
138
+ "@type":
139
+ - http://www.w3.org/2000/01/rdf-schema#Resource
140
+ )
141
+ },
142
+ "@type": {
143
+ input: %(
144
+ "@context":
145
+ type: "@type"
146
+ type: http://www.w3.org/2000/01/rdf-schema#Resource
147
+ http://example.com/foo:
148
+ "@value": bar
149
+ type: http://example.com/baz
150
+ ),
151
+ output: %(
152
+ - "@type":
153
+ - http://www.w3.org/2000/01/rdf-schema#Resource
154
+ http://example.com/foo:
155
+ - "@value": bar
156
+ "@type": http://example.com/baz
157
+ )
158
+ },
159
+ "@language": {
160
+ input: %(
161
+ "@context":
162
+ language: "@language"
163
+ http://example.com/foo:
164
+ "@value": bar
165
+ language: baz
166
+ ),
167
+ output: %(
168
+ - http://example.com/foo:
169
+ - "@value": bar
170
+ "@language": baz
171
+ )
172
+ },
173
+ "@value": {
174
+ input: %(
175
+ "@context":
176
+ literal: "@value"
177
+ http://example.com/foo:
178
+ literal: bar
179
+ ),
180
+ output: %(
181
+ - http://example.com/foo:
182
+ - "@value": bar
183
+ )
184
+ },
185
+ "@list": {
186
+ input: %(
187
+ "@context":
188
+ list: "@list"
189
+ http://example.com/foo:
190
+ list:
191
+ - bar
192
+ ),
193
+ output: %(
194
+ - http://example.com/foo:
195
+ - "@list":
196
+ - "@value": bar
197
+ )
198
+ },
199
+ }.each do |title, params|
200
+ it(title) {run_expand params}
201
+ end
202
+ end
203
+
204
+ context "native types" do
205
+ {
206
+ "true": {
207
+ input: %(
208
+ "@context":
209
+ e: http://example.org/vocab#
210
+ e:bool: true
211
+ ),
212
+ output: %(
213
+ - http://example.org/vocab#bool:
214
+ - "@value": true
215
+ )
216
+ },
217
+ "false": {
218
+ input: %(
219
+ "@context":
220
+ e: http://example.org/vocab#
221
+ e:bool: false
222
+ ),
223
+ output: %(
224
+ - http://example.org/vocab#bool:
225
+ - "@value": false
226
+ )
227
+ },
228
+ "double": {
229
+ input: %(
230
+ "@context":
231
+ e: http://example.org/vocab#
232
+ e:double: 1.23
233
+ ),
234
+ output: %(
235
+ - http://example.org/vocab#double:
236
+ - "@value": 1.23
237
+ )
238
+ },
239
+ "double-zero": {
240
+ input: %(
241
+ "@context":
242
+ e: http://example.org/vocab#
243
+ e:double-zero: 0.0e+0
244
+ ),
245
+ output: %(%YAML 1.2\n---
246
+ - http://example.org/vocab#double-zero:
247
+ - "@value": 0.0e+0
248
+ )
249
+ },
250
+ "integer": {
251
+ input: %(
252
+ "@context":
253
+ e: http://example.org/vocab#
254
+ e:integer: 123
255
+ ),
256
+ output: %(
257
+ - http://example.org/vocab#integer:
258
+ - "@value": 123
259
+ )
260
+ },
261
+ }.each do |title, params|
262
+ it(title) {run_expand params}
263
+ end
264
+ end
265
+
266
+ context "with @type: @json" do
267
+ {
268
+ "true": {
269
+ input: %(
270
+ "@context":
271
+ "@version": 1.1
272
+ e:
273
+ "@id": http://example.org/vocab#bool
274
+ "@type": "@json"
275
+ e: true
276
+ ),
277
+ output: %(
278
+ - http://example.org/vocab#bool:
279
+ - "@value": true
280
+ "@type": "@json"
281
+ )
282
+ },
283
+ "false": {
284
+ input: %(
285
+ "@context":
286
+ "@version": 1.1
287
+ e:
288
+ "@id": http://example.org/vocab#bool
289
+ "@type": "@json"
290
+ e: false
291
+ ),
292
+ output: %(
293
+ - http://example.org/vocab#bool:
294
+ - "@value": false
295
+ "@type": "@json"
296
+ )
297
+ },
298
+ "double": {
299
+ input: %(
300
+ "@context":
301
+ "@version": 1.1
302
+ e:
303
+ "@id": http://example.org/vocab#double
304
+ "@type": "@json"
305
+ e: 1.23
306
+ ),
307
+ output: %(
308
+ - http://example.org/vocab#double:
309
+ - "@value": 1.23
310
+ "@type": "@json"
311
+ )
312
+ },
313
+ "double-zero": {
314
+ input: %(
315
+ "@context":
316
+ "@version": 1.1
317
+ e:
318
+ "@id": http://example.org/vocab#double
319
+ "@type": "@json"
320
+ e: 0.0e+0
321
+ ),
322
+ output: %(
323
+ - http://example.org/vocab#double:
324
+ - "@value": 0.0e+0
325
+ "@type": "@json"
326
+ )
327
+ },
328
+ "integer": {
329
+ input: %(
330
+ "@context":
331
+ "@version": 1.1
332
+ e:
333
+ "@id": http://example.org/vocab#integer
334
+ "@type": "@json"
335
+ e: 123
336
+ ),
337
+ output: %(
338
+ - http://example.org/vocab#integer:
339
+ - "@value": 123
340
+ "@type": "@json"
341
+ )
342
+ },
343
+ "string": {
344
+ input: %(
345
+ "@context":
346
+ "@version": 1.1
347
+ e:
348
+ "@id": http://example.org/vocab#string
349
+ "@type": "@json"
350
+ e: string
351
+ ),
352
+ output: %(
353
+ - http://example.org/vocab#string:
354
+ - "@value": string
355
+ "@type": "@json"
356
+ )
357
+ },
358
+ "null": {
359
+ input: %(
360
+ "@context":
361
+ "@version": 1.1
362
+ e:
363
+ "@id": http://example.org/vocab#null
364
+ "@type": "@json"
365
+ e: null
366
+ ),
367
+ output: %(
368
+ - http://example.org/vocab#null:
369
+ - "@value": null
370
+ "@type": "@json"
371
+ )
372
+ },
373
+ "object": {
374
+ input: %(
375
+ "@context":
376
+ "@version": 1.1
377
+ e:
378
+ "@id": http://example.org/vocab#object
379
+ "@type": "@json"
380
+ e:
381
+ foo: bar
382
+ ),
383
+ output: %(
384
+ - http://example.org/vocab#object:
385
+ - "@value":
386
+ foo: bar
387
+ "@type": "@json"
388
+ )
389
+ },
390
+ "array": {
391
+ input: %(
392
+ "@context":
393
+ "@version": 1.1
394
+ e:
395
+ "@id": http://example.org/vocab#array
396
+ "@type": "@json"
397
+ e:
398
+ - foo: bar
399
+ ),
400
+ output: %(
401
+ - http://example.org/vocab#array:
402
+ - "@value":
403
+ - foo: bar
404
+ "@type": "@json"
405
+ )
406
+ },
407
+ "Does not expand terms inside json": {
408
+ input: %(
409
+ "@context":
410
+ "@version": 1.1
411
+ e:
412
+ "@id": http://example.org/vocab#array
413
+ "@type": "@json"
414
+ e:
415
+ - e: bar
416
+ ),
417
+ output: %(
418
+ - http://example.org/vocab#array:
419
+ - "@value":
420
+ - e: bar
421
+ "@type": "@json"
422
+ )
423
+ },
424
+ "Already expanded object with aliased keys": {
425
+ input: %(
426
+ "@context":
427
+ "@version": 1.1
428
+ value: "@value"
429
+ type: "@type"
430
+ json: "@json"
431
+ http://example.org/vocab#object:
432
+ - value:
433
+ foo: bar
434
+ type: json
435
+ ),
436
+ output: %(
437
+ - http://example.org/vocab#object:
438
+ - "@value":
439
+ foo: bar
440
+ "@type": "@json"
441
+ )
442
+ },
443
+ }.each do |title, params|
444
+ it(title) {run_expand params}
445
+ end
446
+ end
447
+
448
+ context "@direction" do
449
+ {
450
+ "value with coerced null direction": {
451
+ input: %(
452
+ "@context":
453
+ "@direction": rtl
454
+ ex: http://example.org/vocab#
455
+ ex:ltr:
456
+ "@direction": ltr
457
+ ex:none:
458
+ "@direction":
459
+ ex:rtl: rtl
460
+ ex:ltr: ltr
461
+ ex:none: no direction
462
+ ),
463
+ output: %(
464
+ - http://example.org/vocab#rtl:
465
+ - "@value": rtl
466
+ "@direction": rtl
467
+ http://example.org/vocab#ltr:
468
+ - "@value": ltr
469
+ "@direction": ltr
470
+ http://example.org/vocab#none:
471
+ - "@value": no direction
472
+ )
473
+ }
474
+ }.each_pair do |title, params|
475
+ it(title) {run_expand params}
476
+ end
477
+ end
478
+
479
+ context "JSON-LD-star" do
480
+ {
481
+ "node with embedded subject without rdfstar option": {
482
+ input: %(
483
+ "@id":
484
+ "@id": ex:rei
485
+ ex:prop: value
486
+ ex:prop: value2
487
+ ),
488
+ exception: JSON::LD::JsonLdError::InvalidIdValue
489
+ },
490
+ "node with embedded subject with rdfstar option": {
491
+ input: %(
492
+ "@id":
493
+ "@id": ex:rei
494
+ ex:prop: value
495
+ ex:prop: value2
496
+ ),
497
+ output: %(
498
+ - "@id":
499
+ "@id": ex:rei
500
+ ex:prop:
501
+ - "@value": value
502
+ ex:prop:
503
+ - "@value": value2
504
+ ),
505
+ rdfstar: true
506
+ },
507
+ "node object with @annotation property is ignored without rdfstar option": {
508
+ input: %(
509
+ "@id": ex:bob
510
+ ex:knows:
511
+ "@id": ex:fred
512
+ "@annotation":
513
+ ex:certainty: 0.8
514
+ ),
515
+ output: %(
516
+ - "@id": ex:bob
517
+ ex:knows:
518
+ - "@id": ex:fred
519
+ )
520
+ },
521
+ "node object with @annotation property with rdfstar option": {
522
+ input: %(
523
+ "@id": ex:bob
524
+ ex:knows:
525
+ "@id": ex:fred
526
+ "@annotation":
527
+ ex:certainty: 0.8
528
+ ),
529
+ output: %(
530
+ - "@id": ex:bob
531
+ ex:knows:
532
+ - "@id": ex:fred
533
+ "@annotation":
534
+ - ex:certainty:
535
+ - "@value": 0.8
536
+ ),
537
+ rdfstar: true
538
+ },
539
+ }.each do |title, params|
540
+ it(title) {run_expand params}
541
+ end
542
+ end
543
+ end
544
+
545
+ def run_expand(params)
546
+ input, output = params[:input], params[:output]
547
+ params[:base] ||= nil
548
+ input = StringIO.new(input) if input.is_a?(String)
549
+ pending params.fetch(:pending, "test implementation") unless input
550
+ if params[:exception]
551
+ expect {YAML_LD::API.expand(input, **params)}.to raise_error(params[:exception])
552
+ else
553
+ yld = nil
554
+ if params[:write]
555
+ expect{yld = YAML_LD::API.expand(input, logger: logger, **params)}.to write(params[:write]).to(:error)
556
+ else
557
+ expect{yld = YAML_LD::API.expand(input, logger: logger, **params)}.not_to write.to(:error)
558
+ end
559
+ expect(yld).to produce_yamlld(output, logger)
560
+
561
+ # Also expect result to produce itself
562
+ expect(output).to produce_yamlld(output, logger)
563
+ end
564
+ end
565
+ end