/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
+ * 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.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;
+import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
//import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
//import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
public class AbstractCompilerTest extends TestCase {
- public static final String COMPLIANCE_1_3 = "1.3";
- public static final String COMPLIANCE_1_4 = "1.4";
- public static final String COMPLIANCE_1_5 = "1.5";
-
- public static final int F_1_3 = 0x1;
- public static final int F_1_4 = 0x2;
- public static final int F_1_5 = 0x4;
-
private static int possibleComplianceLevels = -1;
protected String complianceLevel;
+ public void checkParsePHP(
+ char[] source,
+ String expectedSyntaxErrorDiagnosis) {
+// String testName) {
- /*
- * 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;
+ 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);
+
+ CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
+// if (computedUnit.types != null) {
+// for (int i = computedUnit.types.size(); --i >= 0;){
+// ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
+// }
+// }
+
+ 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");
}
}
}
- return possibleComplianceLevels;
+ 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) {
- /*
- * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
- * and using the given setup class (CompilerTestSetup or a subclass)
- */
- public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
- TestSuite all = new TestSuite(suiteName);
- int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
- if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
- all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
- }
- if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
- all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
+ 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);
+
+ CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
+// if (computedUnit.types != null) {
+// for (int i = computedUnit.types.size(); --i >= 0;){
+// ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
+// }
+// }
+
+ 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");
+ }
+ }
}
- if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
- all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
+ String computedSyntaxErrorDiagnosis = buffer.toString();
+ if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
+ System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
}
- return all;
+ 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
+ * 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) {
super(name);
}
-// protected Map getCompilerOptions() {
-// Map options = new CompilerOptions().getMap();
-// if (COMPLIANCE_1_3.equals(this.complianceLevel)) {
-// options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_3);
-// options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
-// options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
-// } else if (COMPLIANCE_1_4.equals(this.complianceLevel)) {
-// options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
-// options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
-// options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
-// } else if (COMPLIANCE_1_5.equals(this.complianceLevel)) {
-// options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
-// options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
-// options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
-// }
-// return options;
-// }
+ 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();