-
Notifications
You must be signed in to change notification settings - Fork 10
/
enumerable.rb
544 lines (473 loc) · 19.1 KB
/
enumerable.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
require 'rdf/spec'
RSpec.shared_examples 'an RDF::Enumerable' do
include RDF::Spec::Matchers
before do
raise 'enumerable must be set with `let(:enumerable)' unless
defined? enumerable
@rdf_enumerable_iv_statements ||= RDF::Spec.quads
if enumerable.empty?
if (enumerable.writable? rescue false)
enumerable.insert(*@rdf_enumerable_iv_statements)
elsif enumerable.respond_to?(:<<)
@rdf_enumerable_iv_statements.each { |statement| enumerable << statement }
else
raise "@enumerable must respond to #<< or be pre-populated with the statements in #{RDF::Spec::TRIPLES_FILE} in a before(:each) block"
end
end
end
let(:supports_named_graphs) {enumerable.supports?(:graph_name) rescue true}
let(:subject_count) {@rdf_enumerable_iv_statements.map(&:subject).uniq.length}
let(:bnode_subject_count) {@rdf_enumerable_iv_statements.map(&:subject).uniq.select(&:node?).length}
let(:non_bnode_statements) {@rdf_enumerable_iv_statements.reject(&:node?)}
let(:non_bnode_terms) {@rdf_enumerable_iv_statements.map(&:to_quad).flatten.compact.reject(&:node?).uniq}
subject { enumerable }
it {is_expected.to respond_to(:supports?)}
describe "#valid?" do
it "reports validity" do
if subject.supports?(:validity)
is_expected.to be_valid
else
expect {subject.valid?}.to raise_error(NotImplementedError)
end
end
it "returns false if any statement is invalid" do
if subject.respond_to?(:<<) && (subject.writable? rescue true)
s = RDF::Statement(RDF::URI("https://rubygems.org/gems/rdf"), RDF::Literal("literal"), RDF::URI("http://ar.to/#self"))
if subject.supports?(:validity)
expect(s).not_to be_valid
subject << s
is_expected.not_to be_valid
else
expect {subject.valid?}.to raise_error(NotImplementedError)
end
end
end
end
context "when counting statements" do
it {is_expected.to respond_to(:empty?)}
it {is_expected.to_not be_empty}
it {is_expected.to respond_to(:count)}
its(:count) {is_expected.to eq @rdf_enumerable_iv_statements.size}
it {is_expected.to respond_to(:size)}
its(:size) {is_expected.to eq @rdf_enumerable_iv_statements.size}
context "and empty" do
subject {[].extend(RDF::Enumerable)}
it {is_expected.to be_empty}
its(:count) {is_expected.to eq 0}
its(:size) {is_expected.to eq 0}
end
end
context "when enumerating statements" do
it {is_expected.to respond_to(:statements)}
its(:statements) {is_expected.to be_a(Array)}
describe "#statements" do
specify {expect(subject.statements.size).to eq @rdf_enumerable_iv_statements.size}
specify {expect(subject.statements).to all(be_a_statement)}
end
it {is_expected.to respond_to(:has_statement?)}
describe "#has_statement?" do
let(:unknown_statement) {RDF::Statement.new(RDF::Node.new, RDF::URI.new("http://example.org/unknown"), RDF::Node.new)}
it "should have all statements" do
# Don't check for BNodes, as equivalence depends on their being exactly the same, not just the same identifier. If subject is loaded separately, these won't match.
non_bnode_statements.each do |statement|
is_expected.to have_statement(statement)
end
end
it "does not have statement in different named graph" do
if supports_named_graphs
graph_name = RDF::URI.new("urn:graph_name:1")
non_bnode_statements.each do |statement|
s = statement.dup
s.graph_name = graph_name
is_expected.not_to have_statement(s)
end
end
end
it "does not have an unknown statement" do
is_expected.not_to have_statement(unknown_statement)
end
end
it {is_expected.to respond_to(:each_statement)}
its(:each_statement) {is_expected.to be_an_enumerator}
its(:each_statement) {expect(subject.each_statement).to all(be_a_statement)}
it {is_expected.to respond_to(:enum_statement)}
its(:enum_statement) {is_expected.to be_an_enumerator}
its(:enum_statement) {is_expected.to be_countable}
its(:enum_statement) {is_expected.to be_enumerable}
its(:enum_statement) {is_expected.to be_queryable}
describe "#enum_statement" do
it "should enumerate all statements" do
expect(subject.enum_statement.count).to eq enumerable.each_statement.count
subject.enum_statement.each do |s|
expect(s).to be_a_statement
expect(enumerable.each_statement.to_a).to include(s) unless s.node?
end
end
end
its(:canonicalize) {is_expected.to be_an_enumerator}
describe "#canonicalize" do
specify {expect(subject.canonicalize.count).to eq @rdf_enumerable_iv_statements.size}
specify {expect(subject.canonicalize).to all(be_a_statement)}
its "terms should all be in canonical form" do
subject.canonicalize.terms.all? {|t| t.eql?(t.canonicalize)}
end
end
end
context "when enumerating triples" do
it {is_expected.to respond_to(:triples)}
it {is_expected.to respond_to(:has_triple?)}
it {is_expected.to respond_to(:each_triple)}
it {is_expected.to respond_to(:enum_triple)}
its(:triples) {is_expected.to be_a(Array)}
describe "#triples" do
specify {expect(subject.triples.size).to eq @rdf_enumerable_iv_statements.size}
specify {expect(subject.triples).to all(be_a_triple)}
end
describe "#has_triple?" do
specify do
non_bnode_statements.each do |statement|
is_expected.to have_triple(statement.to_triple)
end
end
end
its(:each_triple) {is_expected.to be_an_enumerator}
describe "#each_triple" do
specify {expect(subject.each_triple).to all(be_a_triple)}
it "should iterate over all triples" do
subject.each_triple do |*triple|
expect(triple.each).to all(be_a_term)
expect(enumerable).to have_triple(triple) unless triple.any?(&:node?)
end
end
end
its(:enum_triple) {is_expected.to be_an_enumerator}
its(:enum_triple) {is_expected.to be_countable}
describe "#enum_triple" do
it "should enumerate all triples" do
expect(subject.enum_triple.count).to eq enumerable.each_triple.count
subject.enum_triple.each do |s, p, o|
expect([s, p, o]).to all(be_a_term)
expect(enumerable).to have_triple([s, p, o]) unless [s, p, o].any?(&:node?)
end
end
end
end
context "when enumerating quads" do
it {is_expected.to respond_to(:quads)}
it {is_expected.to respond_to(:has_quad?)}
it {is_expected.to respond_to(:each_quad)}
it {is_expected.to respond_to(:enum_quad)}
its(:quads) {is_expected.to be_a(Array)}
describe "#quads" do
specify {expect(subject.quads.size).to eq @rdf_enumerable_iv_statements.size}
specify {expect(subject.quads).to all(be_a_quad)}
end
describe "#has_quad?" do
specify do
if supports_named_graphs
non_bnode_statements.each do |statement|
is_expected.to have_quad(statement.to_quad)
end
end
end
end
its(:each_quad) {is_expected.to be_an_enumerator}
describe "#each_quad" do
specify {expect(subject.each_quad).to all(be_a_quad)}
it "should iterate over all quads" do
subject.each_quad do |*quad|
expect(quad.compact).to all(be_a_term)
expect(enumerable).to have_quad(quad) unless quad.compact.any?(&:node?)
end
end
end
its(:enum_quad) {is_expected.to be_an_enumerator}
its(:enum_quad) {is_expected.to be_countable}
describe "#enum_quad" do
it "should enumerate all quads" do
expect(subject.enum_quad.count).to eq enumerable.each_quad.count
subject.enum_quad.each do |s, p, o, c|
expect([s, p, o, c].compact).to all(be_a_term)
expect(enumerable).to have_quad([s, p, o, c]) unless [s, p, o].any?(&:node?)
end
end
end
end
context "when enumerating subjects" do
let(:subjects) {subject.map { |s| s.subject }.reject(&:node?).uniq}
it {is_expected.to respond_to(:subjects)}
it {is_expected.to respond_to(:has_subject?)}
it {is_expected.to respond_to(:each_subject)}
it {is_expected.to respond_to(:enum_subject)}
describe "#subjects" do
subject { enumerable.subjects }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_resource)}
context "unique: false" do
subject { enumerable.subjects(unique: false) }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_resource)}
end
end
describe "#has_subject?" do
specify do
checked = []
non_bnode_statements.each do |statement|
expect(enumerable).to have_subject(statement.subject) unless checked.include?(statement.subject)
checked << statement.subject
end
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
expect(enumerable).not_to have_subject(uri)
end
end
its(:each_subject) {is_expected.to be_an_enumerator}
describe "#each_subject" do
specify {expect(subject.each_subject.reject(&:node?).size).to eq subjects.reject(&:node?).size}
specify {expect(subject.each_subject).to all(be_a_resource)}
specify {subject.each_subject {|value| expect(subjects).to include(value) unless value.node?}}
end
its(:enum_subject) {is_expected.to be_an_enumerator}
its(:enum_subject) {is_expected.to be_countable}
describe "#enum_subject" do
specify {expect(subject.enum_subject.to_a.reject(&:node?).size).to eq subjects.reject(&:node?).size}
it "should enumerate all subjects" do
subject.enum_subject.each do |s|
expect(s).to be_a_resource
expect(subjects.to_a).to include(s) unless s.node?
end
end
end
end
context "when enumerating predicates" do
let(:predicates) {@rdf_enumerable_iv_statements.map { |s| s.predicate }.uniq}
it {is_expected.to respond_to(:predicates)}
it {is_expected.to respond_to(:has_predicate?)}
it {is_expected.to respond_to(:each_predicate)}
it {is_expected.to respond_to(:enum_predicate)}
describe "#predicates" do
subject { enumerable.predicates }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_uri)}
context "unique: false" do
subject { enumerable.predicates(unique: false) }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_uri)}
end
end
describe "#has_predicate?" do
specify do
checked = []
@rdf_enumerable_iv_statements.each do |statement|
expect(enumerable).to have_predicate(statement.predicate) unless checked.include?(statement.predicate)
checked << statement.predicate
end
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
expect(enumerable).not_to have_predicate(uri)
end
end
its(:each_predicate) {is_expected.to be_an_enumerator}
describe "#each_predicate" do
specify {expect(subject.each_predicate.to_a.size).to eq predicates.size}
specify {expect(subject.each_predicate).to all(be_a_uri)}
specify {subject.each_predicate {|value| expect(predicates).to include(value)}}
end
its(:enum_predicate) {is_expected.to be_an_enumerator}
its(:enum_predicate) {is_expected.to be_countable}
describe "#enum_predicate" do
it "should enumerate all predicates" do
expect(subject.enum_predicate.to_a).to include(*predicates)
end
end
end
context "when enumerating objects" do
let(:objects) {subject.map(&:object).reject(&:node?).uniq}
it {is_expected.to respond_to(:objects)}
it {is_expected.to respond_to(:has_object?)}
it {is_expected.to respond_to(:each_object)}
it {is_expected.to respond_to(:enum_object)}
describe "#objects" do
subject { enumerable.objects }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_term)}
context "unique: false" do
subject { enumerable.objects(unique: false) }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_term)}
end
end
describe "#has_object?" do
specify do
checked = []
non_bnode_statements.each do |statement|
expect(enumerable).to have_object(statement.object) unless checked.include?(statement.object)
checked << statement.object
end
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
expect(enumerable).not_to have_object(uri)
end
end
its(:each_object) {is_expected.to be_an_enumerator}
describe "#each_object" do
specify {expect(subject.each_object.reject(&:node?).size).to eq objects.size}
specify {expect(subject.each_object).to all(be_a_term)}
specify {subject.each_object {|value| expect(objects).to include(value) unless value.node?}}
end
its(:enum_object) {is_expected.to be_an_enumerator}
its(:enum_object) {is_expected.to be_countable}
describe "#enum_object" do
it "should enumerate all objects" do
subject.enum_object.each do |o|
expect(o).to be_a_term
expect(objects.to_a).to include(o) unless o.node?
end
end
end
end
context "when enumerating terms" do
let(:terms) {subject.map(&:to_quad).flatten.compact.reject(&:node?).uniq}
it {is_expected.to respond_to(:terms)}
it {is_expected.to respond_to(:has_term?)}
it {is_expected.to respond_to(:each_term)}
it {is_expected.to respond_to(:enum_term)}
describe "#terms" do
subject { enumerable.terms }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_term)}
context "unique: false" do
subject { enumerable.terms(unique: false) }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_term)}
end
end
describe "#has_term?" do
specify do
checked = {}
non_bnode_terms.each do |term|
expect(enumerable).to have_term(term) unless checked.has_key?(term.hash)
checked[term.hash] = true
end
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
expect(enumerable).not_to have_term(uri)
end
end
its(:each_term) {is_expected.to be_an_enumerator}
describe "#each_term" do
it 'has correct number of terms' do
expected_count = non_bnode_terms.length
expected_count = expected_count - 3 unless
subject.supports?(:literal_equality)
expect(subject.each_term.reject(&:node?).size).to eq expected_count
end
specify { expect(subject.each_term).to all(be_a_term) }
specify { subject.each_term {|value| expect(non_bnode_terms).to include(value) unless value.node?} }
end
its(:enum_term) {is_expected.to be_an_enumerator}
its(:enum_term) {is_expected.to be_countable}
describe "#enum_term" do
it "should enumerate all terms" do
subject.enum_term.each do |o|
expect(o).to be_a_term
expect(non_bnode_terms.to_a).to include(o) unless o.node?
end
end
end
end
context "when enumerating graphs" do
it {is_expected.to respond_to(:graph_names)}
it {is_expected.to respond_to(:has_graph?)}
it {is_expected.to respond_to(:each_graph)}
it {is_expected.to respond_to(:enum_graph)}
its(:graph_names) {is_expected.to be_a(Array)}
describe "#graph_names" do
subject { enumerable.graph_names }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_resource)}
context "unique: false" do
subject { enumerable.graph_names(unique: false) }
specify {is_expected.to be_a(Array)}
specify {is_expected.to all(be_a_resource)}
end
end
it "should implement #has_graph?" do
if supports_named_graphs
@rdf_enumerable_iv_statements.each do |statement|
if statement.has_graph?
expect(enumerable).to have_graph(statement.graph_name)
end
end
uri = RDF::URI.new('http://example.org/does/not/have/this/uri')
expect(enumerable).not_to have_graph(uri)
end
end
describe "#project_graph" do
it {is_expected.to respond_to(:project_graph)}
context "default graph" do
let(:graph_name) {nil}
specify {expect(subject.project_graph(graph_name)).to all(be_a_statement)}
it "should return default triples" do
expect(subject.project_graph(graph_name).count).to eql(subject.reject(&:graph_name).count)
end
it "should iterate over default triples" do
subject.project_graph(graph_name) do |statement|
expect(statement.graph_name).to be_nil
end
end
end
context "named graph" do
let(:graph_name) {enumerable.graph_names.first}
specify {expect(subject.project_graph(graph_name)).to all(be_a_statement)}
it "should return named triples" do
expect(subject.project_graph(graph_name).count).to eql(subject.select {|s| s.graph_name == graph_name}.count)
end
it "should iterate over named triples" do
subject.project_graph(graph_name) do |statement|
expect(statement.graph_name).to eql graph_name
end
end
end
context "non-existing graph" do
let(:graph_name) {RDF::URI.new('http://example.org/does/not/have/this/uri')}
specify {expect(subject.project_graph(graph_name)).to be_empty if supports_named_graphs}
end
end
its(:each_graph) {is_expected.to be_an_enumerator}
describe "#each_graph" do
let(:graph_names) {@rdf_enumerable_iv_statements.map { |s| s.graph_name }.uniq.compact}
subject { enumerable.each_graph }
it {is_expected.to be_an_enumerator}
specify {is_expected.to all(be_a_graph) if supports_named_graphs}
it "has appropriate number of graphs" do
if supports_named_graphs
graph_names = @rdf_enumerable_iv_statements.map { |s| s.graph_name }.uniq.compact
expect(subject.to_a.size).to eq (graph_names.size + 1)
end
end
end
describe "#enum_graph" do
subject { enumerable.enum_graph }
it {is_expected.to be_an_enumerator}
it {is_expected.to be_countable}
it "enumerates the same as #each_graph" do
expect(subject.to_a).to include(*enumerable.each_graph.to_a) if supports_named_graphs # expect with match problematic
end
end
end
context "when converting" do
it {is_expected.to respond_to(:to_h)}
it {is_expected.not_to respond_to(:to_hash)}
describe "#to_h" do
it "should have as many keys as subjects" do
expect(subject.to_h.keys.size).to eq enumerable.subjects.to_a.size
end
end
end
context "when dumping" do
it {is_expected.to respond_to(:dump)}
it "should implement #dump" do
expect(subject.dump(:ntriples)).to eq RDF::NTriples::Writer.buffer() {|w| w << enumerable}
end
it "raises error on unknown format" do
expect {subject.dump(:foobar)}.to raise_error(RDF::WriterError, /No writer found/)
end
end
end