pjstadig-rena 0.0.1

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,288 @@
1
+ require 'lib/rena'
2
+
3
+ # w3c test suite: http://www.w3.org/TR/rdf-testcases/
4
+
5
+ describe "RDF/XML Parser" do
6
+ it "should be able to detect whether an XML file is indeed an RDF file" do
7
+ bad_doc = "<?xml version=\"1.0\"?><RDF><foo /></RDF>"
8
+ bad_graph = RdfXmlParser.new(bad_doc)
9
+ bad_graph.is_rdf?.should == false
10
+
11
+ good_doc = "<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"></rdf:RDF>"
12
+ good_graph = RdfXmlParser.new(good_doc)
13
+ good_graph.is_rdf?.should == true
14
+ end
15
+
16
+ it "should be able to parse a simple single-triple document" do
17
+ sampledoc = <<-EOF;
18
+ <?xml version="1.0" ?>
19
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
20
+ xmlns:ex="http://www.example.org/" xml:lang="en" xml:base="http://www.example.org/foo">
21
+ <rdf:Description rdf:about="#" ex:name="bar">
22
+ <ex:belongsTo rdf:resource="http://tommorris.org/" />
23
+ <ex:sampleText rdf:datatype="http://www.w3.org/2001/XMLSchema#string">foo</ex:sampleText>
24
+ <ex:hadADodgyRelationshipWith rdf:parseType="Resource">
25
+ <ex:name>Tom</ex:name>
26
+ </ex:hadADodgyRelationshipWith>
27
+ </rdf:Description>
28
+ </rdf:RDF>
29
+ EOF
30
+
31
+ graph = RdfXmlParser.new(sampledoc)
32
+ graph.is_rdf?.should == true
33
+ graph.graph.size == 6
34
+ # print graph.graph.to_ntriples
35
+ end
36
+
37
+ it "should raise an error if rdf:aboutEach is used, as per the negative parser test rdfms-abouteach-error001 (rdf:aboutEach attribute)" do
38
+ sampledoc = <<-EOF;
39
+ <?xml version="1.0" ?>
40
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
41
+ xmlns:eg="http://example.org/">
42
+
43
+ <rdf:Bag rdf:ID="node">
44
+ <rdf:li rdf:resource="http://example.org/node2"/>
45
+ </rdf:Bag>
46
+
47
+ <rdf:Description rdf:aboutEach="#node">
48
+ <dc:rights xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:rights>
49
+
50
+ </rdf:Description>
51
+
52
+ </rdf:RDF>
53
+ EOF
54
+
55
+ lambda do
56
+ graph = RdfXmlParser.new(sampledoc)
57
+ end.should raise_error
58
+ end
59
+
60
+ it "should raise an error if rdf:aboutEachPrefix is used, as per the negative parser test rdfms-abouteach-error002 (rdf:aboutEachPrefix attribute)" do
61
+ sampledoc = <<-EOF;
62
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
63
+ xmlns:eg="http://example.org/">
64
+
65
+ <rdf:Description rdf:about="http://example.org/node">
66
+ <eg:property>foo</eg:property>
67
+ </rdf:Description>
68
+
69
+ <rdf:Description rdf:aboutEachPrefix="http://example.org/">
70
+ <dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:creator>
71
+
72
+ </rdf:Description>
73
+
74
+ </rdf:RDF>
75
+ EOF
76
+
77
+ lambda do
78
+ graph = RdfXmlParser.new(sampledoc)
79
+ end.should raise_error
80
+ end
81
+
82
+ it "should fail if given a non-ID as an ID (as per rdfcore-rdfms-rdf-id-error001)" do
83
+ sampledoc = <<-EOF;
84
+ <?xml version="1.0"?>
85
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
86
+ <rdf:Description rdf:ID='333-555-666' />
87
+ </rdf:RDF>
88
+ EOF
89
+
90
+ lambda do
91
+ graph = RdfXmlParser.new(sampledoc)
92
+ end.should raise_error
93
+ end
94
+
95
+ it "should make sure that the value of rdf:ID attributes match the XML Name production (child-element version)" do
96
+ sampledoc = <<-EOF;
97
+ <?xml version="1.0" ?>
98
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
99
+ xmlns:eg="http://example.org/">
100
+ <rdf:Description>
101
+ <eg:prop rdf:ID="q:name" />
102
+ </rdf:Description>
103
+ </rdf:RDF>
104
+ EOF
105
+
106
+ lambda do
107
+ graph = RdfXmlParser.new(sampledoc)
108
+ end.should raise_error
109
+ end
110
+
111
+ it "should make sure that the value of rdf:ID attributes match the XML Name production (data attribute version)" do
112
+ sampledoc = <<-EOF;
113
+ <?xml version="1.0" ?>
114
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
115
+ xmlns:eg="http://example.org/">
116
+ <rdf:Description rdf:ID="a/b" eg:prop="val" />
117
+ </rdf:RDF>
118
+ EOF
119
+
120
+ lambda do
121
+ graph = RdfXmlParser.new(sampledoc)
122
+ end.should raise_error
123
+ end
124
+
125
+ it "should handle parseType=Literal according to xml-literal-namespaces-test001.rdf test" do
126
+ sampledoc = <<-EOF;
127
+ <?xml version="1.0"?>
128
+
129
+ <!--
130
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
131
+ Technology, Institut National de Recherche en Informatique et en
132
+ Automatique, Keio University).
133
+
134
+ All Rights Reserved.
135
+
136
+ Please see the full Copyright clause at
137
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
138
+
139
+ Description: Visibly used namespaces must be included in XML
140
+ Literal values. Treatment of namespaces that are not
141
+ visibly used (e.g. rdf: in this example) is implementation
142
+ dependent. Based on example from Issues List.
143
+
144
+
145
+ $Id: test001.rdf,v 1.2 2002/11/22 13:52:15 jcarroll Exp $
146
+
147
+ -->
148
+ <rdf:RDF xmlns="http://www.w3.org/1999/xhtml"
149
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
150
+ xmlns:html="http://NoHTML.example.org"
151
+ xmlns:my="http://my.example.org/">
152
+ <rdf:Description rdf:ID="John_Smith">
153
+ <my:Name rdf:parseType="Literal">
154
+ <html:h1>
155
+ <b>John</b>
156
+ </html:h1>
157
+ </my:Name>
158
+
159
+ </rdf:Description>
160
+ </rdf:RDF>
161
+ EOF
162
+
163
+ graph = RdfXmlParser.new(sampledoc, "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-xml-literal-namespaces/test001.rdf")
164
+ 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> .\n"
165
+ end
166
+
167
+ it "should pass rdfms-syntax-incomplete-test001" do
168
+ sampledoc = <<-EOF;
169
+ <?xml version="1.0"?>
170
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
171
+ xmlns:eg="http://example.org/">
172
+
173
+ <rdf:Description rdf:nodeID="a">
174
+ <eg:property rdf:nodeID="a" />
175
+ </rdf:Description>
176
+
177
+ </rdf:RDF>
178
+ EOF
179
+
180
+ lambda do
181
+ graph = RdfXmlParser.new(sampledoc)
182
+ end.should_not raise_error
183
+ end
184
+
185
+ it "should pass rdfms-syntax-incomplete-test002" do
186
+ sampledoc = <<-EOF;
187
+ <?xml version="1.0"?>
188
+
189
+ <!--
190
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
191
+ Technology, Institut National de Recherche en Informatique et en
192
+ Automatique, Keio University).
193
+
194
+ All Rights Reserved.
195
+
196
+ Please see the full Copyright clause at
197
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
198
+
199
+ -->
200
+ <!--
201
+
202
+ rdf:nodeID can be used to label a blank node.
203
+ These have file scope and are distinct from any
204
+ unlabelled blank nodes.
205
+ $Id: test002.rdf,v 1.1 2002/07/30 09:46:05 jcarroll Exp $
206
+
207
+ -->
208
+
209
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
210
+ xmlns:eg="http://example.org/">
211
+
212
+ <rdf:Description rdf:nodeID="a">
213
+ <eg:property1 rdf:nodeID="a" />
214
+ </rdf:Description>
215
+ <rdf:Description>
216
+ <eg:property2>
217
+
218
+ <!-- Note the rdf:nodeID="b" is redundant. -->
219
+ <rdf:Description rdf:nodeID="b">
220
+ <eg:property3 rdf:nodeID="a" />
221
+ </rdf:Description>
222
+ </eg:property2>
223
+ </rdf:Description>
224
+
225
+ </rdf:RDF>
226
+ EOF
227
+
228
+ lambda do
229
+ graph = RdfXmlParser.new(sampledoc)
230
+ end.should_not raise_error
231
+ end
232
+
233
+ it "should pass rdfms-syntax-incomplete/test003.rdf" do
234
+ sampledoc = <<-EOF;
235
+ <?xml version="1.0"?>
236
+
237
+ <!--
238
+ Copyright World Wide Web Consortium, (Massachusetts Institute of
239
+ Technology, Institut National de Recherche en Informatique et en
240
+ Automatique, Keio University).
241
+
242
+ All Rights Reserved.
243
+
244
+ Please see the full Copyright clause at
245
+ <http://www.w3.org/Consortium/Legal/copyright-software.html>
246
+
247
+ -->
248
+ <!--
249
+
250
+ On an rdf:Description or typed node rdf:nodeID behaves
251
+ similarly to an rdf:about.
252
+ $Id: test003.rdf,v 1.2 2003/07/24 15:51:06 jcarroll Exp $
253
+
254
+ -->
255
+
256
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
257
+ xmlns:eg="http://example.org/">
258
+
259
+ <!-- In this example the rdf:nodeID is redundant. -->
260
+ <rdf:Description rdf:nodeID="a" eg:property1="value" />
261
+
262
+ </rdf:RDF>
263
+ EOF
264
+
265
+ lambda do
266
+ graph = RdfXmlParser.new(sampledoc)
267
+ end.should_not raise_error
268
+ end
269
+
270
+ # when we have decent Unicode support, add http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-rdf-id/error005.rdf
271
+
272
+ it "should support reification" do
273
+ pending
274
+ end
275
+
276
+ it "detect bad bagIDs" do
277
+ sampledoc = <<-EOF;
278
+ <?xml version="1.0" ?>
279
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
280
+ <rdf:Description rdf:bagID='333-555-666' />
281
+ </rdf:RDF>
282
+ EOF
283
+
284
+ lambda do
285
+ graph = RdfXmlParser.new(sampledoc)
286
+ end.should raise_error
287
+ end
288
+ end
@@ -0,0 +1,76 @@
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 = "<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
+ doc2.root.elements[1].elements[1].elements[1].write.should == expectedoutput
73
+
74
+ # pending
75
+ end
76
+ end
@@ -0,0 +1,32 @@
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
+ f = Triple.new(URIRef.new("http://tommorris.org/foaf#me"), URIRef.new("http://xmlns.com/foaf/0.1/name"), Literal.new("Tom Morris"))
30
+ f.to_ntriples.should == "<http://tommorris.org/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" ."
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ require 'lib/rena'
2
+ #require 'lib/uriref'
3
+
4
+ describe "URI References" do
5
+ it "should output NTriples" do
6
+ f = URIRef.new("http://tommorris.org/foaf/")
7
+ f.to_ntriples.should == "<http://tommorris.org/foaf/>"
8
+ end
9
+
10
+ it "should handle Unicode symbols inside URLs" do
11
+ lambda do
12
+ f = URIRef.new("http://example.org/#Andr%E9")
13
+ end.should_not raise_error
14
+ end
15
+
16
+ # it "do not contain any control characters (#x00 - #x1F, #x74-#x9F)" do
17
+ # lambda do
18
+ # f = URIRef.new("http://tommorris.org/blog/")
19
+ # f.test_string("http://tommorris.org/blog")
20
+ # end.should_not raise_error
21
+ #
22
+ # lambda do
23
+ # f = URIRef.new("http://xmlns.com/foaf/0.1/knows")
24
+ # f.test_string("http://xmlns.com/foaf/0.1/knows")
25
+ # end.should_not raise_error
26
+ # end
27
+
28
+ it "produce a valid URI character sequence (per RFC 2396 §2.1) representing an absolute URI with optional fragment identifier" do
29
+ pending "TODO: figure out a series of tests for RFC 2396 §2.1 adherence"
30
+ end
31
+
32
+ it "should throw errors on suspicious protocols and non-protocols" do
33
+ lambda do
34
+ URIRef.new("javascript:alert(\"pass\")")
35
+ end.should raise_error
36
+ end
37
+
38
+ it "must not be a relative URI" do
39
+ lambda do
40
+ URIRef.new("foo")
41
+ end.should raise_error
42
+ end
43
+
44
+ it "should discourage use of %-escaped characters" do
45
+ pending "TODO: figure out a way to discourage %-escaped character usage"
46
+ end
47
+ end