-
Notifications
You must be signed in to change notification settings - Fork 9
/
writer_spec.rb
678 lines (654 loc) · 23.3 KB
/
writer_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
# coding: utf-8
require_relative 'spec_helper'
require 'rdf/spec/writer'
require 'rdf/vocab'
require 'rdf/trig'
describe RDF::N3::Writer do
let(:logger) {RDF::Spec.logger}
it_behaves_like 'an RDF::Writer' do
let(:writer) {RDF::N3::Writer.new(StringIO.new)}
end
describe ".for" do
[
:n3,
'etc/doap.n3',
{file_name: 'etc/doap.n3'},
{file_extension: 'n3'},
{content_type: 'text/n3'},
{content_type: 'text/rdf+n3'},
{content_type: 'application/rdf+n3'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Writer.for(arg)).to eq RDF::N3::Writer
end
end
end
describe "simple tests" do
{
"full URIs without base" => {
input: %(<http://a/b> <http://a/c> <http://a/d> .),
regexp: [%r(^<http://a/b> <http://a/c> <http://a/d> \.$)],
},
"relative URIs with base" => {
input: %(<http://a/b> <http://a/c> <http://a/d> .),
regexp: [ %r(^@base <http://a/> \.$), %r(^<b> <c> <d> \.$)],
base_uri: "http://a/"
},
"qname URIs with prefix" => {
input: %(<http://example.com/b> <http://example.com/c> <http://example.com/d> .),
regexp: [
%r(^@prefix ex: <http://example.com/> \.$),
%r(^ex:b ex:c ex:d \.$)
],
prefixes: {ex: "http://example.com/"}
},
"qname URIs with empty prefix" => {
input: %(<http://example.com/b> <http://example.com/c> <http://example.com/d> .),
regexp: [
%r(^@prefix : <http://example.com/> \.$),
%r(^:b :c :d \.$)
],
prefixes: {"" => "http://example.com/"}
},
# see example-files/arnau-registered-vocab.rb
"qname URIs with empty suffix" => {
input: %(<http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> .),
regexp: [
%r(^@prefix foaf: <http://xmlns.com/foaf/0.1/> \.$),
%r(^foaf: foaf: foaf: \.$)
],
prefixes: { "foaf" => "http://xmlns.com/foaf/0.1/"}
},
"order properties" => {
input: %(
@prefix ex: <http://example.com/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:b ex:c ex:d .
ex:b dc:title "title" .
ex:b a ex:class .
ex:b rdfs:label "label" .
),
regexp: [
%r(^ex:b a ex:class;$),
%r(ex:class;\s+rdfs:label "label")m,
%r("label";\s+ex:c ex:d)m,
%r(ex:d;\s+dc:title "title" \.$)m
],
},
"object list" => {
input: %(@prefix ex: <http://example.com/> . ex:b ex:c ex:d, ex:e .),
regexp: [
%r(^@prefix ex: <http://example.com/> \.$),
%r(^ex:b ex:c ex:[de],\s+ex:[de] \.$)m,
],
},
"property list" => {
input: %(@prefix ex: <http://example.com/> . ex:b ex:c ex:d; ex:e ex:f .),
regexp: [
%r(^@prefix ex: <http://example.com/> \.$),
%r(^ex:b ex:c ex:d;$),
%r(^\s+ex:e ex:f \.$)
],
},
"bare anon" => {
input: %(@prefix ex: <http://example.com/> . [ex:a ex:b] .),
regexp: [%r(^\s*\[ex:a ex:b\] \.$)],
},
"anon as subject" => {
input: %(@prefix ex: <http://example.com/> . [ex:a ex:b] ex:c ex:d .),
regexp: [
%r(^\s*\[\s*ex:a ex:b;$)m,
%r(^\s+ex:c ex:d\s*\] \.$)m
],
},
"anon as object" => {
input: %(@prefix ex: <http://example.com/> . ex:a ex:b [ex:c ex:d] .),
regexp: [%r(^ex:a ex:b \[ex:c ex:d\] \.$)],
},
"reuses BNode labels by default" => {
input: %(@prefix ex: <http://example.com/> . _:a ex:b _:a .),
regexp: [%r(^\s*_:a(_\d+_\d+) ex:b _:a\1 \.$)]
},
"standard prefixes" => {
input: %(
<a> a <http://xmlns.com/foaf/0.1/Person>;
<http://purl.org/dc/terms/title> "Person" .
),
regexp: [
%r(^@prefix foaf: <http://xmlns.com/foaf/0.1/> \.$),
%r(^@prefix dc: <http://purl.org/dc/terms/> \.$),
%r(^<a> a foaf:Person;$),
%r(dc:title "Person" \.$),
],
standard_prefixes: true, prefixes: {}
},
"should not use qname with illegal local part" => {
input: %(
@prefix db: <http://dbpedia.org/resource/> .
@prefix dbo: <http://dbpedia.org/ontology/> .
db:Michael_Jackson dbo:artistOf <http://dbpedia.org/resource/%28I_Can%27t_Make_It%29_Another_Day> .
),
regexp: [
%r(^@prefix db: <http://dbpedia.org/resource/> \.$),
%r(^db:Michael_Jackson dbo:artistOf <http://dbpedia.org/resource/%28I_Can%27t_Make_It%29_Another_Day> \.$)
],
prefixes: {
"db" => RDF::URI("http://dbpedia.org/resource/"),
"dbo" => RDF::URI("http://dbpedia.org/ontology/")}
}
}.each do |name, params|
it name do
serialize(params[:input], params[:regexp], **params)
end
end
end
describe "lists" do
{
"bare list": {
input: %(@prefix ex: <http://example.com/> . (ex:a ex:b) .),
regexp: [%r(^\(\s*ex:a ex:b\s*\) \.$)]
},
"literal list": {
input: %(@prefix ex: <http://example.com/> . ex:a ex:b ( "apple" "banana" ) .),
regexp: [%r(^ex:a ex:b \(\s*"apple" "banana"\s*\) \.$)]
},
"empty list": {
input: %(@prefix ex: <http://example.com/> . ex:a ex:b () .),
regexp: [%r(^ex:a ex:b \(\s*\) \.$)],
prefixes: { "" => RDF::Vocab::FOAF}
},
"should generate empty list(2)" => {
input: %(@prefix : <http://xmlns.com/foaf/0.1/> . :emptyList = () .),
regexp: [%r(^:emptyList (<.*sameAs>|owl:sameAs|=) \(\) \.$)],
prefixes: { "" => "http://xmlns.com/foaf/0.1/"}
},
"empty list as subject": {
input: %(@prefix ex: <http://example.com/> . () ex:a ex:b .),
regexp: [%r(^\(\s*\) ex:a ex:b \.$)]
},
"list as subject": {
input: %(@prefix ex: <http://example.com/> . (ex:a) ex:b ex:c .),
regexp: [%r(^\(\s*ex:a\s*\) ex:b ex:c \.$)]
},
"list of empties": {
input: %(@prefix ex: <http://example.com/> . [ex:listOf2Empties (() ())] .),
regexp: [%r(\[\s*ex:listOf2Empties \(\s*\(\s*\) \(\s*\)\s*\)\s*\] \.$)]
},
"list anon": {
input: %(@prefix ex: <http://example.com/> . [ex:twoAnons ([a ex:mother] [a ex:father])] .),
regexp: [%r(\[\s*ex:twoAnons \(\s*\[\s*a ex:mother\s*\] \[\s*a ex:father\s*\]\)\] \.$)]
},
"list subjects": {
input: %(@prefix ex: <http://example.com/> . (ex:a ex:b) . ex:a a ex:Thing . ex:b a ex:Thing .),
regexp: [
%r(\(ex:a ex:b\) \.),
%r(ex:a a ex:Thing \.),
%r(ex:b a ex:Thing \.),
]
},
"owl:unionOf list": {
input: %(
@prefix ex: <http://example.com/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:a rdfs:domain [
a owl:Class;
owl:unionOf [
a owl:Class;
rdf:first ex:b;
rdf:rest [
a owl:Class;
rdf:first ex:c;
rdf:rest rdf:nil
]
]
] .
),
regexp: [
%r(ex:a rdfs:domain \[\s*a owl:Class;\s+owl:unionOf\s+\(\s*ex:b\s+ex:c\s*\)\s*\]\s*\.$)m,
%r(@prefix ex: <http://example.com/> \.),
%r(@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \.),
]
},
"list with first subject a URI": {
input: %(
<http://example.com> <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com> <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:g47006741228480 .
_:g47006741228480 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:g47006741228480 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:g47006737917560 .
_:g47006737917560 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:g47006737917560 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
),
regexp: [
%r(@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \.),
%r(<http://example.com> rdf:first 1;),
%r(rdf:rest \(\s*2 3\s*\) \.),
],
standard_prefixes: true
},
"list pattern without rdf:nil": {
input: %(
<http://example.com> <http://example.com/property> _:a .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "a" .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:b .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "b" .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:c .
_:c <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "c" .
),
regexp: [%r(<http://example.com> <http://example.com/property> \[),
%r(rdf:first "a";),
%r(rdf:rest \[),
%r(rdf:first "b";),
%r(rdf:rest \[\s*rdf:first "c"\s*\]),
],
standard_prefixes: true
},
"list pattern with extra properties": {
input: %(
<http://example.com> <http://example.com/property> _:a .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "a" .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:b .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "b" .
_:a <http://example.com/other-property> "This list node has also properties other than rdf:first and rdf:rest" .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:c .
_:c <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "c" .
_:c <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
),
regexp: [%r(<http://example.com> <http://example.com/property> \[),
%r(<http://example.com/other-property> "This list node has also properties other than rdf:first and rdf:rest";),
%r(rdf:first "a";),
%r(rdf:rest \(\s*"b" "c"\s*\)),
],
standard_prefixes: true
},
"list with empty list": {
input: %(
<http://example.com/a> <http://example.com/property> _:l1 .
_:l1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:l1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
),
regexp: [
%r(<http://example.com/a> <http://example.com/property> \(\s*\(\)\) .)
],
standard_prefixes: true
},
"list with multiple lists": {
input: %(
<http://example.com/a> <http://example.com/property> _:l1 .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "a" .
_:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "b" .
_:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:l1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:a .
_:l1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:l2 .
_:l2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:b .
_:l2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
),
regexp: [
%r(<http://example.com/a> <http://example.com/property> \(\s*\(\s*"a"\) \(\s*"b"\)\) .)
],
standard_prefixes: true
},
}.each do |name, params|
it name do
serialize(params[:input], params[:regexp], **params)
end
end
end
describe "literals" do
describe "xsd:anyURI" do
it "uses xsd namespace for datatype" do
ttl = %q(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> "http://foo/"^^xsd:anyURI .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
%r("http://foo/"\^\^xsd:anyURI \.),
])
end
end
describe "xsd:boolean" do
[
[%q("true"^^xsd:boolean), /true ./],
[%q("TrUe"^^xsd:boolean), /true ./],
[%q("1"^^xsd:boolean), /true ./],
[%q(true), /true ./],
[%q("false"^^xsd:boolean), /false ./],
[%q("FaLsE"^^xsd:boolean), /false ./],
[%q("0"^^xsd:boolean), /false ./],
[%q(false), /false ./],
].each do |(l,r)|
it "uses token for #{l.inspect}" do
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> #{l} .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
r,
], canonicalize: true)
end
end
[
[true, "true"],
[false, "false"],
[1, "true"],
[0, "false"],
["true", "true"],
["false", "false"],
["1", "true"],
["0", "false"],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#boolean>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Boolean.new(l))).to eql r
end
end
end
describe "xsd:integer" do
[
[%q("1"^^xsd:integer), /1 ./],
[%q(1), /1 ./],
[%q("0"^^xsd:integer), /0 ./],
[%q(0), /0 ./],
[%q("10"^^xsd:integer), /10 ./],
[%q(10), /10 ./],
].each do |(l,r)|
it "uses token for #{l.inspect}" do
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> #{l} .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
r,
], canonicalize: true)
end
end
[
[0, "0"],
[10, "10"],
[-1, "-1"],
["0", "0"],
["true", %{"true"^^<http://www.w3.org/2001/XMLSchema#integer>}],
["false", %{"false"^^<http://www.w3.org/2001/XMLSchema#integer>}],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#integer>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Integer.new(l))).to eql r
end
end
end
describe "xsd:int" do
[
[%q("1"^^xsd:int), /"1"\^\^xsd:int ./],
[%q("0"^^xsd:int), /"0"\^\^xsd:int ./],
[%q("10"^^xsd:int), /"10"\^\^xsd:int ./],
].each do |(l,r)|
it "uses token for #{l.inspect}" do
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> #{l} .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
r,
], canonicalize: true)
end
end
end
describe "xsd:decimal" do
[
[%q("1.0"^^xsd:decimal), /1.0 ./],
[%q(1.0), /1.0 ./],
[%q("0.1"^^xsd:decimal), /0.1 ./],
[%q(0.1), /0.1 ./],
[%q("10.02"^^xsd:decimal), /10.02 ./],
[%q(10.02), /10.02 ./],
].each do |(l,r)|
it "uses token for #{l.inspect}" do
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> #{l} .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
r,
], canonicalize: true)
end
end
[
[0, "0.0"],
[10, "10.0"],
[-1, "-1.0"],
["0", "0.0"],
["10", "10.0"],
["-1", "-1.0"],
["1.0", "1.0"],
["0.1", "0.1"],
["10.01", "10.01"],
["true", %{"true"^^<http://www.w3.org/2001/XMLSchema#decimal>}],
["false", %{"false"^^<http://www.w3.org/2001/XMLSchema#decimal>}],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#decimal>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Decimal.new(l))).to eql r
end
end
end
describe "xsd:double" do
[
[%q("1.0e1"^^xsd:double), /1.0e1 ./],
[%q(1.0e1), /1.0e1 ./],
[%q("0.1e1"^^xsd:double), /1.0e0 ./],
[%q(0.1e1), /1.0e0 ./],
[%q("10.02e1"^^xsd:double), /1.002e2 ./],
[%q(10.02e1), /1.002e2 ./],
[%q("14"^^xsd:double), /1.4e1 ./],
].each do |(l,r)|
it "uses token for #{l.inspect}" do
ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> #{l} .)
serialize(ttl, [
%r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
r,
], canonicalize: true)
end
end
[
[0, "0.0e0"],
[10, "1.0e1"],
[-1, "-1.0e0"],
["0", "0.0e0"],
["10", "1.0e1"],
["-1", "-1.0e0"],
["1.0", "1.0e0"],
["0.1", "1.0e-1"],
["10.01", "1.001e1"],
["true", %{"true"^^<http://www.w3.org/2001/XMLSchema#double>}],
["false", %{"false"^^<http://www.w3.org/2001/XMLSchema#double>}],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#double>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Double.new(l))).to eql r
end
end
end
end
describe "formulae" do
{
"empty subject" => {
input: %({} <b> <c> .),
regexp: [
%r(\[<b> <c>\] \.)
]
},
"empty object" => {
input: %(<a> <b> {} .),
regexp: [
%r(<a> <b> \[\] \.)
]
},
"as subject with constant content" => {
input: %({<x> <y> <z>} <b> <c> .),
regexp: [
%r({\s+<x> <y> <z> \.\s+} <b> <c> \.)m
]
},
"as object with constant content" => {
input: %(<a> <b> {<x> <y> <z>} .),
regexp: [
%r(<a> <b> {\s+<x> <y> <z> \.\s+} \.)m
]
},
"implies" => {
input: %({ _:x :is _:x } => {_:x :is _:x } .),
regexp: [
%r({\s+_:x(_\d+_\d+) :is _:x\1 \.\s+} => {\s+_:x(_\d+_\d+) :is _:x\2 \.\s+} \.)m
]
},
"formula simple" => {
input: %(<> :about { :c :d :e }.),
regexp: [
%r(<> :about {\s+:c :d :e \.\s+} \.)
]
},
"nested" => {
input: %(
@prefix doc: <http://www.w3.org/2000/10/swap/pim/doc#> .
@prefix ex: <http://www.example.net/2000/10/whatever#> .
@prefix contact: <http://www.w3.org/2000/10/swap/pim/contact#> .
[]
doc:creator [contact:email <mailto:fred@example.com> ];
ex:says {
[] doc:title "Huckleberry Finn";
doc:creator [contact:knownAs "Mark Twain"]
}.
),
regexp: [
%r(\[\s+ex:says {\s+\[)m,
%r(doc:creator \[contact:knownAs "Mark Twain"\];),
%r(doc:title "Huckleberry Finn"),
%r(\] \.\s+};)m,
%r(doc:creator \[contact:email <mailto:fred@example.com>)
]
},
"named with URI" => {
input: %q(
<a> <b> <c> .
<C> {<A> <b> <c> .}
),
regexp: [
%r(<a> <b> <c> \.),
%r(<C> = {),
%r(<A> <b> <c> \.),
%r(} \.),
],
input_format: :trig
},
"named with BNode" => {
input: %q(
<a> <b> <c> .
_:C {<A> <b> <c> .}
),
regexp: [
%r(<a> <b> <c> \.),
%r(_:C = {),
%r(<A> <b> <c> \.),
%r(} \.),
],
input_format: :trig
}
}.each do |name, params|
it name do
serialize(params[:input], params[:regexp], **params)
end
end
end
describe "variables" do
{
"@forAll": {
input: %(@forAll :o. :s :p :o .),
regexp: [
%r(@forAll :o_\d+_\d+ \.),
%r(:s :p :o_\d+_\d+ \.),
]
},
"@forSome": {
input: %(@forSome :o. :s :p :o .),
regexp: [
%r(@forSome :o_\d+_\d+ \.),
%r(:s :p :o_\d+_\d+ \.),
]
},
"?o": {
input: %(:s :p ?o .),
regexp: [
%r(@forAll :o_\d+_\d+ \.),
%r(:s :p :o_\d+_\d+ \.),
]
},
}.each do |name, params|
it name do
serialize(params[:input], params[:regexp], **params)
end
end
end
# W3C TriG Test suite
describe "w3c n3 parser tests" do
require_relative 'suite_helper'
Fixtures::SuiteTest::Manifest.open("https://w3c.github.io/n3/tests/manifest-parser.n3") do |m|
describe m.comment do
m.entries.each do |t|
next unless t.positive_test? && t.evaluate?
specify "#{t.name}: #{t.comment} (action)" do
case t.name
when *%w(n3_10003 n3_10004 n3_10008)
skip "Blank Node predicates"
when *%w(n3_10012 n3_10017)
pending "Investigate"
when *%w(n3_10013)
pending "Number syntax"
end
logger.info t.inspect
logger.info "source: #{t.input}"
repo = parse(t.input, base_uri: t.base)
n3 = serialize(repo, [], base_uri: t.base, standard_prefixes: true)
logger.info "serialized: #{n3}"
g2 = parse(n3, base_uri: t.base)
expect(g2).to be_equivalent_graph(repo, logger: logger)
end
specify "#{t.name}: #{t.comment} (result)" do
case t.name
when *%w(n3_10003 n3_10004 n3_10008)
skip "Blank Node predicates"
when *%w(n3_10012 n3_10017)
pending "Investigate"
when *%w(n3_10013)
pending "Number syntax"
end
logger.info t.inspect
logger.info "source: #{t.expected}"
format = detect_format(t.expected)
repo = parse(t.expected, base_uri: t.base, format: format)
n3 = serialize(repo, [], base_uri: t.base, standard_prefixes: true)
logger.info "serialized: #{n3}"
g2 = parse(n3, base_uri: t.base)
expect(g2).to be_equivalent_graph(repo, logger: logger)
end
end
end
end
end unless ENV['CI']
def parse(input, format: :n3, **options)
repo = RDF::Repository.new
reader = RDF::Reader.for(format)
repo << reader.new(input, **options)
repo
end
# Serialize ntstr to a string and compare against regexps
def serialize(ntstr, regexps = [], base_uri: nil, **options)
prefixes = options[:prefixes] || {}
g = ntstr.is_a?(RDF::Enumerable) ? ntstr : parse(ntstr, base_uri: base_uri, prefixes: prefixes, validate: false, logger: [], format: options.fetch(:input_format, :n3))
result = RDF::N3::Writer.buffer(**options.merge(logger: logger, base_uri: base_uri, prefixes: prefixes)) do |writer|
writer << g
end
if $verbose
require 'cgi'
#puts CGI.escapeHTML(result)
end
logger.info "result: #{result}"
regexps.each do |re|
logger.info "match: #{re.inspect}"
expect(result).to match_re(re, about: base_uri, logger: logger, input: ntstr), logger.to_s
end
result
end
end