#!/usr/bin/env ruby
#
# Run RDF Core specs
# $Id: coretests.rb,v 1.10 2003/04/18 18:06:09 danbri Exp $
# Dan Brickley <danbri@w3.org>

# http://esw.w3.org/topic/RubyRDF

#
# NOTE: This script is OBSOLETE. Left here for reference only.
#       We now autogenerate a set of Ruby tests using the ./gcore.rb
#       script, rather than loop through them at runtime. 
#       I'm leaving this script in the tree for reference, when 
#       discussing approaches to automated testing. Best not to use it.
#       --danbri



#
# Note that you need to download the RDF Core tests
# ('make rdfcore' should achieve this).
#
# Here we run the positive and negative parser tests from RDFCore
#
# RubyRDF: http://www.w3.org/2001/12/rubyrdf/intro.html
#
# testing notes:
#   http://rdfig.xmlhack.com/2003/03/26/2003-03-26.html#1048691883.680525
# 
# manifest is:
#   http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf

$LOAD_PATH.unshift '../lib/'

require 'test/unit'
require 'basicrdf'
require 'find'

class TC_CoreTester < Test::Unit::TestCase

  attr_accessor :pos, :neg, :testns, :desc

  def approved
    return @pos + @neg
  end

  def setup
     toc=Loader.get_rdf 'rdf-testcases/Manifest.rdf'
     @pos=[]
     @neg=[]
     @desc={} # desc of each test, looked up by input doc URI
     status=nil
     rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     @testns="http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#"
     positives = toc.ask (Statement.new nil, rdf+'type', testns+'PositiveParserTest').subjects
     negatives = toc.ask (Statement.new nil, rdf+'type', testns+'NegativeParserTest').subjects

     # Fixme: we here assume only one input doc for each +/- parser test. may be multiple?
 
     # tests are things like these:     
     # eg: http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-syntax-incomplete/error006.rdf

     #
     positives.each do |pt| 
       indoc = toc.ask (Statement.new pt, testns+'inputDocument', nil).objects.shift.to_s 
       status = toc.ask (Statement.new pt, testns+'status', nil).objects.shift.to_s
       next unless status == "APPROVED"
       next if indoc =~ /\\n / 		# Known parser problem; see rdfcal/ test
       #puts "Storing +: "+indoc	
       @pos.push indoc
       d = toc.ask (Statement.new pt, testns+'description',nil).objects.shift.to_s
       #puts "[+] approved: doc:"+indoc+" pt:#{pt} with desc: '#{d}'"
       desc[indoc]=d.clone
     end
     negatives.each do |pt| 
       indoc = toc.ask (Statement.new pt, testns+'inputDocument', nil).objects.shift.to_s
       status = toc.ask (Statement.new pt, testns+'status', nil).objects.shift.to_s
       next unless status == "APPROVED"
       next if indoc =~ /\\n /
       #puts "Storing -: "+indoc	
       @neg.push indoc
       d = toc.ask (Statement.new pt, testns+'description',nil).objects.shift.to_s
       #puts "[-] approved: doc:"+indoc+" pt:#{pt} with desc: '#{d}'"
       desc[indoc]=d.clone
     end
  end

  def test_corespec
    results={}
    errors=[]
    testroot='http://www.w3.org/2000/10/rdf-tests/rdfcore/'
    localroot='rdf-testcases/'
    approved.each do |docname|
      begin
        worked=false
        errormsg=''
        filename=docname.clone.gsub!(testroot,localroot)
        t='?' 	# shouldn't be any unknowns
        t='+' if pos.member? docname 
        t='-' if neg.member? docname
        raise("Test has unknown status:#{docname}") if t=='?' # this should never happen, but things could change re allowing other statuses etc
        begin
          Loader.get_rdf filename
          worked=true
        rescue Exception
          worked=false
          errormsg=$!
        end
        if worked == true
          assert(t=='+', "Mistakenly suceeded on '#{t}' #{docname} ")
        end
        if worked == false
          assert(t=='-', "Mistakenly failed on '#{t}' #{docname} Error: '#{errormsg}'")
        end
      rescue Exception
        # puts "ERROR: #{$!}"
        emsg = $!.to_s.clone
        emsg.gsub!(/\s+/," ")
        d=desc[docname]
        d.gsub!(/(\\n)+/," ")
        d.gsub!(/(\\t)+/," ")
        d.gsub!(/\s+/, " ")
        errors.push "[parser error] #{emsg}  more info: '#{d}'"
      end
    end
    if errors.size>0
      summary=''
      errors.each do |e|
        summary += "#{e.to_s}\n"
      end
      raise "One or more rdfcore tests failed. count: #{errors.size} \ndetails: \n#{summary}"
    end
  end
end
