#!/usr/bin/env ruby

# $Id: gcore.rb,v 1.4 2003/04/18 18:06:09 danbri Exp $
# A script (FIXME -- not working yet) to generate a test script for 
# RubyRDF core syntax tests. It reads a manifest and writes out a Ruby file.
# Dan Brickley <danbri@w3.org>

header=%q!
$LOAD_PATH.unshift '../lib/'
require 'test/unit'
require 'basicrdf'
require 'find'

class TC_CoreTester < Test::Unit::TestCase

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

!


looptext=%q!
  def test_syntax_XXXXX1
    worked=false
    errormsg=''
    docname="XXXXX2"
    filename="XXXXX3"
    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

  end
!


###########################################

    
$LOAD_PATH.unshift '../lib/'
require 'basicrdf'
require 'find'
     
toc=Loader.get_rdf 'rdf-testcases/Manifest.rdf'

results={}
errors=[]
testroot='http://www.w3.org/2000/10/rdf-tests/rdfcore/'
localroot='rdf-testcases/'
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

testroot='http://www.w3.org/2000/10/rdf-tests/rdfcore/'
localroot='rdf-testcases/'

n=0
out=header.clone
setuptxt="def setup\n\n"
setuptxt += "  @pos=["
pos.each do |p| 
  setuptxt += "'#{p}' ,"
end
setuptxt.gsub!(/,\s*$/,"")
setuptxt += "]\n\n\n"

setuptxt += "  @neg=["
neg.each do |p| 
  setuptxt += "'#{p}' ,"
end
setuptxt.gsub!(/,\s*$/,"")
setuptxt += "]\n\nend\n"

out += setuptxt

(pos+neg).each do |docname|
  #puts docname
  item=looptext.clone
  item.gsub!(/XXXXX1/,n.to_s)
  item.gsub!(/XXXXX2/,docname)
  item.gsub! /XXXXX3/, docname.clone.gsub(testroot,localroot)
  n+=1
  out += item
end

out += "\n\nend\n\n"

puts out
