rdf-rdfa 0.3.1.2 → 0.3.3

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,97 @@
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
+ SUITE = RDF::URI("http://rdfa.digitalbazaar.com/test-suite/")
9
+
10
+ class TestCase
11
+ HTMLRE = Regexp.new('([0-9]{4,4})\.xhtml')
12
+ TCPATHRE = Regexp.compile('\$TCPATH')
13
+
14
+ HOST_LANGUAGE_VERSION_SETS = [
15
+ ["xhtml1", "rdfa1.1"],
16
+ ["xml1", "rdfa1.1"],
17
+ ["html4", "rdfa1.1"],
18
+ ["html5", "rdfa1.1"],
19
+ ["xhtml5", "rdfa1.1"],
20
+ ["xhtml1", "rdfa1.0"],
21
+ ["svgtiny1.2", "rdfa1.0"],
22
+ ["svg", "rdfa1.1"],
23
+ ]
24
+
25
+ class Test < RDF::Vocabulary("http://www.w3.org/2006/03/test-description#"); end
26
+ class RdfaTest < RDF::Vocabulary("http://rdfa.digitalbazaar.com/vocabs/rdfa-test#"); end
27
+
28
+ attr_accessor :debug
29
+ include Spira::Resource
30
+
31
+ type Test.TestCase
32
+ property :title, :predicate => DC11.title, :type => XSD.string
33
+ property :purpose, :predicate => Test.purpose, :type => XSD.string
34
+ has_many :hostLanguage, :predicate => RdfaTest.hostLanguage, :type => XSD.string
35
+ has_many :version, :predicate => RdfaTest.rdfaVersion, :type => XSD.string
36
+ property :expected, :predicate => Test.expectedResults
37
+ property :contributor, :predicate => DC11.contributor, :type => XSD.string
38
+ property :reference, :predicate => Test.specificationRefference, :type => XSD.string
39
+ property :classification, :predicate => Test.classification
40
+ property :inputDocument, :predicate => Test.informationResourceInput
41
+ property :resultDocument, :predicate => Test.informationResourceResults
42
+
43
+ def self.for_specific(host_language, version, classification = nil)
44
+ each do |tc|
45
+ yield(tc) if tc.hostLanguage.include?(host_language) &&
46
+ tc.version.include?(version) &&
47
+ (classification.nil? || tc.classification == classification)
48
+ end
49
+ end
50
+
51
+ def expectedResults
52
+ RDF::Literal::Boolean.new(expected.nil? ? "true" : expected)
53
+ end
54
+
55
+ def name
56
+ subject.to_s.split("/").last
57
+ end
58
+
59
+ def input(host_language, version)
60
+ base = self.inputDocument.to_s.sub('test-cases/', "test-cases/#{host_language}/#{version}/")
61
+ case host_language
62
+ when /^xml/ then RDF::URI(base.sub(".html", ".xml"))
63
+ when /^xhtml/ then RDF::URI(base.sub(".html", ".xhtml"))
64
+ when /^svg/ then RDF::URI(base.sub(".html", ".svg"))
65
+ else RDF::URI(base)
66
+ end
67
+ end
68
+
69
+ def results(host_language, version)
70
+ RDF::URI(self.resultDocument.to_s.sub('test-cases/', "test-cases/#{host_language}/#{version}/"))
71
+ end
72
+
73
+ def trace
74
+ @debug.to_a.join("\n")
75
+ end
76
+
77
+ def inspect
78
+ "[#{self.class.to_s} " + %w(
79
+ title
80
+ classification
81
+ hostLanguage
82
+ version
83
+ inputDocument
84
+ resultDocument
85
+ ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
86
+ "]"
87
+ end
88
+ end
89
+
90
+ local_manifest = File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'manifest.ttl')
91
+ repo = if File.exist?(local_manifest)
92
+ RDF::Repository.load(local_manifest, :base_uri => SUITE.join("manifest.ttl"), :format => :n3)
93
+ else
94
+ RDF::Repository.load(SUITE.join("manifest.ttl"), :format => :n3)
95
+ end
96
+ Spira.add_repository! :default, repo
97
+ end
@@ -0,0 +1,385 @@
1
+ $:.unshift "."
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+ require 'rdf/spec/writer'
4
+ require 'rspec/matchers'
5
+
6
+ class EX < RDF::Vocabulary("http://example/"); end
7
+
8
+ describe RDF::RDFa::Writer do
9
+ before(:each) do
10
+ @graph = RDF::Graph.new
11
+ end
12
+
13
+ context "generic" do
14
+ before(:each) do
15
+ @writer = RDF::RDFa::Writer.new(StringIO.new)
16
+ end
17
+ it_should_behave_like RDF_Writer
18
+ end
19
+
20
+ describe "#buffer" do
21
+ context "prefix definitions" do
22
+ subject do
23
+ @graph << [EX.a, RDF::DC.title, "foo"]
24
+ serialize(:prefixes => {:dc => "http://purl.org/dc/terms/"})
25
+ end
26
+
27
+ specify { subject.should have_xpath("/xhtml:html/@prefix", %r(dc: http://purl.org/dc/terms/))}
28
+ specify { subject.should have_xpath("/xhtml:html/@prefix", %r(ex: http://example/))}
29
+ specify { subject.should_not have_xpath("/xhtml:html/@prefix", %r(bibo:))}
30
+ end
31
+
32
+ context "plain literal" do
33
+ subject do
34
+ @graph << [EX.a, EX.b, "foo"]
35
+ serialize(:haml_options => {:ugly => false})
36
+ end
37
+
38
+ {
39
+ "/xhtml:html/xhtml:body/xhtml:div/@about" => "ex:a",
40
+ "//xhtml:div[@class='property']/xhtml:span[@property]/@property" => "ex:b",
41
+ "//xhtml:div[@class='property']/xhtml:span[@property]/text()" => "foo",
42
+ }.each do |path, value|
43
+ it "returns #{value.inspect} for xpath #{path}" do
44
+ subject.should have_xpath(path, value)
45
+ end
46
+ end
47
+ end
48
+
49
+ context "dc:title" do
50
+ subject do
51
+ @graph << [EX.a, RDF::DC.title, "foo"]
52
+ serialize(:prefixes => {:dc => RDF::DC.to_s})
53
+ end
54
+
55
+ {
56
+ "/xhtml:html/xhtml:head/xhtml:title/text()" => "foo",
57
+ "/xhtml:html/xhtml:body/xhtml:div/@about" => "ex:a",
58
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/@property" => "dc:title",
59
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/text()" => "foo",
60
+ }.each do |path, value|
61
+ it "returns #{value.inspect} for xpath #{path}" do
62
+ subject.should have_xpath(path, value)
63
+ end
64
+ end
65
+ end
66
+
67
+ context "typed resources" do
68
+ context "typed resource" do
69
+ subject do
70
+ @graph << [EX.a, RDF.type, EX.Type]
71
+ serialize(:haml_options => {:ugly => false})
72
+ end
73
+
74
+ {
75
+ "/xhtml:html/xhtml:body/xhtml:div/@about" => "ex:a",
76
+ "/xhtml:html/xhtml:body/xhtml:div/@typeof" => "ex:Type",
77
+ }.each do |path, value|
78
+ it "returns #{value.inspect} for xpath #{path}" do
79
+ subject.should have_xpath(path, value)
80
+ end
81
+ end
82
+ end
83
+
84
+ context "resource with two types" do
85
+ subject do
86
+ @graph << [EX.a, RDF.type, EX.t1]
87
+ @graph << [EX.a, RDF.type, EX.t2]
88
+ serialize(:haml_options => {:ugly => false})
89
+ end
90
+
91
+ {
92
+ "/xhtml:html/xhtml:body/xhtml:div/@about" => "ex:a",
93
+ "/xhtml:html/xhtml:body/xhtml:div/@typeof" => "ex:t1 ex:t2",
94
+ }.each do |path, value|
95
+ it "returns #{value.inspect} for xpath #{path}" do
96
+ subject.should have_xpath(path, value)
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ context "languaged tagged literals" do
103
+ context "literal with language and no default language" do
104
+ subject do
105
+ @graph << [EX.a, RDF::DC.title, RDF::Literal("foo", :language => :en)]
106
+ serialize(:prefixes => {:dc => "http://purl.org/dc/terms/"})
107
+ end
108
+
109
+ {
110
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/@property" => "dc:title",
111
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/@lang" => "en",
112
+ }.each do |path, value|
113
+ it "returns #{value.inspect} for xpath #{path}" do
114
+ subject.should have_xpath(path, value)
115
+ end
116
+ end
117
+ end
118
+
119
+ context "literal with language and same default language" do
120
+ subject do
121
+ @graph << [EX.a, RDF::DC.title, RDF::Literal("foo", :language => :en)]
122
+ serialize(:lang => :en)
123
+ end
124
+
125
+ {
126
+ "/xhtml:html/@lang" => "en",
127
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/@lang" => false,
128
+ }.each do |path, value|
129
+ it "returns #{value.inspect} for xpath #{path}" do
130
+ subject.should have_xpath(path, value)
131
+ end
132
+ end
133
+ end
134
+
135
+ context "literal with language and different default language" do
136
+ subject do
137
+ @graph << [EX.a, RDF::DC.title, RDF::Literal("foo", :language => :en)]
138
+ serialize(:lang => :de)
139
+ end
140
+
141
+ {
142
+ "/xhtml:html/@lang" => "de",
143
+ "/xhtml:html/xhtml:body/xhtml:div/xhtml:h1/@lang" => "en",
144
+ }.each do |path, value|
145
+ it "returns #{value.inspect} for xpath #{path}" do
146
+ subject.should have_xpath(path, value)
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ context "typed literals" do
153
+ describe "xsd:date" do
154
+ subject do
155
+ @graph << [EX.a, EX.b, RDF::Literal::Date.new("2011-03-18")]
156
+ serialize(:haml_options => {:ugly => false})
157
+ end
158
+
159
+ {
160
+ "//xhtml:span[@property]/@property" => "ex:b",
161
+ "//xhtml:span[@property]/@datatype" => "xsd:date",
162
+ "//xhtml:span[@property]/@content" => "2011-03-18",
163
+ "//xhtml:span[@property]/text()" => "Friday, 18 March 2011",
164
+ }.each do |path, value|
165
+ it "returns #{value.inspect} for xpath #{path}" do
166
+ subject.should have_xpath(path, value)
167
+ end
168
+ end
169
+ end
170
+
171
+ context "xsd:time" do
172
+ subject do
173
+ @graph << [EX.a, EX.b, RDF::Literal::Time.new("12:34:56")]
174
+ serialize(:haml_options => {:ugly => false})
175
+ end
176
+
177
+ {
178
+ "//xhtml:span[@property]/@property" => "ex:b",
179
+ "//xhtml:span[@property]/@datatype" => "xsd:time",
180
+ "//xhtml:span[@property]/@content" => "12:34:56",
181
+ "//xhtml:span[@property]/text()" => /12:34:56/,
182
+ }.each do |path, value|
183
+ it "returns #{value.inspect} for xpath #{path}" do
184
+ subject.should have_xpath(path, value)
185
+ end
186
+ end
187
+ end
188
+
189
+ context "xsd:dateTime" do
190
+ subject do
191
+ @graph << [EX.a, EX.b, RDF::Literal::DateTime.new("2011-03-18T12:34:56")]
192
+ serialize(:haml_options => {:ugly => false})
193
+ end
194
+
195
+ {
196
+ "//xhtml:span[@property]/@property" => "ex:b",
197
+ "//xhtml:span[@property]/@datatype" => "xsd:dateTime",
198
+ "//xhtml:span[@property]/@content" => "2011-03-18T12:34:56",
199
+ "//xhtml:span[@property]/text()" => /12:34:56 \w+ on Friday, 18 March 2011/,
200
+ }.each do |path, value|
201
+ it "returns #{value.inspect} for xpath #{path}" do
202
+ subject.should have_xpath(path, value)
203
+ end
204
+ end
205
+ end
206
+
207
+ context "rdf:XMLLiteral" do
208
+ subject do
209
+ @graph << [EX.a, EX.b, RDF::Literal::XML.new("E = mc<sup>2</sup>: The Most Urgent Problem of Our Time")]
210
+ serialize(:haml_options => {:ugly => false})
211
+ end
212
+
213
+ {
214
+ "//xhtml:span[@property]/@property" => "ex:b",
215
+ "//xhtml:span[@property]/@datatype" => "rdf:XMLLiteral",
216
+ "//xhtml:span[@property]" => %r(<span [^>]+>E = mc<sup>2</sup>: The Most Urgent Problem of Our Time<\/span>),
217
+ }.each do |path, value|
218
+ it "returns #{value.inspect} for xpath #{path}" do
219
+ subject.should have_xpath(path, value)
220
+ end
221
+ end
222
+ end
223
+
224
+ context "xsd:string" do
225
+ subject do
226
+ @graph << [EX.a, EX.b, RDF::Literal.new("Albert Einstein", :datatype => RDF::XSD.string)]
227
+ serialize(:haml_options => {:ugly => false})
228
+ end
229
+
230
+ {
231
+ "//xhtml:span[@property]/@property" => "ex:b",
232
+ "//xhtml:span[@property]/@datatype" => "xsd:string",
233
+ "//xhtml:span[@property]/text()" => "Albert Einstein",
234
+ }.each do |path, value|
235
+ it "returns #{value.inspect} for xpath #{path}" do
236
+ subject.should have_xpath(path, value)
237
+ end
238
+ end
239
+ end
240
+
241
+ context "unknown" do
242
+ subject do
243
+ @graph << [EX.a, EX.b, RDF::Literal.new("Albert Einstein", :datatype => EX.unknown)]
244
+ serialize(:haml_options => {:ugly => false})
245
+ end
246
+
247
+ {
248
+ "//xhtml:span[@property]/@property" => "ex:b",
249
+ "//xhtml:span[@property]/@datatype" => "ex:unknown",
250
+ "//xhtml:span[@property]/text()" => "Albert Einstein",
251
+ }.each do |path, value|
252
+ it "returns #{value.inspect} for xpath #{path}" do
253
+ subject.should have_xpath(path, value)
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ context "multi-valued literals" do
260
+ subject do
261
+ @graph << [EX.a, EX.b, "c"]
262
+ @graph << [EX.a, EX.b, "d"]
263
+ serialize(:haml_options => {:ugly => false})
264
+ end
265
+
266
+ {
267
+ "//xhtml:ul/@property" => "ex:b",
268
+ "//xhtml:ul/xhtml:li[1]/text()" => "c",
269
+ "//xhtml:ul/xhtml:li[2]/text()" => "d",
270
+ }.each do |path, value|
271
+ it "returns #{value.inspect} for xpath #{path}" do
272
+ subject.should have_xpath(path, value)
273
+ end
274
+ end
275
+ end
276
+
277
+ context "resource objects" do
278
+ subject do
279
+ @graph << [EX.a, EX.b, EX.c]
280
+ serialize(:haml_options => {:ugly => false})
281
+ end
282
+
283
+ {
284
+ "//xhtml:div/@about" => "ex:a",
285
+ "//xhtml:a/@rel" => "ex:b",
286
+ "//xhtml:a/@href" => EX.c.to_s,
287
+ }.each do |path, value|
288
+ it "returns #{value.inspect} for xpath #{path}" do
289
+ subject.should have_xpath(path, value)
290
+ end
291
+ end
292
+ end
293
+
294
+ context "multi-valued resource objects" do
295
+ subject do
296
+ @graph << [EX.a, EX.b, EX.c]
297
+ @graph << [EX.a, EX.b, EX.d]
298
+ serialize(:haml_options => {:ugly => false})
299
+ end
300
+
301
+ {
302
+ "//xhtml:div/@about" => "ex:a",
303
+ "//xhtml:ul/@rel" => "ex:b",
304
+ "//xhtml:ul/xhtml:li[1]/xhtml:a/@href" => EX.c.to_s,
305
+ "//xhtml:ul/xhtml:li[2]/xhtml:a/@href" => EX.d.to_s,
306
+ }.each do |path, value|
307
+ it "returns #{value.inspect} for xpath #{path}" do
308
+ subject.should have_xpath(path, value)
309
+ end
310
+ end
311
+ end
312
+
313
+ context "included resource definitions" do
314
+ subject do
315
+ @graph << [EX.a, EX.b, EX.c]
316
+ @graph << [EX.c, EX.d, EX.e]
317
+ serialize(:haml_options => {:ugly => false})
318
+ end
319
+
320
+ {
321
+ "/xhtml:html/xhtml:body/xhtml:div/@about" => "ex:a",
322
+ "//xhtml:div[@about='ex:a']/xhtml:div[@class='property']/xhtml:div[@rel]/@rel" => "ex:b",
323
+ "//xhtml:div[@rel]/@resource" => "ex:c",
324
+ "//xhtml:div[@rel]/xhtml:div[@class='property']/xhtml:a/@href" => EX.e.to_s,
325
+ "//xhtml:div[@rel]/xhtml:div[@class='property']/xhtml:a/@rel" => "ex:d",
326
+ }.each do |path, value|
327
+ it "returns #{value.inspect} for xpath #{path}" do
328
+ subject.should have_xpath(path, value)
329
+ end
330
+ end
331
+ end
332
+
333
+ # W3C Test suite from http://www.w3.org/2006/07/SWD/RDFa/testsuite/
334
+ describe "w3c xhtml testcases" do
335
+ require 'test_helper'
336
+
337
+ # Generate with each template set
338
+ RDF::RDFa::Writer::HAML_TEMPLATES.each do |name, template|
339
+ context "Using #{name} template" do
340
+ Fixtures::TestCase.for_specific("xhtml1", "rdfa1.1", Fixtures::TestCase::Test.required) do |t|
341
+ next if t.name == "0212" # XMLLiteral equivalence
342
+ specify "test #{t.name}: #{t.title}" do
343
+ begin
344
+ input = t.input("xhtml1", "rdfa1.1")
345
+ @graph = RDF::Graph.load(t.input("xhtml1", "rdfa1.1"))
346
+ result = serialize(:haml => template, :haml_options => {:ugly => false})
347
+ graph2 = parse(result, :format => :rdfa)
348
+ graph2.should be_equivalent_graph(@graph, :trace => @debug.unshift(result).join("\n"))
349
+ rescue RSpec::Expectations::ExpectationNotMetError => e
350
+ if %w(0198).include?(t.name) || result =~ /XMLLiteral/m
351
+ pending("XMLLiteral canonicalization not implemented yet")
352
+ else
353
+ raise
354
+ end
355
+ end
356
+ end
357
+ end
358
+ end
359
+ end
360
+ end
361
+ end
362
+
363
+ require 'rdf/n3'
364
+ def parse(input, options = {})
365
+ reader_class = RDF::Reader.for(options[:format]) if options[:format]
366
+ reader_class ||= options.fetch(:reader, RDF::Reader.for(detect_format(input)))
367
+
368
+ graph = RDF::Graph.new
369
+ reader_class.new(input, options).each do |statement|
370
+ graph << statement
371
+ end
372
+ graph
373
+ end
374
+
375
+ # Serialize @graph to a string and compare against regexps
376
+ def serialize(options = {})
377
+ @debug = []
378
+ result = RDF::RDFa::Writer.buffer({:debug => @debug, :standard_prefixes => true}.merge(options)) do |writer|
379
+ writer << @graph
380
+ end
381
+ require 'cgi'
382
+ puts CGI.escapeHTML(result) if $verbose
383
+ result
384
+ end
385
+ end