misc parser bugfixes; still very ugly state
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpeclipse / phpeditor / php / test / DualParseSyntaxErrorTest.java
1 /*
2  * Created on 29.02.2004
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */
7 package net.sourceforge.phpeclipse.phpeditor.php.test;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.util.Locale;
12
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
15 import net.sourceforge.phpdt.core.tests.util.Util;
16 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
17 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
18 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
19 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
20 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
21 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
22 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
23 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
24 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
25
26 /**
27  * @author khartlage
28  *
29  * To change the template for this generated type comment go to
30  * Window - Preferences - Java - Code Generation - Code and Comments
31  */
32 public class DualParseSyntaxErrorTest extends AbstractCompilerTest {
33                 public static boolean optimizeStringLiterals = false;
34                 
35         public DualParseSyntaxErrorTest(String testName){
36                 super(testName);
37         }
38         public void checkParse(
39                 char[] source, 
40                 String expectedSyntaxErrorDiagnosis) {
41 //              String testName) {
42
43                 UnitParser parser = 
44                         new UnitParser(
45                                 new ProblemReporter(
46                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
47                                         //new CompilerOptions(getCompilerOptions()), 
48                                         new DefaultProblemFactory(Locale.getDefault())));
49
50                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
51                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
52                 
53                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
54 //              if (computedUnit.types != null) {
55 //                      for (int i = computedUnit.types.size(); --i >= 0;){
56 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
57 //                      }
58 //              }
59
60                 StringBuffer buffer = new StringBuffer(100);
61                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
62                         IProblem[] problems = compilationResult.getAllProblems();
63                         int count = problems.length;
64                         int problemCount = 0;
65                         char[] unitSource = compilationResult.compilationUnit.getContents();
66                         for (int i = 0; i < count; i++) { 
67                                 if (problems[i] != null) {
68                                         if (problemCount == 0)
69                                                 buffer.append("----------\n");
70                                         problemCount++;
71                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
72                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
73                                         try {
74                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
75                                                 buffer.append("\n");
76                                                 buffer.append(problems[i].getMessage());
77                                                 buffer.append("\n");
78                                         } catch (Exception e) {
79                                                 StringWriter stringWriter = new StringWriter();
80                                                 e.printStackTrace(new PrintWriter(stringWriter));
81                                                 buffer.append(stringWriter.getBuffer());
82                                         }
83                                         buffer.append("----------\n");
84                                 }
85                         }
86                 }
87                 String computedSyntaxErrorDiagnosis = buffer.toString();
88                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
89                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
90                 }
91                 assertEquals(
92                         "Invalid syntax error diagnosis",
93                         expectedSyntaxErrorDiagnosis,
94                         computedSyntaxErrorDiagnosis);
95         }
96         public void test01() {
97                 String s = 
98                         " ";    
99
100                 String expectedSyntaxErrorDiagnosis =
101                         "";
102
103                 String testName = "<test01>";
104                 checkParse(
105                         s.toCharArray(),
106                         expectedSyntaxErrorDiagnosis);
107 //                      testName);
108         }
109         public void test02() {
110                 String s = 
111                         "class test {                                                       \n"+
112                         "       function f0() \n"+
113                         "       {                                                               \n"+
114                         "       }                                                                               \n"+
115                         "}                                                                                      \n";    
116
117                 String expectedSyntaxErrorDiagnosis =
118                         "";
119
120                 String testName = "<test02>";
121                 checkParse(
122                         s.toCharArray(),
123                         expectedSyntaxErrorDiagnosis);
124 //                      testName);
125         }
126         
127         public void test97() {
128                 String s = 
129                         "class class {                                                      \n"+
130                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
131                         "       {                                                               \n"+
132                         "       }                                                                               \n"+
133                         "}                                                                                      \n";    
134
135                 String expectedSyntaxErrorDiagnosis =
136                         "----------\n" + 
137                         "1. ERROR in <test1> (at line 1)\n" + 
138                         "       class class {                                                       \n" + 
139                         "             ^^^^^\n" + 
140                         "Parse error \"Don\'t use keyword for class declaration [class].\"\n" + 
141                         "----------\n" + 
142                         "2. ERROR in <test1> (at line 1)\n" + 
143                         "       class class {                                                       \n" + 
144                         "                   ^\n" + 
145                         "Parse error \"Class name expected after keyword \'class\'.\"\n" + 
146                         "----------\n";
147
148                 checkParse(
149                         s.toCharArray(),
150                         expectedSyntaxErrorDiagnosis);
151 //                      testName);
152         }
153         public void test98() {
154                 String s = 
155                         "if(!$result = mysql_query($sql)) return(array());\n";  
156
157                 String expectedSyntaxErrorDiagnosis =
158                         "";
159
160                 checkParse(
161                         s.toCharArray(),
162                         expectedSyntaxErrorDiagnosis);
163 //                      testName);
164         }
165         
166         public void test99() {
167                 String s = 
168                         "class test {                                                       \n"+
169                         "   murks;                                        \n"+
170                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
171                         "       {                                                               \n"+
172                         "       }                                                                               \n"+
173                         "}                                                                                      \n";    
174
175                 String expectedSyntaxErrorDiagnosis =
176                         "";
177
178                 checkParse(
179                         s.toCharArray(),
180                         expectedSyntaxErrorDiagnosis);
181 //                      testName);
182         }
183 }