[DoXQuery-Devel] doxquery/test/src/com/doxological/doxquery/test ParserTester.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
jpcs
From: John S. <jp...@us...> - 2005-05-06 01:19:04
|
Update of /cvsroot/doxquery/doxquery/test/src/com/doxological/doxquery/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11861/test/src/com/doxological/doxquery/test Added Files: ParserTester.java Log Message: Fixed bug #1196090, an issue with the lexer and comments in certain positions. Added a functional regression test suits for the parser, which compares query AST output to the expected results. The tests can be built using the ant target "compile_test", and run using the target "test". --- NEW FILE: ParserTester.java --- /* * Copyright 2004 Doxological Ltd. * * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.doxological.doxquery.test; import java.io.*; import java.util.*; import java.lang.Math; import java.lang.System; import javax.xml.namespace.QName; import javax.xml.XMLConstants; import com.doxological.doxquery.*; import com.doxological.doxquery.grammar.*; import com.doxological.doxquery.utils.*; import com.doxological.doxquery.context.*; import com.doxological.doxquery.types.*; import com.doxological.doxquery.types.Type; import com.doxological.doxquery.parser.*; /** * Functional tests for the XQuery parser. Works by * parsing any files with an ".xqy" extension in the given * directory, and calling the "dump" method on the * resulting AST. It then compares this string with a file * with the same file stem, bu an extension of ".expected". * * @author John Snelson * * @see com.doxological.doxquery.grammar.XQueryGrammar * */ public class ParserTester { /** * Takes one argument - the test directory to scan for * ".xqy" and ".expected" files. */ public static void main(String args[]) { if(args.length != 1) { error("Wrong number of arguments"); } File dir = new File(args[0]); if(!dir.isDirectory()) { error("Argument should specify the test directory"); } List<String> failures = new LinkedList<String>(); int testsRun = runTests(dir, "", failures); System.out.println(); System.out.println("Tests run: " + testsRun + ", Tests passed: " + (testsRun - failures.size()) + ", Tests failed: " + failures.size()); System.out.println(); if(failures.isEmpty()) { System.out.println("All tests passed"); System.exit(0); } else { System.out.println("Failures have occured:"); int count = 1; for(String f: failures) { System.out.println(count + ") " + f); ++count; } System.exit(1); } } private static void error(String message) { System.err.println(message); System.exit(1); } private static int runTests(File dir, String indent, List<String> failures) { int testsRun = 0; File testFiles[] = dir.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isFile() && pathname.getName().endsWith(".xqy"); } }); File testDirs[] = dir.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory(); } }); if(testDirs.length != 0 || testFiles.length != 0) { System.err.print(indent + dir.getName() + ": "); for(File f: testFiles) { try { System.err.print("."); ++testsRun; runTest(f); } catch(Exception e) { failures.add(f.toString() + ": " + e.toString()); System.err.print("F"); } } System.err.println(); for(File d: testDirs) { testsRun += runTests(d, indent + " ", failures); } } return testsRun; } private static void runTest(File file) throws Exception { XQueryEnvironment env = new XQueryEnvironment(); XQueryGrammar parser = new XQueryGrammar(new FileReader(file)); parser.setEnvironment(env); parser.setResourceName(file.toString()); MutableStaticContext sContext = env.createStaticContext(); Module module = parser.Module(sContext); String actual = ""; if(!module.isModule()) { actual = module.getQueryBody().dumpToString(""); actual += "\n"; } actual += sContext.toString(true); // Read the expected file File exp = new File(file.getParentFile(), file.getName().replace(".xqy", ".expected")); if(!exp.isFile()) { // Write the failed test output writeActualFile(file, actual); throw new Exception("The expected file cannot be found: " + exp.toString()); } StringBuilder expected = new StringBuilder(); FileReader reader = new FileReader(exp); int c; while((c = reader.read()) != -1) { expected.append((char)c); } reader.close(); if(!expected.toString().equals(actual)) { // Write the failed test output writeActualFile(file, actual); throw new Exception("The expected file does not match the actual output."); } } private static void writeActualFile(File file, String content) throws Exception { File act = new File(file.getParentFile(), file.getName().replace(".xqy", ".actual")); FileWriter writer = new FileWriter(act); writer.write(content); writer.close(); } } |