rdf-n3 0.3.4.1 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/script/tc DELETED
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env ruby -s
2
- require 'rubygems'
3
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
4
- require 'rdf/n3'
5
- require File.expand_path(File.join(File.dirname(__FILE__), "..", 'spec', 'rdfa_helper'))
6
- require 'getoptlong'
7
-
8
- def run_tc(tc)
9
- puts "run #{tc.name}"
10
- puts RDF::Writer.for($output_format.to_sym).buffer { |writer|
11
- RDF::N3::Reader.new(tc.input, :base_uri => tc.about, :strict => $strict).each do |statement|
12
- writer << statement
13
- end
14
- }
15
- end
16
-
17
- $verbose = false
18
- $output_format = :ntriples
19
- $strict = false
20
- suite = "turtle"
21
- opts = GetoptLong.new(
22
- ["--debug", GetoptLong::NO_ARGUMENT],
23
- ["--verbose", GetoptLong::NO_ARGUMENT],
24
- ["--quiet", GetoptLong::NO_ARGUMENT],
25
- ["--suite", GetoptLong::OPTIONAL_ARGUMENT],
26
- ["--strict", GetoptLong::NO_ARGUMENT],
27
- ["--format", GetoptLong::REQUIRED_ARGUMENT]
28
- )
29
- opts.each do |opt, arg|
30
- case opt
31
- when '--verbose' then $verbose = true
32
- when '--quiet' then $quiet = true
33
- when '--debug' then ::RDF::N3::debug = true
34
- when '--format' then $output_format = arg
35
- when '--suite' then suite = arg
36
- when '--strict' then $strict = true
37
- end
38
- end
39
-
40
- # Test URI and directory for different suites
41
- TEST_PARAMS = {
42
- "rdfxml" => [RDFCORE_TEST, RDFCORE_DIR],
43
- "cwm" => [CWM_TEST, SWAP_DIR],
44
- "swap" => [SWAP_TEST, SWAP_DIR],
45
- "turtle" => [TURTLE_TEST, TURTLE_DIR],
46
- "turtlebad" => [TURTLE_BAD_TEST, TURTLE_DIR],
47
- }
48
- test_cases = RdfHelper::TestCase.test_cases(*TEST_PARAMS[suite])
49
-
50
- test_cases = test_cases.detect do |tc|
51
- next unless ARGV.empty? || ARGV.any? {|n| tc.name.match(/#{n}/)}
52
- run_tc(tc)
53
- end
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env sh
2
- rsync -azv -e "ssh -i /Users/gregg/.ssh/id_dsa" doc/yard/ gkellogg@rubyforge.org:/var/www/gforge-projects/rdf/n3/
data/spec/.gitignore DELETED
@@ -1 +0,0 @@
1
- /uri-cache/
data/spec/cwm_spec.rb DELETED
@@ -1,44 +0,0 @@
1
- $:.unshift "."
2
- require File.join(File.dirname(__FILE__), 'spec_helper')
3
-
4
- describe RDF::N3::Reader do
5
- describe "w3c cwm tests" do
6
- require 'cwm_test'
7
-
8
- # Negative parser tests should raise errors.
9
- Fixtures::Cwm::CwmTest.each do |t|
10
- next unless t.inputDocument
11
- #next unless t.about.uri.to_s =~ /rdfms-rdf-names-use/
12
- #next unless t.name =~ /1018/
13
- #puts t.inspect
14
- specify "test #{t.name}: #{t.description}: #{t.inputDocument} against #{t.referenceOutput}" do
15
- begin
16
- if t.name =~ /1018/
17
- pending("matcher does not stop")
18
- next
19
- elsif !t.arguments.to_s.empty?
20
- pending("proofs not supported")
21
- next
22
- end
23
- t.run_test do
24
- t.debug = []
25
- g = RDF::Graph.new
26
- RDF::Reader.for(t.inputDocument).new(t.input,
27
- :base_uri => t.inputDocument,
28
- :strict => true,
29
- :debug => t.debug).each do |statement|
30
- g << statement
31
- end
32
- g
33
- end
34
- rescue RSpec::Expectations::ExpectationNotMetError => e
35
- if t.status == "pending"
36
- pending("Formulae not supported") { raise }
37
- else
38
- raise
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
data/spec/cwm_test.rb DELETED
@@ -1,76 +0,0 @@
1
- # Spira class for manipulating test-manifest style test suites.
2
- # Used for SWAP tests
3
- require 'spira'
4
- require 'rdf/n3'
5
- require 'open-uri'
6
-
7
- module Fixtures
8
- module Cwm
9
- class SWAP < RDF::Vocabulary("http://www.w3.org/2000/10/swap/test.n3#"); end
10
-
11
- class CwmTest
12
- attr_accessor :debug
13
- attr_accessor :compare
14
- include Spira::Resource
15
-
16
- default_source :cwm
17
-
18
- type SWAP.CwmTest
19
- property :description, :predicate => SWAP.description, :type => XSD.string
20
- property :referenceOutput, :predicate => SWAP.referenceOutput
21
- property :arguments, :predicate => SWAP.arguments, :type => XSD.string
22
-
23
- def inputDocument
24
- self.arguments if self.arguments.match(/\w+[^\s]*.n3$/)
25
- end
26
-
27
- def name
28
- subject.to_s.split("#").last
29
- end
30
-
31
- def input
32
- Kernel.open(self.inputDocument)
33
- end
34
-
35
- def reference
36
- self.referenceOutput ? Kernel.open(self.referenceOutput) : ""
37
- end
38
-
39
- def information; self.description; end
40
-
41
- def inspect
42
- "[#{self.class.to_s} " + %w(
43
- subject
44
- description
45
- inputDocument
46
- referenceOutput
47
- ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
48
- "]"
49
- end
50
-
51
- # Run test case, yields input for parser to create triples
52
- def run_test(options = {})
53
- # Run
54
- graph = yield
55
-
56
- return unless self.outputDocument
57
-
58
- case self.compare
59
- when :none
60
- # Don't check output, just parse to graph
61
- when :array
62
- @parser.graph.should be_equivalent_graph(self.output, self)
63
- else
64
- #puts "parse #{self.outputDocument} as #{RDF::Reader.for(self.outputDocument)}"
65
- format = detect_format(self.reference)
66
- output_graph = RDF::Graph.load(self.referenceOutput, :format => format, :base_uri => self.subject)
67
- puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if ::RDF::N3::debug?
68
- graph.should Matchers::be_equivalent_graph(output_graph, self)
69
- end
70
- end
71
- end
72
-
73
- repo = RDF::Repository.load("http://www.w3.org/2000/10/swap/test/regression.n3", :format => :n3)
74
- Spira.add_repository! :cwm, repo
75
- end
76
- end
data/spec/format_spec.rb DELETED
@@ -1,41 +0,0 @@
1
- $:.unshift "."
2
- require File.join(File.dirname(__FILE__), 'spec_helper')
3
-
4
- describe RDF::N3::Format do
5
- context "discovery" do
6
- {
7
- "n3" => RDF::Format.for(:n3),
8
- "etc/foaf.n3" => RDF::Format.for("etc/foaf.n3"),
9
- "etc/foaf.ttl" => RDF::Format.for("etc/foaf.ttl"),
10
- "foaf.n3" => RDF::Format.for(:file_name => "foaf.n3"),
11
- "foaf.ttl" => RDF::Format.for(:file_name => "foaf.ttl"),
12
- ".n3" => RDF::Format.for(:file_extension => "n3"),
13
- ".ttl" => RDF::Format.for(:file_extension => "ttl"),
14
- "text/n3" => RDF::Format.for(:content_type => "text/n3"),
15
- "text/rdf+n3" => RDF::Format.for(:content_type => "text/rdf+n3"),
16
- "application/rdf+n3" => RDF::Format.for(:content_type => "application/rdf+n3"),
17
- "text/turtle" => RDF::Format.for(:content_type => "text/turtle"),
18
- "application/turtle" => RDF::Format.for(:content_type => "application/turtle"),
19
- "application/x-turtle" => RDF::Format.for(:content_type => "application/x-turtle"),
20
- }.each_pair do |label, format|
21
- it "should discover '#{label}'" do
22
- format.should == RDF::N3::Format
23
- end
24
- end
25
-
26
- it "should discover 'notation3'" do
27
- RDF::Format.for(:notation3).reader.should == RDF::N3::Reader
28
- RDF::Format.for(:notation3).writer.should == RDF::N3::Writer
29
- end
30
-
31
- it "should discover 'turtle'" do
32
- RDF::Format.for(:turtle).reader.should == RDF::N3::Reader
33
- RDF::Format.for(:turtle).writer.should == RDF::N3::Writer
34
- end
35
-
36
- it "should discover 'ttl'" do
37
- RDF::Format.for(:ttl).reader.should == RDF::N3::Reader
38
- RDF::Format.for(:ttl).writer.should == RDF::N3::Writer
39
- end
40
- end
41
- end
data/spec/matchers.rb DELETED
@@ -1,130 +0,0 @@
1
- require 'rdf/isomorphic'
2
-
3
- module Matchers
4
- class BeEquivalentGraph
5
- Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
6
- def normalize(graph)
7
- case @info.compare
8
- when :array
9
- array = case graph
10
- when RDF::Graph
11
- raise ":compare => :array used with Graph"
12
- when Array
13
- graph.sort
14
- else
15
- graph.to_s.split("\n").
16
- map {|t| t.gsub(/^\s*(.*)\s*$/, '\1')}.
17
- reject {|t2| t2.match(/^\s*$/)}.
18
- compact.
19
- sort.
20
- uniq
21
- end
22
-
23
- # Implement to_ntriples on array, to simplify logic later
24
- def array.to_ntriples; self.join("\n") + "\n"; end
25
- array
26
- else
27
- case graph
28
- when RDF::Graph then graph
29
- when IO, StringIO
30
- RDF::Graph.new.load(graph, :base_uri => @info.about)
31
- else
32
- # Figure out which parser to use
33
- g = RDF::Graph.new
34
- reader_class = RDF::Reader.for(detect_format(graph))
35
- reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
36
- g
37
- end
38
- end
39
- end
40
-
41
- def initialize(expected, info)
42
- @info = if info.respond_to?(:about)
43
- info
44
- elsif info.is_a?(Hash)
45
- identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
46
- trace = info[:trace]
47
- trace = trace.join("\n") if trace.is_a?(Array)
48
- Info.new(identifier, info[:information] || "", trace, info[:compare])
49
- else
50
- Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
51
- end
52
- @expected = normalize(expected)
53
- end
54
-
55
- def matches?(actual)
56
- @actual = normalize(actual)
57
- if @info.compare == :array
58
- @actual == @expected
59
- else
60
- @actual.isomorphic_with?(@expected)
61
- end
62
- end
63
-
64
- def failure_message_for_should
65
- info = @info.respond_to?(:information) ? @info.information : @info.inspect
66
- if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size
67
- "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
68
- elsif @expected.is_a?(Array) && @actual.size != @expected.length
69
- "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
70
- else
71
- "Graph differs"
72
- end +
73
- "\n#{info + "\n" unless info.empty?}" +
74
- (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
75
- (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
76
- "Unsorted Expected:\n#{@expected.to_ntriples}" +
77
- "Unsorted Results:\n#{@actual.to_ntriples}" +
78
- # "Unsorted Expected Dump:\n#{@expected.dump}\n" +
79
- # "Unsorted Results Dump:\n#{@actual.dump}" +
80
- (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
81
- end
82
- def negative_failure_message
83
- "Graphs do not differ\n"
84
- end
85
- end
86
-
87
- def be_equivalent_graph(expected, info = nil)
88
- BeEquivalentGraph.new(expected, info)
89
- end
90
-
91
- class MatchRE
92
- Info = Struct.new(:about, :information, :trace, :inputDocument, :outputDocument)
93
- def initialize(expected, info)
94
- @info = if info.respond_to?(:about)
95
- info
96
- elsif info.is_a?(Hash)
97
- identifier = info[:identifier] || info[:about]
98
- trace = info[:trace]
99
- trace = trace.join("\n") if trace.is_a?(Array)
100
- Info.new(identifier, info[:information] || "", trace, info[:inputDocument], info[:outputDocument])
101
- else
102
- Info.new(info, info.to_s)
103
- end
104
- @expected = expected
105
- end
106
-
107
- def matches?(actual)
108
- @actual = actual
109
- @actual.to_s.match(@expected)
110
- end
111
-
112
- def failure_message_for_should
113
- info = @info.respond_to?(:information) ? @info.information : @info.inspect
114
- "Match failed"
115
- "\n#{info + "\n" unless info.empty?}" +
116
- (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
117
- (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
118
- "Expression: #{@expected}\n" +
119
- "Unsorted Results:\n#{@actual}" +
120
- (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
121
- end
122
- def negative_failure_message
123
- "Match succeeded\n"
124
- end
125
- end
126
-
127
- def match_re(expected, info = nil)
128
- MatchRE.new(expected, info)
129
- end
130
- end
data/spec/reader_spec.rb DELETED
@@ -1,1243 +0,0 @@
1
- # coding: utf-8
2
- $:.unshift "."
3
- require File.join(File.dirname(__FILE__), 'spec_helper')
4
-
5
- describe "RDF::N3::Reader" do
6
- context "discovery" do
7
- {
8
- "n3" => RDF::Reader.for(:n3),
9
- "etc/foaf.n3" => RDF::Reader.for("etc/foaf.n3"),
10
- "etc/foaf.ttl" => RDF::Reader.for("etc/foaf.ttl"),
11
- "foaf.n3" => RDF::Reader.for(:file_name => "foaf.n3"),
12
- "foaf.ttl" => RDF::Reader.for(:file_name => "foaf.ttl"),
13
- ".n3" => RDF::Reader.for(:file_extension => "n3"),
14
- ".ttl" => RDF::Reader.for(:file_extension => "ttl"),
15
- "text/n3" => RDF::Reader.for(:content_type => "text/n3"),
16
- "text/turtle" => RDF::Reader.for(:content_type => "text/turtle"),
17
- }.each_pair do |label, format|
18
- it "should discover '#{label}'" do
19
- format.should == RDF::N3::Reader
20
- end
21
- end
22
- end
23
-
24
- context :interface do
25
- before(:each) do
26
- @sampledoc = <<-EOF;
27
- @prefix dc: <http://purl.org/dc/elements/1.1/>.
28
- @prefix po: <http://purl.org/ontology/po/>.
29
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
30
- _:broadcast
31
- a po:Broadcast;
32
- po:schedule_date """2008-06-24T12:00:00Z""";
33
- po:broadcast_of _:version;
34
- po:broadcast_on <http://www.bbc.co.uk/programmes/service/6music>;
35
- .
36
- _:version
37
- a po:Version;
38
- .
39
- <http://www.bbc.co.uk/programmes/b0072l93>
40
- dc:title """Nemone""";
41
- a po:Brand;
42
- .
43
- <http://www.bbc.co.uk/programmes/b00c735d>
44
- a po:Episode;
45
- po:episode <http://www.bbc.co.uk/programmes/b0072l93>;
46
- po:version _:version;
47
- po:long_synopsis """Actor and comedian Rhys Darby chats to Nemone.""";
48
- dc:title """Nemone""";
49
- po:synopsis """Actor and comedian Rhys Darby chats to Nemone.""";
50
- .
51
- <http://www.bbc.co.uk/programmes/service/6music>
52
- a po:Service;
53
- dc:title """BBC 6 Music""";
54
- .
55
-
56
- #_:abcd a po:Episode.
57
- EOF
58
- end
59
-
60
- it "should yield reader" do
61
- inner = mock("inner")
62
- inner.should_receive(:called).with(RDF::N3::Reader)
63
- RDF::N3::Reader.new(@sampledoc) do |reader|
64
- inner.called(reader.class)
65
- end
66
- end
67
-
68
- it "should return reader" do
69
- RDF::N3::Reader.new(@sampledoc).should be_a(RDF::N3::Reader)
70
- end
71
-
72
- it "should yield statements" do
73
- inner = mock("inner")
74
- inner.should_receive(:called).with(RDF::Statement).exactly(15)
75
- RDF::N3::Reader.new(@sampledoc).each_statement do |statement|
76
- inner.called(statement.class)
77
- end
78
- end
79
-
80
- it "should yield triples" do
81
- inner = mock("inner")
82
- inner.should_receive(:called).exactly(15)
83
- RDF::N3::Reader.new(@sampledoc).each_triple do |subject, predicate, object|
84
- inner.called(subject.class, predicate.class, object.class)
85
- end
86
- end
87
- end
88
-
89
- describe "with simple ntriples" do
90
- context "simple triple" do
91
- before(:each) do
92
- n3_string = %(<http://example.org/> <http://xmlns.com/foaf/0.1/name> "Gregg Kellogg" .)
93
- @graph = parse(n3_string)
94
- @statement = @graph.statements.first
95
- end
96
-
97
- it "should have a single triple" do
98
- @graph.size.should == 1
99
- end
100
-
101
- it "should have subject" do
102
- @statement.subject.to_s.should == "http://example.org/"
103
- end
104
- it "should have predicate" do
105
- @statement.predicate.to_s.should == "http://xmlns.com/foaf/0.1/name"
106
- end
107
- it "should have object" do
108
- @statement.object.to_s.should == "Gregg Kellogg"
109
- end
110
- end
111
-
112
- # NTriple tests from http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt
113
- describe "with blank lines" do
114
- {
115
- "comment" => "# comment lines",
116
- "comment after whitespace" => " # comment after whitespace",
117
- "empty line" => "",
118
- "line with spaces" => " "
119
- }.each_pair do |name, statement|
120
- specify "test #{name}" do
121
- parse(statement).size.should == 0
122
- end
123
- end
124
- end
125
-
126
- describe "with literal encodings" do
127
- {
128
- 'Dürst' => ':a :b "D\u00FCrst" .',
129
- 'simple literal' => ':a :b "simple literal" .',
130
- 'backslash:\\' => ':a :b "backslash:\\\\" .',
131
- 'dquote:"' => ':a :b "dquote:\"" .',
132
- "newline:\n" => ':a :b "newline:\n" .',
133
- "return\r" => ':a :b "return\r" .',
134
- "tab:\t" => ':a :b "tab:\t" .',
135
- "é" => ':a :b "\u00E9" .',
136
- "€" => ':a :b "\u20AC" .',
137
- "resumé" => ':a :resume "resum\u00E9" .',
138
- }.each_pair do |contents, triple|
139
- specify "test #{triple}" do
140
- graph = parse(triple, :base_uri => "http://a/b")
141
- statement = graph.statements.first
142
- graph.size.should == 1
143
- statement.object.value.should == contents
144
- end
145
- end
146
-
147
- {
148
- 'Dürst' => ':a :b "Dürst" .',
149
- "é" => ':a :b "é" .',
150
- "€" => ':a :b "€" .',
151
- "resumé" => ':a :resume "resumé" .',
152
- }.each_pair do |contents, triple|
153
- specify "test #{triple}" do
154
- graph = parse(triple, :base_uri => "http://a/b")
155
- statement = graph.statements.first
156
- graph.size.should == 1
157
- statement.object.value.should == contents
158
- end
159
- end
160
-
161
- it "should parse long literal with escape" do
162
- n3 = %(@prefix : <http://example.org/foo#> . :a :b "\\U00015678another" .)
163
- if defined?(::Encoding)
164
- statement = parse(n3).statements.first
165
- statement.object.value.should == "\u{15678}another"
166
- else
167
- pending("Not supported in Ruby 1.8")
168
- end
169
- end
170
-
171
- context "string3 literals" do
172
- {
173
- "simple" => %q(foo),
174
- "muti-line" => %q(
175
- Foo
176
- <html:b xmlns:html="http://www.w3.org/1999/xhtml" html:a="b">
177
- bar
178
- <rdf:Thing xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
179
- <a:b xmlns:a="foo:"></a:b>
180
- here
181
- <a:c xmlns:a="foo:"></a:c>
182
- </rd
183
- f:Thing>
184
- </html:b>
185
- baz
186
- <html:i xmlns:html="http://www.w3.org/1999/xhtml">more</html:i>
187
- ),
188
- "trailing escaped double-quote" => %q( "),
189
- "regression.n3" => %q(sameDan.n3 sameThing.n3 --think --apply=forgetDups.n3 --purge --n3="/" )
190
- }.each do |test, string|
191
- it "parses #{test}" do
192
- graph = parse(%(:a :b """#{string}"""))
193
- graph.size.should == 1
194
- graph.statements.first.object.value.should == string
195
- end
196
- end
197
- end
198
- end
199
-
200
- it "should create named subject bnode" do
201
- graph = parse("_:anon <http://example.org/property> <http://example.org/resource2> .")
202
- graph.size.should == 1
203
- statement = graph.statements.first
204
- statement.subject.should be_a(RDF::Node)
205
- statement.subject.id.should =~ /anon/
206
- statement.predicate.to_s.should == "http://example.org/property"
207
- statement.object.to_s.should == "http://example.org/resource2"
208
- end
209
-
210
- it "should create named predicate bnode" do
211
- graph = parse("<http://example.org/resource2> _:anon <http://example.org/object> .")
212
- graph.size.should == 1
213
- statement = graph.statements.first
214
- statement.subject.to_s.should == "http://example.org/resource2"
215
- statement.predicate.should be_a(RDF::Node)
216
- statement.predicate.id.should =~ /anon/
217
- statement.object.to_s.should == "http://example.org/object"
218
- end
219
-
220
- it "should create named object bnode" do
221
- graph = parse("<http://example.org/resource2> <http://example.org/property> _:anon .")
222
- graph.size.should == 1
223
- statement = graph.statements.first
224
- statement.subject.to_s.should == "http://example.org/resource2"
225
- statement.predicate.to_s.should == "http://example.org/property"
226
- statement.object.should be_a(RDF::Node)
227
- statement.object.id.should =~ /anon/
228
- end
229
-
230
- {
231
- "three uris" => "<http://example.org/resource1> <http://example.org/property> <http://example.org/resource2> .",
232
- "spaces and tabs throughout" => " <http://example.org/resource3> <http://example.org/property> <http://example.org/resource2> . ",
233
- "line ending with CR NL" => "<http://example.org/resource4> <http://example.org/property> <http://example.org/resource2> .\r\n",
234
- "literal escapes (1)" => '<http://example.org/resource7> <http://example.org/property> "simple literal" .',
235
- "literal escapes (2)" => '<http://example.org/resource8> <http://example.org/property> "backslash:\\\\" .',
236
- "literal escapes (3)" => '<http://example.org/resource9> <http://example.org/property> "dquote:\"" .',
237
- "literal escapes (4)" => '<http://example.org/resource10> <http://example.org/property> "newline:\n" .',
238
- "literal escapes (5)" => '<http://example.org/resource11> <http://example.org/property> "return:\r" .',
239
- "literal escapes (6)" => '<http://example.org/resource12> <http://example.org/property> "tab:\t" .',
240
- "Space is optional before final . (1)" => ['<http://example.org/resource13> <http://example.org/property> <http://example.org/resource2>.', '<http://example.org/resource13> <http://example.org/property> <http://example.org/resource2> .'],
241
- "Space is optional before final . (2)" => ['<http://example.org/resource14> <http://example.org/property> "x".', '<http://example.org/resource14> <http://example.org/property> "x" .'],
242
-
243
- "XML Literals as Datatyped Literals (1)" => '<http://example.org/resource21> <http://example.org/property> ""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
244
- "XML Literals as Datatyped Literals (2)" => '<http://example.org/resource22> <http://example.org/property> " "^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
245
- "XML Literals as Datatyped Literals (3)" => '<http://example.org/resource23> <http://example.org/property> "x"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
246
- "XML Literals as Datatyped Literals (4)" => '<http://example.org/resource23> <http://example.org/property> "\""^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
247
- "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> .',
248
- "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> .',
249
- "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> .',
250
- "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> .',
251
- "XML Literals as Datatyped Literals (9)" => '<http://example.org/resource27> <http://example.org/property> "chat"^^<http://www.w3.org/2000/01/rdf-schema#XMLLiteral> .',
252
-
253
- "Plain literals with languages (1)" => '<http://example.org/resource30> <http://example.org/property> "chat"@fr .',
254
- "Plain literals with languages (2)" => '<http://example.org/resource31> <http://example.org/property> "chat"@en .',
255
-
256
- "Typed Literals" => '<http://example.org/resource32> <http://example.org/property> "abc"^^<http://example.org/datatype1> .',
257
- }.each_pair do |name, statement|
258
- specify "test #{name}" do
259
- graph = parse([statement].flatten.first)
260
- graph.size.should == 1
261
- graph.to_ntriples.chomp.should == [statement].flatten.last.gsub(/\s+/, " ").strip
262
- end
263
- end
264
-
265
- it "should allow mixed-case language" do
266
- n3doc = %(:x2 :p "xyz"@EN .)
267
- statement = parse(n3doc).statements.first
268
- statement.object.to_ntriples.should == %("xyz"@EN)
269
- end
270
-
271
- it "should create typed literals" do
272
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"^^<http://www.w3.org/2001/XMLSchema#string> ."
273
- statement = parse(n3doc).statements.first
274
- statement.object.class.should == RDF::Literal
275
- end
276
-
277
- it "should create BNodes" do
278
- n3doc = "_:a a _:c ."
279
- statement = parse(n3doc).statements.first
280
- statement.subject.class.should == RDF::Node
281
- statement.object.class.should == RDF::Node
282
- end
283
-
284
- describe "should create URIs" do
285
- {
286
- %(<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> .) => %(<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> .),
287
- %(<joe> <knows> <jane> .) => %(<http://a/joe> <http://a/knows> <http://a/jane> .),
288
- %(:joe :knows :jane .) => %(<http://a/b#joe> <http://a/b#knows> <http://a/b#jane> .),
289
- %(<#D%C3%BCrst> a "URI percent ^encoded as C3, BC".) => %(<http://a/b#D%C3%BCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI percent ^encoded as C3, BC" .),
290
- }.each_pair do |n3, nt|
291
- it "for '#{n3}'" do
292
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
293
- end
294
- end
295
-
296
- {
297
- %(<#Dürst> :knows :jane.) => '<http://a/b#D\u00FCrst> <http://a/b#knows> <http://a/b#jane> .',
298
- %(:Dürst :knows :jane.) => '<http://a/b#D\u00FCrst> <http://a/b#knows> <http://a/b#jane> .',
299
- %(:bob :resumé "Bob's non-normalized resumé".) => '<http://a/b#bob> <http://a/b#resumé> "Bob\'s non-normalized resumé" .',
300
- %(:alice :resumé "Alice's normalized resumé".) => '<http://a/b#alice> <http://a/b#resumé> "Alice\'s normalized resumé" .',
301
- }.each_pair do |n3, nt|
302
- it "for '#{n3}'" do
303
- begin
304
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
305
- rescue
306
- if defined?(::Encoding)
307
- raise
308
- else
309
- pending("Unicode URIs not supported in Ruby 1.8") { raise }
310
- end
311
- end
312
- end
313
- end
314
-
315
- {
316
- %(<#Dürst> a "URI straight in UTF8".) => %(<http://a/b#D\\u00FCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8" .),
317
- #%(:a :related :ひらがな .) => %(<http://a/b#a> <http://a/b#related> <http://a/b#\\u3072\\u3089\\u304C\\u306A> .),
318
- }.each_pair do |n3, nt|
319
- it "for '#{n3}'" do
320
- begin
321
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
322
- rescue
323
- if defined?(::Encoding)
324
- raise
325
- else
326
- pending("Unicode URIs not supported in Ruby 1.8") { raise }
327
- end
328
- end
329
- end
330
- end
331
- end
332
-
333
- it "should create URIs" do
334
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/knows> <http://example.org/jane> ."
335
- statement = parse(n3doc).statements.first
336
- statement.subject.class.should == RDF::URI
337
- statement.object.class.should == RDF::URI
338
- end
339
-
340
- it "should create literals" do
341
- n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"."
342
- statement = parse(n3doc).statements.first
343
- statement.object.class.should == RDF::Literal
344
- end
345
- end
346
-
347
- describe "with n3 grammar" do
348
- describe "syntactic expressions" do
349
- it "should create typed literals with qname" do
350
- n3doc = %(
351
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
352
- @prefix foaf: <http://xmlns.com/foaf/0.1/> .
353
- @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
354
- <http://example.org/joe> foaf:name \"Joe\"^^xsd:string .
355
- )
356
- statement = parse(n3doc).statements.first
357
- statement.object.class.should == RDF::Literal
358
- end
359
-
360
- it "should use <> as a prefix and as a triple node" do
361
- n3 = %(@prefix : <> . <> a :a.)
362
- nt = %(
363
- <http://a/b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/ba> .
364
- )
365
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
366
- end
367
-
368
- it "should use <#> as a prefix and as a triple node" do
369
- n3 = %(@prefix : <#> . <#> a :a.)
370
- nt = %(
371
- <http://a/b#> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#a> .
372
- )
373
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
374
- end
375
-
376
- it "should generate rdf:type for 'a'" do
377
- n3 = %(@prefix a: <http://foo/a#> . a:b a <http://www.w3.org/2000/01/rdf-schema#resource> .)
378
- nt = %(<http://foo/a#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#resource> .)
379
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
380
- end
381
-
382
- it "should generate rdf:type for '@a'" do
383
- n3 = %(@prefix a: <http://foo/a#> . a:b @a <http://www.w3.org/2000/01/rdf-schema#resource> .)
384
- nt = %(<http://foo/a#b> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#resource> .)
385
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
386
- end
387
-
388
- it "should generate inverse predicate for 'is xxx of'" do
389
- n3 = %("value" is :prop of :b . :b :prop "value" .)
390
- nt = %(<http://a/b#b> <http://a/b#prop> "value" .)
391
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
392
- end
393
-
394
- it "should generate inverse predicate for '@is xxx @of'" do
395
- n3 = %("value" @is :prop @of :b . :b :prop "value" .)
396
- nt = %(<http://a/b#b> <http://a/b#prop> "value" .)
397
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
398
- end
399
-
400
- it "should generate inverse predicate for 'is xxx of' with object list" do
401
- n3 = %("value" is :prop of :b, :c . )
402
- nt = %(
403
- <http://a/b#b> <http://a/b#prop> "value" .
404
- <http://a/b#c> <http://a/b#prop> "value" .
405
- )
406
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
407
- end
408
-
409
- it "should generate predicate for 'has xxx'" do
410
- n3 = %(@prefix a: <http://foo/a#> . a:b has :pred a:c .)
411
- nt = %(<http://foo/a#b> <http://a/b#pred> <http://foo/a#c> .)
412
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
413
- end
414
-
415
- it "should generate predicate for '@has xxx'" do
416
- n3 = %(@prefix a: <http://foo/a#> . a:b @has :pred a:c .)
417
- nt = %(<http://foo/a#b> <http://a/b#pred> <http://foo/a#c> .)
418
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
419
- end
420
-
421
- it "should create log:implies predicate for '=>'" do
422
- n3 = %(@prefix a: <http://foo/a#> . _:a => a:something .)
423
- nt = %(_:a <http://www.w3.org/2000/10/swap/log#implies> <http://foo/a#something> .)
424
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
425
- end
426
-
427
- it "should create log:implies inverse predicate for '<='" do
428
- n3 = %(@prefix a: <http://foo/a#> . _:a <= a:something .)
429
- nt = %(<http://foo/a#something> <http://www.w3.org/2000/10/swap/log#implies> _:a .)
430
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
431
- end
432
-
433
- it "should create owl:sameAs predicate for '='" do
434
- n3 = %(@prefix a: <http://foo/a#> . _:a = a:something .)
435
- nt = %(_:a <http://www.w3.org/2002/07/owl#sameAs> <http://foo/a#something> .)
436
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
437
- end
438
-
439
- {
440
- %(:a :b @true) => %(<http://a/b#a> <http://a/b#b> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
441
- %(:a :b @false) => %(<http://a/b#a> <http://a/b#b> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
442
- %(:a :b 1) => %(<http://a/b#a> <http://a/b#b> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
443
- %(:a :b -1) => %(<http://a/b#a> <http://a/b#b> "-1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
444
- %(:a :b +1) => %(<http://a/b#a> <http://a/b#b> "+1"^^<http://www.w3.org/2001/XMLSchema#integer> .),
445
- %(:a :b 1.0) => %(<http://a/b#a> <http://a/b#b> "1.0"^^<http://www.w3.org/2001/XMLSchema#decimal> .),
446
- %(:a :b 1.0e1) => %(<http://a/b#a> <http://a/b#b> "1.0e1"^^<http://www.w3.org/2001/XMLSchema#double> .),
447
- %(:a :b 1.0e-1) => %(<http://a/b#a> <http://a/b#b> "1.0e-1"^^<http://www.w3.org/2001/XMLSchema#double> .),
448
- %(:a :b 1.0e+1) => %(<http://a/b#a> <http://a/b#b> "1.0e+1"^^<http://www.w3.org/2001/XMLSchema#double> .),
449
- %(:a :b 1.0E1) => %(<http://a/b#a> <http://a/b#b> "1.0e1"^^<http://www.w3.org/2001/XMLSchema#double> .),
450
- }.each_pair do |n3, nt|
451
- it "should create typed literal for '#{n3}'" do
452
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
453
- end
454
- end
455
-
456
- it "should accept empty localname" do
457
- n3 = %(: : : .)
458
- nt = %(<http://a/b#> <http://a/b#> <http://a/b#> .)
459
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
460
- end
461
-
462
- it "should accept prefix with empty local name" do
463
- n3 = %(@prefix foo: <http://foo/bar#> . foo: foo: foo: .)
464
- nt = %(<http://foo/bar#> <http://foo/bar#> <http://foo/bar#> .)
465
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
466
- end
467
-
468
- it "substitutes variable for URI with @forAll" do
469
- n3 = %(@forAll :x . :x :y :z .)
470
- g = parse(n3, :base_uri => "http://a/b")
471
- statement = g.statements.first
472
- statement.subject.should be_a(RDF::Query::Variable)
473
- statement.predicate.to_s.should == "http://a/b#y"
474
- statement.object.to_s.should == "http://a/b#z"
475
- end
476
-
477
- it "substitutes variable for URIs with @forAll" do
478
- n3 = %(@forAll :x, :y, :z . :x :y :z .)
479
- g = parse(n3, :base_uri => "http://a/b")
480
- statement = g.statements.first
481
- statement.subject.should be_a(RDF::Query::Variable)
482
- statement.predicate.should be_a(RDF::Query::Variable)
483
- statement.object.should be_a(RDF::Query::Variable)
484
- statement.subject.should_not equal statement.predicate
485
- statement.object.should_not equal statement.predicate
486
- statement.predicate.should_not equal statement.object
487
- end
488
-
489
- it "substitutes node for URI with @forEach" do
490
- n3 = %(@forSome :x . :x :y :z .)
491
- g = parse(n3, :base_uri => "http://a/b")
492
- statement = g.statements.first
493
- statement.subject.should be_a(RDF::Node)
494
- statement.predicate.to_s.should == "http://a/b#y"
495
- statement.object.to_s.should == "http://a/b#z"
496
- end
497
-
498
- it "substitutes node for URIs with @forEach" do
499
- n3 = %(@forSome :x, :y, :z . :x :y :z .)
500
- g = parse(n3, :base_uri => "http://a/b")
501
- statement = g.statements.first
502
- statement.subject.should be_a(RDF::Node)
503
- statement.predicate.should be_a(RDF::Node)
504
- statement.object.should be_a(RDF::Node)
505
- statement.subject.should_not == statement.predicate
506
- statement.object.should_not == statement.predicate
507
- statement.predicate.should_not == statement.object
508
- end
509
- end
510
-
511
- describe "prefixes" do
512
- it "should not append # for http://foo/bar" do
513
- n3 = %(@prefix : <http://foo/bar> . :a : :b .)
514
- nt = %(
515
- <http://foo/bara> <http://foo/bar> <http://foo/barb> .
516
- )
517
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
518
- end
519
-
520
- it "should not append # for http://foo/bar/" do
521
- n3 = %(@prefix : <http://foo/bar/> . :a : :b .)
522
- nt = %(
523
- <http://foo/bar/a> <http://foo/bar/> <http://foo/bar/b> .
524
- )
525
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
526
- end
527
-
528
- it "should not append # for http://foo/bar#" do
529
- n3 = %(@prefix : <http://foo/bar#> . :a : :b .)
530
- nt = %(
531
- <http://foo/bar#a> <http://foo/bar#> <http://foo/bar#b> .
532
- )
533
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
534
- end
535
-
536
- it "should set absolute base" do
537
- n3 = %(@base <http://foo/bar> . <> :a <b> . <#c> :d </e>.)
538
- nt = %(
539
- <http://foo/bar> <http://foo/bar#a> <http://foo/b> .
540
- <http://foo/bar#c> <http://foo/bar#d> <http://foo/e> .
541
- )
542
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
543
- end
544
-
545
- it "should set absolute base (trailing /)" do
546
- n3 = %(@base <http://foo/bar/> . <> :a <b> . <#c> :d </e>.)
547
- nt = %(
548
- <http://foo/bar/> <http://foo/bar/a> <http://foo/bar/b> .
549
- <http://foo/bar/#c> <http://foo/bar/d> <http://foo/e> .
550
- )
551
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
552
- end
553
-
554
- it "should set absolute base (trailing #)" do
555
- n3 = %(@base <http://foo/bar#> . <> :a <b> . <#c> :d </e>.)
556
- nt = %(
557
- <http://foo/bar#> <http://foo/bar#a> <http://foo/b> .
558
- <http://foo/bar#c> <http://foo/bar#d> <http://foo/e> .
559
- )
560
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
561
- end
562
-
563
- it "should set relative base" do
564
- n3 = %(
565
- @base <http://example.org/products/>.
566
- <> :a <b>, <#c>.
567
- @base <prod123/>.
568
- <> :a <b>, <#c>.
569
- @base <../>.
570
- <> :a <d>, <#e>.
571
- )
572
- nt = %(
573
- <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/b> .
574
- <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/#c> .
575
- <http://example.org/products/prod123/> <http://example.org/products/prod123/a> <http://example.org/products/prod123/b> .
576
- <http://example.org/products/prod123/> <http://example.org/products/prod123/a> <http://example.org/products/prod123/#c> .
577
- <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/d> .
578
- <http://example.org/products/> <http://example.org/products/a> <http://example.org/products/#e> .
579
- )
580
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
581
- end
582
-
583
- it "returns defined prefixes" do
584
- n3 = %(
585
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
586
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
587
- @prefix : <http://test/> .
588
- :foo a rdfs:Class.
589
- :bar :d :c.
590
- :a :d :c.
591
- )
592
- reader = RDF::N3::Reader.new(n3)
593
- reader.each {|statement|}
594
- reader.prefixes.should == {
595
- :rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
596
- :rdfs => "http://www.w3.org/2000/01/rdf-schema#",
597
- nil => "http://test/"}
598
- end
599
- end
600
-
601
- describe "keywords" do
602
- [
603
- %(base <>.),
604
- %(keywords a.),
605
- %(:a is :b of :c.),
606
- %(:a @is :b of :c.),
607
- %(:a is :b @of :c.),
608
- %(:a has :b :c.),
609
- ].each do |n3|
610
- it "should require @ if keywords set to empty for '#{n3}'" do
611
- lambda do
612
- parse("@keywords . #{n3}", :base_uri => "http://a/b")
613
- end.should raise_error(RDF::ReaderError)
614
- end
615
- end
616
-
617
- [
618
- %(prefix :<>.),
619
- ].each do |n3|
620
- it "parses as local name if keywords set to empty for '#{n3}'" do
621
- lambda do
622
- parse("@keywords . #{n3}", :base_uri => "http://a/b")
623
- end.should_not raise_error(RDF::ReaderError)
624
- end
625
- end
626
- {
627
- %(:a a :b) => %(<http://a/b#a> <http://a/b#a> <http://a/b#b> .),
628
- %(:a :b true) => %(<http://a/b#a> <http://a/b#b> <http://a/b#true> .),
629
- %(:a :b false) => %(<http://a/b#a> <http://a/b#b> <http://a/b#false> .),
630
- %(c :a :t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
631
- %(:c a :t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
632
- %(:c :a t) => %(<http://a/b#c> <http://a/b#a> <http://a/b#t> .),
633
- }.each_pair do |n3, nt|
634
- it "should use default_ns for '#{n3}'" do
635
- parse("@keywords . #{n3}", :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
636
- end
637
- end
638
-
639
- {
640
- %(@keywords true. :a :b true.) => %(<http://a/b#a> <http://a/b#b> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
641
- %(@keywords false. :a :b false.) => %(<http://a/b#a> <http://a/b#b> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .),
642
- %(@keywords a. :a a :b.) => %(<http://a/b#a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#b> .),
643
- %(@keywords is. :a is :b @of :c.) => %(<http://a/b#c> <http://a/b#b> <http://a/b#a> .),
644
- %(@keywords of. :a @is :b of :c.) => %(<http://a/b#c> <http://a/b#b> <http://a/b#a> .),
645
- %(@keywords has. :a has :b :c.) => %(<http://a/b#a> <http://a/b#b> <http://a/b#c> .),
646
- } .each_pair do |n3, nt|
647
- it "should use keyword for '#{n3}'" do
648
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
649
- end
650
- end
651
-
652
- it "should raise error if unknown keyword set" do
653
- n3 = %(@keywords foo.)
654
- lambda do
655
- parse(n3, :base_uri => "http://a/b")
656
- end.should raise_error(RDF::ReaderError, /Undefined keywords used: foo/)
657
- end
658
- end
659
-
660
- describe "declaration ordering" do
661
- it "should process _ namespace binding after an initial use as a BNode" do
662
- n3 = %(
663
- _:a a :p.
664
- @prefix _: <http://underscore/> .
665
- _:a a :p.
666
- )
667
- nt = %(
668
- <http://underscore/a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#p> .
669
- _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#p> .
670
- )
671
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
672
- end
673
-
674
- it "should allow a prefix to be redefined" do
675
- n3 = %(
676
- @prefix a: <http://host/A#>.
677
- a:b a:p a:v .
678
-
679
- @prefix a: <http://host/Z#>.
680
- a:b a:p a:v .
681
- )
682
- nt = %(
683
- <http://host/A#b> <http://host/A#p> <http://host/A#v> .
684
- <http://host/Z#b> <http://host/Z#p> <http://host/Z#v> .
685
- )
686
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
687
- end
688
-
689
- it "should process sequential @base declarations (swap base.n3)" do
690
- n3 = %(
691
- @base <http://example.com/ontolgies>. <a> :b <foo/bar#baz>.
692
- @base <path/DFFERENT/>. <a2> :b2 <foo/bar#baz2>.
693
- @prefix : <#>. <d3> :b3 <e3>.
694
- )
695
- nt = %(
696
- <http://example.com/a> <http://example.com/ontolgies#b> <http://example.com/foo/bar#baz> .
697
- <http://example.com/path/DFFERENT/a2> <http://example.com/path/DFFERENT/b2> <http://example.com/path/DFFERENT/foo/bar#baz2> .
698
- <http://example.com/path/DFFERENT/d3> <http://example.com/path/DFFERENT/#b3> <http://example.com/path/DFFERENT/e3> .
699
- )
700
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
701
- end
702
- end
703
-
704
- describe "BNodes" do
705
- it "should create BNode for identifier with '_' prefix" do
706
- n3 = %(@prefix a: <http://foo/a#> . _:a a:p a:v .)
707
- nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
708
- end
709
-
710
- it "should create BNode for [] as subject" do
711
- n3 = %(@prefix a: <http://foo/a#> . [] a:p a:v .)
712
- nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
713
- g = parse(n3, :base_uri => "http://a/b")
714
- normalize_bnodes(g, "bnode0").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug, :compare => :array)
715
- end
716
-
717
- it "should create BNode for [] as predicate" do
718
- n3 = %(@prefix a: <http://foo/a#> . a:s [] a:o .)
719
- nt = %(<http://foo/a#s> _:bnode0 <http://foo/a#o> .)
720
- g = parse(n3, :base_uri => "http://a/b")
721
- normalize_bnodes(g, "bnode0").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug, :compare => :array, :anon => "bnode")
722
- end
723
-
724
- it "should create BNode for [] as object" do
725
- n3 = %(@prefix a: <http://foo/a#> . a:s a:p [] .)
726
- nt = %(<http://foo/a#s> <http://foo/a#p> _:bnode0 .)
727
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
728
- end
729
-
730
- it "should create BNode for [] as statement" do
731
- n3 = %([:a :b] .)
732
- nt = %(_:bnode0 <http://a/b#a> <http://a/b#b> .)
733
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
734
- end
735
-
736
- it "should create statements with BNode subjects using [ pref obj]" do
737
- n3 = %(@prefix a: <http://foo/a#> . [ a:p a:v ] .)
738
- nt = %(_:bnode0 <http://foo/a#p> <http://foo/a#v> .)
739
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
740
- end
741
-
742
- it "should create BNode as a single object" do
743
- n3 = %(@prefix a: <http://foo/a#> . a:b a:oneRef [ a:pp "1" ; a:qq "2" ] .)
744
- nt = %(
745
- _:bnode0 <http://foo/a#pp> "1" .
746
- _:bnode0 <http://foo/a#qq> "2" .
747
- <http://foo/a#b> <http://foo/a#oneRef> _:bnode0 .
748
- )
749
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
750
- end
751
-
752
- it "should create a shared BNode" do
753
- n3 = %(
754
- @prefix a: <http://foo/a#> .
755
-
756
- a:b1 a:twoRef _:a .
757
- a:b2 a:twoRef _:a .
758
-
759
- _:a :pred [ a:pp "1" ; a:qq "2" ].
760
- )
761
- nt = %(
762
- <http://foo/a#b1> <http://foo/a#twoRef> _:a .
763
- <http://foo/a#b2> <http://foo/a#twoRef> _:a .
764
- _:bnode0 <http://foo/a#pp> "1" .
765
- _:bnode0 <http://foo/a#qq> "2" .
766
- _:a :pred _:bnode0 .
767
- )
768
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
769
- end
770
-
771
- it "should create nested BNodes" do
772
- n3 = %(
773
- @prefix a: <http://foo/a#> .
774
-
775
- a:a a:p [ a:p2 [ a:p3 "v1" , "v2" ; a:p4 "v3" ] ; a:p5 "v4" ] .
776
- )
777
- nt = %(
778
- _:bnode0 <http://foo/a#p3> "v1" .
779
- _:bnode0 <http://foo/a#p3> "v2" .
780
- _:bnode0 <http://foo/a#p4> "v3" .
781
- _:bnode1 <http://foo/a#p2> _:bnode0 .
782
- _:bnode1 <http://foo/a#p5> "v4" .
783
- <http://foo/a#a> <http://foo/a#p> _:bnode1 .
784
- )
785
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
786
- end
787
-
788
- describe "from paths" do
789
- it "should create bnode for path x!p" do
790
- n3 = %(:x2!:y2 :p2 "3" .)
791
- nt = %(:x2 :y2 _:bnode0 . _:bnode0 :p2 "3" .)
792
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
793
- end
794
-
795
- it "should create bnode for path x^p" do
796
- n3 = %(:x2^:y2 :p2 "3" .)
797
- nt = %(_:bnode0 :y2 :x2 . _:bnode0 :p2 "3" .)
798
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
799
- end
800
-
801
- it "should decode :joe!fam:mother!loc:office!loc:zip as Joe's mother's office's zipcode" do
802
- n3 = %(
803
- @prefix fam: <http://foo/fam#> .
804
- @prefix loc: <http://foo/loc#> .
805
-
806
- :joe!fam:mother!loc:office!loc:zip .
807
- )
808
- nt = %(
809
- :joe <http://foo/fam#mother> _:bnode0 .
810
- _:bnode0 <http://foo/loc#office> _:bnode1 .
811
- _:bnode1 <http://foo/loc#zip> _:bnode2 .
812
- )
813
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
814
- end
815
-
816
- it "should decode :joe!fam:mother^fam:mother Anyone whose mother is Joe's mother." do
817
- n3 = %(
818
- @prefix fam: <http://foo/fam#> .
819
- @prefix loc: <http://foo/loc#> .
820
-
821
- :joe!fam:mother^fam:mother .
822
- )
823
- nt = %(
824
- :joe <http://foo/fam#mother> _:bnode0 .
825
- _:bnode1 <http://foo/fam#mother> _:bnode0 .
826
- )
827
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
828
- end
829
-
830
- it "should decode path with property list." do
831
- n3 = %(
832
- @prefix a: <http://a/ns#>.
833
- :a2!a:b2!a:c2 :q1 "3" ; :q2 "4" , "5" .
834
- )
835
- nt = %(
836
- :a2 <http://a/ns#b2> _:bnode0 .
837
- _:bnode0 <http://a/ns#c2> _:bnode1 .
838
- _:bnode1 :q1 "3" .
839
- _:bnode1 :q2 "4" .
840
- _:bnode1 :q2 "5" .
841
- )
842
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
843
- end
844
-
845
- it "should decode path as object(1)" do
846
- n3 = %(:a :b "lit"^:c.)
847
- nt = %(
848
- :a :b _:bnode .
849
- _:bnode :c "lit" .
850
- )
851
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
852
- end
853
-
854
- it "should decode path as object(2)" do
855
- n3 = %(@prefix a: <http://a/ns#>. :r :p :o!a:p1!a:p2 .)
856
- nt = %(
857
- :o <http://a/ns#p1> _:bnode0 .
858
- _:bnode0 <http://a/ns#p2> _:bnode1 .
859
- :r :p _:bnode1 .
860
- )
861
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
862
- end
863
- end
864
- end
865
-
866
- describe "formulae" do
867
- before(:each) { @repo = RDF::Repository.new }
868
-
869
- it "creates an RDF::Graph instance for formula" do
870
- n3 = %(:a :b {})
871
- parse(n3, :graph => @repo, :base_uri => "http://a/b")
872
- statement = @repo.statements.first
873
- statement.object.should be_a(RDF::Node)
874
- end
875
-
876
- it "adds statements with context" do
877
-
878
- end
879
-
880
- it "creates variables with ?" do
881
-
882
- end
883
-
884
- context "contexts" do
885
- before(:each) do
886
- n3 = %(
887
- # Test data in notation3 http://www.w3.org/DesignIssues/Notation3.html
888
- #
889
- @prefix u: <http://www.example.org/utilities#> .
890
- @prefix : <#> .
891
-
892
- :assumption = { :fred u:knows :john .
893
- :john u:knows :mary .} .
894
-
895
- :conclusion = { :fred u:knows :mary . } .
896
-
897
- # The empty context is trivially true.
898
- # Check that we can input it and output it!
899
-
900
- :trivialTruth = { }.
901
-
902
- # ENDS
903
- )
904
- @repo = RDF::Repository.new
905
- parse(n3, :graph => @repo, :base_uri => "http://a/b")
906
- end
907
-
908
- it "assumption graph has 2 statements" do
909
- tt = @repo.first(:subject => RDF::URI.new("http://a/b#assumption"), :predicate => RDF::OWL.sameAs)
910
- tt.object.should be_a(RDF::Node)
911
- @repo.query(:context => tt.object).to_a.length.should == 2
912
- end
913
-
914
- it "conclusion graph has 1 statements" do
915
- tt = @repo.first(:subject => RDF::URI.new("http://a/b#conclusion"), :predicate => RDF::OWL.sameAs)
916
- tt.object.should be_a(RDF::Node)
917
- @repo.query(:context => tt.object).to_a.length.should == 1
918
- end
919
-
920
- it "trivialTruth equivalent to empty graph" do
921
- tt = @repo.first(:subject => RDF::URI.new("http://a/b#trivialTruth"), :predicate => RDF::OWL.sameAs)
922
- tt.object.should be_a(RDF::Node)
923
- @repo.query(:context => tt.object) do |s|
924
- puts "statement: #{s}"
925
- end
926
- end
927
-
928
- end
929
- end
930
-
931
- describe "object lists" do
932
- it "should create 2 statements for simple list" do
933
- n3 = %(:a :b :c, :d)
934
- nt = %(<http://a/b#a> <http://a/b#b> <http://a/b#c> . <http://a/b#a> <http://a/b#b> <http://a/b#d> .)
935
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
936
- end
937
- end
938
-
939
- describe "property lists" do
940
- it "should parse property list" do
941
- n3 = %(
942
- @prefix a: <http://foo/a#> .
943
-
944
- a:b a:p1 "123" ; a:p1 "456" .
945
- a:b a:p2 a:v1 ; a:p3 a:v2 .
946
- )
947
- nt = %(
948
- <http://foo/a#b> <http://foo/a#p1> "123" .
949
- <http://foo/a#b> <http://foo/a#p1> "456" .
950
- <http://foo/a#b> <http://foo/a#p2> <http://foo/a#v1> .
951
- <http://foo/a#b> <http://foo/a#p3> <http://foo/a#v2> .
952
- )
953
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
954
- end
955
- end
956
-
957
- describe "lists" do
958
- it "should parse empty list" do
959
- n3 = %(@prefix :<http://example.com/>. :empty :set ().)
960
- nt = %(
961
- <http://example.com/empty> <http://example.com/set> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .)
962
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
963
- end
964
-
965
- it "should parse list with single element" do
966
- n3 = %(@prefix :<http://example.com/>. :gregg :wrote ("RdfContext").)
967
- nt = %(
968
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "RdfContext" .
969
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
970
- <http://example.com/gregg> <http://example.com/wrote> _:bnode0 .
971
- )
972
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
973
- end
974
-
975
- it "should parse list with multiple elements" do
976
- n3 = %(@prefix :<http://example.com/>. :gregg :name ("Gregg" "Barnum" "Kellogg").)
977
- nt = %(
978
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Gregg" .
979
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode1 .
980
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Barnum" .
981
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
982
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "Kellogg" .
983
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
984
- <http://example.com/gregg> <http://example.com/name> _:bnode0 .
985
- )
986
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
987
- end
988
-
989
- it "should parse unattached lists" do
990
- n3 = %(
991
- @prefix a: <http://foo/a#> .
992
-
993
- ("1" "2" "3") .
994
- # This is not a statement.
995
- () .
996
- )
997
- nt = %(
998
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1" .
999
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode1 .
1000
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "2" .
1001
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
1002
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "3" .
1003
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
1004
- )
1005
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1006
- end
1007
-
1008
- it "should add property to nil list" do
1009
- n3 = %(@prefix a: <http://foo/a#> . () a:prop "nilProp" .)
1010
- nt = %(<http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://foo/a#prop> "nilProp" .)
1011
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1012
- end
1013
-
1014
- it "should parse with compound items" do
1015
- n3 = %(
1016
- @prefix a: <http://foo/a#> .
1017
- a:a a:p (
1018
- [ a:p2 "v1" ]
1019
- <http://resource1>
1020
- <http://resource2>
1021
- ("inner list")
1022
- ) .
1023
- <http://resource1> a:p "value" .
1024
- )
1025
- nt = %(
1026
- <http://foo/a#a> <http://foo/a#p> _:bnode3 .
1027
- <http://resource1> <http://foo/a#p> "value" .
1028
- _:bnode3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:bnode5 .
1029
- _:bnode3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode2 .
1030
- _:bnode5 <http://foo/a#p2> "v1" .
1031
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://resource1> .
1032
- _:bnode2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode1 .
1033
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://resource2> .
1034
- _:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:bnode0 .
1035
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:bnode4 .
1036
- _:bnode0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
1037
- _:bnode4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "inner list" .
1038
- _:bnode4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
1039
- )
1040
- g = parse(n3, :base_uri => "http://a/b")
1041
- g.subjects.to_a.length.should == 8
1042
- n = g.first_object(:subject => RDF::URI.new("http://foo/a#a"), :predicate => RDF::URI.new("http://foo/a#p"))
1043
- n.should be_a(RDF::Node)
1044
- seq = RDF::List.new(n, g)
1045
- seq.to_a.length.should == 4
1046
- seq.first.should be_a(RDF::Node)
1047
- seq.second.should == RDF::URI.new("http://resource1")
1048
- seq.third.should == RDF::URI.new("http://resource2")
1049
- seq.fourth.should be_a(RDF::Node)
1050
- end
1051
-
1052
- end
1053
-
1054
- # n3p tests taken from http://inamidst.com/n3p/test/
1055
- describe "with real data tests" do
1056
- dirs = %w(misc lcsh rdflib n3p)
1057
- dirs.each do |dir|
1058
- dir_name = File.join(File.dirname(__FILE__), '..', 'test', 'n3_tests', dir, '*.n3')
1059
- Dir.glob(dir_name).each do |n3|
1060
- it "#{dir} #{n3}" do
1061
- test_file(n3)
1062
- end
1063
- end
1064
- end
1065
- end
1066
-
1067
- describe "with AggregateGraph tests" do
1068
- describe "with a type" do
1069
- it "should have 3 namespaces" do
1070
- n3 = %(
1071
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1072
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
1073
- @prefix : <http://test/> .
1074
- :foo a rdfs:Class.
1075
- :bar :d :c.
1076
- :a :d :c.
1077
- )
1078
- nt = %(
1079
- <http://test/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
1080
- <http://test/bar> <http://test/d> <http://test/c> .
1081
- <http://test/a> <http://test/d> <http://test/c> .
1082
- )
1083
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1084
- end
1085
- end
1086
-
1087
- describe "with blank clause" do
1088
- it "should have 4 namespaces" do
1089
- n3 = %(
1090
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1091
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
1092
- @prefix : <http://test/> .
1093
- @prefix log: <http://www.w3.org/2000/10/swap/log#>.
1094
- :foo a rdfs:Resource.
1095
- :bar rdfs:isDefinedBy [ a log:Formula ].
1096
- :a :d :e.
1097
- )
1098
- nt = %(
1099
- <http://test/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Resource> .
1100
- _:g2160128180 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/swap/log#Formula> .
1101
- <http://test/bar> <http://www.w3.org/2000/01/rdf-schema#isDefinedBy> _:g2160128180 .
1102
- <http://test/a> <http://test/d> <http://test/e> .
1103
- )
1104
- parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1105
- end
1106
- end
1107
-
1108
- describe "with empty subject" do
1109
- before(:each) do
1110
- @graph = parse(%(
1111
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1112
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
1113
- @prefix log: <http://www.w3.org/2000/10/swap/log#>.
1114
- @prefix : <http://test/> .
1115
- <> a log:N3Document.
1116
- ), :base_uri => "http://test/")
1117
- end
1118
-
1119
- it "should have 4 namespaces" do
1120
- nt = %(
1121
- <http://test/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/swap/log#N3Document> .
1122
- )
1123
- @graph.should be_equivalent_graph(nt, :about => "http://test/", :trace => @debug)
1124
- end
1125
-
1126
- it "should have default subject" do
1127
- @graph.size.should == 1
1128
- @graph.statements.first.subject.to_s.should == "http://test/"
1129
- end
1130
- end
1131
- end
1132
- end
1133
-
1134
- describe "canonicalization" do
1135
- # {
1136
- # "<http://foo>" => "<http://foo>",
1137
- # "<http://foo/a>" => "<http://foo/a>",
1138
- # "<http://foo#a>" => "<http://foo#a>",
1139
- #
1140
- # "<http://foo/>" => "<http://foo/>",
1141
- # "<http://foo/#a>" => "<http://foo/#a>",
1142
- #
1143
- # "<http://foo#>" => "<http://foo#>",
1144
- # "<http://foo#a>" => "<http://foo/a>",
1145
- # "<http://foo#/a>" => "<http://foo/a>",
1146
- # "<http://foo##a>" => "<http://foo#a>",
1147
- #
1148
- # "<http://foo/bar>" => "<http://foo/bar>",
1149
- # "<http://foo/bar>" => "<http://foo/a>",
1150
- # "<http://foo/bar/a>" => "<http://foo/a>",
1151
- # "<http://foo/bar#a>" => "<http://foo/bar#a>",
1152
- #
1153
- # "<http://foo/bar/>" => "<http://foo/bar/>",
1154
- # "<http://foo/bar/a>" => "<http://foo/bar/a>",
1155
- # "<http://foo/bar//a>" => "<http://foo/a>",
1156
- # "<http://foo/bar/#a>" => "<http://foo/bar/#a>",
1157
- #
1158
- # "<http://foo/bar#>" => "<http://foo/bar#>",
1159
- # "<http://foo/bar#a>" => "<http://foo/a>",
1160
- # "<http://foo/bar#/a>" => "<http://foo/a>",
1161
- # "<http://foo/bar##a>" => "<http://foo/bar#a>",
1162
- #
1163
- # "<http://foo/bar##D%C3%BCrst>" => "<http://foo/bar#D%C3%BCrst>",
1164
- # "<http://foo/bar##Dürst>" => "<http://foo/bar#D\\u00FCrst>",
1165
- # }.each_pair do |input, result|
1166
- # it "returns subject #{result} given #{input}" do
1167
- # n3 = %(#{input} a :b)
1168
- # nt = %(#{result} <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://a/b#b> .)
1169
- # parse(n3, :base_uri => "http://a/b", :canonicalize => true).should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1170
- # end
1171
- # end
1172
-
1173
- {
1174
- %("+1"^^xsd:integer) => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
1175
- %(+1) => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
1176
- %(true) => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
1177
- %("lang"@EN) => %("lang"@en),
1178
- }.each_pair do |input, result|
1179
- it "returns object #{result} given #{input}" do
1180
- n3 = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{input} .)
1181
- nt = %(<http://a/b#a> <http://a/b#b> #{result} .)
1182
- parse(n3, :base_uri => "http://a/b", :canonicalize => true).should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
1183
- end
1184
- end
1185
- end
1186
-
1187
- describe "validation" do
1188
- {
1189
- %(:y :p1 "xyz"^^xsd:integer .) => %r("xyz" is not a valid .*),
1190
- %(:y :p1 "12xyz"^^xsd:integer .) => %r("12xyz" is not a valid .*),
1191
- %(:y :p1 "xy.z"^^xsd:double .) => %r("xy\.z" is not a valid .*),
1192
- %(:y :p1 "+1.0z"^^xsd:double .) => %r("\+1.0z" is not a valid .*),
1193
- %(:a :b .) =>RDF::ReaderError,
1194
- %(:a :b 'single quote' .) => RDF::ReaderError,
1195
- %(:a "literal value" :b .) => RDF::ReaderError,
1196
- %(@keywords prefix. :e prefix :f .) => RDF::ReaderError
1197
- }.each_pair do |n3, error|
1198
- it "should raise '#{error}' for '#{n3}'" do
1199
- lambda {
1200
- parse("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . #{n3}", :base_uri => "http://a/b", :validate => true)
1201
- }.should raise_error(error)
1202
- end
1203
- end
1204
- end
1205
-
1206
- it "should parse rdf_core testcase" do
1207
- sampledoc = <<-EOF;
1208
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#PositiveParserTest> .
1209
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#approval> <http://lists.w3.org/Archives/Public/w3c-rdfcore-wg/2002Mar/0235.html> .
1210
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#inputDocument> <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/test001.rdf> .
1211
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#issue> <http://www.w3.org/2000/03/rdf-tracking/#rdfms-xml-base> .
1212
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#outputDocument> <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/test001.nt> .
1213
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/Manifest.rdf#test001> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#status> "APPROVED" .
1214
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/test001.nt> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#NT-Document> .
1215
- <http://www.w3.org/2000/10/rdf-tests/rdfcore/xmlbase/test001.rdf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#RDF-XML-Document> .
1216
- EOF
1217
- graph = parse(sampledoc, :base_uri => "http://www.w3.org/2000/10/rdf-tests/rdfcore/amp-in-url/Manifest.rdf")
1218
-
1219
- graph.should be_equivalent_graph(sampledoc,
1220
- :about => "http://www.w3.org/2000/10/rdf-tests/rdfcore/amp-in-url/Manifest.rdf",
1221
- :trace => @debug
1222
- )
1223
- end
1224
-
1225
- def parse(input, options = {})
1226
- @debug = []
1227
- graph = options[:graph] || RDF::Graph.new
1228
- RDF::N3::Reader.new(input, {:debug => @debug, :validate => true, :canonicalize => false}.merge(options)).each do |statement|
1229
- graph << statement
1230
- end
1231
- graph
1232
- end
1233
-
1234
- def test_file(filepath)
1235
- n3_string = File.read(filepath)
1236
- @graph = parse(File.open(filepath), :base_uri => "file:#{filepath}")
1237
-
1238
- nt_string = File.read(filepath.sub('.n3', '.nt'))
1239
- @graph.should be_equivalent_graph(nt_string,
1240
- :about => "file:#{filepath}",
1241
- :trace => @debug)
1242
- end
1243
- end