rdf-n3 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/script/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rdf/n3.rb'}"
7
+ puts "Loading rdf-n3 gem"
8
+ exec "#{irb} #{libs} --simple-prompt"
data/script/parse ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby -s
2
+ require 'rubygems'
3
+ $:.unshift(File.join(File.dirname(__FILE__), "..", 'lib'))
4
+ require 'rdf/n3'
5
+ require 'getoptlong'
6
+
7
+ def parse(input, base)
8
+ puts RDF::Writer.for($format.to_sym).buffer { |writer|
9
+ RDF::N3::Reader.new(input, :base_uri => base, :strict => true).each do |statement|
10
+ writer << statement
11
+ end
12
+ }
13
+ end
14
+
15
+ mode = ARGV.shift
16
+ raise "Mode must be one of 'parse'" unless mode == "parse"
17
+
18
+ $verbose = false
19
+ $format = :ntriples
20
+ base_uri = "http://example.com"
21
+ input = nil
22
+
23
+ opts = GetoptLong.new(
24
+ ["--debug", GetoptLong::NO_ARGUMENT],
25
+ ["--verbose", GetoptLong::NO_ARGUMENT],
26
+ ["--quiet", GetoptLong::NO_ARGUMENT],
27
+ ["--format", GetoptLong::REQUIRED_ARGUMENT],
28
+ ["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
29
+ ["--uri", GetoptLong::REQUIRED_ARGUMENT]
30
+ )
31
+ opts.each do |opt, arg|
32
+ case opt
33
+ when '--verbose' then $verbose = true
34
+ when '--quiet' then $quiet = true
35
+ when '--debug' then $DEBUG = true
36
+ when '--execute' then input = arg
37
+ when '--format' then $format = arg
38
+ when '--uri' then base_uri = arg
39
+ end
40
+ end
41
+
42
+ if ARGV.empty?
43
+ s = input ? input : $stdin.read
44
+ parse(StringIO.new(s), base_uri)
45
+ else
46
+ ARGV.each do |test_file|
47
+ parse(File.open(test_file), base_uri)
48
+ end
49
+ end
data/spec/cwm_spec.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
- include RdfContext
2
+ require 'rdf/rdfxml'
3
3
 
4
- describe "RDF::N3::Reader" do
4
+ describe RDF::N3::Reader do
5
5
  describe "w3c cwm tests" do
6
6
  require 'rdf_helper'
7
7
 
8
8
  def self.test_cases
9
- RdfHelper::TestCase.test_cases(CWM_TEST, SWAP_DIR) rescue []
9
+ RdfHelper::TestCase.test_cases(CWM_TEST, SWAP_DIR)
10
10
  end
11
11
 
12
12
  # Negative parser tests should raise errors.
@@ -16,8 +16,16 @@ describe "RDF::N3::Reader" do
16
16
  #puts t.inspect
17
17
  specify "test #{t.name}: " + (t.description || "#{t.inputDocument} against #{t.outputDocument}") do
18
18
  begin
19
- t.run_test do |rdf_string, parser|
20
- parser.parse(rdf_string, t.about.uri.to_s, :strict => true, :debug => [])
19
+ t.run_test do |rdf_string|
20
+ t.debug = []
21
+ g = RDF::Graph.new
22
+ RDF::Reader.for(t.inputDocument).new(rdf_string,
23
+ :base_uri => t.about,
24
+ :strict => true,
25
+ :debug => t.debug).each do |statement|
26
+ g << statement
27
+ end
28
+ g
21
29
  end
22
30
  rescue #Spec::Expectations::ExpectationNotMetError => e
23
31
  if t.status == "pending"
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe RDF::N3::Format do
4
+ context "discovery" do
5
+ {
6
+ "n3" => RDF::Format.for(:n3),
7
+ "etc/foaf.n3" => RDF::Format.for("etc/foaf.n3"),
8
+ "etc/foaf.ttl" => RDF::Format.for("etc/foaf.ttl"),
9
+ "foaf.n3" => RDF::Format.for(:file_name => "foaf.n3"),
10
+ "foaf.ttl" => RDF::Format.for(:file_name => "foaf.ttl"),
11
+ ".n3" => RDF::Format.for(:file_extension => "n3"),
12
+ ".ttl" => RDF::Format.for(:file_extension => "ttl"),
13
+ "text/n3" => RDF::Format.for(:content_type => "text/n3"),
14
+ "text/turtle" => RDF::Format.for(:content_type => "text/turtle"),
15
+ }.each_pair do |label, format|
16
+ it "should discover '#{label}'" do
17
+ format.should == RDF::N3::Format
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,382 @@
1
+ # coding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+ require 'date'
4
+ require 'time'
5
+ require 'nokogiri'
6
+
7
+ describe RDF::Literal do
8
+ describe "an untyped string" do
9
+ subject {RDF::Literal.new("gregg")}
10
+ it "should be equal if they have the same contents" do should == RDF::Literal.new("gregg") end
11
+ it "should not be equal if they do not have the same contents" do should_not == RDF::Literal.new("tim") end
12
+ it "should match a string" do should == "gregg" end
13
+ it "should return a string using to_s" do subject.to_s.should == %("gregg") end
14
+
15
+ describe "should handle specific cases" do
16
+ {
17
+ '"Gregg"' => RDF::Literal.new("Gregg"),
18
+ '"\u677E\u672C \u540E\u5B50"' => RDF::Literal.new("松本 后子"),
19
+ '"D\u00FCrst"' => RDF::Literal.new("Dürst"),
20
+ }.each_pair do |encoded, literal|
21
+ it "should encode '#{literal.value}'" do
22
+ literal.to_s.should == encoded
23
+ end
24
+ end
25
+
26
+ # Ruby 1.9 only
27
+ {
28
+ '"\U00015678another"' => RDF::Literal.new("\u{15678}another"),
29
+ }.each_pair do |encoded, literal|
30
+ it "should encode '#{literal.value}'" do
31
+ literal.to_s.should == encoded
32
+ end
33
+ end if defined?(::Encoding)
34
+ end
35
+
36
+ describe "encodings" do
37
+ it "should return n3" do subject.to_s.should == "\"gregg\"" end
38
+ end
39
+
40
+ describe "with extended characters" do
41
+ subject { RDF::Literal.new("松本 后子") }
42
+
43
+ describe "encodings" do
44
+ it "should return n3" do subject.to_s.should == '"\u677E\u672C \u540E\u5B50"' end
45
+ end
46
+ end
47
+
48
+ describe "with a language" do
49
+ subject { RDF::Literal.new("gregg", :language => "en") }
50
+
51
+ it "should accept a language tag" do
52
+ subject.language.should == :en
53
+ end
54
+
55
+ it "should be equal if they have the same contents and language" do
56
+ should == RDF::Literal.new("gregg", :language => "en")
57
+ end
58
+
59
+ it "should not be equal if they do not have the same contents" do
60
+ should_not == RDF::Literal.new("tim", :language => "en")
61
+ end
62
+
63
+ it "should not be equal if they do not have the same language" do
64
+ should_not == RDF::Literal.new("gregg", :language => "fr")
65
+ end
66
+
67
+ describe "encodings" do
68
+ it "should return n3" do subject.to_s.should == "\"gregg\"@en" end
69
+ end
70
+
71
+ it "should normalize language tags to lower case" do
72
+ f = RDF::Literal.new("gregg", :language => "EN")
73
+ f.language.should == :en
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "a typed string" do
79
+ subject { RDF::Literal.new("gregg", :datatype => RDF::XSD.string) }
80
+
81
+ it "accepts an encoding" do
82
+ subject.datatype.to_s.should == RDF::XSD.string.to_s
83
+ end
84
+
85
+ it "should be equal if they have the same contents and datatype" do
86
+ should == RDF::Literal.new("gregg", :datatype => RDF::XSD.string)
87
+ end
88
+
89
+ it "should not be equal if they do not have the same contents" do
90
+ should_not == RDF::Literal.new("tim", :datatype => RDF::XSD.string)
91
+ end
92
+
93
+ it "should not be equal if they do not have the same datatype" do
94
+ should_not == RDF::Literal.new("gregg", :datatype => RDF::XSD.token)
95
+ end
96
+
97
+ describe "encodings" do
98
+ it "should return n3" do subject.to_s.should == "\"gregg\"^^<http://www.w3.org/2001/XMLSchema#string>" end
99
+ end
100
+ end
101
+
102
+ describe "a boolean" do
103
+ subject { RDF::Literal.new(true, :datatype => RDF::XSD.boolean) }
104
+ describe "encodings" do
105
+ it "should return n3" do subject.to_s.should == "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>" end
106
+ end
107
+
108
+ it "should infer type" do
109
+ int = RDF::Literal.new(true)
110
+ int.datatype.should == RDF::XSD.boolean
111
+ end
112
+
113
+ it "should have string contents" do subject.value.should == "true" end
114
+ it "should have native contents" do subject.object.should == true end
115
+ end
116
+
117
+ describe "an integer" do
118
+ subject { RDF::Literal.new(5, :datatype => RDF::XSD.int) }
119
+ describe "encodings" do
120
+ it "should return n3" do subject.to_s.should == "\"5\"^^<http://www.w3.org/2001/XMLSchema#int>" end
121
+ end
122
+
123
+ it "should infer type" do
124
+ int = RDF::Literal.new(15)
125
+ int.datatype.should == RDF::XSD.integer
126
+ end
127
+
128
+ it "should have string contents" do subject.value.should == "5" end
129
+ it "should have native contents" do subject.object.should == 5 end
130
+ end
131
+
132
+ describe "a float" do
133
+ subject { RDF::Literal.new(15.4, :datatype => RDF::XSD.float) }
134
+ describe "encodings" do
135
+ it "should return n3" do subject.to_s.should == "\"15.4\"^^<http://www.w3.org/2001/XMLSchema#float>" end
136
+ end
137
+
138
+ it "should infer type" do
139
+ float = RDF::Literal.new(15.4)
140
+ float.datatype.should == RDF::XSD.double
141
+ end
142
+
143
+ it "should have string contents" do subject.value.should == "15.4" end
144
+ it "should have native contents" do subject.object.should == 15.4 end
145
+ end
146
+
147
+ describe "a date" do
148
+ before(:each) { @value = Date.parse("2010-01-02Z") }
149
+ subject { RDF::Literal.new(@value, :datatype => RDF::XSD.date) }
150
+ describe "encodings" do
151
+ it "should return n3" do subject.to_s.should == "\"2010-01-02Z\"^^<http://www.w3.org/2001/XMLSchema#date>" end
152
+ end
153
+
154
+ it "should infer type" do
155
+ int = RDF::Literal.new(@value)
156
+ int.datatype.should == RDF::XSD.date
157
+ end
158
+
159
+ it "should have string contents" do subject.value.should == "2010-01-02Z" end
160
+ it "should have native contents" do subject.object.should == @value end
161
+ end
162
+
163
+ describe "a dateTime" do
164
+ before(:each) { @value = DateTime.parse('2010-01-03T01:02:03Z') }
165
+ subject { RDF::Literal.new(@value, :datatype => RDF::XSD.dateTime) }
166
+ describe "encodings" do
167
+ it "should return n3" do subject.to_s.should == "\"2010-01-03T01:02:03Z\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" end
168
+ end
169
+
170
+ it "should infer type" do
171
+ int = RDF::Literal.new(@value)
172
+ int.datatype.should == RDF::XSD.dateTime
173
+ end
174
+
175
+ it "should have string contents" do subject.value.should == "2010-01-03T01:02:03Z" end
176
+ it "should have native contents" do subject.object.should == @value end
177
+ end
178
+
179
+ describe "a time" do
180
+ before(:each) { @value = Time.parse('01:02:03Z') }
181
+ subject { RDF::Literal.new(@value, :datatype => RDF::XSD.time) }
182
+ describe "encodings" do
183
+ it "should return n3" do subject.to_s.should == "\"01:02:03Z\"^^<http://www.w3.org/2001/XMLSchema#time>" end
184
+ end
185
+
186
+ it "should infer type" do
187
+ int = RDF::Literal.new(@value)
188
+ int.datatype.should == RDF::XSD.time
189
+ end
190
+
191
+ it "should have string contents" do subject.value.should == "01:02:03Z" end
192
+ it "should have native contents" do subject.object.should == @value end
193
+ end
194
+
195
+ describe "XML Literal" do
196
+ describe "with no namespace" do
197
+ subject { RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF.XMLLiteral) }
198
+ it "should indicate xmlliteral?" do
199
+ subject.xmlliteral?.should == true
200
+ end
201
+
202
+ describe "encodings" do
203
+ it "should return n3" do subject.to_s.should == "\"foo <sup>bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
204
+ end
205
+
206
+ it "should be equal if they have the same contents" do
207
+ should == RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF.XMLLiteral)
208
+ end
209
+
210
+ it "should be a XMLLiteral encoding" do
211
+ subject.datatype.should == RDF.XMLLiteral
212
+ end
213
+ end
214
+
215
+ describe "with a namespace" do
216
+ subject {
217
+ RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF.XMLLiteral,
218
+ :namespaces => {"dc" => RDF::DC.to_s})
219
+ }
220
+
221
+ describe "encodings" do
222
+ it "should return n3" do subject.to_s.should == "\"foo <sup>bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
223
+ end
224
+
225
+ describe "and language" do
226
+ subject {
227
+ RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF.XMLLiteral,
228
+ :namespaces => {"dc" => RDF::DC.to_s},
229
+ :language => :fr)
230
+ }
231
+
232
+ describe "encodings" do
233
+ it "should return n3" do subject.to_s.should == "\"foo <sup xml:lang=\\\"fr\\\">bar</sup> baz!\"\^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
234
+ end
235
+ end
236
+
237
+ describe "and language with an existing language embedded" do
238
+ subject {
239
+ RDF::Literal.new("foo <sup>bar</sup><sub xml:lang=\"en\">baz</sub>",
240
+ :datatype => RDF.XMLLiteral,
241
+ :language => :fr)
242
+ }
243
+
244
+ describe "encodings" do
245
+ it "should return n3" do subject.to_s.should == "\"foo <sup xml:lang=\\\"fr\\\">bar</sup><sub xml:lang=\\\"en\\\">baz</sub>\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
246
+ end
247
+ end
248
+
249
+ describe "with a default namespace" do
250
+ subject {
251
+ RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF.XMLLiteral,
252
+ :namespaces => {"" => RDF::DC.to_s})
253
+ }
254
+
255
+ describe "encodings" do
256
+ it "should return n3" do subject.to_s.should == "\"foo <sup xmlns=\\\"http://purl.org/dc/terms/\\\">bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
257
+ end
258
+ end
259
+
260
+ describe "with multiple namespaces" do
261
+ subject {
262
+ RDF::Literal.new("foo <sup xmlns:dc=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">bar</sup> baz!", :datatype => RDF.XMLLiteral)
263
+ }
264
+ it "should ignore namespace order" do
265
+ g = RDF::Literal.new("foo <sup xmlns:dc=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">bar</sup> baz!", :datatype => RDF.XMLLiteral)
266
+ should == g
267
+ end
268
+ end
269
+ end
270
+
271
+ describe "an n3 literal" do
272
+ {
273
+ "Gregg" => 'Gregg',
274
+ "Dürst" => 'D\u00FCrst',
275
+ "simple literal" => 'simple literal',
276
+ "backslash:\\" => 'backslash:\\\\',
277
+ "dquote:\"" => 'dquote:\\"',
278
+ "newline:\n" => 'newline:\\n',
279
+ "return:\r" => 'return:\\r',
280
+ "tab:\t" => 'tab:\\t',
281
+ }.each_pair do |name, value|
282
+ specify "test #{name}" do
283
+ RDF::Literal.new(value.rdf_unescape).value.should == name
284
+ end
285
+ end
286
+ end
287
+
288
+ describe "valid content" do
289
+ {
290
+ "true" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
291
+ "false" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
292
+ "tRuE" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
293
+ "FaLsE" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
294
+ "1" => %("true"^^<http://www.w3.org/2001/XMLSchema#boolean>),
295
+ "0" => %("false"^^<http://www.w3.org/2001/XMLSchema#boolean>),
296
+ }.each_pair do |lit, n3|
297
+ it "should validate boolean '#{lit}'" do
298
+ RDF::Literal.new(lit, :datatype => RDF::XSD.boolean).valid?.should be_true
299
+ end
300
+
301
+ it "should normalize boolean '#{lit}'" do
302
+ RDF::Literal.new(lit, :datatype => RDF::XSD.boolean).to_s.should == n3
303
+ end
304
+ end
305
+
306
+ {
307
+ "01" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
308
+ "1" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
309
+ "-1" => %("-1"^^<http://www.w3.org/2001/XMLSchema#integer>),
310
+ "+1" => %("1"^^<http://www.w3.org/2001/XMLSchema#integer>),
311
+ }.each_pair do |lit, n3|
312
+ it "should validate integer '#{lit}'" do
313
+ RDF::Literal.new(lit, :datatype => RDF::XSD.integer).valid?.should be_true
314
+ end
315
+
316
+ it "should normalize integer '#{lit}'" do
317
+ RDF::Literal.new(lit, :datatype => RDF::XSD.integer).to_s.should == n3
318
+ end
319
+ end
320
+
321
+ {
322
+ "1" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
323
+ "-1" => %("-1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
324
+ "1." => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
325
+ "1.0" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
326
+ "1.00" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
327
+ "+001.00" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
328
+ "123.456" => %("123.456"^^<http://www.w3.org/2001/XMLSchema#decimal>),
329
+ "2.345" => %("2.345"^^<http://www.w3.org/2001/XMLSchema#decimal>),
330
+ "1.000000000" => %("1.0"^^<http://www.w3.org/2001/XMLSchema#decimal>),
331
+ "2.3" => %("2.3"^^<http://www.w3.org/2001/XMLSchema#decimal>),
332
+ "2.234000005" => %("2.234000005"^^<http://www.w3.org/2001/XMLSchema#decimal>),
333
+ "2.2340000000000005" => %("2.2340000000000005"^^<http://www.w3.org/2001/XMLSchema#decimal>),
334
+ "2.23400000000000005" => %("2.234"^^<http://www.w3.org/2001/XMLSchema#decimal>),
335
+ "2.23400000000000000000005" => %("2.234"^^<http://www.w3.org/2001/XMLSchema#decimal>),
336
+ "1.2345678901234567890123457890" => %("1.2345678901234567"^^<http://www.w3.org/2001/XMLSchema#decimal>),
337
+ }.each_pair do |lit, n3|
338
+ it "should validate decimal '#{lit}'" do
339
+ RDF::Literal.new(lit, :datatype => RDF::XSD.decimal).valid?.should be_true
340
+ end
341
+
342
+ it "should normalize decimal '#{lit}'" do
343
+ RDF::Literal.new(lit, :datatype => RDF::XSD.decimal).to_s.should == n3
344
+ end
345
+ end
346
+
347
+ {
348
+ "1" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
349
+ "-1" => %("-1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
350
+ "+01.000" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
351
+ "1." => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
352
+ "1.0" => %("1.0E0"^^<http://www.w3.org/2001/XMLSchema#double>),
353
+ "123.456" => %("1.23456E2"^^<http://www.w3.org/2001/XMLSchema#double>),
354
+ "1.0e+1" => %("1.0E1"^^<http://www.w3.org/2001/XMLSchema#double>),
355
+ "1.0e-10" => %("1.0E-10"^^<http://www.w3.org/2001/XMLSchema#double>),
356
+ "123.456e4" => %("1.23456E6"^^<http://www.w3.org/2001/XMLSchema#double>),
357
+ }.each_pair do |lit, n3|
358
+ it "should validate double '#{lit}'" do
359
+ RDF::Literal.new(lit, :datatype => RDF::XSD.double).valid?.should be_true
360
+ end
361
+
362
+ it "should normalize double '#{lit}'" do
363
+ RDF::Literal.new(lit, :datatype => RDF::XSD.double).to_s.should == n3
364
+ end
365
+ end
366
+ end
367
+
368
+ describe "invalid content" do
369
+ [
370
+ RDF::Literal.new("foo", :datatype => RDF::XSD.boolean),
371
+ RDF::Literal.new("xyz", :datatype => RDF::XSD.integer),
372
+ RDF::Literal.new("12xyz", :datatype => RDF::XSD.integer),
373
+ RDF::Literal.new("12.xyz", :datatype => RDF::XSD.decimal),
374
+ RDF::Literal.new("xy.z", :datatype => RDF::XSD.double),
375
+ RDF::Literal.new("+1.0z", :datatype => RDF::XSD.double),
376
+ ].each do |lit|
377
+ it "should detect invalid encoding for '#{lit.to_s}'" do
378
+ lit.valid?.should be_false
379
+ end
380
+ end
381
+ end
382
+ end