misc parser changes
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / util / AbstractCompilerTest.java
index 5ea14a5..031d81e 100644 (file)
  *******************************************************************************/
 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 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.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;
+//     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");
+               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");
                                }
                        }
-                       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;
+               }
+               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);       
+               
+               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");
                                }
                        }
                }
-               return possibleComplianceLevels;
+               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 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));
-               }
-               if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
-                       all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
-               }
-               return all;
-       }
+//     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));
+//             }
+//             if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
+//                     all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
+//             }
+//             return all;
+//     }
 
        /*
         * Returns a test suite including the tests defined by the given classes for the given complianceLevel