json-ld 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHORS +1 -1
  3. data/README.md +90 -53
  4. data/UNLICENSE +1 -1
  5. data/VERSION +1 -1
  6. data/bin/jsonld +4 -4
  7. data/lib/json/ld.rb +27 -10
  8. data/lib/json/ld/api.rb +325 -96
  9. data/lib/json/ld/compact.rb +75 -27
  10. data/lib/json/ld/conneg.rb +188 -0
  11. data/lib/json/ld/context.rb +677 -292
  12. data/lib/json/ld/expand.rb +240 -75
  13. data/lib/json/ld/flatten.rb +5 -3
  14. data/lib/json/ld/format.rb +19 -19
  15. data/lib/json/ld/frame.rb +135 -85
  16. data/lib/json/ld/from_rdf.rb +44 -17
  17. data/lib/json/ld/html/nokogiri.rb +151 -0
  18. data/lib/json/ld/html/rexml.rb +186 -0
  19. data/lib/json/ld/reader.rb +25 -5
  20. data/lib/json/ld/resource.rb +2 -2
  21. data/lib/json/ld/streaming_writer.rb +3 -1
  22. data/lib/json/ld/to_rdf.rb +47 -17
  23. data/lib/json/ld/utils.rb +4 -2
  24. data/lib/json/ld/writer.rb +75 -14
  25. data/spec/api_spec.rb +13 -34
  26. data/spec/compact_spec.rb +968 -9
  27. data/spec/conneg_spec.rb +373 -0
  28. data/spec/context_spec.rb +447 -53
  29. data/spec/expand_spec.rb +1872 -416
  30. data/spec/flatten_spec.rb +434 -47
  31. data/spec/frame_spec.rb +979 -344
  32. data/spec/from_rdf_spec.rb +305 -5
  33. data/spec/spec_helper.rb +177 -0
  34. data/spec/streaming_writer_spec.rb +4 -4
  35. data/spec/suite_compact_spec.rb +2 -2
  36. data/spec/suite_expand_spec.rb +14 -2
  37. data/spec/suite_flatten_spec.rb +10 -2
  38. data/spec/suite_frame_spec.rb +3 -2
  39. data/spec/suite_from_rdf_spec.rb +2 -2
  40. data/spec/suite_helper.rb +55 -20
  41. data/spec/suite_html_spec.rb +22 -0
  42. data/spec/suite_http_spec.rb +35 -0
  43. data/spec/suite_remote_doc_spec.rb +2 -2
  44. data/spec/suite_to_rdf_spec.rb +14 -3
  45. data/spec/support/extensions.rb +5 -1
  46. data/spec/test-files/test-4-input.json +3 -3
  47. data/spec/test-files/test-5-input.json +2 -2
  48. data/spec/test-files/test-8-framed.json +14 -18
  49. data/spec/to_rdf_spec.rb +606 -16
  50. data/spec/writer_spec.rb +5 -5
  51. metadata +144 -88
@@ -196,14 +196,223 @@ describe JSON::LD::API do
196
196
  expect(parse(js)).to be_equivalent_graph(ttl, logger: logger, inputDocument: js)
197
197
  end
198
198
  end
199
+
200
+ context "with @type: @json" do
201
+ {
202
+ "true": {
203
+ input: %({
204
+ "@context": {
205
+ "@version": 1.1,
206
+ "e": {"@id": "http://example.org/vocab#bool", "@type": "@json"}
207
+ },
208
+ "e": true
209
+ }),
210
+ output:%(
211
+ @prefix ex: <http://example.org/vocab#> .
212
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
213
+ [ex:bool "true"^^rdf:JSON] .
214
+ )
215
+ },
216
+ "false": {
217
+ input: %({
218
+ "@context": {
219
+ "@version": 1.1,
220
+ "e": {"@id": "http://example.org/vocab#bool", "@type": "@json"}
221
+ },
222
+ "e": false
223
+ }),
224
+ output: %(
225
+ @prefix ex: <http://example.org/vocab#> .
226
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
227
+ [ex:bool "false"^^rdf:JSON] .
228
+ )
229
+ },
230
+ "double": {
231
+ input: %({
232
+ "@context": {
233
+ "@version": 1.1,
234
+ "e": {"@id": "http://example.org/vocab#double", "@type": "@json"}
235
+ },
236
+ "e": 1.23
237
+ }),
238
+ output: %(
239
+ @prefix ex: <http://example.org/vocab#> .
240
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
241
+ [ex:double "1.23"^^rdf:JSON] .
242
+ )
243
+ },
244
+ "double-zero": {
245
+ input: %({
246
+ "@context": {
247
+ "@version": 1.1,
248
+ "e": {"@id": "http://example.org/vocab#double", "@type": "@json"}
249
+ },
250
+ "e": 0
251
+ }),
252
+ output: %(
253
+ @prefix ex: <http://example.org/vocab#> .
254
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
255
+ [ex:double "0"^^rdf:JSON] .
256
+ )
257
+ },
258
+ "integer": {
259
+ input: %({
260
+ "@context": {
261
+ "@version": 1.1,
262
+ "e": {"@id": "http://example.org/vocab#integer", "@type": "@json"}
263
+ },
264
+ "e": 123
265
+ }),
266
+ output: %(
267
+ @prefix ex: <http://example.org/vocab#> .
268
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
269
+ [ex:integer "123"^^rdf:JSON] .
270
+ )
271
+ },
272
+ "string": {
273
+ input: %({
274
+ "@context": {
275
+ "@version": 1.1,
276
+ "e": {"@id": "http://example.org/vocab#string", "@type": "@json"}
277
+ },
278
+ "e": "string"
279
+ }),
280
+ output: %(
281
+ @prefix ex: <http://example.org/vocab#> .
282
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
283
+ [ex:string "\\"string\\""^^rdf:JSON] .
284
+ )
285
+ },
286
+ "null": {
287
+ input: %({
288
+ "@context": {
289
+ "@version": 1.1,
290
+ "e": {"@id": "http://example.org/vocab#null", "@type": "@json"}
291
+ },
292
+ "e": null
293
+ }),
294
+ output: %(
295
+ @prefix ex: <http://example.org/vocab#> .
296
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
297
+ [ex:null "null"^^rdf:JSON] .
298
+ )
299
+ },
300
+ "object": {
301
+ input: %({
302
+ "@context": {
303
+ "@version": 1.1,
304
+ "e": {"@id": "http://example.org/vocab#object", "@type": "@json"}
305
+ },
306
+ "e": {"foo": "bar"}
307
+ }),
308
+ output: %(
309
+ @prefix ex: <http://example.org/vocab#> .
310
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
311
+ [ex:object """{"foo":"bar"}"""^^rdf:JSON] .
312
+ )
313
+ },
314
+ "array": {
315
+ input: %({
316
+ "@context": {
317
+ "@version": 1.1,
318
+ "e": {"@id": "http://example.org/vocab#array", "@type": "@json"}
319
+ },
320
+ "e": [{"foo": "bar"}]
321
+ }),
322
+ output: %(
323
+ @prefix ex: <http://example.org/vocab#> .
324
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
325
+ [ex:array """[{"foo":"bar"}]"""^^rdf:JSON] .
326
+ )
327
+ },
328
+ "c14n-arrays": {
329
+ input: %({
330
+ "@context": {
331
+ "@version": 1.1,
332
+ "e": {"@id": "http://example.org/vocab#c14n", "@type": "@json"}
333
+ },
334
+ "e": [
335
+ 56,
336
+ {
337
+ "d": true,
338
+ "10": null,
339
+ "1": [ ]
340
+ }
341
+ ]
342
+ }),
343
+ output: %(
344
+ @prefix ex: <http://example.org/vocab#> .
345
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
346
+ [ex:c14n """[56,{"1":[],"10":null,"d":true}]"""^^rdf:JSON] .
347
+ )
348
+ },
349
+ "c14n-french": {
350
+ input: %({
351
+ "@context": {
352
+ "@version": 1.1,
353
+ "e": {"@id": "http://example.org/vocab#c14n", "@type": "@json"}
354
+ },
355
+ "e": {
356
+ "peach": "This sorting order",
357
+ "péché": "is wrong according to French",
358
+ "pêche": "but canonicalization MUST",
359
+ "sin": "ignore locale"
360
+ }
361
+ }),
362
+ output: %(
363
+ @prefix ex: <http://example.org/vocab#> .
364
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
365
+ [ex:c14n """{"peach":"This sorting order","péché":"is wrong according to French","pêche":"but canonicalization MUST","sin":"ignore locale"}"""^^rdf:JSON] .
366
+ )
367
+ },
368
+ "c14n-structures": {
369
+ input: %({
370
+ "@context": {
371
+ "@version": 1.1,
372
+ "e": {"@id": "http://example.org/vocab#c14n", "@type": "@json"}
373
+ },
374
+ "e": {
375
+ "1": {"f": {"f": "hi","F": 5} ," ": 56.0},
376
+ "10": { },
377
+ "": "empty",
378
+ "a": { },
379
+ "111": [ {"e": "yes","E": "no" } ],
380
+ "A": { }
381
+ }
382
+ }),
383
+ output: %(
384
+ @prefix ex: <http://example.org/vocab#> .
385
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
386
+ [ex:c14n """{"":"empty","1":{" ":56,"f":{"F":5,"f":"hi"}},"10":{},"111":[{"E":"no","e":"yes"}],"A":{},"a":{}}"""^^rdf:JSON] .
387
+ )
388
+ },
389
+ "c14n-unicode": {
390
+ input: %({
391
+ "@context": {
392
+ "@version": 1.1,
393
+ "e": {"@id": "http://example.org/vocab#c14n", "@type": "@json"}
394
+ },
395
+ "e": {
396
+ "Unnormalized Unicode":"A\u030a"
397
+ }
398
+ }),
399
+ output: %(
400
+ @prefix ex: <http://example.org/vocab#> .
401
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
402
+ [ex:c14n """{"Unnormalized Unicode":"Å"}"""^^rdf:JSON] .
403
+ )
404
+ },
405
+ }.each do |title, params|
406
+ it title do
407
+ params[:output] = RDF::Graph.new << RDF::Turtle::Reader.new(params[:output])
408
+ run_to_rdf params
409
+ end
410
+ end
411
+ end
199
412
  end
200
413
 
201
414
  context "prefixes" do
202
415
  {
203
- "empty prefix" => [
204
- %q({"@context": {"": "http://example.com/default#"}, ":foo": "bar"}),
205
- %q(_:a <http://example.com/default#foo> "bar"^^xsd:string .)
206
- ],
207
416
  "empty suffix" => [
208
417
  %q({"@context": {"prefix": "http://example.com/default#"}, "prefix:": "bar"}),
209
418
  %q(_:a <http://example.com/default#> "bar"^^xsd:string .)
@@ -600,13 +809,234 @@ describe JSON::LD::API do
600
809
  subject {%q({"@id": "http://example/subj", "_:foo": "bar"})}
601
810
 
602
811
  it "outputs statements with blank node predicates if :produceGeneralizedRdf is true" do
603
- graph = parse(subject, produceGeneralizedRdf: true)
604
- expect(graph.count).to eq 1
812
+ expect do
813
+ graph = parse(subject, produceGeneralizedRdf: true)
814
+ expect(graph.count).to eq 1
815
+ end.to write("[DEPRECATION]").to(:error)
605
816
  end
606
817
 
607
818
  it "rejects statements with blank node predicates if :produceGeneralizedRdf is false" do
608
- graph = parse(subject, produceGeneralizedRdf: false)
609
- expect(graph.count).to eq 0
819
+ expect do
820
+ graph = parse(subject, produceGeneralizedRdf: false)
821
+ expect(graph.count).to eq 0
822
+ end.to write("[DEPRECATION]").to(:error)
823
+ end
824
+ end
825
+
826
+ context "@included" do
827
+ {
828
+ "Basic Included array": {
829
+ input: %({
830
+ "@context": {
831
+ "@version": 1.1,
832
+ "@vocab": "http://example.org/"
833
+ },
834
+ "prop": "value",
835
+ "@included": [{
836
+ "prop": "value2"
837
+ }]
838
+ }),
839
+ output: %(
840
+ [<http://example.org/prop> "value"] .
841
+ [<http://example.org/prop> "value2"] .
842
+ )
843
+ },
844
+ "Basic Included object": {
845
+ input: %({
846
+ "@context": {
847
+ "@version": 1.1,
848
+ "@vocab": "http://example.org/"
849
+ },
850
+ "prop": "value",
851
+ "@included": {
852
+ "prop": "value2"
853
+ }
854
+ }),
855
+ output: %(
856
+ [<http://example.org/prop> "value"] .
857
+ [<http://example.org/prop> "value2"] .
858
+ )
859
+ },
860
+ "Multiple properties mapping to @included are folded together": {
861
+ input: %({
862
+ "@context": {
863
+ "@version": 1.1,
864
+ "@vocab": "http://example.org/",
865
+ "included1": "@included",
866
+ "included2": "@included"
867
+ },
868
+ "included1": {"prop": "value1"},
869
+ "included2": {"prop": "value2"}
870
+ }),
871
+ output: %(
872
+ [<http://example.org/prop> "value1"] .
873
+ [<http://example.org/prop> "value2"] .
874
+ )
875
+ },
876
+ "Included containing @included": {
877
+ input: %({
878
+ "@context": {
879
+ "@version": 1.1,
880
+ "@vocab": "http://example.org/"
881
+ },
882
+ "prop": "value",
883
+ "@included": {
884
+ "prop": "value2",
885
+ "@included": {
886
+ "prop": "value3"
887
+ }
888
+ }
889
+ }),
890
+ output: %(
891
+ [<http://example.org/prop> "value"] .
892
+
893
+ [<http://example.org/prop> "value2"] .
894
+
895
+ [<http://example.org/prop> "value3"] .
896
+ )
897
+ },
898
+ "Property value with @included": {
899
+ input: %({
900
+ "@context": {
901
+ "@version": 1.1,
902
+ "@vocab": "http://example.org/"
903
+ },
904
+ "prop": {
905
+ "@type": "Foo",
906
+ "@included": {
907
+ "@type": "Bar"
908
+ }
909
+ }
910
+ }),
911
+ output: %(
912
+ [<http://example.org/prop> [a <http://example.org/Foo>]] .
913
+ [a <http://example.org/Bar>] .
914
+ )
915
+ },
916
+ "json.api example": {
917
+ input: %({
918
+ "@context": {
919
+ "@version": 1.1,
920
+ "@vocab": "http://example.org/vocab#",
921
+ "@base": "http://example.org/base/",
922
+ "id": "@id",
923
+ "type": "@type",
924
+ "data": "@nest",
925
+ "attributes": "@nest",
926
+ "links": "@nest",
927
+ "relationships": "@nest",
928
+ "included": "@included",
929
+ "self": {"@type": "@id"},
930
+ "related": {"@type": "@id"},
931
+ "comments": {
932
+ "@context": {
933
+ "data": null
934
+ }
935
+ }
936
+ },
937
+ "data": [{
938
+ "type": "articles",
939
+ "id": "1",
940
+ "attributes": {
941
+ "title": "JSON:API paints my bikeshed!"
942
+ },
943
+ "links": {
944
+ "self": "http://example.com/articles/1"
945
+ },
946
+ "relationships": {
947
+ "author": {
948
+ "links": {
949
+ "self": "http://example.com/articles/1/relationships/author",
950
+ "related": "http://example.com/articles/1/author"
951
+ },
952
+ "data": { "type": "people", "id": "9" }
953
+ },
954
+ "comments": {
955
+ "links": {
956
+ "self": "http://example.com/articles/1/relationships/comments",
957
+ "related": "http://example.com/articles/1/comments"
958
+ },
959
+ "data": [
960
+ { "type": "comments", "id": "5" },
961
+ { "type": "comments", "id": "12" }
962
+ ]
963
+ }
964
+ }
965
+ }],
966
+ "included": [{
967
+ "type": "people",
968
+ "id": "9",
969
+ "attributes": {
970
+ "first-name": "Dan",
971
+ "last-name": "Gebhardt",
972
+ "twitter": "dgeb"
973
+ },
974
+ "links": {
975
+ "self": "http://example.com/people/9"
976
+ }
977
+ }, {
978
+ "type": "comments",
979
+ "id": "5",
980
+ "attributes": {
981
+ "body": "First!"
982
+ },
983
+ "relationships": {
984
+ "author": {
985
+ "data": { "type": "people", "id": "2" }
986
+ }
987
+ },
988
+ "links": {
989
+ "self": "http://example.com/comments/5"
990
+ }
991
+ }, {
992
+ "type": "comments",
993
+ "id": "12",
994
+ "attributes": {
995
+ "body": "I like XML better"
996
+ },
997
+ "relationships": {
998
+ "author": {
999
+ "data": { "type": "people", "id": "9" }
1000
+ }
1001
+ },
1002
+ "links": {
1003
+ "self": "http://example.com/comments/12"
1004
+ }
1005
+ }]
1006
+ }),
1007
+ output: %(
1008
+ <http://example.org/base/1> a <http://example.org/vocab#articles>;
1009
+ <http://example.org/vocab#author> <http://example.org/base/9>;
1010
+ <http://example.org/vocab#comments> [
1011
+ <http://example.org/vocab#related> <http://example.com/articles/1/comments>;
1012
+ <http://example.org/vocab#self> <http://example.com/articles/1/relationships/comments>
1013
+ ];
1014
+ <http://example.org/vocab#self> <http://example.com/articles/1>;
1015
+ <http://example.org/vocab#title> "JSON:API paints my bikeshed!" .
1016
+
1017
+ <http://example.org/base/12> a <http://example.org/vocab#comments>;
1018
+ <http://example.org/vocab#author> <http://example.org/base/9>;
1019
+ <http://example.org/vocab#body> "I like XML better";
1020
+ <http://example.org/vocab#self> <http://example.com/comments/12> .
1021
+
1022
+ <http://example.org/base/5> a <http://example.org/vocab#comments>;
1023
+ <http://example.org/vocab#author> <http://example.org/base/2>;
1024
+ <http://example.org/vocab#body> "First!";
1025
+ <http://example.org/vocab#self> <http://example.com/comments/5> .
1026
+
1027
+ <http://example.org/base/2> a <http://example.org/vocab#people> .
1028
+
1029
+ <http://example.org/base/9> a <http://example.org/vocab#people>;
1030
+ <http://example.org/vocab#first-name> "Dan";
1031
+ <http://example.org/vocab#last-name> "Gebhardt";
1032
+ <http://example.org/vocab#related> <http://example.com/articles/1/author>;
1033
+ <http://example.org/vocab#self> <http://example.com/articles/1/relationships/author>,
1034
+ <http://example.com/people/9>;
1035
+ <http://example.org/vocab#twitter> "dgeb" .
1036
+ )
1037
+ },
1038
+ }.each do |title, params|
1039
+ it(title) {run_to_rdf params}
610
1040
  end
611
1041
  end
612
1042
 
@@ -676,6 +1106,75 @@ describe JSON::LD::API do
676
1106
  end
677
1107
  end
678
1108
 
1109
+ context "@direction" do
1110
+ context "rdfDirection: null" do
1111
+ {
1112
+ "no language rtl": [
1113
+ %q({"http://example.org/label": {"@value": "no language", "@direction": "rtl"}}),
1114
+ %q(_:a <http://example.org/label> "no language" .)
1115
+ ],
1116
+ "en-US rtl": [
1117
+ %q({"http://example.org/label": {"@value": "en-US", "@language": "en-US", "@direction": "rtl"}}),
1118
+ %q(_:a <http://example.org/label> "en-US"@en-us .)
1119
+ ]
1120
+ }.each do |title, (js, ttl)|
1121
+ it title do
1122
+ ttl = "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . #{ttl}"
1123
+ expect(parse(js, rdfDirection: nil)).to be_equivalent_graph(ttl, logger: logger, inputDocument: js)
1124
+ end
1125
+ end
1126
+ end
1127
+
1128
+ context "rdfDirection: i18n-datatype" do
1129
+ {
1130
+ "no language rtl": [
1131
+ %q({"http://example.org/label": {"@value": "no language", "@direction": "rtl"}}),
1132
+ %q(_:a <http://example.org/label> "no language"^^<https://www.w3.org/ns/i18n#_rtl> .)
1133
+ ],
1134
+ "en-US rtl": [
1135
+ %q({"http://example.org/label": {"@value": "en-US", "@language": "en-US", "@direction": "rtl"}}),
1136
+ %q(_:a <http://example.org/label> "en-US"^^<https://www.w3.org/ns/i18n#en-US_rtl> .)
1137
+ ]
1138
+ }.each do |title, (js, ttl)|
1139
+ it title do
1140
+ ttl = "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . #{ttl}"
1141
+ expect(parse(js, rdfDirection: 'i18n-datatype')).to be_equivalent_graph(ttl, logger: logger, inputDocument: js)
1142
+ end
1143
+ end
1144
+ end
1145
+
1146
+ context "rdfDirection: compound-literal" do
1147
+ {
1148
+ "no language rtl": [
1149
+ %q({"http://example.org/label": {"@value": "no language", "@direction": "rtl"}}),
1150
+ %q(
1151
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1152
+ _:a <http://example.org/label> [
1153
+ rdf:value "no language";
1154
+ rdf:direction "rtl"
1155
+ ] .
1156
+ )
1157
+ ],
1158
+ "en-US rtl": [
1159
+ %q({"http://example.org/label": {"@value": "en-US", "@language": "en-US", "@direction": "rtl"}}),
1160
+ %q(
1161
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1162
+ _:a <http://example.org/label> [
1163
+ rdf:value "en-US";
1164
+ rdf:language "en-US";
1165
+ rdf:direction "rtl"
1166
+ ] .
1167
+ )
1168
+ ]
1169
+ }.each do |title, (js, ttl)|
1170
+ it title do
1171
+ ttl = "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . #{ttl}"
1172
+ expect(parse(js, rdfDirection: 'compound-literal')).to be_equivalent_graph(ttl, logger: logger, inputDocument: js)
1173
+ end
1174
+ end
1175
+ end
1176
+ end
1177
+
679
1178
  context "exceptions" do
680
1179
  {
681
1180
  "Invalid subject" => {
@@ -711,7 +1210,8 @@ describe JSON::LD::API do
711
1210
  "@id": "http://example.com/foo",
712
1211
  "http://example.com/bar": {"@value": "bar", "@language": "a b"}
713
1212
  }),
714
- output: %()
1213
+ output: %(),
1214
+ write: "@language must be valid BCP47"
715
1215
  },
716
1216
  "Invalid datatype" => {
717
1217
  input: %({
@@ -725,30 +1225,120 @@ describe JSON::LD::API do
725
1225
  "@id": "http://foo/> <http://bar/> <http://baz> .\n<data:little> <data:bobby> <data:tables> .\n<data:in-ur-base",
726
1226
  "http://killin/#yer": "dudes"
727
1227
  }),
728
- output: %()
1228
+ output: %(),
1229
+ pending: "jruby"
729
1230
  },
730
1231
  }.each do |title, params|
731
- it(title) {run_to_rdf params}
1232
+ it(title) do
1233
+ pending params[:pending] if params[:pending] == RUBY_ENGINE
1234
+ run_to_rdf params
1235
+ end
1236
+ end
1237
+ end
1238
+ end
1239
+
1240
+ context "html" do
1241
+ {
1242
+ "Transforms embedded JSON-LD script element": {
1243
+ input: %(
1244
+ <html>
1245
+ <head>
1246
+ <script type="application/ld+json">
1247
+ {
1248
+ "@context": {
1249
+ "foo": {"@id": "http://example.com/foo", "@container": "@list"}
1250
+ },
1251
+ "foo": [{"@value": "bar"}]
1252
+ }
1253
+ </script>
1254
+ </head>
1255
+ </html>),
1256
+ output: %([ <http://example.com/foo> ( "bar")] .)
1257
+ },
1258
+ "Transforms first script element with extractAllScripts: false": {
1259
+ input: %(
1260
+ <html>
1261
+ <head>
1262
+ <script type="application/ld+json">
1263
+ {
1264
+ "@context": {
1265
+ "foo": {"@id": "http://example.com/foo", "@container": "@list"}
1266
+ },
1267
+ "foo": [{"@value": "bar"}]
1268
+ }
1269
+ </script>
1270
+ <script type="application/ld+json">
1271
+ {
1272
+ "@context": {"ex": "http://example.com/"},
1273
+ "@graph": [
1274
+ {"ex:foo": {"@value": "foo"}},
1275
+ {"ex:bar": {"@value": "bar"}}
1276
+ ]
1277
+ }
1278
+ </script>
1279
+ </head>
1280
+ </html>),
1281
+ output: %([ <http://example.com/foo> ( "bar")] .),
1282
+ extractAllScripts: false
1283
+ },
1284
+ "Transforms targeted script element": {
1285
+ input: %(
1286
+ <html>
1287
+ <head>
1288
+ <script id="first" type="application/ld+json">
1289
+ {
1290
+ "@context": {
1291
+ "foo": {"@id": "http://example.com/foo", "@container": "@list"}
1292
+ },
1293
+ "foo": [{"@value": "bar"}]
1294
+ }
1295
+ </script>
1296
+ <script id="second" type="application/ld+json">
1297
+ {
1298
+ "@context": {"ex": "http://example.com/"},
1299
+ "@graph": [
1300
+ {"ex:foo": {"@value": "foo"}},
1301
+ {"ex:bar": {"@value": "bar"}}
1302
+ ]
1303
+ }
1304
+ </script>
1305
+ </head>
1306
+ </html>),
1307
+ output: %(
1308
+ [ <http://example.com/foo> "foo"] .
1309
+ [ <http://example.com/bar> "bar"] .
1310
+ ),
1311
+ base: "http://example.org/doc#second"
1312
+ },
1313
+ }.each do |title, params|
1314
+ it(title) do
1315
+ params[:input] = StringIO.new(params[:input])
1316
+ params[:input].send(:define_singleton_method, :content_type) {"text/html"}
1317
+ run_to_rdf params.merge(validate: true)
732
1318
  end
733
1319
  end
734
1320
  end
735
1321
 
736
- def parse(input, options = {})
1322
+ def parse(input, **options)
737
1323
  graph = options[:graph] || RDF::Graph.new
738
1324
  options = {logger: logger, validate: true, canonicalize: false}.merge(options)
739
- JSON::LD::API.toRdf(StringIO.new(input), options) {|st| graph << st}
1325
+ JSON::LD::API.toRdf(StringIO.new(input), **options) {|st| graph << st}
740
1326
  graph
741
1327
  end
742
1328
 
743
1329
  def run_to_rdf(params)
744
- input, output, processingMode = params[:input], params[:output], params[:processingMode]
1330
+ input, output = params[:input], params[:output]
745
1331
  graph = params[:graph] || RDF::Graph.new
746
1332
  input = StringIO.new(input) if input.is_a?(String)
747
1333
  pending params.fetch(:pending, "test implementation") unless input
748
1334
  if params[:exception]
749
- expect {JSON::LD::API.toRdf(input, {processingMode: processingMode}.merge(params))}.to raise_error(params[:exception])
1335
+ expect {JSON::LD::API.toRdf(input, **params)}.to raise_error(params[:exception])
750
1336
  else
751
- JSON::LD::API.toRdf(input, base: params[:base], logger: logger, processingMode: processingMode) {|st| graph << st}
1337
+ if params[:write]
1338
+ expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, **params) {|st| graph << st}}.to write(params[:write]).to(:error)
1339
+ else
1340
+ expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, **params) {|st| graph << st}}.not_to write.to(:error)
1341
+ end
752
1342
  expect(graph).to be_equivalent_graph(output, logger: logger, inputDocument: input)
753
1343
  end
754
1344
  end