#!/usr/bin/env python
# Version: $Header: /compromis/home/xmlschemata/cvs/downloads/python/xvif/test-suite.py,v 1.3 2002/09/26 21:46:35 vdv Exp $

"""
The contents of this file are subject to the Mozilla Public License  Version 1.1 (the "License"); you may not use this file except in  compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS"  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  License for the specific language governing rights and limitations under  the License. 

The Original Code is available at http://downloads.xmlschemata.org/python/xvif/

The Initial Developer of the Original Code is Eric van der Vlist. Portions  created by Eric van der Vlist are Copyright (C) 2002. All Rights Reserved. 

Relax NG is a specification edited by the OASIS RELAX NG Technical Committee:
http://www.oasis-open.org/committees/relax-ng/

This implementation uses the implementation notes written by James Clark:
http://www.thaiopensource.com/relaxng/implement.html

Contributor(s): 
"""

import unittest
import rng
import copy
import os
import re
import sys
from xml.sax import make_parser, parseString, parse
from Ft.Xml import InputSource
from Ft.Xml.Domlette import NonvalidatingReader


class TestRelaxNg(unittest.TestCase):
	
	def schemaParser(self, schemaLoc, valid=1, display=0):
		parser = make_parser()
		schema = rng.RngParser()
		parser.setContentHandler(schema)
		parser.setFeature("http://xml.org/sax/features/namespaces", 1)
		factory = InputSource.DefaultFactory
		isrc=factory.fromUri(schemaLoc)
		exception = 0
		#display = schemaLoc.find("/home/vdv/dsdl/tests/069") == -1
		display =0
		if display:
			print "-------------------------"
			print u"%s" % schemaLoc
		msg=""
		try:
			parser.parse(isrc.stream)
		except (rng.RngSchemaInvalidException, rng.RngSchemaInvalidRecursionException), msg:
			exception=1
		if display:
			print u"<Schema>\n%s\n</Schema>" % schema
		if valid:
			self.failUnless(exception==0,
				'the schema %s should be valid (%s)' % (schemaLoc, msg))
		else:
			self.failUnless(exception==1,
				'the schema %s should be invalid' % schemaLoc)
		return schema

	def validateInstance(self, schema, instance, valid):
		display = instance.find("/home/vdv/dsdl/tests/241AAA/") > -1
		#print "-------------------------"
		if display:
			print u"%s" % schema
		reader = NonvalidatingReader
		doc = reader.parseUri(instance)
		s = copy.deepcopy(schema.grammar)
		deriv = s.deriv(doc.firstChild)
		if display:
			print "-------------------------"
			print u"deriv: %s" % deriv
		if valid:
			self.failUnless(deriv.nullable(),
				u'Should be valid (%r, %r)' % (instance, deriv))
		else:
			self.failUnless(not deriv.nullable(),
				u'Should not be valid (%r, %r)' % (instance, deriv))
		return deriv

	def run1test(self):
		global suite
		print self._TestCase__testMethodName
		dir = "%s/%s/" % (suite, self._TestCase__testMethodName[4:])
		try:
			schema = self.schemaParser(dir + "c.rng", 1, 0)
			for F in os.listdir(dir):
				if F.rfind(".xml") == len(F)-4:
					valid = F.find(".v.") != -1
					self.validateInstance(schema, dir + F, valid)
		except (IOError, OSError):
			schema = self.schemaParser(dir + "i.rng", 0, 0)


if __name__ == "__main__":
	if len(sys.argv) > 1: 
		suite = sys.argv[1]
	else:
		suite = "tests/rng/jjc"
	print suite
	for rep in os.listdir(suite):
		if re.match("\d{3}", rep):
			f =  lambda self: self.run1test()
			setattr(TestRelaxNg, "test"+rep, f)
	unittest.TestProgram(argv=[sys.argv[0]]) # Just pretend that there were no args :-)

