09830f38a6d0fecd0a5fb0485cf756314e2c66e8
[phpeclipse.git] /
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                         "final class test {\n" + 
99                         "\n" + 
100                         "private function f1() {\n" + 
101                         "\n" + 
102                         "}\n" + 
103                         "public function f2() {\n" + 
104                         "\n" +
105                         "  \n" + 
106                         "}  \n" + 
107                         "}";    
108
109                 String expectedSyntaxErrorDiagnosis =
110                         "";
111
112                 String testName = "<test01>";
113                 checkParse(
114                         s.toCharArray(),
115                         expectedSyntaxErrorDiagnosis);
116 //                      testName);
117         }
118         public void test02() {
119                 String s = 
120                         "class test {                                                       \n"+
121                         "       function f0() \n"+
122                         "       {                                                               \n"+
123                         "       }                                                                               \n"+
124                         "}                                                                                      \n";    
125
126                 String expectedSyntaxErrorDiagnosis =
127                         "";
128
129                 String testName = "<test02>";
130                 checkParse(
131                         s.toCharArray(),
132                         expectedSyntaxErrorDiagnosis);
133 //                      testName);
134         }
135         
136         public void test97() {
137                 String s = 
138                         "class momo {                                                       \n"+
139                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
140                         "       {                                                               \n"+
141                         "       }                                                                               \n"+
142                         "}                                                                                      \n";    
143
144                 String expectedSyntaxErrorDiagnosis =
145                         "";
146
147                 checkParse(
148                         s.toCharArray(),
149                         expectedSyntaxErrorDiagnosis);
150 //                      testName);
151         }
152         public void test98() {
153                 String s = 
154                         "if(!$result = mysql_query($sql)) return(array());\n";  
155
156                 String expectedSyntaxErrorDiagnosis =
157                         "";
158
159                 checkParse(
160                         s.toCharArray(),
161                         expectedSyntaxErrorDiagnosis);
162 //                      testName);
163         }
164         
165         public void test99() {
166                 String s = 
167                         "class test {                                                       \n"+
168                         "   murks;                                        \n"+
169                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
170                         "       {                                                               \n"+
171                         "       }                                                                               \n"+
172                         "}                                                                                      \n";    
173
174                 String expectedSyntaxErrorDiagnosis =
175                         "";
176
177                 checkParse(
178                         s.toCharArray(),
179                         expectedSyntaxErrorDiagnosis);
180 //                      testName);
181         }
182 }