-
Notifications
You must be signed in to change notification settings - Fork 98
/
ntriples_spec.rb
1167 lines (1063 loc) · 47.3 KB
/
ntriples_spec.rb
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- encoding: utf-8 -*-
require_relative 'spec_helper'
require 'rdf/ntriples'
require 'rdf/spec/format'
require 'rdf/spec/reader'
require 'rdf/spec/writer'
require 'rdf/turtle'
describe RDF::NTriples::Format do
# @see lib/rdf/spec/format.rb in rdf-spec
it_behaves_like 'an RDF::Format' do
let(:format_class) { described_class }
end
# @see lib/rdf/spec/format.rb in rdf-spec
it_behaves_like 'an RDF::Format' do
let(:format_class) { described_class }
end
subject { described_class }
describe ".for" do
[
:ntriples,
'etc/doap.nt',
{file_name: 'etc/doap.nt'},
{file_extension: 'nt'},
{content_type: 'application/n-triples'},
{content_type: 'text/plain'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Format.for(arg)).to eq subject
end
end
{
ntriples: "<a> <b> <c> .",
literal: '<a> <b> "literal" .',
bnode: %(<a> <b> _:c .),
}.each do |sym, str|
it "detects #{sym}" do
expect(subject.detect(str)).to be_truthy
end
end
end
describe "#to_sym" do
specify {expect(subject.to_sym).to eq :ntriples}
end
describe "#name" do
specify {expect(subject.name).to eq "N-Triples"}
end
describe ".detect" do
{
ntriples: "<a> <b> <c> .",
literal: '<a> <b> "literal" .',
multi_line: %(<a>\n <b>\n "literal"\n .),
}.each do |sym, str|
it "detects #{sym}" do
expect(subject.detect(str)).to be_truthy
end
end
{
nquads: "<a> <b> <c> <d> . ",
nq_literal: '<a> <b> "literal" <d> .',
nq_multi_line: %(<a>\n <b>\n "literal"\n <d>\n .),
turtle: "@prefix foo: <bar> .\n foo:a foo:b <c> .",
trig: "{<a> <b> <c> .}",
rdfxml: '<rdf:RDF about="foo"></rdf:RDF>',
n3: '@prefix foo: <bar> .\nfoo:bar = {<a> <b> <c>} .',
}.each do |sym, str|
it "does not detect #{sym}" do
expect(subject.detect(str)).to be_falsey
end
end
end
end
describe RDF::NTriples::Reader do
let(:logger) {RDF::Spec.logger}
let(:testfile) {fixture_path('test.nt')}
let(:reader) {RDF::NTriples::Reader}
let(:writer) {RDF::NTriples::Writer}
let!(:doap) {File.expand_path("../../etc/doap.nt", __FILE__)}
let!(:doap_count) {File.open(doap).each_line.to_a.length}
subject { RDF::NTriples::Reader.new }
# @see lib/rdf/spec/reader.rb in rdf-spec
it_behaves_like 'an RDF::Reader' do
let(:reader) { RDF::NTriples::Reader.new }
let(:reader_input) { File.read(doap) }
let(:reader_count) { doap_count }
end
describe ".for" do
[
:ntriples,
'etc/doap.nt',
{file_name: 'etc/doap.nt'},
{file_extension: 'nt'},
{content_type: 'application/n-triples'},
{content_type: 'text/plain'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Reader.for(arg)).to eq described_class
end
end
context "content_type text/plain with non-N-Triples content" do
{
:nquads => "<a> <b> <c> <d> . ",
:nq_literal => '<a> <b> "literal" <d> .',
:nq_multi_line => %(<a>\n <b>\n "literal"\n <d>\n .),
}.each do |sym, str|
it "does not detect #{sym}" do
f = RDF::Reader.for(:content_type => "text/plain", :sample => str.freeze)
expect(f).not_to eq described_class
end
end
end
end
it "should return :ntriples for to_sym" do
expect(subject.class.to_sym).to eq :ntriples
expect(subject.to_sym).to eq :ntriples
end
describe ".initialize" do
it "reads doap string" do
g = RDF::Graph.new << described_class.new(File.read(doap))
expect(g.count).to eq doap_count
end
it "reads doap IO" do
g = RDF::Graph.new
described_class.new(File.open(doap)) do |r|
g << r
end
expect(g.count).to eq doap_count
end
it "should accept files" do
expect { reader.new(File.open(testfile)) }.not_to raise_error
end
it "should accept IO streams" do
expect { reader.new(StringIO.new('')) }.not_to raise_error
end
it "should accept strings" do
expect { reader.new('') }.not_to raise_error
end
end
describe ".open" do
it "reads doap string" do
g = RDF::Graph.new
described_class.open(doap) do |r|
g << r
end
expect(g.count).to eq doap_count
end
end
context "when decoding text" do
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly unescape ASCII characters (#x0-#x7F)" do
(0x00..0x7F).each do |u|
expect(reader.unescape(writer.escape(u.chr))).to eq u.chr
end
end
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly unescape Unicode characters (#x80-#x10FFFF)" do
(0x7F..0xFFFF).to_a.sample(100).each do |u|
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(reader.unescape(writer.escape(c))).to eq c
rescue RangeError
end
end
(0x10000..0x2FFFF).to_a.sample(100).each do |u| # NB: there's nothing much beyond U+2FFFF
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(reader.unescape(writer.escape(c))).to eq c
rescue RangeError
end
end
end
context "unescape Unicode strings" do
strings = {
"\u677E\u672C \u540E\u5B50" => "松本 后子",
"D\u00FCrst" => "Dürst",
"\\U00015678another" => "\u{15678}another",
}
strings.each do |string, unescaped|
specify string do
unescaped = unescaped.encode(Encoding::UTF_8)
expect(reader.unescape(string)).to eq unescaped
end
end
end
context "unescape escaped Unicode strings" do
strings = {
# U+221E, infinity symbol
"_\\u221E_" => "_\xE2\x88\x9E_",
# U+6C34, 'water' in Chinese
"_\\u6C34_" => "_\xE6\xB0\xB4_",
"\\u677E\\u672C \\u540E\\u5B50"=> "松本 后子",
"D\\u00FCrst" => "Dürst",
"\\u0039" => "9",
"\\\\u0039" => "\\u0039",
}
strings.each do |string, unescaped|
specify string do
unescaped = unescaped.encode(Encoding::UTF_8)
expect(reader.unescape(string.freeze)).to eq unescaped
end
end
end
end
context "comments" do
{
%(\n) => %(),
%(\r\n) => %(),
%(\r) => %q(),
%(#\n) => %q(),
%(# \n) => %q(),
%(# <http://example/a> <http://example/b> <http://example/c> .\n) => %q(),
%(\t#\n) => %q(),
%( #\n) => %q(),
%(<http://example/a> <http://example/b> <http://example/c> . # comment\n) =>
%q(<http://example/a> <http://example/b> <http://example/c> .)
}.each_pair do |input, output|
it "for #{input.inspect}" do
expect(parse(input, validate: true).dump(:ntriples)).to eq parse(output).dump(:ntriples)
end
end
end
it "should parse W3C's test data" do
gn = RDF::Graph.new {|g| reader.new(File.open(testfile)) {|r| g << r}}
gt = RDF::Graph.new {|g| RDF::Turtle::Reader.new(File.open(testfile)) {|r| g << r}}
expect(gn).to be_equivalent_graph(gt)
expect(gn.to_a.size).to eq 31
end
it "should parse terms" do
bnode = reader.unserialize('_:foobar')
expect(bnode).not_to be_nil
expect(bnode).to be_a_node
expect(bnode.id).to eq 'foobar'
uri = reader.unserialize('<http://ar.to/#self>'.freeze)
expect(uri).not_to be_nil
expect(uri).to be_a_uri
expect(uri.to_s).to eq 'http://ar.to/#self'
hello = reader.unserialize('"Hello"')
expect(hello).not_to be_nil
expect(hello).to be_a_literal
expect(hello.value).to eq 'Hello'
stmt = reader.unserialize("<https://rubygems.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> .".freeze)
expect(stmt).not_to be_nil
expect(stmt).to be_a_statement
end
describe "BNodes" do
it "should read two named nodes as the same node" do
stmt = reader.new("_:a <http://www.w3.org/2002/07/owl#sameAs> _:a .".freeze).first
expect(stmt.subject).to eq stmt.object
expect(stmt.subject).to be_eql(stmt.object)
end
it "should read two named nodes in different instances as different nodes" do
stmt1 = reader.new("_:a <http://www.w3.org/2002/07/owl#sameAs> _:a .".freeze).first
stmt2 = reader.new("_:a <http://www.w3.org/2002/07/owl#sameAs> _:a .".freeze).first
expect(stmt1.subject).to eq stmt2.subject
expect(stmt1.subject).not_to be_eql(stmt2.subject)
end
end
describe "literal encodings" do
{
'Dürst' => '_:a <http://pred> "D\u00FCrst" .',
'simple literal' => '<http://subj> <http://pred> "simple literal" .',
'backslash:\\' => '<http://subj> <http://pred> "backslash:\\\\" .',
'squote:\'' => '<http://subj> <http://pred> "squote:\'" .',
'dquote:"' => '<http://subj> <http://pred> "dquote:\"" .',
"newline:\n" => '<http://subj> <http://pred> "newline:\n" .',
"return\r" => '<http://subj> <http://pred> "return\r" .',
"tab:\t" => '<http://subj> <http://pred> "tab:\t" .',
"form feed:\f" => '<http://subj> <http://pred> "form feed:\u000C" .',
"backspace:\b" => '<http://subj> <http://pred> "backspace:\u0008" .',
"é" => '<http://subj> <http://pred> "\u00E9" .',
"€" => '<http://subj> <http://pred> "\u20AC" .',
}.each_pair do |contents, triple|
specify "test #{contents}" do
stmt = reader.new(triple).first
expect(stmt.object.value).to eq contents
ttl = RDF::Turtle::Reader.new(triple).first
expect(stmt).to eq ttl
end
end
context 'should parse a value that was written without passing through the writer encoding' do
[
%(<http://subj> <http://pred> "Procreation Metaphors in S\xC3\xA9an \xC3\x93 R\xC3\xADord\xC3\xA1in's Poetry" .),
%q(<http://example.org/resource33> <http://example.org/property> "From \\"Voyage dans l’intérieur de l’Amérique du Nord, executé pendant les années 1832, 1833 et 1834, par le prince Maximilien de Wied-Neuwied\\" (Paris & Coblenz, 1839-1843)" .),
].map {|s| s.force_encoding("ASCII-8BIT")}.each do |nt|
it nt do
statement = reader.new(nt).first
ttl = RDF::Turtle::Reader.new(nt).first
expect(statement).to eq(ttl)
end
end
end
it "should parse long literal with escape" do
nt = %(<http://subj> <http://pred> "\\U00015678another" .)
statement = reader.new(nt).first
ttl = RDF::Turtle::Reader.new(nt).first
expect(statement).to eq(ttl)
end
{
"three uris" => "<http://example.org/resource1> <http://example.org/property> <http://example.org/resource2> .",
"spaces and tabs throughout" => " <http://example.org/resource3> <http://example.org/property> <http://example.org/resource2> . ",
"line ending with CR NL" => "<http://example.org/resource4> <http://example.org/property> <http://example.org/resource2> .\r\n",
"literal escapes (1)" => '<http://example.org/resource7> <http://example.org/property> "simple literal" .',
"literal escapes (2)" => '<http://example.org/resource8> <http://example.org/property> "backslash:\\\\" .',
"literal escapes (3)" => '<http://example.org/resource9> <http://example.org/property> "squote:\'" .',
"literal escapes (4)" => '<http://example.org/resource9> <http://example.org/property> "dquote:\"" .',
"literal escapes (5)" => '<http://example.org/resource10> <http://example.org/property> "newline:\n" .',
"literal escapes (6)" => '<http://example.org/resource11> <http://example.org/property> "return:\r" .',
"literal escapes (7)" => '<http://example.org/resource12> <http://example.org/property> "tab:\t" .',
"Space is optional before final . (2)" => ['<http://example.org/resource14> <http://example.org/property> "x".', '<http://example.org/resource14> <http://example.org/property> "x" .'],
"XML Literals as Datatyped Literals (1)" => '<http://example.org/resource21> <http://example.org/property> ""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (2)" => '<http://example.org/resource22> <http://example.org/property> " "^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (3)" => '<http://example.org/resource23> <http://example.org/property> "x"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (4)" => '<http://example.org/resource23> <http://example.org/property> "\""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (5)" => '<http://example.org/resource24> <http://example.org/property> "<a></a>"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (6)" => '<http://example.org/resource25> <http://example.org/property> "a <b></b>"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (7)" => '<http://example.org/resource26> <http://example.org/property> "a <b></b> c"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (8)" => '<http://example.org/resource26> <http://example.org/property> "a\n<b></b>\nc"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"XML Literals as Datatyped Literals (9)" => '<http://example.org/resource27> <http://example.org/property> "chat"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
"Plain literals with languages (1)" => '<http://example.org/resource30> <http://example.org/property> "chat"@fr .',
"Plain literals with languages (2)" => '<http://example.org/resource31> <http://example.org/property> "chat"@en .',
"Typed Literals" => '<http://example.org/resource32> <http://example.org/property> "abc"^^<http://example.org/datatype1> .',
"Plain lieral with embedded quote" => %q(<http://example.org/resource33> <http://example.org/property> "From \\"Voyage dans l’intérieur de l’Amérique du Nord, executé pendant les années 1832, 1833 et 1834, par le prince Maximilien de Wied-Neuwied\\" (Paris & Coblenz, 1839-1843)" .),
}.each_pair do |name, nt|
specify "test #{name}" do
Array(nt).each do |s|
r = reader.new(s)
statement = r.first
ttl = RDF::Turtle::Reader.new(s).first
expect(statement).to eq(ttl)
end
end
end
end
describe "i18n URIs" do
{
%(<http://a/b#Dürst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8".) => %(<http://a/b#D\\u00FCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8" .),
%(<http://a/b#a> <http://a/b#related> <http://a/b#\u3072\u3089\u304C\u306A>.) => %(<http://a/b#a> <http://a/b#related> <http://a/b#\\u3072\\u3089\\u304C\\u306A> .),
}.each_pair do |src, res|
specify src do
stmt1 = reader.new(src, validate: false).first
stmt2 = reader.new(res, validate: true).first
expect(stmt1).to eq stmt2
ttl1 = RDF::Turtle::Reader.new(src).first
ttl2 = RDF::Turtle::Reader.new(res).first
expect(stmt1).to eq ttl1
expect(stmt2).to eq ttl2
end
end
end
context "ASCII-8BIT input" do
{
"uris" => %q(<http://example/> <http://example/> <http://example/> .),
"bnode subject" => %q(_:s <http://example/> <http://example/> .),
"bnode object" => %q(<http://example/> <http://example/> _:o .),
"simple literal" => %q(<http://example/> <http://example/> "o" .),
"language tagged literal" => %q(<http://example/> <http://example/> "o"@en .),
"datatyped literal" => %q(<http://example/> <http://example/> "o"^^<http://example/> .),
}.each do |name, src|
specify name do
g = parse(src.encode("ASCII-8BIT"))
expect {g.dump(:ntriples)}.not_to raise_error
ttl = RDF::Turtle::Reader.new(src.encode("ASCII-8BIT")).first
expect(g.first).to eq(ttl)
end
end
end
context "RDF*" do
statements = {
"subject-iii": '<<<http://example/s1> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .',
"subject-iib": '<<<http://example/s1> <http://example/p1> _:o1>> <http://example/p> <http://example/o> .',
"subject-iil": '<<<http://example/s1> <http://example/p1> "o1">> <http://example/p> <http://example/o> .',
"subject-bii": '<<_:s1 <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .',
"subject-bib": '<<_:s1 <http://example/p1> _:o1>> <http://example/p> <http://example/o> .',
"subject-bil": '<<_:s1 <http://example/p1> "o">> <http://example/p> <http://example/o> .',
"subject-ws": '<< <http://example/s1> <http://example/p1> <http://example/o1> >> <http://example/p> <http://example/o> .',
"object-iii": '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> <http://example/o1>>> .',
"object-iib": '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> _:o1>> .',
"object-iil": '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> "o1">> .',
"object-ws": '<http://example/s> <http://example/p> << <http://example/s1> <http://example/p1> <http://example/o1> >> .',
"recursive-subject": '<<<<<http://example/s2> <http://example/p2> <http://example/o2>>> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .',
}
context "without rdfstar option" do
it "Raises an error" do
expect do
expect {parse(statements.values.first)}.to raise_error(RDF::ReaderError)
end.to write(:something).to(:error)
end
end
context "with rdfstar option" do
statements.each do |name, st|
context name do
let(:graph) {parse(st, rdfstar: true)}
it "creates two unquoted statements" do
expect(graph.count).to eql(1)
graph.statements.each do |stmt|
expect(stmt).not_to be_quoted
end
end
it "has a statement whose subject or object is a statement" do
referencing = graph.statements.first
expect(referencing).to be_a_statement
if referencing.subject.statement?
expect(referencing.subject).to be_a_statement
else
expect(referencing.object).to be_a_statement
end
end
it "statements which are subject or object of another statement are quoted" do
referencing = graph.statements.first
expect(referencing).to be_a_statement
if referencing.subject.statement?
expect(referencing.subject).to be_a_statement
expect(referencing.subject).to be_quoted
else
expect(referencing.object).to be_a_statement
end
end
end
end
end
end
context "invalid input" do
{
"nt-syntax-bad-struct-01" => [
%q(<http://example/s> <http://example/p> <http://example/o>, <http://example/o2> .),
%r(Expected end of statement \(found: ", .* \."\))
],
"nt-syntax-bad-struct-02" => [
%q(<http://example/s> <http://example/p> <http://example/o>; <http://example/p2>, <http://example/o2> .),
%r(Expected end of statement \(found: "; .* \."\))
],
"nt-syntax-bad-lang-01" => [
%q(<http://example/s> <http://example/p> "string"@1 .),
%r(Expected end of statement \(found: "@1 \."\))
],
"nt-syntax-bad-string-05" => [
%q(<http://example/s> <http://example/p> """abc""" .),
%r(Expected end of statement \(found: .* \."\))
],
"nt-syntax-bad-num-01" => [
%q(<http://example/s> <http://example/p> 1 .),
%r(Expected object \(found: "1 \."\))
],
"nt-syntax-bad-num-02" => [
%q(<http://example/s> <http://example/p> 1.0 .),
%r(Expected object \(found: "1\.0 \."\))
],
"nt-syntax-bad-num-03" => [
%q(<http://example/s> <http://example/p> 1.0e0 .),
%r(Expected object \(found: "1\.0e0 \."\))
],
"nt-syntax-bad-uri-02" => [
%(# Bad IRI : space.\n<http://example/ space> <http://example/p> <http://example/o> .),
%r(Expected subject)
],
"nt-syntax-bad-uri-07" => [
%(# No relative IRIs in N-Triples\n<http://example/s> <p> <http://example/o> .),
%r(Invalid URI)
],
"bnode predicate" => [
%q(<http://example/s> _:p <http://example/o> .),
%r(Expected predicate)
]
}.each do |name, (nt, error)|
it name do
expect {reader.new(nt.freeze, validate: true, logger: logger).to_a}.to raise_error(RDF::ReaderError)
expect(logger.to_s).to match error
end
end
end
end
describe RDF::NTriples::Writer do
let(:logger) {RDF::Spec.logger}
let(:writer) { RDF::NTriples::Writer }
let!(:stmt) {
s = RDF::URI("https://rubygems.org/gems/rdf")
p = RDF::URI("http://purl.org/dc/terms/creator")
o = RDF::URI("http://ar.to/#self")
RDF::Statement(s, p, o)
}
let!(:stmt_string) {
"<https://rubygems.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> ."
}
let!(:graph) {RDF::Graph.new << stmt}
subject { RDF::NTriples::Writer.new }
describe ".for" do
[
:ntriples,
'etc/doap.nt',
{file_name: 'etc/doap.nt'},
{file_extension: 'nt'},
{content_type: 'application/n-triples'},
{content_type: 'text/plain'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Writer.for(arg)).to eq RDF::NTriples::Writer
end
end
end
# @see lib/rdf/spec/writer.rb in rdf-spec
it_behaves_like 'an RDF::Writer' do
let(:writer) { RDF::NTriples::Writer.new }
end
it "defaults validation to be true" do
expect(subject).to be_validate
end
it "should return :ntriples for to_sym" do
expect(RDF::NTriples::Writer.to_sym).to eq :ntriples
end
context "Writing a Graph" do
let(:graph) {
g = RDF::Graph.new
g << [RDF::URI('s'), RDF::URI('p'), RDF::URI('o1')]
g << [RDF::URI('s'), RDF::URI('p'), RDF::URI('o2'), RDF::URI('c')]
g
}
it "#insert" do
expect do
writer.new($stdout, validate: false).insert(graph)
end.to write_each("<s> <p> <o1> .\n", "<s> <p> <o2> .\n")
end
end
context "Writing Statements" do
let(:statements) {[
RDF::Statement(RDF::URI('s'), RDF::URI('p'), RDF::URI('o1')),
RDF::Statement(RDF::URI('s'), RDF::URI('p'), RDF::URI('o2'))
]}
it "#insert" do
expect do
writer.new($stdout, validate: false).insert(*statements)
end.to write_each("<s> <p> <o1> .\n", "<s> <p> <o2> .\n")
end
it "should correctly format statements" do
expect(writer.new.format_statement(stmt)).to eq stmt_string
end
context "should correctly format blank nodes" do
specify {expect(writer.new.format_node(RDF::Node.new('foobar'))).to eq '_:foobar'}
specify {expect(writer.new.format_node(RDF::Node.new(''))).not_to eq '_:'}
end
it "should correctly format URI references" do
expect(writer.new.format_uri(RDF::URI('https://rubygems.org/gems/rdf'))).to eq '<https://rubygems.org/gems/rdf>'
end
it "should correctly format plain literals" do
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!'))).to eq '"Hello, world!"'
end
it "should correctly format string literals" do
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!', datatype: RDF::XSD.string))).to eq '"Hello, world!"'
end
it "should correctly format language-tagged literals" do
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!', language: :en))).to eq '"Hello, world!"@en'
end
it "should correctly format datatyped literals" do
expect(writer.new.format_literal(RDF::Literal.new(3.1415))).to eq '"3.1415"^^<http://www.w3.org/2001/XMLSchema#double>'
end
it "should correctly format language-tagged literals with rdf:langString" do
l = RDF::Literal.new('Hello, world!', language: :en, datatype: RDF.langString)
expect(writer.new.format_literal(l)).to eq '"Hello, world!"@en'
end
it "should output statements to a string buffer" do
output = writer.buffer { |writer| writer << stmt }
expect(output).to eq "#{stmt_string}\n"
end
it "should dump statements to a string buffer" do
output = StringIO.new
writer.dump(graph, output)
expect(output.string).to eq "#{stmt_string}\n"
end
it "should dump arrays of statements to a string buffer" do
output = StringIO.new
writer.dump(graph.to_a, output)
expect(output.string).to eq "#{stmt_string}\n"
end
it "should dump statements to a file", skip: ('not windows' if Gem.win_platform?) do
require 'tmpdir' # for Dir.tmpdir
writer.dump(graph, filename = File.join(Dir.tmpdir, "test.nt"))
expect(File.read(filename)).to eq "#{stmt_string}\n"
File.unlink(filename)
end
end
context "Nodes" do
let(:statement) {RDF::Statement(RDF::Node("a"), RDF.type, RDF::Node("b"))}
it "uses node lables by default" do
expect(writer.buffer {|w| w << statement}).to match %r(_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> _:b \.)
end
it "uses unique labels if :unique_bnodes is true" do
expect(writer.buffer(unique_bnodes:true) {|w| w << statement}).to match %r(_:g\w+ <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> _:g\w+ \.)
end
end
describe "IRIs" do
{
%(<http://example/joe> <http://xmlns.com/foaf/0.1/knows> <http://example/jane> .) =>
%(<http://example/joe> <http://xmlns.com/foaf/0.1/knows> <http://example/jane> .),
%(<http://example/#D%C3%BCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI percent ^encoded as C3, BC".) =>
%(<http://example/#D%C3%BCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI percent ^encoded as C3, BC" .),
%q(<http://example/node> <http://example/prop> <scheme:!$%25&'()*+,-./0123456789:/@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~?#> .) =>
%q(<http://example/node> <http://example/prop> <scheme:!$%25&'()*+,-./0123456789:/@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~?#> .),
}.each_pair do |input, output|
it "for '#{input}'" do
expect(parse(input, validate: true).dump(:ntriples)).to eq parse(output).dump(:ntriples)
end
end
{
%(<http://example/#Dürst> <http://example/knows> <http://example/jane>.) => '<http://example/#D\u00FCrst> <http://example/knows> <http://example/jane> .',
%(<http://example/Dürst> <http://example/knows> <http://example/jane>.) => '<http://example/D\u00FCrst> <http://example/knows> <http://example/jane> .',
%(<http://example/bob> <http://example/resumé> "Bob's non-normalized resumé".) => '<http://example/bob> <http://example/resumé> "Bob\'s non-normalized resumé" .',
%(<http://example/alice> <http://example/resumé> "Alice's normalized resumé".) => '<http://example/alice> <http://example/resumé> "Alice\'s normalized resumé" .',
}.each_pair do |input, output|
it "for '#{input}'" do
expect(parse(input).dump(:ntriples)).to eq parse(output).dump(:ntriples)
end
end
{
%(<http://example/#Dürst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8".) => %(<http://example/#D\\u00FCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8" .),
%(<http://example/a> <http://example/related> <http://example/ひらがな> .) => %(<http://example/a> <http://example/related> <http://example/\\u3072\\u3089\\u304C\\u306A> .),
}.each_pair do |input, output|
it "for '#{input}'" do
expect(parse(input).dump(:ntriples)).to eq parse(output).dump(:ntriples)
end
end
[
%(\x00),
%(\x01),
%(\x0f),
%(\x10),
%(\x1f),
%(\x20),
%(<),
%(>),
%("),
%({),
%(}),
%(|),
%(\\),
%(^),
%(``),
%(http://example.com/\u0020),
%(http://example.com/\u003C),
%(http://example.com/\u003E),
].each do |uri|
it "rejects #{('<' + uri + '>').inspect}" do
logger = RDF::Spec.logger
expect {parse(%(<s> <p> <#{uri}>), validate: true, logger: logger)}.to raise_error RDF::ReaderError
expect(logger.to_s).not_to be_empty
end
end
end
context "RDF*" do
{
"subject-iii": {
input: RDF::Statement(
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::URI('http://example/o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<<http://example/s1> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .'
},
"subject-iib": {
input: RDF::Statement(
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::Node.new('o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<<http://example/s1> <http://example/p1> _:o1>> <http://example/p> <http://example/o> .'
},
"subject-iil": {
input: RDF::Statement(
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::Literal('o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<<http://example/s1> <http://example/p1> "o1">> <http://example/p> <http://example/o> .'
},
"subject-bii": {
input: RDF::Statement(
RDF::Statement(
RDF::Node('s1'),
RDF::URI('http://example/p1'),
RDF::URI('http://example/o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<_:s1 <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .'
},
"subject-bib": {
input: RDF::Statement(
RDF::Statement(
RDF::Node('s1'),
RDF::URI('http://example/p1'),
RDF::Node.new('o1')),
RDF::URI('http://example/p'), RDF::URI('http://example/o')),
output: '<<_:s1 <http://example/p1> _:o1>> <http://example/p> <http://example/o> .'
},
"subject-bil": {
input: RDF::Statement(
RDF::Statement(
RDF::Node('s1'),
RDF::URI('http://example/p1'),
RDF::Literal('o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<_:s1 <http://example/p1> "o1">> <http://example/p> <http://example/o> .'
},
"object-iii": {
input: RDF::Statement(
RDF::URI('http://example/s'),
RDF::URI('http://example/p'),
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::URI('http://example/o1'))),
output: '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> <http://example/o1>>> .'
},
"object-iib": {
input: RDF::Statement(
RDF::URI('http://example/s'),
RDF::URI('http://example/p'),
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::Node.new('o1'))),
output: '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> _:o1>> .'
},
"object-iil": {
input: RDF::Statement(
RDF::URI('http://example/s'),
RDF::URI('http://example/p'),
RDF::Statement(
RDF::URI('http://example/s1'),
RDF::URI('http://example/p1'),
RDF::Literal('o1'))),
output: '<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> "o1">> .'
},
"recursive-subject": {
input: RDF::Statement(
RDF::Statement(
RDF::Statement(
RDF::URI('http://example/s2'),
RDF::URI('http://example/p2'),
RDF::URI('http://example/o2')),
RDF::URI('http://example/p1'),
RDF::URI('http://example/o1')),
RDF::URI('http://example/p'),
RDF::URI('http://example/o')),
output: '<<<<<http://example/s2> <http://example/p2> <http://example/o2>>> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .'
},
}.each do |name, params|
it name do
graph = RDF::Graph.new {|g| g << params[:input]}
s = writer.dump(graph)
expect(s).to eql(params[:output] + "\n")
end
end
end
context ":encoding" do
%w(US-ASCII UTF-8).each do |encoding_name|
context encoding_name do
let(:encoding) { ::Encoding.find(encoding_name)}
it "dumps to String" do
s = writer.dump(graph, nil, encoding: encoding)
expect(s).to be_a(String)
expect(s.encoding).to eq encoding
end
it "dumps to file" do
output = StringIO.new
writer.dump(graph, output, encoding: encoding)
expect(output.external_encoding).to eq encoding
end
end
end
context "when encoding text to ASCII" do
let(:encoding) { "".respond_to?(:encoding) ? ::Encoding::ASCII : nil}
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly escape ASCII characters (#x0-#x7F)" do
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x27.chr, encoding)).to eq "'"
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x7F.chr, encoding)).to eq "\\u007F"
end
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
# @see http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Planes
it "should correctly escape Unicode characters (#x80-#x10FFFF)" do
(0x80..0xFFFF).to_a.sample(100).each do |u|
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(writer.escape(c, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}"
rescue RangeError
end
end
(0x10000..0x2FFFF).to_a.sample(100).each do |u| # NB: there's nothing much beyond U+2FFFF
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(writer.escape(c, encoding)).to eq "\\U#{u.to_s(16).upcase.rjust(8, '0')}"
rescue RangeError
end
end
end
it "should correctly escape Unicode strings" do
strings = {
"_\xE2\x88\x9E_" => "_\\u221E_", # U+221E, infinity symbol
"_\xE6\xB0\xB4_" => "_\\u6C34_", # U+6C34, 'water' in Chinese
}
strings.each do |string, escaped|
string = string.encode(Encoding::UTF_8)
expect(writer.escape(string)).to eq escaped
end
end
# @see https://github.com/ruby-rdf/rdf/issues/#issue/7
it "should correctly handle RDF.rb issue #7" do
input = %Q(<http://openlibrary.org/b/OL3M> <http://RDVocab.info/Elements/titleProper> "Jh\xC5\xABl\xC4\x81." .)
output = %Q(<http://openlibrary.org/b/OL3M> <http://RDVocab.info/Elements/titleProper> "Jh\\u016Bl\\u0101." .)
writer = RDF::NTriples::Writer.new(StringIO.new, encoding: :ascii)
expect(writer.format_statement(RDF::NTriples.unserialize(input))).to eq output
end
end
context "when encoding text to UTF-8" do
let(:encoding) { "".respond_to?(:encoding) ? ::Encoding::UTF_8 : nil}
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly escape ASCII characters (#x0-#x7F)" do
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x27.chr, encoding)).to eq "'"
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x7F.chr, encoding)).to eq "\\u007F"
end
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
# @see http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Planes
it "should not escape Unicode characters (#x80-#x10FFFF)" do
(0x80..0xFFFF).to_a.sample(100).each do |u|
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(writer.escape(c, encoding)).to eq c
rescue RangeError
end
end
(0x10000..0x2FFFF).to_a.sample(100).each do |u| # NB: there's nothing much beyond U+2FFFF
begin
next unless (c = u.chr(::Encoding::UTF_8)).valid_encoding?
expect(writer.escape(c, encoding)).to eq c
rescue RangeError
end
end
end
it "should not escape Unicode strings" do
strings = [
"_\u221E_", # U+221E, infinity symbol
"_\u6C34_", # U+6C34, 'water' in Chinese
]
strings.each do |string|
string = string.encode(Encoding::UTF_8)
expect(writer.escape(string, encoding)).to eq string
end
end
end
end
context "validataion" do
shared_examples "validation" do |statement, valid|
context "given #{statement}" do
subject {RDF::NTriples::Writer.buffer(validate: true, logger: logger) {|w| w << statement}}
if valid
specify {
expect {subject}.not_to raise_error
#expect(logger.to_s).to be_empty
}
else
specify {
expect {subject}.to raise_error(RDF::WriterError)
#expect(logger.to_s).not_to be_empty
}
end
end
end
{
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement(RDF::Node("node"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => true,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Node("node")) => true,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::Literal("literal")) => true,
RDF::Statement(RDF::URI('file:///path/to/file with spaces.txt'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(nil, RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), nil, RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::URI("http://purl.org/dc/terms/creator"), nil) => false,
RDF::Statement(RDF::Literal("literal"), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Node("node"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Literal("literal"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI('scheme://auth/\\u0000'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI('scheme://auth/^'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI('scheme://auth/`'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,
RDF::Statement(RDF::URI('scheme://auth/\\'), RDF::URI("http://purl.org/dc/terms/creator"), RDF::URI("http://ar.to/#self")) => false,