X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse.test/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java b/net.sourceforge.phpeclipse.test/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java new file mode 100644 index 0000000..3c77d24 --- /dev/null +++ b/net.sourceforge.phpeclipse.test/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java @@ -0,0 +1,278 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package net.sourceforge.phpdt.core.tests.util; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Locale; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.sourceforge.phpdt.core.compiler.IProblem; +import net.sourceforge.phpdt.internal.compiler.CompilationResult; +import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies; +import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit; +import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit; +import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions; +import net.sourceforge.phpdt.internal.compiler.parser.Scanner; +import net.sourceforge.phpdt.internal.compiler.parser.UnitParser; +import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem; +import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory; +import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter; + +public class AbstractCompilerTest extends TestCase { + + protected String complianceLevel; + + public void checkParsePHP(char[] source, String expectedSyntaxErrorDiagnosis) { + // String testName) { + + UnitParser parser = new UnitParser(new ProblemReporter( + DefaultErrorHandlingPolicies.proceedWithAllProblems(), + new CompilerOptions(getCompilerOptions()), + new DefaultProblemFactory(Locale.getDefault()))); + + ICompilationUnit sourceUnit = new CompilationUnit(source, "", null); + CompilationResult compilationResult = new CompilationResult(sourceUnit, + 0, 0, 0); + + parser.dietParse(sourceUnit, compilationResult, true); + StringBuffer buffer = new StringBuffer(100); + if (compilationResult.hasProblems() || compilationResult.hasTasks()) { + IProblem[] problems = compilationResult.getAllProblems(); + int count = problems.length; + int problemCount = 0; + char[] unitSource = compilationResult.compilationUnit.getContents(); + for (int i = 0; i < count; i++) { + if (problems[i] != null) { + if (problemCount == 0) + buffer.append("----------\n"); + problemCount++; + buffer + .append(problemCount + + (problems[i].isError() ? ". ERROR" + : ". WARNING")); + buffer.append(" in " + + new String(problems[i].getOriginatingFileName()) + .replace('/', '\\')); + try { + buffer.append(((DefaultProblem) problems[i]) + .errorReportSource(unitSource)); + buffer.append("\n"); + buffer.append(problems[i].getMessage()); + buffer.append("\n"); + } catch (Exception e) { + StringWriter stringWriter = new StringWriter(); + e.printStackTrace(new PrintWriter(stringWriter)); + buffer.append(stringWriter.getBuffer()); + } + buffer.append("----------\n"); + } + } + } + String computedSyntaxErrorDiagnosis = buffer.toString(); + if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) { + System.out + .println(Util.displayString(computedSyntaxErrorDiagnosis)); + } + assertEquals("Invalid syntax error diagnosis", + expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis); + } + + public void checkParseHTML(char[] source, + String expectedSyntaxErrorDiagnosis) { + // String testName) { + + UnitParser parser = new UnitParser(new ProblemReporter( + DefaultErrorHandlingPolicies.proceedWithAllProblems(), + new CompilerOptions(getCompilerOptions()), + new DefaultProblemFactory(Locale.getDefault()))); + + ICompilationUnit sourceUnit = new CompilationUnit(source, "", null); + CompilationResult compilationResult = new CompilationResult(sourceUnit, + 0, 0, 0); + + parser.dietParse(sourceUnit, compilationResult, false); + + StringBuffer buffer = new StringBuffer(100); + if (compilationResult.hasProblems() || compilationResult.hasTasks()) { + IProblem[] problems = compilationResult.getAllProblems(); + int count = problems.length; + int problemCount = 0; + char[] unitSource = compilationResult.compilationUnit.getContents(); + for (int i = 0; i < count; i++) { + if (problems[i] != null) { + if (problemCount == 0) + buffer.append("----------\n"); + problemCount++; + buffer + .append(problemCount + + (problems[i].isError() ? ". ERROR" + : ". WARNING")); + buffer.append(" in " + + new String(problems[i].getOriginatingFileName()) + .replace('/', '\\')); + try { + buffer.append(((DefaultProblem) problems[i]) + .errorReportSource(unitSource)); + buffer.append("\n"); + buffer.append(problems[i].getMessage()); + buffer.append("\n"); + } catch (Exception e) { + StringWriter stringWriter = new StringWriter(); + e.printStackTrace(new PrintWriter(stringWriter)); + buffer.append(stringWriter.getBuffer()); + } + buffer.append("----------\n"); + } + } + } + String computedSyntaxErrorDiagnosis = buffer.toString(); + if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) { + System.out + .println(Util.displayString(computedSyntaxErrorDiagnosis)); + } + assertEquals("Invalid syntax error diagnosis", + expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis); + } + + /* + * Returns the possible compliance levels this VM instance can run. + */ + // public static int getPossibleComplianceLevels() { + // if (possibleComplianceLevels == -1) { + // String compliance = System.getProperty("compliance"); + // if (compliance != null) { + // if (COMPLIANCE_1_3.equals(compliance)) { + // possibleComplianceLevels = F_1_3; + // } else if (COMPLIANCE_1_4.equals(compliance)) { + // possibleComplianceLevels = F_1_4; + // } else if (COMPLIANCE_1_5.equals(compliance)) { + // possibleComplianceLevels = F_1_5; + // } else { + // System.out.println("Invalid compliance specified (" + compliance + ")"); + // System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + // + ", " + COMPLIANCE_1_5); + // System.out.println("Defaulting to all possible compliances"); + // } + // } + // if (possibleComplianceLevels == -1) { + // possibleComplianceLevels = F_1_3; + // String specVersion = System.getProperty("java.specification.version"); + // boolean canRun1_4 = !"1.0".equals(specVersion) && + // !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && + // !"1.3".equals(specVersion); + // if (canRun1_4) { + // possibleComplianceLevels |= F_1_4; + // } + // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion); + // if (canRun1_5) { + // possibleComplianceLevels |= F_1_5; + // } + // } + // } + // return possibleComplianceLevels; + // } + /* + * Returns a test suite including the tests defined by the given classes for + * the given complianceLevel (see AbstractCompilerTest for valid values) and + * using the given setup class (CompilerTestSetup or a subclass) + */ + public static Test suiteForComplianceLevel(String complianceLevel, + Class setupClass, ArrayList testClasses) { + TestSuite suite; + if (testClasses.size() == 1) { + suite = new TestSuite((Class) testClasses.get(0), complianceLevel); + } else { + suite = new TestSuite(complianceLevel); + for (int i = 0, length = testClasses.size(); i < length; i++) { + Class testClass = (Class) testClasses.get(i); + TestSuite innerSuite = new TestSuite(testClass); + suite.addTest(innerSuite); + } + } + + // call the setup constructor with the suite and compliance level + try { + Constructor constructor = setupClass.getConstructor(new Class[] { + Test.class, String.class }); + Test setUp = (Test) constructor.newInstance(new Object[] { suite, + complianceLevel }); + return setUp; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.getTargetException().printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return null; + } + + public AbstractCompilerTest(String name) { + super(name); + } + + protected Map getCompilerOptions() { + Map options = new CompilerOptions().getMap(); + // ignore warnings + options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning, + CompilerOptions.IGNORE); + options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning, + CompilerOptions.IGNORE); + options.put( + CompilerOptions.OPTION_PHPBadStyleUppercaseIdentifierWarning, + CompilerOptions.IGNORE); + options.put(CompilerOptions.OPTION_PHPIncludeNotExistWarning, + CompilerOptions.IGNORE); + options.put(CompilerOptions.OPTION_UninitializedLocalVariableWarning, + CompilerOptions.IGNORE); + options.put(CompilerOptions.OPTION_CodeCannotBeReachedWarning, + CompilerOptions.IGNORE); + return options; + } + + public String getName() { + String name = super.getName(); + if (this.complianceLevel != null) { + name = this.complianceLevel + " - " + name; + } + return name; + } + + protected void checkPHP(String strEval) { + checkPHP(strEval, ""); + } + + protected void checkPHP(String strEval, String expectedSyntaxErrorDiagnosis) { + if (Scanner.DEBUG) { + System.out.println("\n------------------------------------"); + System.out.println(strEval); + } + checkParsePHP(strEval.toCharArray(), expectedSyntaxErrorDiagnosis); + } + + protected void checkHTML(String strEval) { + if (Scanner.DEBUG) { + System.out.println("\n------------------------------------"); + System.out.println(strEval); + } + checkParseHTML(strEval.toCharArray(), ""); + } +}