rdf-rdfxml 0.3.2.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/spec/matchers.rb CHANGED
@@ -1,61 +1,79 @@
1
- module Matchers
2
- class BeEquivalentGraph
3
- Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
4
- def normalize(graph)
5
- case graph
6
- when RDF::Graph then graph
7
- when IO, StringIO
8
- RDF::Graph.new.load(graph, :base_uri => @info.about)
9
- else
10
- # Figure out which parser to use
11
- g = RDF::Graph.new
12
- reader_class = detect_format(graph)
13
- reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
14
- g
15
- end
16
- end
17
-
18
- def initialize(expected, info)
19
- @info = if info.respond_to?(:about)
20
- info
21
- elsif info.is_a?(Hash)
22
- identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
23
- trace = info[:trace]
24
- trace = trace.join("\n") if trace.is_a?(Array)
25
- Info.new(identifier, info[:information] || "", trace, info[:compare])
26
- else
27
- Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
28
- end
29
- @expected = normalize(expected)
30
- end
1
+ require 'rspec/matchers'
31
2
 
32
- def matches?(actual)
33
- @actual = normalize(actual)
34
- @actual == @expected
3
+ RSpec::Matchers.define :have_xpath do |xpath, value, namespaces = {}|
4
+ match do |actual|
5
+ @doc = Nokogiri::XML.parse(actual)
6
+ @doc.should be_a(Nokogiri::XML::Document)
7
+ @doc.root.should be_a(Nokogiri::XML::Element)
8
+ @namespaces = @doc.namespaces.
9
+ merge(namespaces).
10
+ merge("xhtml" => "http://www.w3.org/1999/xhtml", "xml" => "http://www.w3.org/XML/1998/namespace")
11
+ @result = @doc.root.at_xpath(xpath, @namespaces) rescue false
12
+ case value
13
+ when false
14
+ @result.should be_nil
15
+ when Array
16
+ @result.to_s.split(" ").should include(*value)
17
+ when Regexp
18
+ @result.to_s.should =~ value
19
+ else
20
+ @result.to_s.should == value
35
21
  end
22
+ end
23
+
24
+ failure_message_for_should do |actual|
25
+ msg = "expected to that #{xpath.inspect} would be #{value.inspect} in:\n" + actual.to_s
26
+ msg += "was: #{@result}"
27
+ end
28
+ end
36
29
 
37
- def failure_message_for_should
38
- info = @info.respond_to?(:information) ? @info.information : @info.inspect
39
- if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size
40
- "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
41
- elsif @expected.is_a?(Array) && @actual.size != @expected.length
42
- "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
43
- else
44
- "Graph differs"
45
- end +
46
- "\n#{info + "\n" unless info.empty?}" +
47
- (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
48
- (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
49
- "Unsorted Expected:\n#{@expected.to_ntriples}" +
50
- "Unsorted Results:\n#{@actual.to_ntriples}" +
51
- (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
52
- end
53
- def negative_failure_message
54
- "Graphs do not differ\n"
30
+ def normalize(graph)
31
+ case graph
32
+ when RDF::Graph then graph
33
+ when IO, StringIO
34
+ RDF::Graph.new.load(graph, :base_uri => @info.about)
35
+ else
36
+ # Figure out which parser to use
37
+ g = RDF::Graph.new
38
+ reader_class = RDF::Reader.for(detect_format(graph))
39
+ reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
40
+ g
41
+ end
42
+ end
43
+
44
+ Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
45
+
46
+ RSpec::Matchers.define :be_equivalent_graph do |expected, info|
47
+ match do |actual|
48
+ @info = if info.respond_to?(:about)
49
+ info
50
+ elsif info.is_a?(Hash)
51
+ identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
52
+ trace = info[:trace]
53
+ trace = trace.join("\n") if trace.is_a?(Array)
54
+ Info.new(identifier, info[:information] || "", trace, info[:compare])
55
+ else
56
+ Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
55
57
  end
58
+ @expected = normalize(expected)
59
+ @actual = normalize(actual)
60
+ @actual.isomorphic_with?(@expected)
56
61
  end
57
62
 
58
- def be_equivalent_graph(expected, info = nil)
59
- BeEquivalentGraph.new(expected, info)
60
- end
63
+ failure_message_for_should do |actual|
64
+ info = @info.respond_to?(:information) ? @info.information : @info.inspect
65
+ if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size
66
+ "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
67
+ elsif @expected.is_a?(Array) && @actual.size != @expected.length
68
+ "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
69
+ else
70
+ "Graph differs"
71
+ end +
72
+ "\n#{info + "\n" unless info.empty?}" +
73
+ (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
74
+ (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
75
+ "Unsorted Expected:\n#{@expected.dump(:ntriples)}" +
76
+ "Unsorted Results:\n#{@actual.dump(:ntriples)}" +
77
+ (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
78
+ end
61
79
  end
data/spec/rdf_test.rb CHANGED
@@ -46,29 +46,8 @@ module Fixtures
46
46
  ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") +
47
47
  "]"
48
48
  end
49
-
50
- # Run test case, yields input for parser to create triples
51
- def run_test(options = {})
52
- # Run
53
- graph = yield
54
-
55
- return unless self.outputDocument
56
-
57
- case self.compare
58
- when :none
59
- # Don't check output, just parse to graph
60
- when :array
61
- @parser.graph.should be_equivalent_graph(self.output, self)
62
- else
63
- #puts "parse #{self.outputDocument} as #{RDF::Reader.for(self.outputDocument)}"
64
- format = detect_format(self.output)
65
- output_graph = RDF::Graph.load(self.outputDocument, :format => format, :base_uri => self.inputDocument)
66
- puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if ::RDF::N3::debug?
67
- graph.should Matchers::be_equivalent_graph(output_graph, self)
68
- end
69
- end
70
49
  end
71
-
50
+
72
51
  class PositiveParserTest < Entry
73
52
  default_source :entries
74
53
  type Test.PositiveParserTest
data/spec/reader_spec.rb CHANGED
@@ -144,7 +144,7 @@ EOF
144
144
  graph = parse(sampledoc, :base_uri => "http://example.com", :validate => true)
145
145
  #puts @debug
146
146
  graph.size.should == 10
147
- # print graph.to_ntriples
147
+ # print graph.dump(:ntriples
148
148
  # TODO: add datatype parsing
149
149
  # TODO: make sure the BNode forging is done correctly - an internal element->nodeID mapping
150
150
  # TODO: proper test
@@ -309,17 +309,17 @@ EOF
309
309
  #puts t.inspect
310
310
  specify "#{t.name}: " + (t.description || "#{t.inputDocument} against #{t.outputDocument}") do
311
311
  begin
312
- t.run_test do
313
- t.debug = []
314
- g = RDF::Graph.new
315
- @reader.new(t.input,
316
- :base_uri => t.inputDocument,
317
- :validate => false,
318
- :debug => t.debug).each do |statement|
319
- g << statement
320
- end
321
- g
322
- end
312
+ graph = RDF::Graph.new << @reader.new(t.input,
313
+ :base_uri => t.inputDocument,
314
+ :validate => false,
315
+ :debug => t.debug)
316
+
317
+ # Parse result graph
318
+ #puts "parse #{self.outputDocument} as #{RDF::Reader.for(self.outputDocument)}"
319
+ format = detect_format(t.output)
320
+ output_graph = RDF::Graph.load(t.outputDocument, :format => format, :base_uri => t.inputDocument)
321
+ puts "result: #{CGI.escapeHTML(graph.dump(:ntriples))}" if ::RDF::N3::debug?
322
+ graph.should be_equivalent_graph(output_graph, t)
323
323
  rescue RSpec::Expectations::ExpectationNotMetError => e
324
324
  if t.inputDocument =~ %r(xml-literal|xml-canon)
325
325
  pending("XMLLiteral canonicalization not implemented yet")
@@ -339,17 +339,11 @@ EOF
339
339
  #next unless t.name =~ /1/
340
340
  #puts t.inspect
341
341
  specify "test #{t.name}: " + (t.description || t.inputDocument) do
342
- t.run_test do
343
- lambda do
344
- g = RDF::Graph.new
345
- @reader.new(t.input,
346
- :base_uri => t.inputDocument,
347
- :validate => true).each do |statement|
348
- g << statement
349
- end
350
- g.should be_empty
351
- end.should raise_error(RDF::ReaderError)
352
- end
342
+ lambda do
343
+ RDF::Graph.new << @reader.new(t.input,
344
+ :base_uri => t.inputDocument,
345
+ :validate => true)
346
+ end.should raise_error(RDF::ReaderError)
353
347
  end
354
348
  end
355
349
  end
data/spec/spec_helper.rb CHANGED
@@ -5,16 +5,14 @@ $:.unshift File.dirname(__FILE__)
5
5
  require 'rubygems'
6
6
  require 'rspec'
7
7
  require 'matchers'
8
- require 'bigdecimal' # XXX Remove Me
9
8
  require 'rdf/rdfxml'
10
9
  require 'rdf/ntriples'
11
10
  require 'rdf/spec'
12
11
  require 'rdf/spec/matchers'
13
12
  require 'rdf/isomorphic'
13
+ require 'yaml' # XXX should be in open-uri/cached
14
14
  require 'open-uri/cached'
15
15
 
16
- include Matchers
17
-
18
16
  # Create and maintain a cache of downloaded URIs
19
17
  URI_CACHE = File.expand_path(File.join(File.dirname(__FILE__), "uri-cache"))
20
18
  Dir.mkdir(URI_CACHE) unless File.directory?(URI_CACHE)
@@ -24,15 +22,6 @@ module RDF
24
22
  module Isomorphic
25
23
  alias_method :==, :isomorphic_with?
26
24
  end
27
- class Graph
28
- def to_ntriples
29
- RDF::Writer.for(:ntriples).buffer do |writer|
30
- self.each_statement do |statement|
31
- writer << statement
32
- end
33
- end
34
- end
35
- end
36
25
  end
37
26
 
38
27
  ::RSpec.configure do |c|
@@ -41,7 +30,6 @@ end
41
30
  c.exclusion_filter = {
42
31
  :ruby => lambda { |version| !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) },
43
32
  }
44
- c.include(Matchers)
45
33
  c.include(RDF::Spec::Matchers)
46
34
  end
47
35
 
@@ -56,10 +44,10 @@ def detect_format(stream)
56
44
  string = stream.to_s
57
45
  end
58
46
  case string
59
- when /<\w+:RDF/ then RDF::RDFXML::Reader
60
- when /<RDF/ then RDF::RDFXML::Reader
61
- #when /<html/i then RDF::RDFa::Reader
62
- when /@prefix/i then RDF::N3::Reader
63
- else RDF::NTriples::Reader
47
+ when /<\w+:RDF/ then :rdf
48
+ when /<RDF/ then :rdf
49
+ #when /<html/i then :rdfa
50
+ when /@prefix/i then :n3
51
+ else :ntriples
64
52
  end
65
53
  end