metade-rena 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'lib/rena'
2
+
3
+ describe "Namespaces" do
4
+ it "should use method_missing to create URIRefs on the fly" do
5
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
6
+ foaf.knows.to_s.should == "http://xmlns.com/foaf/0.1/knows"
7
+
8
+ foaf_frag = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf", true)
9
+ foaf_frag.knows.to_s.should == "http://xmlns.com/foaf/0.1/#knows"
10
+ end
11
+
12
+ it "should have a URI" do
13
+ lambda do
14
+ test = Namespace.new(short='foaf')
15
+ end.should raise_error
16
+ end
17
+
18
+ it "should have equality with URIRefs" do
19
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
20
+ foaf_name = URIRef.new("http://xmlns.com/foaf/0.1/name")
21
+ foaf.name.should == foaf_name
22
+ end
23
+
24
+ it "should have an XML and N3-friendly prefix" do
25
+ lambda do
26
+ test = Namespace.new('http://xmlns.com/foaf/0.1/', '*~{')
27
+ end.should raise_error
28
+ end
29
+
30
+ it "should be able to attach to the graph for substitution" do
31
+ # rdflib does this using graph.bind('prefix', namespace)
32
+ g = Graph.new
33
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
34
+ foaf.bind(g)
35
+ g.nsbinding["foaf"].should == foaf
36
+ end
37
+
38
+ it "should not allow you to attach to non-graphs" do
39
+ lambda do
40
+ foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
41
+ foaf.bind("cheese")
42
+ end.should raise_error
43
+ end
44
+ end
@@ -0,0 +1,314 @@
1
+ require 'lib/rena'
2
+ include Rena
3
+
4
+ # w3c test suite: http://www.w3.org/TR/rdf-testcases/
5
+
6
+ describe "RDF/XML Parser" do
7
+ it "should be able to detect whether an XML file is indeed an RDF file" do
8
+ bad_doc = "<?xml version=\"1.0\"?><RDF><foo /></RDF>"
9
+ bad_graph = RdfXmlParser.new(bad_doc)
10
+ bad_graph.is_rdf?.should == false
11
+
12
+ good_doc = "<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"></rdf:RDF>"
13
+ good_graph = RdfXmlParser.new(good_doc)
14
+ good_graph.is_rdf?.should == true
15
+ end
16
+
17
+ it "should be able to parse a simple single-triple document" do
18
+ sampledoc = <<-EOF;
19
+ <?xml version="1.0" ?>
20
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
21
+ xmlns:ex="http://www.example.org/" xml:lang="en" xml:base="http://www.example.org/foo">
22
+ <rdf:Description rdf:about="#" ex:name="bar">
23
+ <ex:belongsTo rdf:resource="http://tommorris.org/" />
24
+ <ex:sampleText rdf:datatype="http://www.w3.org/2001/XMLSchema#string">foo</ex:sampleText>
25
+ <ex:hadADodgyRelationshipWith rdf:parseType="Resource">
26
+ <ex:name>Tom</ex:name>
27
+ </ex:hadADodgyRelationshipWith>
28
+ </rdf:Description>
29
+ </rdf:RDF>
30
+ EOF
31
+
32
+ graph = RdfXmlParser.new(sampledoc)
33
+ graph.is_rdf?.should == true
34
+ graph.graph.size == 6
35
+ # print graph.graph.to_ntriples
36
+ end
37
+
38
+ it "should raise an error if rdf:aboutEach is used, as per the negative parser test rdfms-abouteach-error001 (rdf:aboutEach attribute)" do
39
+ sampledoc = <<-EOF;
40
+ <?xml version="1.0" ?>
41
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
42
+ xmlns:eg="http://example.org/">
43
+
44
+ <rdf:Bag rdf:ID="node">
45
+ <rdf:li rdf:resource="http://example.org/node2"/>
46
+ </rdf:Bag>
47
+
48
+ <rdf:Description rdf:aboutEach="#node">
49
+ <dc:rights xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:rights>
50
+
51
+ </rdf:Description>
52
+
53
+ </rdf:RDF>
54
+ EOF
55
+
56
+ lambda do
57
+ graph = RdfXmlParser.new(sampledoc)
58
+ end.should raise_error
59
+ end
60
+
61
+ it "should raise an error if rdf:aboutEachPrefix is used, as per the negative parser test rdfms-abouteach-error002 (rdf:aboutEachPrefix attribute)" do
62
+ sampledoc = <<-EOF;
63
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
64
+ xmlns:eg="http://example.org/">
65
+
66
+ <rdf:Description rdf:about="http://example.org/node">
67
+ <eg:property>foo</eg:property>
68
+ </rdf:Description>
69
+
70
+ <rdf:Description rdf:aboutEachPrefix="http://example.org/">
71
+ <dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:creator>
72
+
73
+ </rdf:Description>
74
+
75
+ </rdf:RDF>
76
+ EOF
77
+
78
+ lambda do
79
+ graph = RdfXmlParser.new(sampledoc)
80
+ end.should raise_error
81
+ end
82
+
83
+ it "should fail if given a non-ID as an ID (as per rdfcore-rdfms-rdf-id-error001)" do
84
+ sampledoc = <<-EOF;
85
+ <?xml version="1.0"?>
86
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
87
+ <rdf:Description rdf:ID='333-555-666' />
88
+ </rdf:RDF>
89
+ EOF
90
+
91
+ lambda do
92
+ graph = RdfXmlParser.new(sampledoc)
93
+ end.should raise_error
94
+ end
95
+
96
+ it "should make sure that the value of rdf:ID attributes match the XML Name production (child-element version)" do
97
+ sampledoc = <<-EOF;
98
+ <?xml version="1.0" ?>
99
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
100
+ xmlns:eg="http://example.org/">
101
+ <rdf:Description>
102
+ <eg:prop rdf:ID="q:name" />
103
+ </rdf:Description>
104
+ </rdf:RDF>
105
+ EOF
106
+
107
+ lambda do
108
+ graph = RdfXmlParser.new(sampledoc)
109
+ end.should raise_error
110
+ end
111
+
112
+ it "should make sure that the value of rdf:ID attributes match the XML Name production (data attribute version)" do
113
+ sampledoc = <<-EOF;
114
+ <?xml version="1.0" ?>
115
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
116
+ xmlns:eg="http://example.org/">
117
+ <rdf:Description rdf:ID="a/b" eg:prop="val" />
118
+ </rdf:RDF>
119
+ EOF
120
+
121
+ lambda do
122
+ graph = RdfXmlParser.new(sampledoc)
123
+ end.should raise_error
124
+ end
125
+
126
+ it "should handle parseType=Literal according to xml-literal-namespaces-test001.rdf test" do
127
+ sampledoc = <<-EOF;
128
+ <?xml version="1.0"?>
129
+
130
+ <!--
131
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
132
+ Technology, Institut National de Recherche en Informatique et en
133
+ Automatique, Keio University).
134
+
135
+ All Rights Reserved.
136
+
137
+ Please see the full Copyright clause at
138
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
139
+
140
+ Description: Visibly used namespaces must be included in XML
141
+ Literal values. Treatment of namespaces that are not
142
+ visibly used (e.g. rdf: in this example) is implementation
143
+ dependent. Based on example from Issues List.
144
+
145
+
146
+ $Id: test001.rdf,v 1.2 2002/11/22 13:52:15 jcarroll Exp $
147
+
148
+ -->
149
+ <rdf:RDF xmlns="http://www.w3.org/1999/xhtml"
150
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
151
+ xmlns:html="http://NoHTML.example.org"
152
+ xmlns:my="http://my.example.org/">
153
+ <rdf:Description rdf:ID="John_Smith">
154
+ <my:Name rdf:parseType="Literal">
155
+ <html:h1>
156
+ <b>John</b>
157
+ </html:h1>
158
+ </my:Name>
159
+
160
+ </rdf:Description>
161
+ </rdf:RDF>
162
+ EOF
163
+
164
+ graph = RdfXmlParser.new(sampledoc, "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf")
165
+ graph.graph.to_ntriples.should == "<http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf#John_Smith> <http://my.example.org/Name> \"<html:h1>\n <b>John</b>\n </html:h1>\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> ."
166
+ end
167
+
168
+ it "should pass rdfms-syntax-incomplete-test001" do
169
+ sampledoc = <<-EOF;
170
+ <?xml version="1.0"?>
171
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
172
+ xmlns:eg="http://example.org/">
173
+
174
+ <rdf:Description rdf:nodeID="a">
175
+ <eg:property rdf:nodeID="a" />
176
+ </rdf:Description>
177
+
178
+ </rdf:RDF>
179
+ EOF
180
+
181
+ lambda do
182
+ graph = RdfXmlParser.new(sampledoc)
183
+ end.should_not raise_error
184
+ end
185
+
186
+ it "should pass rdfms-syntax-incomplete-test002" do
187
+ sampledoc = <<-EOF;
188
+ <?xml version="1.0"?>
189
+
190
+ <!--
191
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
192
+ Technology, Institut National de Recherche en Informatique et en
193
+ Automatique, Keio University).
194
+
195
+ All Rights Reserved.
196
+
197
+ Please see the full Copyright clause at
198
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
199
+
200
+ -->
201
+ <!--
202
+
203
+ rdf:nodeID can be used to label a blank node.
204
+ These have file scope and are distinct from any
205
+ unlabelled blank nodes.
206
+ $Id: test002.rdf,v 1.1 2002/07/30 09:46:05 jcarroll Exp $
207
+
208
+ -->
209
+
210
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
211
+ xmlns:eg="http://example.org/">
212
+
213
+ <rdf:Description rdf:nodeID="a">
214
+ <eg:property1 rdf:nodeID="a" />
215
+ </rdf:Description>
216
+ <rdf:Description>
217
+ <eg:property2>
218
+
219
+ <!-- Note the rdf:nodeID="b" is redundant. -->
220
+ <rdf:Description rdf:nodeID="b">
221
+ <eg:property3 rdf:nodeID="a" />
222
+ </rdf:Description>
223
+ </eg:property2>
224
+ </rdf:Description>
225
+
226
+ </rdf:RDF>
227
+ EOF
228
+
229
+ lambda do
230
+ graph = RdfXmlParser.new(sampledoc)
231
+ end.should_not raise_error
232
+ end
233
+
234
+ it "should pass rdfms-syntax-incomplete/test003.rdf" do
235
+ sampledoc = <<-EOF;
236
+ <?xml version="1.0"?>
237
+
238
+ <!--
239
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
240
+ Technology, Institut National de Recherche en Informatique et en
241
+ Automatique, Keio University).
242
+
243
+ All Rights Reserved.
244
+
245
+ Please see the full Copyright clause at
246
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
247
+
248
+ -->
249
+ <!--
250
+
251
+ On an rdf:Description or typed node rdf:nodeID behaves
252
+ similarly to an rdf:about.
253
+ $Id: test003.rdf,v 1.2 2003/07/24 15:51:06 jcarroll Exp $
254
+
255
+ -->
256
+
257
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
258
+ xmlns:eg="http://example.org/">
259
+
260
+ <!-- In this example the rdf:nodeID is redundant. -->
261
+ <rdf:Description rdf:nodeID="a" eg:property1="value" />
262
+
263
+ </rdf:RDF>
264
+ EOF
265
+
266
+ lambda do
267
+ graph = RdfXmlParser.new(sampledoc)
268
+ end.should_not raise_error
269
+ end
270
+
271
+ # when we have decent Unicode support, add http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-rdf-id/error005.rdf
272
+
273
+ it "should support reification" do
274
+ pending
275
+ end
276
+
277
+ it "detect bad bagIDs" do
278
+ sampledoc = <<-EOF;
279
+ <?xml version="1.0" ?>
280
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
281
+ <rdf:Description rdf:bagID='333-555-666' />
282
+ </rdf:RDF>
283
+ EOF
284
+
285
+ lambda do
286
+ graph = RdfXmlParser.new(sampledoc)
287
+ end.should raise_error
288
+ end
289
+
290
+ describe "parsing rdf files" do
291
+ def test_file(filepath, uri = nil)
292
+ n3_string = File.read(filepath)
293
+ parser = RdfXmlParser.new(n3_string, uri)
294
+ ntriples = parser.graph.to_ntriples
295
+ ntriples.gsub!(/_:bn\d+/, '_:node1')
296
+ ntriples = ntriples.split("\n").sort
297
+
298
+ nt_string = File.read(filepath.sub('.rdf', '.nt'))
299
+ nt_string = nt_string.split("\n").sort
300
+
301
+ ntriples.should == nt_string
302
+ end
303
+
304
+ before(:all) do
305
+ @rdf_dir = File.join(File.dirname(__FILE__), '..', 'test', 'rdf_tests')
306
+ end
307
+
308
+ # it "should parse Coldplay's BBC Music profile" do
309
+ # gid = 'cc197bad-dc9c-440d-a5b5-d52ba2e14234'
310
+ # file = File.join(@rdf_dir, "#{gid}.rdf")
311
+ # test_file(file, "http://www.bbc.co.uk/music/artists/#{gid}")
312
+ # end
313
+ end
314
+ end
@@ -0,0 +1,75 @@
1
+ require 'lib/rena'
2
+ require 'rexml/document'
3
+ #require 'lib/rexml_hacks'
4
+
5
+ describe "REXML" do
6
+ before do
7
+ string = <<-EOF;
8
+ <?xml version="1.0" ?>
9
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ex="http://example.org/" xml:lang="en" xml:base="http://example.org/">
10
+ <rdf:Description>
11
+ <foo>bar</foo>
12
+ <bar:bar xmlns:bar="http://foo.com/">foo</bar:bar>
13
+ <ex:ex>fap</ex:ex>
14
+ </rdf:Description>
15
+ </rdf:RDF>
16
+ EOF
17
+
18
+ @doc = REXML::Document.new(string)
19
+ end
20
+
21
+ it "should have support for xml:base" do
22
+ @doc.root.elements[1].base?.should == true
23
+ @doc.root.elements[1].base.should == "http://example.org/"
24
+ end
25
+
26
+ it "should have support for xml:lang" do
27
+ @doc.root.elements[1].lang?.should == true
28
+ @doc.root.elements[1].lang.should == "en"
29
+ end
30
+
31
+ it "should allow individual writing-out of XML" do
32
+ # puts @doc.root.elements[1].write
33
+
34
+ sampledoc = <<-EOF;
35
+ <?xml version="1.0"?>
36
+
37
+ <!--
38
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
39
+ Technology, Institut National de Recherche en Informatique et en
40
+ Automatique, Keio University).
41
+
42
+ All Rights Reserved.
43
+
44
+ Please see the full Copyright clause at
45
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
46
+
47
+ Description: Visibly used namespaces must be included in XML
48
+ Literal values. Treatment of namespaces that are not
49
+ visibly used (e.g. rdf: in this example) is implementation
50
+ dependent. Based on example from Issues List.
51
+
52
+
53
+ $Id: test001.rdf,v 1.2 2002/11/22 13:52:15 jcarroll Exp $
54
+
55
+ -->
56
+ <rdf:RDF xmlns="http://www.w3.org/1999/xhtml"
57
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
58
+ xmlns:html="http://NoHTML.example.org"
59
+ xmlns:my="http://my.example.org/">
60
+ <rdf:Description rdf:ID="John_Smith">
61
+ <my:Name rdf:parseType="Literal">
62
+ <html:h1>
63
+ <b>John</b>
64
+ </html:h1>
65
+ </my:Name>
66
+
67
+ </rdf:Description>
68
+ </rdf:RDF>
69
+ EOF
70
+ doc2 = REXML::Document.new(sampledoc)
71
+ expectedoutput_str = "<html:h1 xmlns:html=\'http://NoHTML.example.org\' xmlns:my=\'http://my.example.org/\' xmlns:rdf=\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\'>\n <b xmlns=\'http://www.w3.org/1999/xhtml\'>John</b>\n </html:h1>"
72
+ expectedout = REXML::Document.new(expectedoutput_str)
73
+ doc2.root.elements[1].elements[1].elements[1] == expectedout.root
74
+ end
75
+ end
@@ -0,0 +1,100 @@
1
+ require 'lib/rena'
2
+
3
+ describe "Triples" do
4
+ it "should have a subject" do
5
+ f = Triple.new(BNode.new, URIRef.new('http://xmlns.com/foaf/0.1/knows'), BNode.new)
6
+ f.subject.class.should == BNode
7
+ # puts f.to_ntriples
8
+ end
9
+
10
+ it "should require that the subject is a URIRef or BNode" do
11
+ lambda do
12
+ Triple.new(Literal.new("foo"), URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new)
13
+ end.should raise_error
14
+ end
15
+
16
+ it "should require that the predicate is a URIRef" do
17
+ lambda do
18
+ Triple.new(BNode.new, BNode.new, BNode.new)
19
+ end.should raise_error
20
+ end
21
+
22
+ it "should require that the object is a URIRef, BNode, Literal or Typed Literal" do
23
+ lambda do
24
+ Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), [])
25
+ end.should raise_error
26
+ end
27
+
28
+ it "should emit an NTriple" do
29
+ s = URIRef.new("http://tommorris.org/foaf#me")
30
+ p = URIRef.new("http://xmlns.com/foaf/0.1/name")
31
+ o = Literal.untyped("Tom Morris")
32
+ f = Triple.new(s,p,o)
33
+
34
+ f.to_ntriples.should == "<http://tommorris.org/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" ."
35
+ end
36
+
37
+ describe "#coerce_subject" do
38
+ it "should accept a URIRef" do
39
+ ref = URIRef.new('http://localhost/')
40
+ Triple.coerce_subject(ref).should == ref
41
+ end
42
+
43
+ it "should accept a BNode" do
44
+ node = BNode.new('a')
45
+ Triple.coerce_subject(node).should == node
46
+ end
47
+
48
+ it "should accept a uri string and make URIRef" do
49
+ Triple.coerce_subject('http://localhost/').should == URIRef.new('http://localhost/')
50
+ end
51
+
52
+ it "should turn an other string into a BNode" do
53
+ Triple.coerce_subject('foo').should == BNode.new('foo')
54
+ end
55
+
56
+ it "should raise an InvalidSubject exception with any other class argument" do
57
+ lambda do
58
+ Triple.coerce_subject(Object.new)
59
+ end.should raise_error(Rena::Triple::InvalidSubject)
60
+ end
61
+ end
62
+
63
+ describe "#coerce_predicate" do
64
+ it "should make a string into a URI ref" do
65
+ Triple.coerce_predicate("http://localhost/").should == URIRef.new('http://localhost')
66
+ end
67
+
68
+ it "should leave a URIRef alone" do
69
+ ref = URIRef.new('http://localhost/')
70
+ Triple.coerce_predicate(ref).should == ref
71
+ end
72
+
73
+ it "should barf on an illegal uri string" do
74
+ lambda do
75
+ Triple.coerce_predicate("I'm just a soul whose intention is good")
76
+ end.should raise_error(Rena::Triple::InvalidPredicate)
77
+ end
78
+ end
79
+
80
+ describe "#coerce_object" do
81
+ it "should leave URIRefs alone" do
82
+ ref = URIRef.new("http://localhost/")
83
+ Triple.coerce_object(ref).should == ref
84
+ end
85
+
86
+ it "should leave BNodes alone" do
87
+ ref = BNode.new()
88
+ Triple.coerce_object(ref).should == ref
89
+ end
90
+
91
+ it "should leave Literals alone" do
92
+ ref = Literal.untyped('foo')
93
+ Triple.coerce_object(ref).should == ref
94
+
95
+ typedref = Literal.build_from('foo')
96
+ Triple.coerce_object(ref).should == ref
97
+ end
98
+
99
+ end
100
+ end