pius-rena 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ require 'lib/rena'
2
+
3
+ describe "Literals" do
4
+ it "accept a language tag" do
5
+ f = Literal.new("tom", "en")
6
+ f.lang.should == "en"
7
+ end
8
+
9
+ it "accepts an encoding" do
10
+ f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
11
+ f.encoding.should == "http://www.w3.org/2001/XMLSchema#string"
12
+ end
13
+
14
+ it "should be equal if they have the same contents" do
15
+ f = Literal.new("tom")
16
+ g = Literal.new("tom")
17
+ f.should == g
18
+ end
19
+
20
+ it "should not be equal if they do not have the same contents" do
21
+ f = Literal.new("tom")
22
+ g = Literal.new("tim")
23
+ f.should_not == g
24
+ end
25
+
26
+ it "should be equal if they have the same contents and language" do
27
+ f = Literal.new("tom", "en")
28
+ g = Literal.new("tom", "en")
29
+ f.should == g
30
+ end
31
+
32
+ it "should not be equal if they do not have the same contents and language" do
33
+ f = Literal.new("tom", "en")
34
+ g = Literal.new("tim", "en")
35
+ f.should_not == g
36
+
37
+ lf = Literal.new("tom", "en")
38
+ lg = Literal.new("tom", "fr")
39
+ lf.should_not == lg
40
+ end
41
+
42
+ it "should be equal if they have the same contents and datatype" do
43
+ f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
44
+ g = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
45
+ f.should == g
46
+ end
47
+
48
+ it "should not be equal if they do not have the same contents and datatype" do
49
+ f = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
50
+ g = TypedLiteral.new("tim", "http://www.w3.org/2001/XMLSchema#string")
51
+ f.should_not == g
52
+
53
+ dtf = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
54
+ dtg = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#token")
55
+ dtf.should_not == dtg
56
+ end
57
+
58
+ it "should return valid N3/NTriples format strings" do
59
+ f = Literal.new("tom")
60
+ f.to_n3.should == "\"tom\""
61
+ f.to_ntriples.should == f.to_n3
62
+
63
+ g = Literal.new("tom", "en")
64
+ g.to_n3.should == "\"tom\"@en"
65
+ f.to_ntriples.should == f.to_n3
66
+
67
+ typed_int = TypedLiteral.new(5, "http://www.w3.org/2001/XMLSchema#int")
68
+ typed_int.to_n3.should == "5^^<http://www.w3.org/2001/XMLSchema#int>"
69
+ typed_int.to_ntriples.should == typed_int.to_n3
70
+
71
+ typed_string = TypedLiteral.new("foo", "http://www.w3.org/2001/XMLSchema#string")
72
+ typed_string.to_n3.should == "\"foo\"^^<http://www.w3.org/2001/XMLSchema#string>"
73
+ typed_string.to_ntriples.should == typed_string.to_n3
74
+ end
75
+
76
+ it "should normalize language tags to lower case" do
77
+ f = Literal.new("tom", "EN")
78
+ f.lang.should == "en"
79
+ end
80
+
81
+ it "should support TriX encoding" do
82
+ e = Literal.new("tom")
83
+ e.to_trix.should == "<plainLiteral>tom</plainLiteral>"
84
+
85
+ f = Literal.new("tom", "en")
86
+ f.to_trix.should == "<plainLiteral xml:lang=\"en\">tom</plainLiteral>"
87
+
88
+ g = TypedLiteral.new("tom", "http://www.w3.org/2001/XMLSchema#string")
89
+ g.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">tom</typedLiteral>"
90
+ end
91
+
92
+ it "should handle XML litearls" do
93
+ # first we should detect XML literals and mark them as such in the class
94
+ f = TypedLiteral.new("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
95
+ f.xmlliteral?.should == true
96
+ # pending "TODO: the thought of XML literals makes me want to wretch"
97
+ end
98
+
99
+ it "should be able to infer!" do
100
+ int = TypedLiteral.new(15, nil)
101
+ int.infer!
102
+ int.encoding.should == "http://www.w3.org/2001/XMLSchema#int"
103
+
104
+ float = TypedLiteral.new(15.4, nil)
105
+ float.infer!
106
+ float.encoding.should == "http://www.w3.org/2001/XMLSchema#float"
107
+
108
+ other = TypedLiteral.new("foo", nil)
109
+ other.infer!
110
+ other.encoding.should == "http://www.w3.org/2001/XMLSchema#string"
111
+ end
112
+ end
@@ -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