spec/rdfa_helper.rb in rdf-rdfa-0.2.2 vs spec/rdfa_helper.rb in rdf-rdfa-0.3.0

- old
+ new

@@ -2,11 +2,10 @@ require 'open-uri' autoload :YAML, "yaml" autoload :CGI, 'cgi' RDFA_DIR = File.join(File.dirname(__FILE__), 'rdfa-test-suite') -RDFA_NT_DIR = File.join(File.dirname(__FILE__), 'rdfa-triples') RDFA_MANIFEST_URL = "http://rdfa.digitalbazaar.com/test-suite/" RDFA_TEST_CASE_URL = "#{RDFA_MANIFEST_URL}test-cases/" class SparqlException < IOError; end @@ -22,10 +21,11 @@ attr_accessor :title attr_accessor :informationResourceInput attr_accessor :informationResourceResults attr_accessor :purpose attr_accessor :reviewStatus + attr_accessor :classification attr_accessor :suite attr_accessor :specificationReference attr_accessor :expectedResults attr_accessor :parser attr_accessor :debug @@ -41,11 +41,11 @@ obj = statement.object.is_a?(RDF::Literal) ? statement.object.value : statement.object.to_s puts "#{pred}: #{obj}" if ::RDF::RDFa::debug? unless self.about - self.about = statement.subject + self.about = statement.subject.to_s self.name = self.about.to_s.split(/[\#\/]/).last || self.about end if pred == "expectedResults" self.expectedResults = obj == "true" @@ -64,10 +64,11 @@ title informationResourceInput informationResourceResults purpose reviewStatus + classification specificationReference expectedResults ).map {|a| v = self.send(a); "#{a}='#{v}'" if v}.compact.join(", ") + "]" end @@ -94,10 +95,11 @@ body = File.readlines(File.join(RDFA_DIR, "tests", f)).map do |line| found_head ||= line.match(/<head/) if found_head line.chop else + found_head ||= line.match(%r(http://www.w3.org/2000/svg)) namespaces << line nil end end.compact.join("\n") @@ -105,52 +107,48 @@ case suite when "xhtml" head = "" + %(<?xml version="1.0" encoding="UTF-8"?>\n) + - %(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">\n) + - %(<html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0"\n) - head + "#{namespaces}>\n#{body.gsub(TCPATHRE, tcpath)}\n</html>" - when "xhtml11" - head = "" + - %(<?xml version="1.0" encoding="UTF-8"?>\n) + %(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">\n) + - %(<html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.1"\n) + %(<html xmlns="http://www.w3.org/1999/xhtml"\n) head + "#{namespaces}>\n#{body.gsub(TCPATHRE, tcpath)}\n</html>" when "html4" head ="" + - %(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n) + - %(<html version="XHTML+RDFa 1.0"\n) + %(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/MarkUp/DTD/html401-rdfa11-1.dtd">\n) + + %(<html\n) head + "#{namespaces}>\n#{body.gsub(TCPATHRE, tcpath).gsub(HTMLRE, '\1.html')}\n</html>" when "html5" head = "<!DOCTYPE html>\n" - head += namespaces.empty? ? %(<html version="HTML+RDFa 1.0">) : "<html\n#{namespaces}>" + head += namespaces.empty? ? %(<html>) : "<html\n#{namespaces}>" head + "\n#{body.gsub(TCPATHRE, tcpath).gsub(HTMLRE, '\1.html')}\n</html>" + when "svgtiny" + head = %(<?xml version="1.0" encoding="UTF-8"?>\n) + head += namespaces.empty? ? %(<svg>) : "<svg\n#{namespaces}>" + head + "\n#{body.gsub(TCPATHRE, tcpath).gsub(HTMLRE, '\1.svg')}\n</svg>" else nil end end # Read in file, and apply modifications reference either .html or .xhtml def results f = self.name + ".sparql" body = File.read(File.join(RDFA_DIR, "tests", f)).gsub(TCPATHRE, tcpath) - suite =~ /xhtml/ ? body : body.gsub(HTMLRE, '\1.html') + case suite + when /xhtml/ then body + when /svg/ then body.gsub(HTMLRE, '\1.svg') + else body.gsub(HTMLRE, '\1.html') + end end - def triples - f = self.name + ".nt" - body = File.read(File.join(RDFA_NT_DIR, f)).gsub(TCPATHRE, tcpath) - suite =~ /xhtml/ ? body : body.gsub(HTMLRE, '\1.html') - end - def inputDocument; self.name + ".txt"; end def outputDocument; self.name + ".sparql"; end def version - suite == "xhtml11" ? :rdfa_1_1 : :rdfa_1_0 + :rdfa_1_1 end # Run test case, yields input for parser to create triples def run_test rdfa_string = input @@ -158,16 +156,11 @@ # Run graph = yield(rdfa_string) query_string = results - triples = self.triples rescue nil - - if (query_string.match(/UNION|OPTIONAL/) || title.match(/XML/)) && triples - # Check triples, as Rasql doesn't implement UNION - graph.should be_equivalent_graph(triples, self) - elsif $redland_enabled + if $redland_enabled # Run SPARQL query graph.should pass_query(query_string, self) else raise SparqlException, "Query skipped, Redland not installed" end @@ -188,13 +181,16 @@ manifest_file = File.join(RDFA_DIR, "#{suite}-manifest.rdf") yaml_file = File.join(File.dirname(__FILE__), "#{suite}-manifest.yml") @test_cases = unless File.file?(yaml_file) + t = Time.now puts "parse #{manifest_file} @#{Time.now}" - graph = RDF::Graph.load(manifest_file, :base_uri => @manifest_url) - puts "parsed #{graph.size} statements @#{Time.now}" + graph = RDF::Graph.new + graph << RDF::RDFXML::Reader.new(File.open(manifest_file), :base_url => @manifest_url) + diff = Time.now - t + puts "parsed #{graph.size} statements in #{diff} seconds (#{(graph.size / diff).to_i} statements/sec) @#{Time.now}" graph.subjects.map do |subj| t = TestCase.new(graph.query(:subject => subj), suite) t.name ? t : nil end. @@ -223,34 +219,39 @@ end end end end +# Stub RDF::Reader.open module RDF - class Graph - def self.load(url, options = {}, &block) - url = case url.to_s + class Reader + class << self + alias orig_open open + end + + def self.stub_file(filename) + case filename.to_s + when %r(http://rdfa.digitalbazaar.com/test-suite/profile/\w+) + filename.to_s.sub(%r(http://rdfa.digitalbazaar.com/test-suite/profile/\w+), + File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'profile')) when %r(http://rdfa.digitalbazaar.com/test-suite/test-cases/\w+) - file = url.to_s.sub(%r(http://rdfa.digitalbazaar.com/test-suite/test-cases/\w+), + filename.to_s.sub(%r(http://rdfa.digitalbazaar.com/test-suite/test-cases/\w+), File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'tests')) - file when "http://www.w3.org/1999/xhtml/vocab" - file = File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'profile', "xhv") - file + File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'profile', "xhv") when "http://www.w3.org/2005/10/profile" - file = File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'profile', "xhv") + File.join(File.expand_path(File.dirname(__FILE__)), 'rdfa-test-suite', 'profile', "xhv") + when "http://example.com/profile" + File.join(TMP_DIR, "profile.html") else - url + filename end - - self.new(url, options) do |graph| - graph.load! unless graph.unnamed? - if block_given? - case block.arity - when 1 then block.call(graph) - else graph.instance_eval(&block) - end - end - end + end + + def self.open(filename, options = {}, &block) + filename = ::RDF::Reader.stub_file(filename) + options[:format] ||= :rdfa + puts "parse #{filename} with format #{options[:format]}" if $verbose + orig_open(filename, options, &block) end end end