rdf_context 0.5.3 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -66,16 +66,12 @@ module RdfContext
66
66
  # Return a QName for the URI, or nil. Adds namespace of QName to defined namespaces
67
67
  def get_qname(uri)
68
68
  if uri.is_a?(URIRef)
69
- begin
70
- qn = @graph.qname(uri)
71
- rescue RdfException
72
- return false # no namespace
73
- end
69
+ qn = @graph.qname(uri)
74
70
  # Local parts with . will mess up serialization
75
- return false if qn.index('.')
71
+ return false if qn.nil? || qn.index('.')
76
72
 
77
73
  add_namespace(uri.namespace)
78
- return qn
74
+ qn
79
75
  end
80
76
  end
81
77
 
@@ -32,6 +32,8 @@ module RdfContext
32
32
 
33
33
  doc = Nokogiri::XML::Document.new
34
34
 
35
+ puts "\nserialize: graph namespaces: #{@graph.nsbinding.inspect}" if $DEBUG
36
+
35
37
  preprocess
36
38
 
37
39
  predicates = @graph.predicates.uniq
@@ -40,13 +42,12 @@ module RdfContext
40
42
  required_namespaces = {}
41
43
  possible.each do |res|
42
44
  next unless res.is_a?(URIRef)
43
- if !get_qname(res) # Creates Namespace mappings
44
- [RDF_NS, DC_NS, OWL_NS, LOG_NS, RDF_NS, RDFS_NS, XHV_NS, XML_NS, XSD_NS, XSI_NS].each do |ns|
45
- # Bind a standard namespace to the graph and try the lookup again
46
- @graph.bind(ns) if ns.uri == res.base
47
- required_namespaces[res.base] = true if !get_qname(res) && predicates.include?(res)
48
- end
45
+ if res.namespace
46
+ add_namespace(res.namespace)
47
+ else
48
+ required_namespaces[res.base] = true
49
49
  end
50
+ #puts "possible namespace for #{res}: #{res.namespace || %(<#{res.base}>)}"
50
51
  end
51
52
  add_namespace(RDF_NS)
52
53
  add_namespace(XML_NS) if @base || @lang
@@ -64,7 +65,7 @@ module RdfContext
64
65
  # Add bindings for predicates not already having bindings
65
66
  tmp_ns = "ns0"
66
67
  required_namespaces.keys.each do |uri|
67
- puts "serialize: create temporary namespace for <#{uri}>" if $DEBUG
68
+ puts "create namespace definition for #{uri}" if $DEBUG
68
69
  add_namespace(Namespace.new(uri, tmp_ns))
69
70
  tmp_ns = tmp_ns.succ
70
71
  end
@@ -103,7 +104,8 @@ module RdfContext
103
104
  if rdf_type.is_a?(URIRef)
104
105
  element = get_qname(rdf_type)
105
106
  properties[RDF_TYPE.to_s] = rest
106
- if rdf_type.namespace && @default_ns && rdf_type.namespace.uri == @default_ns.uri
107
+ type_ns = rdf_type.namespace rescue nil
108
+ if type_ns && @default_ns && type_ns.uri == @default_ns.uri
107
109
  properties[RDF_TYPE.to_s] = rest
108
110
  element = rdf_type.short_name
109
111
  end
@@ -143,6 +145,7 @@ module RdfContext
143
145
  # If _is_unique_ is true, this predicate may be able to be serialized as an attribute
144
146
  def predicate(prop, object, node, is_unique)
145
147
  qname = prop.to_qname(uri_binding)
148
+ raise RdfException, "No qname generated for <#{prop}>" unless qname
146
149
 
147
150
  # See if we can serialize as attribute.
148
151
  # * untyped attributes that aren't duplicated where xml:lang == @lang
@@ -233,5 +236,15 @@ module RdfContext
233
236
  end
234
237
  node.add_child(pred_node) if pred_node
235
238
  end
239
+
240
+ def preprocess_triple(triple)
241
+ super
242
+
243
+ # Pre-fetch qnames, to fill namespaces
244
+ get_qname(triple.predicate)
245
+ get_qname(triple.object) if triple.predicate == RDF_TYPE
246
+
247
+ @references[triple.predicate] = ref_count(triple.predicate) + 1
248
+ end
236
249
  end
237
250
  end
@@ -35,7 +35,6 @@ module RdfContext
35
35
 
36
36
  # Bind namespace to store, returns bound namespace
37
37
  def bind(namespace)
38
- puts "bind #{namespace.inspect}"
39
38
  # Over-write an empty prefix
40
39
  uri = namespace.uri.to_s
41
40
  @uri_binding.delete(uri)
@@ -47,14 +47,17 @@ module RdfContext
47
47
  # short_name of URI for creating QNames.
48
48
  # "#{base]{#short_name}}" == uri
49
49
  def short_name
50
- @short_name ||= if @namespace
51
- self.to_s.sub(@namespace.uri.to_s, "")
52
- elsif @uri.fragment()
53
- @uri.fragment()
54
- elsif @uri.path.split("/").last.class == String and @uri.path.split("/").last.length > 0
55
- @uri.path.split("/").last
56
- else
57
- false
50
+ @short_name ||= begin
51
+ path = @uri.path.split("/")
52
+ if @namespace
53
+ self.to_s.sub(@namespace.uri.to_s, "")
54
+ elsif @uri.fragment
55
+ @uri.fragment
56
+ elsif path && path.length > 1 && path.last.class == String && path.last.length > 0 && path.last.index("/") != 0
57
+ path.last
58
+ else
59
+ false
60
+ end
58
61
  end
59
62
  end
60
63
 
@@ -63,7 +66,7 @@ module RdfContext
63
66
  def base
64
67
  @base ||= begin
65
68
  uri_base = self.to_s
66
- sn = short_name.to_s
69
+ sn = short_name ? short_name.to_s : ""
67
70
  uri_base[0, uri_base.length - sn.length]
68
71
  end
69
72
  end
@@ -90,21 +93,24 @@ module RdfContext
90
93
  alias_method :to_ntriples, :to_n3
91
94
 
92
95
  # Output URI as QName using URI binding
93
- def to_qname(uri_binding = {})
94
- ns = namespace(uri_binding)
95
- "#{ns.prefix}:#{short_name}"
96
+ def to_qname(uri_binding = [])
97
+ namespaces = case uri_binding
98
+ when Hash then uri_binding.values
99
+ when Array then uri_binding
100
+ else []
101
+ end
102
+ ns = namespace(namespaces)
103
+ "#{ns.prefix}:#{short_name}" if ns
96
104
  end
97
105
 
98
- def namespace(uri_binding = {})
99
- @namespace ||= begin
100
- uri = uri_binding.keys.detect {|u| self.to_s.index(u) == 0 }
101
- raise RdfException, "Couldn't find namespace for #{@uri}" unless uri
102
- uri_binding[uri]
103
- end
106
+ # Look at namespaces and find first that matches this URI, ordering by longest URI first
107
+ def namespace(namespaces = [])
108
+ @namespace ||=
109
+ namespaces.sort_by {|ns| -ns.uri.to_s.length}.detect {|ns| self.to_s.index(ns.uri.to_s) == 0}
104
110
  end
105
111
 
106
112
  def inspect
107
- "#{self.class}[#{self.to_n3}]"
113
+ "#{self.class}[#{self.to_n3}, ns=#{namespace.inspect}]"
108
114
  end
109
115
 
110
116
  # Output URI as resource reference for RDF/XML
data/spec/graph_spec.rb CHANGED
@@ -120,13 +120,13 @@ describe "Graphs" do
120
120
 
121
121
  describe "with XML Literal objects" do
122
122
  subject {
123
- dc = Namespace.new("http://purl.org/dc/elements/1.1/", "dc")
123
+ dc = Namespace.new("http://purl.org/dc/terms/", "dc")
124
124
  xhtml = Namespace.new("http://www.w3.org/1999/xhtml", "")
125
125
  g = Graph.new(:store => ListStore.new)
126
126
  g << Triple.new(
127
127
  URIRef.new("http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml"),
128
- URIRef.new("http://purl.org/dc/elements/1.1/title"),
129
- Literal.typed("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">2</sup>: The Most Urgent Problem of Our Time",
128
+ URIRef.new("http://purl.org/dc/terms/title"),
129
+ Literal.typed("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time",
130
130
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
131
131
  g.nsbinding)
132
132
  )
@@ -136,20 +136,20 @@ describe "Graphs" do
136
136
  }
137
137
 
138
138
  it "should output NTriple" do
139
- nt = '<http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml> <http://purl.org/dc/elements/1.1/title> "E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">2</sup>: The Most Urgent Problem of Our Time"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .' + "\n"
139
+ nt = '<http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml> <http://purl.org/dc/terms/title> "E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .' + "\n"
140
140
  subject.to_ntriples.should == nt
141
141
  end
142
142
 
143
143
  it "should output RDF/XML" do
144
144
  rdfxml = <<-HERE
145
145
  <?xml version="1.0" encoding="UTF-8"?>
146
- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xhv=\"http://www.w3.org/1999/xhtml/vocab#\">
146
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:dc=\"http://purl.org/dc/terms/\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xhv=\"http://www.w3.org/1999/xhtml/vocab#\">
147
147
  <rdf:Description rdf:about="http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml">
148
- <dc:title rdf:parseType="Literal">E = mc<sup xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/elements/1.1/">2>/sup>: The Most Urgent Problem of Our Time</dc:title>
148
+ <dc:title rdf:parseType="Literal">E = mc<sup xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/terms/">2>/sup>: The Most Urgent Problem of Our Time</dc:title>
149
149
  </rdf:Description>
150
150
  </rdf:RDF>
151
151
  HERE
152
- subject.to_rdfxml.should include("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">2</sup>: The Most Urgent Problem of Our Time")
152
+ subject.to_rdfxml.should include("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time")
153
153
  end
154
154
  end
155
155
 
@@ -328,6 +328,15 @@ HERE
328
328
  subject.properties(@ex.a).has_key?(@ex.b.to_s).should be_true
329
329
  subject.properties(@ex.a)[@ex.b.to_s].should include(@ex.c, @ex.d, @ex.e)
330
330
  end
331
+
332
+ it "should get asserted properties for a BNode" do
333
+ bn = BNode.new
334
+ subject.add_triple(bn, @ex.b, @ex.c)
335
+ subject.properties(bn).should be_a(Hash)
336
+ subject.properties(bn).size.should == 1
337
+ subject.properties(bn).has_key?(@ex.b.to_s).should be_true
338
+ subject.properties(bn)[@ex.b.to_s].should == [@ex.c]
339
+ end
331
340
 
332
341
  it "should get asserted type with single type" do
333
342
  subject.add_triple(@ex.a, RDF_TYPE, @ex.Audio)
data/spec/literal_spec.rb CHANGED
@@ -269,7 +269,7 @@ describe "Literals: " do
269
269
  describe "with a namespace" do
270
270
  subject {
271
271
  Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
272
- :namespaces => {"dc" => Namespace.new("http://purl.org/dc/elements/1.1/", "dc")})
272
+ :namespaces => {"dc" => Namespace.new("http://purl.org/dc/terms/", "dc")})
273
273
  }
274
274
 
275
275
  describe "encodings" do
@@ -282,7 +282,7 @@ describe "Literals: " do
282
282
  describe "and language" do
283
283
  subject {
284
284
  Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
285
- :namespaces => {"dc" => Namespace.new("http://purl.org/dc/elements/1.1/", "dc")},
285
+ :namespaces => {"dc" => Namespace.new("http://purl.org/dc/terms/", "dc")},
286
286
  :language => "fr")
287
287
  }
288
288
 
@@ -315,7 +315,7 @@ describe "Literals: " do
315
315
  <?xml version="1.0" encoding="UTF-8"?>
316
316
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
317
317
  <html xmlns="http://www.w3.org/1999/xhtml"
318
- xmlns:dc="http://purl.org/dc/elements/1.1/"
318
+ xmlns:dc="http://purl.org/dc/terms/"
319
319
  xmlns:ex="http://example.org/rdf/"
320
320
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
321
321
  xmlns:svg="http://www.w3.org/2000/svg">
@@ -334,7 +334,7 @@ describe "Literals: " do
334
334
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
335
335
  :namespaces => {
336
336
  "svg" => Namespace.new("http://www.w3.org/2000/svg", "svg"),
337
- "dc" => Namespace.new("http://purl.org/dc/elements/1.1/", "dc")
337
+ "dc" => Namespace.new("http://purl.org/dc/terms/", "dc")
338
338
  })
339
339
  }
340
340
  it "should return xml_args" do subject.xml_args.should == ["<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"></svg:svg>", {"rdf:parseType" => "Literal"}] end
@@ -353,23 +353,23 @@ describe "Literals: " do
353
353
  describe "with a default namespace" do
354
354
  subject {
355
355
  Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
356
- :namespaces => {"" => Namespace.new("http://purl.org/dc/elements/1.1/", "")})
356
+ :namespaces => {"" => Namespace.new("http://purl.org/dc/terms/", "")})
357
357
  }
358
358
 
359
359
  describe "encodings" do
360
- it "should return n3" do subject.to_n3.should == "\"foo <sup xmlns=\\\"http://purl.org/dc/elements/1.1/\\\">bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
360
+ it "should return n3" do subject.to_n3.should == "\"foo <sup xmlns=\\\"http://purl.org/dc/terms/\\\">bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
361
361
  it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
362
- it "should return xml_args" do subject.xml_args.should == ["foo <sup xmlns=\"http://purl.org/dc/elements/1.1/\">bar</sup> baz!", {"rdf:parseType" => "Literal"}] end
363
- it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\">foo <sup xmlns=\"http://purl.org/dc/elements/1.1/\">bar</sup> baz!</typedLiteral>" end
362
+ it "should return xml_args" do subject.xml_args.should == ["foo <sup xmlns=\"http://purl.org/dc/terms/\">bar</sup> baz!", {"rdf:parseType" => "Literal"}] end
363
+ it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\">foo <sup xmlns=\"http://purl.org/dc/terms/\">bar</sup> baz!</typedLiteral>" end
364
364
  end
365
365
  end
366
366
 
367
367
  describe "with multiple namespaces" do
368
368
  subject {
369
- Literal.typed("foo <sup <sup xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
369
+ Literal.typed("foo <sup <sup xmlns:dc=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
370
370
  }
371
371
  it "should ignore namespace order" do
372
- g = Literal.typed("foo <sup <sup xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
372
+ g = Literal.typed("foo <sup <sup xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/terms/\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
373
373
  should == g
374
374
  end
375
375
  end
data/spec/matchers.rb CHANGED
@@ -110,6 +110,7 @@ module Matchers
110
110
  ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
111
111
 
112
112
  @results = @query.execute(model)
113
+ #puts "Redland query results: #{@results.inspect}"
113
114
  if @expected_results
114
115
  @results.is_boolean? && @results.get_boolean?
115
116
  else
@@ -72,7 +72,7 @@ describe "N3 parser" do
72
72
 
73
73
  it "should parse multi-line literal" do
74
74
  @parser.parse(%(
75
- <http://www.example.com/books#book12345> <http://purl.org/dc/elements/1.1/title> """
75
+ <http://www.example.com/books#book12345> <http://purl.org/dc/terms/title> """
76
76
  Foo
77
77
  <html:b xmlns:html="http://www.w3.org/1999/xhtml" html:a="b">bar<rdf:Thing xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><a:b xmlns:a="foo:"></a:b>here<a:c xmlns:a="foo:"></a:c></rd
78
78
  f:Thing></html:b>
@@ -261,7 +261,7 @@ describe "N3 parser" do
261
261
  it "should map <> to document uri" do
262
262
  n3doc = "@prefix : <> ."
263
263
  @parser.parse(n3doc, "http://the.document.itself")
264
- @parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself", "", true)}
264
+ @parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself#", "")}
265
265
  end
266
266
 
267
267
  it "should use <> as a prefix and as a triple node" do
@@ -9,8 +9,8 @@ describe "Namespace" do
9
9
  end
10
10
 
11
11
  it "should create URIRef for frag" do
12
- foaf_frag = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf", true)
13
- foaf_frag.knows.to_s.should == "http://xmlns.com/foaf/0.1/#knows"
12
+ foaf_frag = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
13
+ foaf_frag.knows.to_s.should == "http://xmlns.com/foaf/0.1/knows"
14
14
  end
15
15
  end
16
16
 
@@ -50,6 +50,20 @@ describe "Namespace" do
50
50
  subject.foo.should == "http://xmlns.com/foaf/0.1/foo"
51
51
  end
52
52
 
53
+ describe "no URI normalization" do
54
+ specify {Namespace.new("http://foo", "foo").uri.should == "http://foo"}
55
+ specify {Namespace.new("http://foo#", "foo").uri.should == "http://foo#"}
56
+ specify {Namespace.new("http://foo/", "foo").uri.should == "http://foo/"}
57
+ specify {Namespace.new("xyz:foo", "foo").uri.should == "xyz:foo"}
58
+ end
59
+
60
+ it "should not normalize URI" do
61
+ end
62
+
63
+ describe "serialization" do
64
+ specify {subject.to_s.should == "foaf: http://xmlns.com/foaf/0.1/"}
65
+ end
66
+
53
67
  describe '#+' do
54
68
  it "should construct URI with +" do
55
69
  (subject + "foo").class.should == URIRef
data/spec/rdfa_helper.rb CHANGED
@@ -3,11 +3,6 @@ module RdfaHelper
3
3
  class TestCase
4
4
  include Matchers
5
5
 
6
- TEST_DIR = File.join(File.dirname(__FILE__), 'rdfa-test-suite')
7
- NT_DIR = File.join(File.dirname(__FILE__), 'rdfa-triples')
8
- BASE_MANIFEST_URL = "http://rdfa.digitalbazaar.com/test-suite/"
9
- BASE_TEST_CASE_URL = "#{BASE_MANIFEST_URL}test-cases/"
10
-
11
6
  HTMLRE = Regexp.new('([0-9]{4,4})\.xhtml')
12
7
  TCPATHRE = Regexp.compile('\$TCPATH')
13
8
 
@@ -34,7 +29,7 @@ module RdfaHelper
34
29
  #next unless statement.subject.uri.to_s.match(/0001/)
35
30
  unless self.about
36
31
  self.about = Addressable::URI.parse(statement.subject.uri.to_s)
37
- self.name = statement.subject.short_name
32
+ self.name = statement.subject.short_name || self.about
38
33
  end
39
34
 
40
35
  if statement.predicate.short_name == "expectedResults"
@@ -74,7 +69,7 @@ module RdfaHelper
74
69
  end
75
70
 
76
71
  def tcpath
77
- BASE_TEST_CASE_URL + (suite == "xhtml" ? "xhtml1" : suite)
72
+ RDFA_TEST_CASE_URL + (suite == "xhtml" ? "xhtml1" : suite)
78
73
  end
79
74
 
80
75
  # Read in file, and apply modifications to create a properly formatted HTML
@@ -82,7 +77,7 @@ module RdfaHelper
82
77
  f = self.inputDocument
83
78
  found_head = false
84
79
  namespaces = ""
85
- body = File.readlines(File.join(TEST_DIR, "tests", f)).map do |line|
80
+ body = File.readlines(File.join(RDFA_DIR, "tests", f)).map do |line|
86
81
  found_head ||= line.match(/<head/)
87
82
  if found_head
88
83
  line.chop
@@ -118,14 +113,14 @@ module RdfaHelper
118
113
  # Read in file, and apply modifications reference either .html or .xhtml
119
114
  def results
120
115
  f = self.name + ".sparql"
121
- body = File.read(File.join(TEST_DIR, "tests", f)).gsub(TCPATHRE, tcpath)
116
+ body = File.read(File.join(RDFA_DIR, "tests", f)).gsub(TCPATHRE, tcpath)
122
117
 
123
118
  suite == "xhtml" ? body : body.gsub(HTMLRE, '\1.html')
124
119
  end
125
120
 
126
121
  def triples
127
122
  f = self.name + ".nt"
128
- body = File.read(File.join(NT_DIR, f)).gsub(TCPATHRE, tcpath)
123
+ body = File.read(File.join(RDFA_NT_DIR, f)).gsub(TCPATHRE, tcpath)
129
124
  suite == "xhtml" ? body : body.gsub(HTMLRE, '\1.html')
130
125
  end
131
126
 
@@ -151,7 +146,7 @@ module RdfaHelper
151
146
  # Run SPARQL query
152
147
  @parser.graph.should pass_query(query_string, self)
153
148
  else
154
- #pending("Query skipped, Redland not installed")
149
+ raise SparqlException, "Query skipped, Redland not installed"
155
150
  end
156
151
 
157
152
  @parser.graph.to_rdfxml.should be_valid_xml
@@ -166,9 +161,9 @@ module RdfaHelper
166
161
  return @test_cases unless @test_cases.empty?
167
162
 
168
163
  @suite = suite # Process the given test suite
169
- @manifest_url = "#{BASE_MANIFEST_URL}#{suite}-manifest.rdf"
164
+ @manifest_url = "#{RDFA_MANIFEST_URL}#{suite}-manifest.rdf"
170
165
 
171
- manifest_str = File.read(File.join(TEST_DIR, "#{suite}-manifest.rdf"))
166
+ manifest_str = File.read(File.join(RDFA_DIR, "#{suite}-manifest.rdf"))
172
167
  parser = RdfXmlParser.new
173
168
 
174
169
  begin
@@ -1,13 +1,47 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
3
  require 'rdfa_helper'
4
+ require 'patron'
4
5
 
5
6
  # Time to add your specs!
6
7
  # http://rspec.info/
7
8
  describe "RDFa parser" do
8
- before(:each) { @parser = RdfaParser.new }
9
+ before(:each) do
10
+ @parser = RdfaParser.new
11
+
12
+ # Don't load external profiles when testing
13
+ basic_resp = mock("basic_resp")
14
+ basic_resp.stub(:status).and_return(200)
15
+ basic_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "basic.html")))
16
+
17
+ foaf_resp = mock("foaf_resp")
18
+ foaf_resp.stub(:status).and_return(200)
19
+ foaf_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "foaf.html")))
20
+
21
+ hcard_resp = mock("hcard_resp")
22
+ hcard_resp.stub(:status).and_return(200)
23
+ hcard_resp.stub(:body).and_return("HCARD")
24
+
25
+ profile_resp = mock("profile_resp")
26
+ profile_resp.stub(:status).and_return(200)
27
+ profile_resp.stub(:body).and_return("PROFILE")
28
+
29
+ xhv_resp = mock("xhv_resp")
30
+ xhv_resp.stub(:status).and_return(200)
31
+ xhv_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "xhv.html")))
32
+
33
+ sess = mock("session")
34
+ sess.stub(:base_url=)
35
+ sess.stub(:timeout=)
36
+ sess.stub(:get).with("http://www.w3.org/2007/08/pyRdfa/profiles/foaf").and_return(foaf_resp)
37
+ sess.stub(:get).with("http://www.w3.org/2007/08/pyRdfa/profiles/basic").and_return(basic_resp)
38
+ sess.stub(:get).with("http://www.w3.org/1999/xhtml/vocab").and_return(xhv_resp)
39
+ sess.stub(:get).with("http://microformats.org/profiles/hcard").and_return(hcard_resp)
40
+ sess.stub(:get).with("http://www.w3.org/2005/10/profile").and_return(profile_resp)
41
+ Patron::Session.stub!(:new).and_return(sess)
42
+ end
9
43
 
10
- it "should parse simple doc" do
44
+ it "should parse simple doc" do
11
45
  sampledoc = <<-EOF;
12
46
  <?xml version="1.0" encoding="UTF-8"?>
13
47
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
@@ -119,8 +153,12 @@ describe "RDFa parser" do
119
153
  specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
120
154
  #puts t.input
121
155
  #puts t.results
122
- t.run_test do |rdfa_string, rdfa_parser|
123
- rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
156
+ begin
157
+ t.run_test do |rdfa_string, rdfa_parser|
158
+ rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
159
+ end
160
+ rescue SparqlException => e
161
+ pending(e.message) { raise }
124
162
  end
125
163
  end
126
164
  end
@@ -128,15 +166,21 @@ describe "RDFa parser" do
128
166
  describe "that are unreviewed" do
129
167
  test_cases(suite).each do |t|
130
168
  next unless t.status == "unreviewed"
131
- #next unless t.name =~ /017\d/
169
+ #next unless t.name =~ /0185/
132
170
  #puts t.inspect
133
171
  specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
134
172
  begin
135
173
  t.run_test do |rdfa_string, rdfa_parser|
136
174
  rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
137
175
  end
176
+ rescue SparqlException => e
177
+ pending(e.message) { raise }
138
178
  rescue Spec::Expectations::ExpectationNotMetError => e
139
- pending() { raise }
179
+ if t.name =~ /01[789]\d/
180
+ raise
181
+ else
182
+ pending() { raise }
183
+ end
140
184
  end
141
185
  end
142
186
  end