yaml-ld 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/psych/amazing_print.rb +61 -0
  4. data/lib/yaml_ld/api.rb +69 -11
  5. data/lib/yaml_ld/format.rb +25 -0
  6. data/lib/yaml_ld/reader.rb +3 -1
  7. data/lib/yaml_ld/representation.rb +256 -0
  8. data/lib/yaml_ld/version.rb +1 -1
  9. data/lib/yaml_ld.rb +1 -0
  10. data/spec/api_spec.rb +21 -27
  11. data/spec/compact_spec.rb +1 -1
  12. data/spec/expand_spec.rb +1 -1
  13. data/spec/format_spec.rb +56 -0
  14. data/spec/frame_spec.rb +55 -110
  15. data/spec/from_rdf_spec.rb +3 -3
  16. data/spec/matchers.rb +2 -2
  17. data/spec/reader_spec.rb +1 -1
  18. data/spec/representation_spec.rb +170 -0
  19. data/spec/spec_helper.rb +1 -0
  20. data/spec/test-files/test-1-compacted.yamlld +9 -0
  21. data/spec/test-files/test-1-context.yamlld +6 -0
  22. data/spec/test-files/test-1-expanded.yamlld +8 -0
  23. data/spec/test-files/test-1-input.yamlld +1 -0
  24. data/spec/test-files/test-2-compacted.yamlld +17 -0
  25. data/spec/test-files/test-2-context.yamlld +5 -0
  26. data/spec/test-files/test-2-expanded.yamlld +21 -0
  27. data/spec/test-files/test-3-compacted.yamlld +14 -0
  28. data/spec/test-files/test-3-context.yamlld +11 -0
  29. data/spec/test-files/test-3-expanded.yamlld +9 -0
  30. data/spec/test-files/test-4-compacted.yamlld +10 -0
  31. data/spec/test-files/test-4-context.yamlld +5 -0
  32. data/spec/test-files/test-4-expanded.yamlld +9 -0
  33. data/spec/test-files/test-5-compacted.yamlld +11 -0
  34. data/spec/test-files/test-5-context.yamlld +5 -0
  35. data/spec/test-files/test-5-expanded.yamlld +12 -0
  36. data/spec/test-files/test-6-compacted.yamlld +13 -0
  37. data/spec/test-files/test-6-context.yamlld +5 -0
  38. data/spec/test-files/test-6-expanded.yamlld +12 -0
  39. data/spec/test-files/test-7-compacted.yamlld +17 -0
  40. data/spec/test-files/test-7-context.yamlld +4 -0
  41. data/spec/test-files/test-7-expanded.yamlld +23 -0
  42. data/spec/test-files/test-8-compacted.yamlld +25 -0
  43. data/spec/test-files/test-8-context.yamlld +12 -0
  44. data/spec/test-files/test-8-expanded.yamlld +23 -0
  45. data/spec/test-files/test-8-frame.yamlld +15 -0
  46. data/spec/test-files/test-8-framed.yamlld +22 -0
  47. data/spec/test-files/test-9-compacted.yamlld +31 -0
  48. data/spec/test-files/test-9-context.yamlld +20 -0
  49. data/spec/test-files/test-9-expanded.yamlld +17 -0
  50. data/spec/to_rdf_spec.rb +10 -8
  51. metadata +89 -61
  52. data/spec/test-files/test-1-expanded.jsonld +0 -5
  53. data/spec/test-files/test-2-compacted.jsonld +0 -20
  54. data/spec/test-files/test-2-context.jsonld +0 -7
  55. data/spec/test-files/test-2-expanded.jsonld +0 -16
  56. data/spec/test-files/test-3-compacted.jsonld +0 -11
  57. data/spec/test-files/test-3-context.jsonld +0 -8
  58. data/spec/test-files/test-3-expanded.jsonld +0 -10
  59. data/spec/test-files/test-4-compacted.jsonld +0 -10
  60. data/spec/test-files/test-4-context.jsonld +0 -7
  61. data/spec/test-files/test-4-expanded.jsonld +0 -6
  62. data/spec/test-files/test-5-compacted.jsonld +0 -13
  63. data/spec/test-files/test-5-context.jsonld +0 -7
  64. data/spec/test-files/test-5-expanded.jsonld +0 -9
  65. data/spec/test-files/test-6-compacted.jsonld +0 -10
  66. data/spec/test-files/test-6-context.jsonld +0 -7
  67. data/spec/test-files/test-6-expanded.jsonld +0 -10
  68. data/spec/test-files/test-7-compacted.jsonld +0 -23
  69. data/spec/test-files/test-7-context.jsonld +0 -4
  70. data/spec/test-files/test-7-expanded.jsonld +0 -20
  71. data/spec/test-files/test-8-compacted.jsonld +0 -34
  72. data/spec/test-files/test-8-context.jsonld +0 -11
  73. data/spec/test-files/test-8-expanded.jsonld +0 -24
  74. data/spec/test-files/test-8-frame.jsonld +0 -18
  75. data/spec/test-files/test-8-framed.jsonld +0 -25
  76. data/spec/test-files/test-9-compacted.jsonld +0 -20
  77. data/spec/test-files/test-9-context.jsonld +0 -13
  78. data/spec/test-files/test-9-expanded.jsonld +0 -14
data/spec/api_spec.rb CHANGED
@@ -10,12 +10,11 @@ describe YAML_LD::API do
10
10
  %i(psych).each do |adapter|
11
11
  Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename|
12
12
  test = File.basename(filename).sub(/-input\..*$/, '')
13
- frame = filename.sub(/-input\..*$/, '-frame.jsonld')
14
- framed = filename.sub(/-input\..*$/, '-framed.jsonld')
15
- compacted = filename.sub(/-input\..*$/, '-compacted.jsonld')
16
- context = filename.sub(/-input\..*$/, '-context.jsonld')
17
- expanded = filename.sub(/-input\..*$/, '-expanded.jsonld')
18
- expanded_yaml = filename.sub(/-input\..*$/, '-expanded.yamlld')
13
+ frame = filename.sub(/-input\..*$/, '-frame.yamlld')
14
+ framed = filename.sub(/-input\..*$/, '-framed.yamlld')
15
+ compacted = filename.sub(/-input\..*$/, '-compacted.yamlld')
16
+ context = filename.sub(/-input\..*$/, '-context.yamlld')
17
+ expanded = filename.sub(/-input\..*$/, '-expanded.yamlld')
19
18
  ttl = filename.sub(/-input\..*$/, '-rdf.ttl')
20
19
 
21
20
  context test do
@@ -27,7 +26,7 @@ describe YAML_LD::API do
27
26
  when /.jsonld$/
28
27
  @file.define_singleton_method(:content_type) {'application/ld+json'}
29
28
  end
30
- if context
29
+ if File.exist?(context)
31
30
  @ctx_io = File.open(context)
32
31
  case context
33
32
  when /\.yamlld$/
@@ -36,9 +35,19 @@ describe YAML_LD::API do
36
35
  @ctx_io.define_singleton_method(:content_type) {'application/ld+json'}
37
36
  end
38
37
  end
38
+ if File.exist?(frame)
39
+ @frame_io = File.open(frame)
40
+ case frame
41
+ when /\.yamlld$/
42
+ @frame_io.define_singleton_method(:content_type) {'application/ld+yaml'}
43
+ when /.jsonld$/
44
+ @frame_io.define_singleton_method(:content_type) {'application/ld+json'}
45
+ end
46
+ end
39
47
  example.run
40
48
  @file.close
41
49
  @ctx_io.close if @ctx_io
50
+ @frame_io.close if @frame_io
42
51
  end
43
52
 
44
53
  if File.exist?(expanded)
@@ -47,18 +56,7 @@ describe YAML_LD::API do
47
56
  options[:expandContext] = @ctx_io if context
48
57
  yaml = described_class.expand(@file, **options)
49
58
  expect(yaml).to be_a(String)
50
- parsed_json = JSON.parse(File.read(expanded))
51
- expect(yaml).to produce_yamlld(parsed_json, logger)
52
- end
53
- end
54
-
55
- if File.exist?(expanded_yaml)
56
- it "expands to YAML" do
57
- options = {logger: logger, adapter: adapter}
58
- options[:expandContext] = @ctx_io if context
59
- yaml = described_class.expand(@file, **options)
60
- expect(yaml).to be_a(String)
61
- expect(yaml).to produce_yamlld(YAML.load_file(expanded_yaml), logger)
59
+ expect(yaml).to produce_yamlld(File.read(expanded), logger)
62
60
  end
63
61
  end
64
62
 
@@ -66,19 +64,15 @@ describe YAML_LD::API do
66
64
  it "compacts" do
67
65
  yaml = described_class.compact(@file, @ctx_io, adapter: adapter, logger: logger)
68
66
  expect(yaml).to be_a(String)
69
- parsed_json = JSON.parse(File.read(compacted))
70
- expect(yaml).to produce_yamlld(parsed_json, logger)
67
+ expect(yaml).to produce_yamlld(File.read(compacted), logger)
71
68
  end
72
69
  end
73
70
 
74
71
  if File.exist?(framed) && File.exist?(frame)
75
72
  it "frames" do
76
- File.open(frame) do |frame_io|
77
- yaml = described_class.frame(@file, frame_io, adapter: adapter, logger: logger)
78
- expect(yaml).to be_a(String)
79
- parsed_json = JSON.parse(File.read(framed))
80
- expect(yaml).to produce_yamlld(parsed_json, logger)
81
- end
73
+ yaml = described_class.frame(@file, @frame_io, adapter: adapter, logger: logger)
74
+ expect(yaml).to be_a(String)
75
+ expect(yaml).to produce_yamlld(File.read(framed), logger)
82
76
  end
83
77
  end
84
78
 
data/spec/compact_spec.rb CHANGED
@@ -328,7 +328,7 @@ describe YAML_LD::API do
328
328
  )
329
329
  },
330
330
  }.each do |title, params|
331
- it(title) {run_compact(processingMode: 'json-ld-1.1', **params)}
331
+ it(title) {run_compact(**params)}
332
332
  end
333
333
  end
334
334
  end
data/spec/expand_spec.rb CHANGED
@@ -118,7 +118,7 @@ describe JSON::LD::API do
118
118
  output: %(
119
119
  - http://example.com/p:
120
120
  - "@id": http://example.com/Sub1
121
- }])
121
+ )
122
122
  },
123
123
  }.each_pair do |title, params|
124
124
  it(title) {run_expand params}
data/spec/format_spec.rb CHANGED
@@ -51,4 +51,60 @@ describe YAML_LD::Format do
51
51
  describe "#to_uri" do
52
52
  specify {expect(described_class.to_uri).to eq RDF::URI('http://www.w3.org/ns/formats/YAML-LD')}
53
53
  end
54
+
55
+ describe ".cli_commands", skip: Gem.win_platform? do
56
+ require 'rdf/cli'
57
+ let(:ttl) {File.expand_path("../test-files/test-1-rdf.ttl", __FILE__)}
58
+ let(:yaml) {File.expand_path("../test-files/test-1-input.yamlld", __FILE__)}
59
+ let(:json) {File.expand_path("../test-files/test-1-compacted.jsonld", __FILE__)}
60
+ let(:context) {File.expand_path("../test-files/test-1-context.jsonld", __FILE__)}
61
+
62
+ describe "#expand" do
63
+ it "expands RDF" do
64
+ expect {RDF::CLI.exec(["expand", ttl], format: :ttl, output_format: :yamlld)}.to write.to(:output)
65
+ end
66
+ it "expands JSON" do
67
+ expect {RDF::CLI.exec(["expand", json], format: :jsonld, output_format: :yamlld, validate: false)}.to write.to(:output)
68
+ end
69
+ it "expands YAML" do
70
+ expect {RDF::CLI.exec(["expand", yaml], format: :yamlld, output_format: :yamlld, validate: false)}.to write.to(:output)
71
+ end
72
+ end
73
+
74
+ describe "#compact" do
75
+ it "compacts RDF" do
76
+ expect {RDF::CLI.exec(["compact", ttl], context: context, format: :ttl, output_format: :yamlld, validate: false)}.to write.to(:output)
77
+ end
78
+ it "compacts JSON" do
79
+ expect {RDF::CLI.exec(["compact", json], context: context, format: :jsonld, output_format: :yamlld, validate: false)}.to write.to(:output)
80
+ end
81
+ it "compacts YAML" do
82
+ expect {RDF::CLI.exec(["compact", yaml], context: context, format: :yamlld, output_format: :yamlld, validate: false)}.to write.to(:output)
83
+ end
84
+ end
85
+
86
+ describe "#flatten" do
87
+ it "flattens RDF" do
88
+ expect {RDF::CLI.exec(["flatten", ttl], context: context, format: :ttl, output_format: :yamlld, validate: false)}.to write.to(:output)
89
+ end
90
+ it "flattens JSON" do
91
+ expect {RDF::CLI.exec(["flatten", json], context: context, format: :jsonld, output_format: :yamlld, validate: false)}.to write.to(:output)
92
+ end
93
+ it "flattens YAML" do
94
+ expect {RDF::CLI.exec(["flatten", yaml], context: context, format: :yamlld, output_format: :yamlld, validate: false)}.to write.to(:output)
95
+ end
96
+ end
97
+
98
+ describe "#frame" do
99
+ it "frames RDF" do
100
+ expect {RDF::CLI.exec(["frame", ttl], frame: context, format: :ttl, output_format: :yamlld)}.to write.to(:output)
101
+ end
102
+ it "frames JSON" do
103
+ expect {RDF::CLI.exec(["frame", json], frame: context, format: :jsonld, output_format: :yamlld, validate: false)}.to write.to(:output)
104
+ end
105
+ it "frames YAML" do
106
+ expect {RDF::CLI.exec(["frame", yaml], frame: context, format: :yamlld, output_format: :yamlld, validate: false)}.to write.to(:output)
107
+ end
108
+ end
109
+ end
54
110
  end
data/spec/frame_spec.rb CHANGED
@@ -22,12 +22,11 @@ describe YAML_LD::API do
22
22
  "@id": ex:Sub2
23
23
  "@type": ex:Type2
24
24
  ),
25
- output: %(---
25
+ output: %(
26
26
  "@context":
27
27
  ex: http://example.org/
28
- "@graph":
29
- - "@id": ex:Sub1
30
- "@type": ex:Type1
28
+ "@id": ex:Sub1
29
+ "@type": ex:Type1
31
30
  )
32
31
  },
33
32
  "wildcard @type match": {
@@ -76,9 +75,8 @@ describe YAML_LD::API do
76
75
  output: %(
77
76
  "@context":
78
77
  ex: http://example.org/
79
- "@graph":
80
- - "@id": ex:Sub2
81
- ex:p: Bar
78
+ "@id": ex:Sub2
79
+ ex:p: Bar
82
80
  )
83
81
  },
84
82
  "multiple matches on @type": {
@@ -136,9 +134,8 @@ describe YAML_LD::API do
136
134
  output: %(
137
135
  "@context":
138
136
  ex: http://example.org/
139
- "@graph":
140
- - "@id": ex:Sub1
141
- "@type": ex:Type1
137
+ "@id": ex:Sub1
138
+ "@type": ex:Type1
142
139
  )
143
140
  },
144
141
  "multiple @id match": {
@@ -194,10 +191,9 @@ describe YAML_LD::API do
194
191
  output: %(
195
192
  "@context":
196
193
  ex: http://example.org/
197
- "@graph":
198
- - "@id": ex:Sub1
199
- ex:p:
200
- ex:q: bar
194
+ "@id": ex:Sub1
195
+ ex:p:
196
+ ex:q: bar
201
197
  )
202
198
  },
203
199
  "match on any property if @requireAll is false": {
@@ -308,8 +304,7 @@ describe YAML_LD::API do
308
304
  familyName: Doe
309
305
  givenName: John
310
306
  name: John Doe
311
- ),
312
- processingMode: 'json-ld-1.1'
307
+ )
313
308
  },
314
309
  "mixed content": {
315
310
  frame: %(
@@ -329,11 +324,10 @@ describe YAML_LD::API do
329
324
  output: %(
330
325
  "@context":
331
326
  ex: http://example.org/
332
- "@graph":
333
- - "@id": ex:Sub1
334
- ex:mixed:
335
- - "@id": ex:Sub2
336
- - literal1
327
+ "@id": ex:Sub1
328
+ ex:mixed:
329
+ - "@id": ex:Sub2
330
+ - literal1
337
331
  )
338
332
  },
339
333
  "framed list": {
@@ -365,13 +359,12 @@ describe YAML_LD::API do
365
359
  list:
366
360
  "@id": ex:list
367
361
  "@container": "@list"
368
- "@graph":
369
- - "@id": ex:Sub1
370
- "@type": ex:Type1
371
- list:
372
- - "@id": ex:Sub2
373
- "@type": ex:Element
374
- - literal1
362
+ "@id": ex:Sub1
363
+ "@type": ex:Type1
364
+ list:
365
+ - "@id": ex:Sub2
366
+ "@type": ex:Element
367
+ - literal1
375
368
  )
376
369
  },
377
370
  "presentation example": {
@@ -410,12 +403,11 @@ describe YAML_LD::API do
410
403
  sameAs:
411
404
  "@id": http://www.w3.org/2002/07/owl#sameAs
412
405
  "@type": "@id"
413
- "@graph":
414
- - "@id": http://en.wikipedia.org/wiki/Linked_Data
415
- primaryTopic:
416
- "@id": http://dbpedia.org/resource/Linked_Data
417
- "@type": http://dbpedia.org/class/yago/Buzzwords
418
- sameAs: http://rdf.freebase.com/ns/m/02r2kb1
406
+ "@id": http://en.wikipedia.org/wiki/Linked_Data
407
+ primaryTopic:
408
+ "@id": http://dbpedia.org/resource/Linked_Data
409
+ "@type": http://dbpedia.org/class/yago/Buzzwords
410
+ sameAs: http://rdf.freebase.com/ns/m/02r2kb1
419
411
  )
420
412
  },
421
413
  "library": {
@@ -455,20 +447,19 @@ describe YAML_LD::API do
455
447
  xsd: http://www.w3.org/2001/XMLSchema#
456
448
  ex:contains:
457
449
  "@type": "@id"
458
- "@graph":
459
- - "@id": http://example.org/library
460
- "@type": ex:Library
461
- dc:name: Library
450
+ "@id": http://example.org/library
451
+ "@type": ex:Library
452
+ dc:name: Library
453
+ ex:contains:
454
+ "@id": http://example.org/library/the-republic
455
+ "@type": ex:Book
456
+ dc:creator: Plato
457
+ dc:title: The Republic
462
458
  ex:contains:
463
- "@id": http://example.org/library/the-republic
464
- "@type": ex:Book
465
- dc:creator: Plato
466
- dc:title: The Republic
467
- ex:contains:
468
- "@id": http://example.org/library/the-republic#introduction
469
- "@type": ex:Chapter
470
- dc:description: An introductory chapter on The Republic.
471
- dc:title: The Introduction
459
+ "@id": http://example.org/library/the-republic#introduction
460
+ "@type": ex:Chapter
461
+ dc:description: An introductory chapter on The Republic.
462
+ dc:title: The Introduction
472
463
  )
473
464
  }
474
465
  }.each do |title, params|
@@ -502,15 +493,14 @@ describe YAML_LD::API do
502
493
  output: %(
503
494
  "@context":
504
495
  ex: http://example.org/
505
- "@graph":
506
- - "@id": ex:Sub1
507
- "@type": ex:Type1
508
- "@reverse":
496
+ "@id": ex:Sub1
497
+ "@type": ex:Type1
498
+ "@reverse":
499
+ ex:includes:
500
+ "@id": ex:Sub2
501
+ "@type": ex:Type2
509
502
  ex:includes:
510
- "@id": ex:Sub2
511
- "@type": ex:Type2
512
- ex:includes:
513
- "@id": ex:Sub1
503
+ "@id": ex:Sub1
514
504
  )
515
505
  },
516
506
  "embed matched frames with reversed property": {
@@ -539,14 +529,13 @@ describe YAML_LD::API do
539
529
  ex: http://example.org/
540
530
  excludes:
541
531
  "@reverse": ex:includes
542
- "@graph":
543
- - "@id": ex:Sub1
544
- "@type": ex:Type1
545
- excludes:
546
- "@id": ex:Sub2
547
- "@type": ex:Type2
548
- ex:includes:
549
- "@id": ex:Sub1
532
+ "@id": ex:Sub1
533
+ "@type": ex:Type1
534
+ excludes:
535
+ "@id": ex:Sub2
536
+ "@type": ex:Type2
537
+ ex:includes:
538
+ "@id": ex:Sub1
550
539
  )
551
540
  },
552
541
  }.each do |title, params|
@@ -558,27 +547,7 @@ describe YAML_LD::API do
558
547
 
559
548
  context "omitGraph option" do
560
549
  {
561
- "Defaults to false in 1.0": {
562
- input: %(
563
- - http://example.org/prop:
564
- - "@value": value
565
- http://example.org/foo:
566
- - "@value": bar
567
- ),
568
- frame: %(
569
- "@context":
570
- "@vocab": http://example.org/
571
- ),
572
- output: %(
573
- "@context":
574
- "@vocab": http://example.org/
575
- "@graph":
576
- - foo: bar
577
- prop: value
578
- ),
579
- processingMode: "json-ld-1.0"
580
- },
581
- "Set with option in 1.0": {
550
+ "Defaults to true": {
582
551
  input: %(
583
552
  - http://example.org/prop:
584
553
  - "@value": value
@@ -594,30 +563,9 @@ describe YAML_LD::API do
594
563
  "@vocab": http://example.org/
595
564
  foo: bar
596
565
  prop: value
597
- ),
598
- processingMode: "json-ld-1.0",
599
- omitGraph: true
600
- },
601
- "Defaults to true in 1.1": {
602
- input: %(
603
- - http://example.org/prop:
604
- - "@value": value
605
- http://example.org/foo:
606
- - "@value": bar
607
- ),
608
- frame: %(
609
- "@context":
610
- "@vocab": http://example.org/
611
- ),
612
- output: %(
613
- "@context":
614
- "@vocab": http://example.org/
615
- foo: bar
616
- prop: value
617
- ),
618
- processingMode: "json-ld-1.1"
566
+ )
619
567
  },
620
- "Set with option in 1.1": {
568
+ "Set with option": {
621
569
  input: %(
622
570
  - http://example.org/prop:
623
571
  - "@value": value
@@ -635,7 +583,6 @@ describe YAML_LD::API do
635
583
  - foo: bar
636
584
  prop: value
637
585
  ),
638
- processingMode: "json-ld-1.1",
639
586
  omitGraph: false
640
587
  },
641
588
  }.each do |title, params|
@@ -643,11 +590,9 @@ describe YAML_LD::API do
643
590
  end
644
591
  end
645
592
  end
646
-
647
593
  def do_frame(params)
648
594
  begin
649
595
  input, frame, output = params[:input], params[:frame], params[:output]
650
- params = {processingMode: 'json-ld-1.0'}.merge(params)
651
596
  input = StringIO.new(input) if input.is_a?(String)
652
597
  frame = StringIO.new(frame) if frame.is_a?(String)
653
598
  yld = nil
@@ -296,7 +296,7 @@ describe YAML_LD::API do
296
296
  },
297
297
  }.each do |title, params|
298
298
  params[:input] = RDF::Graph.new << RDF::Turtle::Reader.new(params[:input])
299
- it(title) {do_fromRdf(processingMode: "json-ld-1.1", **params)}
299
+ it(title) {do_fromRdf(**params)}
300
300
  end
301
301
  end
302
302
  end
@@ -442,7 +442,7 @@ describe YAML_LD::API do
442
442
  }
443
443
  }.each_pair do |name, params|
444
444
  it name do
445
- do_fromRdf(params.merge(reader: RDF::Turtle::Reader, rdfDirection: 'i18n-datatype', processingMode: 'json-ld-1.1'))
445
+ do_fromRdf(params.merge(reader: RDF::Turtle::Reader, rdfDirection: 'i18n-datatype'))
446
446
  end
447
447
  end
448
448
  end
@@ -505,7 +505,7 @@ describe YAML_LD::API do
505
505
  }
506
506
  }.each_pair do |name, params|
507
507
  it name do
508
- do_fromRdf(params.merge(reader: RDF::Turtle::Reader, rdfDirection: 'compound-literal', processingMode: 'json-ld-1.1'))
508
+ do_fromRdf(params.merge(reader: RDF::Turtle::Reader, rdfDirection: 'compound-literal'))
509
509
  end
510
510
  end
511
511
  end
data/spec/matchers.rb CHANGED
@@ -3,8 +3,8 @@ require_relative 'support/extensions'
3
3
 
4
4
  RSpec::Matchers.define :produce_yamlld do |expected, logger|
5
5
  match do |actual|
6
- actual = Psych.load(actual, aliases: true) if actual.is_a?(String)
7
- expected = Psych.load(expected, aliases: true) if expected.is_a?(String)
6
+ actual = YAML_LD::Representation.load(actual, aliases: true) if actual.is_a?(String)
7
+ expected = YAML_LD::Representation.load(expected, aliases: true) if expected.is_a?(String)
8
8
  expect(actual).to be_equivalent_structure expected
9
9
  end
10
10
 
data/spec/reader_spec.rb CHANGED
@@ -58,7 +58,7 @@ describe YAML_LD::Reader do
58
58
  http://example.com/bob/
59
59
  "foaf:name":
60
60
  Bob
61
- }),
61
+ ),
62
62
  leading_comment: %q(---
63
63
  # A comment before content
64
64
  "@context":
@@ -0,0 +1,170 @@
1
+ # coding: utf-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe YAML_LD::Representation do
5
+ describe "load_stream" do
6
+ {
7
+ "Stream": {
8
+ input: %(
9
+ ---
10
+ a
11
+ ...
12
+ ---
13
+ b
14
+ ...
15
+ ),
16
+ expected: %w(a b)
17
+ },
18
+ "String": {
19
+ input: %(a),
20
+ expected: %w(a)
21
+ },
22
+ }.each do |name, params|
23
+ it name do
24
+ input = params[:input]
25
+ ir = YAML_LD::Representation.load_stream(input.unindent.strip)
26
+ expected = params[:expected]
27
+ expect(ir).to be_equivalent_structure expected
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "load" do
33
+ {
34
+ "Stream": {
35
+ input: %(
36
+ ---
37
+ a
38
+ ...
39
+ ---
40
+ b
41
+ ...
42
+ ),
43
+ expected: "a"
44
+ },
45
+ "Null": {
46
+ input: %(null),
47
+ expected: nil
48
+ },
49
+ "!!null null": {
50
+ input: %(!!null null),
51
+ expected: nil
52
+ },
53
+ "!<tag:yaml.org,2002:null> null": {
54
+ input: %(!<tag:yaml.org,2002:null> null),
55
+ expected: nil
56
+ },
57
+ "Boolean": {
58
+ input: %(true),
59
+ expected: true
60
+ },
61
+ "!!bool true": {
62
+ input: %(!!bool true),
63
+ expected: true
64
+ },
65
+ "!<tag:yaml.org,2002:bool> true": {
66
+ input: %(!<tag:yaml.org,2002:bool> true),
67
+ expected: true
68
+ },
69
+ "String": {
70
+ input: %(a),
71
+ expected: "a"
72
+ },
73
+ "Tagged !!str String": {
74
+ input: %(!!str string),
75
+ expected: "string"
76
+ },
77
+ "Tagged !<tag:yaml.org,2002:str> String": {
78
+ input: %(!<tag:yaml.org,2002:str> string),
79
+ expected: "string"
80
+ },
81
+ "Integer": {
82
+ input: %(1),
83
+ expected: 1
84
+ },
85
+ "Tagged !!int 1": {
86
+ input: %(!!int 1),
87
+ expected: 1
88
+ },
89
+ "Tagged !<tag:yaml.org,2002:int> 1": {
90
+ input: %(!<tag:yaml.org,2002:int> 1),
91
+ expected: 1
92
+ },
93
+ "Float": {
94
+ input: %(1.0),
95
+ expected: Float(1.0)
96
+ },
97
+ "Tagged !!float -1": {
98
+ input: %(!!float -1),
99
+ expected: Float(-1)
100
+ },
101
+ "Tagged !<tag:yaml.org,2002:float> 2.3e4": {
102
+ input: %(!<tag:yaml.org,2002:float> 2.3e4),
103
+ expected: Float(2.3e4)
104
+ },
105
+ "Tagged !<tag:yaml.org,2002:float> .inf": {
106
+ input: %(!<tag:yaml.org,2002:float> .inf),
107
+ expected: Float::INFINITY
108
+ },
109
+ }.each do |name, params|
110
+ it name do
111
+ input = params[:input]
112
+ ir = YAML_LD::Representation.load(input.unindent.strip)
113
+ expected = params[:expected]
114
+ expect(ir).to be_equivalent_structure expected
115
+ end
116
+ end
117
+
118
+ {
119
+ "!<http://www.w3.org/2001/XMLSchema%23integer> 123": {
120
+ input: %(!<http://www.w3.org/2001/XMLSchema%23integer> 123),
121
+ xsd: RDF::Literal("123", datatype: "http://www.w3.org/2001/XMLSchema#integer"),
122
+ plain: 123
123
+ },
124
+ "!<http://www.w3.org/2001/XMLSchema%23decimal> 123.456": {
125
+ input: %(!<http://www.w3.org/2001/XMLSchema%23decimal> 123.456),
126
+ xsd: RDF::Literal("123.456", datatype: "http://www.w3.org/2001/XMLSchema#decimal"),
127
+ plain: 123.456
128
+ },
129
+ "!<http://www.w3.org/2001/XMLSchema%23double> 123.456e78": {
130
+ input: %(!<http://www.w3.org/2001/XMLSchema%23double> 123.456e+78),
131
+ xsd: RDF::Literal("123.456e+78", datatype: "http://www.w3.org/2001/XMLSchema#double"),
132
+ plain: 123.456e+78
133
+ },
134
+ "!<http://www.w3.org/2001/XMLSchema%23boolean> true": {
135
+ input: %(!<http://www.w3.org/2001/XMLSchema%23boolean> true),
136
+ xsd: RDF::Literal("true", datatype: "http://www.w3.org/2001/XMLSchema#boolean"),
137
+ plain: true
138
+ },
139
+ "!<http://www.w3.org/2001/XMLSchema%23date> 2022-08-17": {
140
+ input: %(!<http://www.w3.org/2001/XMLSchema%23date> "2022-08-17"),
141
+ xsd: RDF::Literal("2022-08-17", datatype: "http://www.w3.org/2001/XMLSchema#date"),
142
+ plain: "2022-08-17"
143
+ },
144
+ "!<http://www.w3.org/2001/XMLSchema%23time> 12:00:00.000": {
145
+ input: %(!<http://www.w3.org/2001/XMLSchema%23time> "12:00:00.000"),
146
+ xsd: RDF::Literal("12:00:00.000", datatype: "http://www.w3.org/2001/XMLSchema#time"),
147
+ plain: "12:00:00.000"
148
+ },
149
+ "!<http://www.w3.org/2001/XMLSchema%23dateTime> 2022-08-17T12:00:00.000": {
150
+ input: %(!<http://www.w3.org/2001/XMLSchema%23dateTime> "2022-08-17T12:00:00.000"),
151
+ xsd: RDF::Literal("2022-08-17T12:00:00.000", datatype: "http://www.w3.org/2001/XMLSchema#dateTime"),
152
+ plain: "2022-08-17T12:00:00.000"
153
+ },
154
+ }.each do |name, params|
155
+ it "#{name} with xsd" do
156
+ input = params[:input]
157
+ ir = YAML_LD::Representation.load(input.unindent.strip, extendedYAML: true)
158
+ expected = params[:xsd]
159
+ expect(ir).to be_equivalent_structure expected
160
+ end
161
+
162
+ it "#{name} without xsd" do
163
+ input = params[:input]
164
+ ir = YAML_LD::Representation.load(input.unindent.strip, extendedYAML: false)
165
+ expected = params[:plain]
166
+ expect(ir).to be_equivalent_structure expected
167
+ end
168
+ end
169
+ end
170
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ require 'rdf/vocab'
12
12
  require 'rdf/spec'
13
13
  require 'rdf/spec/matchers'
14
14
  require_relative 'matchers'
15
+ require_relative '../lib/psych/amazing_print'
15
16
  require 'yaml'
16
17
  begin
17
18
  require 'simplecov'