Skip to content

Commit 76d6a0f

Browse files
committed
N-Quads reader updates for VERSION.
1 parent b3f9a12 commit 76d6a0f

3 files changed

Lines changed: 103 additions & 93 deletions

File tree

lib/rdf/nquads.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ def read_triple
7070
line = @line # for backtracking input in case of parse error
7171

7272
begin
73-
unless blank? || read_comment
73+
if blank? || read_comment
74+
# No-op
75+
elsif version = read_version
76+
@options[:version] = version
77+
else
7478
# FIXME: quoted triples are now deprecated
7579
subject = read_uriref || read_node || read_quotedTriple || fail_subject
7680
predicate = read_uriref(intern: true) || fail_predicate

spec/nquads_spec.rb

Lines changed: 94 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@
4747
end
4848
end
4949

50-
describe "#to_sym" do
51-
specify {expect(subject.to_sym).to eq :nquads}
52-
end
53-
54-
describe "#name" do
55-
specify {expect(subject.name).to eq "N-Quads"}
56-
end
57-
5850
describe ".detect" do
5951
{
6052
nquads: "<a> <b> <c> <d> . ",
@@ -82,6 +74,14 @@
8274
end
8375
end
8476
end
77+
78+
describe "#to_sym" do
79+
specify {expect(subject.to_sym).to eq :nquads}
80+
end
81+
82+
describe "#name" do
83+
specify {expect(subject.name).to eq "N-Quads"}
84+
end
8585
end
8686

8787
describe RDF::NQuads::Reader do
@@ -123,6 +123,10 @@
123123
it "should accept strings" do
124124
expect { described_class.new('') }.to_not raise_error
125125
end
126+
127+
it "sets version from reader option" do
128+
expect(described_class.new('', version: '1.2').version).to eql '1.2'
129+
end
126130
end
127131

128132
context "#initialize" do
@@ -143,104 +147,106 @@
143147
end
144148
end
145149

146-
context "with simple triples" do
147-
[
148-
['<a> <b> <c> .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"))],
149-
['<a> <b> _:c .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::Node.new("c"))],
150-
['<a> <b> "c" .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::Literal("c"))],
151-
['_:a <b> <c> .', RDF::Statement(RDF::Node.new("a"), RDF::URI("b"), RDF::URI("c"))],
152-
].each do |(str, statement)|
153-
it "parses #{str.inspect}" do
154-
graph = RDF::Graph.new << described_class.new(str)
155-
expect(graph.size).to eq 1
156-
expect(graph.statements.first).to eq statement
157-
end
158-
end
159-
end
160-
161-
context "with simple quads" do
162-
[
163-
['<a> <b> <c> <d> .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::URI("d"))],
164-
['<a> <b> <c> _:d .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::Node.new("d"))],
165-
['<a> <b> <c> "d" .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::Literal("d"))],
166-
].each do |(str, statement)|
167-
it "parses #{str.inspect}" do
168-
graph = RDF::Graph.new << described_class.new(str)
169-
expect(graph.size).to eq 1
170-
expect(graph.statements.first).to eq statement
171-
end
172-
173-
it "serializes #{statement.inspect}" do
174-
expect(RDF::NQuads.serialize(statement).chomp).to eq str
175-
end
176-
177-
it "unserializes #{str.inspect}" do
178-
expect(RDF::NQuads.unserialize(str)).to eq statement
150+
describe "Grammar" do
151+
context "with simple triples" do
152+
[
153+
['<a> <b> <c> .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"))],
154+
['<a> <b> _:c .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::Node.new("c"))],
155+
['<a> <b> "c" .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::Literal("c"))],
156+
['_:a <b> <c> .', RDF::Statement(RDF::Node.new("a"), RDF::URI("b"), RDF::URI("c"))],
157+
].each do |(str, statement)|
158+
it "parses #{str.inspect}" do
159+
graph = RDF::Graph.new << described_class.new(str)
160+
expect(graph.size).to eq 1
161+
expect(graph.statements.first).to eq statement
162+
end
179163
end
180164
end
181-
end
182165

183-
context "triple terms" do
184-
ill_statements = {
185-
"subject-iii": '<<<http://example/s1> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> <http://example/g> .',
186-
"subject-iib": '<<<http://example/s1> <http://example/p1> _:o1>> <http://example/p> <http://example/o> <http://example/g> .',
187-
"subject-iil": '<<<http://example/s1> <http://example/p1> "o1">> <http://example/p> <http://example/o> <http://example/g> .',
188-
"subject-bii": '<<_:s1 <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> <http://example/g> .',
189-
"subject-bib": '<<_:s1 <http://example/p1> _:o1>> <http://example/p> <http://example/o> <http://example/g> .',
190-
"subject-bil": '<<_:s1 <http://example/p1> "o">> <http://example/p> <http://example/o> <http://example/g> .',
191-
"subject-ws": '<< <http://example/s1> <http://example/p1> <http://example/o1> >> <http://example/p> <http://example/o> <http://example/g> .',
192-
"recursive-subject": '<<(<<(<http://example/s2> <http://example/p2> <http://example/o2>)>> <http://example/p1> <http://example/o1>)>> <http://example/p> <http://example/o> <http://example/g> .',
193-
}
166+
context "with simple quads" do
167+
[
168+
['<a> <b> <c> <d> .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::URI("d"))],
169+
['<a> <b> <c> _:d .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::Node.new("d"))],
170+
['<a> <b> <c> "d" .', RDF::Statement(RDF::URI("a"), RDF::URI("b"), RDF::URI("c"), graph_name: RDF::Literal("d"))],
171+
].each do |(str, statement)|
172+
it "parses #{str.inspect}" do
173+
graph = RDF::Graph.new << described_class.new(str)
174+
expect(graph.size).to eq 1
175+
expect(graph.statements.first).to eq statement
176+
end
194177

195-
statements = {
196-
"object-iii": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> <http://example/o1>)>> <http://example/g> .',
197-
"object-iib": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> _:o1)>> <http://example/g> .',
198-
"object-iil": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> "o1")>> <http://example/g> .',
199-
"object-ws": '<http://example/s> <http://example/p> <<( <http://example/s1> <http://example/p1> <http://example/o1> )>> <http://example/g> .',
200-
}
178+
it "serializes #{statement.inspect}" do
179+
expect(RDF::NQuads.serialize(statement).chomp).to eq str
180+
end
201181

202-
context "without rdfstar option" do
203-
it "Raises an error" do
204-
expect do
205-
expect {RDF::Repository.new << RDF::NQuads::Reader.new(statements.values.first)}.to raise_error(RDF::ReaderError)
206-
end.to write(:something).to(:error)
182+
it "unserializes #{str.inspect}" do
183+
expect(RDF::NQuads.unserialize(str)).to eq statement
184+
end
207185
end
208186
end
209187

210-
context "with rdfstar option" do
211-
ill_statements.each do |name, st|
212-
context name do
213-
it "Raises an error" do
214-
expect do
215-
expect {RDF::Repository.new << RDF::NQuads::Reader.new(statements.values.first)}.to raise_error(RDF::ReaderError)
216-
end.to write(:something).to(:error)
217-
end
188+
context "triple terms" do
189+
ill_statements = {
190+
"subject-iii": '<<<http://example/s1> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> <http://example/g> .',
191+
"subject-iib": '<<<http://example/s1> <http://example/p1> _:o1>> <http://example/p> <http://example/o> <http://example/g> .',
192+
"subject-iil": '<<<http://example/s1> <http://example/p1> "o1">> <http://example/p> <http://example/o> <http://example/g> .',
193+
"subject-bii": '<<_:s1 <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> <http://example/g> .',
194+
"subject-bib": '<<_:s1 <http://example/p1> _:o1>> <http://example/p> <http://example/o> <http://example/g> .',
195+
"subject-bil": '<<_:s1 <http://example/p1> "o">> <http://example/p> <http://example/o> <http://example/g> .',
196+
"subject-ws": '<< <http://example/s1> <http://example/p1> <http://example/o1> >> <http://example/p> <http://example/o> <http://example/g> .',
197+
"recursive-subject": '<<(<<(<http://example/s2> <http://example/p2> <http://example/o2>)>> <http://example/p1> <http://example/o1>)>> <http://example/p> <http://example/o> <http://example/g> .',
198+
}
199+
200+
statements = {
201+
"object-iii": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> <http://example/o1>)>> <http://example/g> .',
202+
"object-iib": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> _:o1)>> <http://example/g> .',
203+
"object-iil": '<http://example/s> <http://example/p> <<(<http://example/s1> <http://example/p1> "o1")>> <http://example/g> .',
204+
"object-ws": '<http://example/s> <http://example/p> <<( <http://example/s1> <http://example/p1> <http://example/o1> )>> <http://example/g> .',
205+
}
206+
207+
context "without rdfstar option" do
208+
it "Raises an error" do
209+
expect do
210+
expect {RDF::Repository.new << RDF::NQuads::Reader.new(statements.values.first)}.to raise_error(RDF::ReaderError)
211+
end.to write(:something).to(:error)
218212
end
219213
end
220214

221-
statements.each do |name, st|
222-
context name do
223-
let(:graph) do
224-
RDF::Repository.new {|r| r << RDF::NQuads::Reader.new(st, rdfstar: true)}
225-
end
226-
227-
it "creates a statement" do
228-
expect(graph.count).to eql(1)
215+
context "with rdfstar option" do
216+
ill_statements.each do |name, st|
217+
context name do
218+
it "Raises an error" do
219+
expect do
220+
expect {RDF::Repository.new << RDF::NQuads::Reader.new(statements.values.first)}.to raise_error(RDF::ReaderError)
221+
end.to write(:something).to(:error)
222+
end
229223
end
224+
end
230225

231-
it "statements which are object of another statement are triple terms" do
232-
referencing = graph.statements.first
233-
expect(referencing).to be_a_statement
234-
expect(referencing.object).to be_a_statement
235-
expect(referencing.object).to be_tripleTerm
226+
statements.each do |name, st|
227+
context name do
228+
let(:graph) do
229+
RDF::Repository.new {|r| r << RDF::NQuads::Reader.new(st, rdfstar: true)}
230+
end
231+
232+
it "creates a statement" do
233+
expect(graph.count).to eql(1)
234+
end
235+
236+
it "statements which are object of another statement are triple terms" do
237+
referencing = graph.statements.first
238+
expect(referencing).to be_a_statement
239+
expect(referencing.object).to be_a_statement
240+
expect(referencing.object).to be_tripleTerm
241+
end
236242
end
237243
end
238244
end
239245
end
240-
end
241246

242-
it "should parse W3C's test data" do
243-
expect(described_class.new(File.open(testfile)).to_a.size).to eq 19
247+
it "should parse W3C's test data" do
248+
expect(described_class.new(File.open(testfile)).to_a.size).to eq 19
249+
end
244250
end
245251
end
246252

spec/ntriples_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,19 @@
142142
end
143143

144144
it "should accept files" do
145-
expect { reader.new(File.open(testfile)) }.not_to raise_error
145+
expect { described_class.new(File.open(testfile)) }.not_to raise_error
146146
end
147147

148148
it "should accept IO streams" do
149-
expect { reader.new(StringIO.new('')) }.not_to raise_error
149+
expect { described_class.new(StringIO.new('')) }.not_to raise_error
150150
end
151151

152152
it "should accept strings" do
153-
expect { reader.new('') }.not_to raise_error
153+
expect { described_class.new('') }.not_to raise_error
154154
end
155155

156156
it "sets version from reader option" do
157-
expect(reader.new('', version: '1.2').version).to eql '1.2'
157+
expect(described_class.new('', version: '1.2').version).to eql '1.2'
158158
end
159159
end
160160

0 commit comments

Comments
 (0)