949a9add5c3dc9cdaeaf0dedf08d906c764117db
[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 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
26
27 /**
28  * @author khartlage
29  *
30  * To change the template for this generated type comment go to
31  * Window - Preferences - Java - Code Generation - Code and Comments
32  */
33 public class DualParseSyntaxErrorTest extends AbstractCompilerTest {
34                 public static boolean optimizeStringLiterals = false;
35                 
36         public DualParseSyntaxErrorTest(String testName){
37                 super(testName);
38         }
39         public void checkParse(
40                 char[] source, 
41                 String expectedSyntaxErrorDiagnosis,
42                 String testName) {
43
44                 UnitParser parser = 
45                         new UnitParser(
46                                 new ProblemReporter(
47                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
48                                         //new CompilerOptions(getCompilerOptions()), 
49                                         new DefaultProblemFactory(Locale.getDefault())));
50
51                 ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null);
52                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
53                 
54                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
55 //              if (computedUnit.types != null) {
56 //                      for (int i = computedUnit.types.size(); --i >= 0;){
57 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
58 //                      }
59 //              }
60
61                 StringBuffer buffer = new StringBuffer(100);
62                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
63                         IProblem[] problems = compilationResult.getAllProblems();
64                         int count = problems.length;
65                         int problemCount = 0;
66                         char[] unitSource = compilationResult.compilationUnit.getContents();
67                         for (int i = 0; i < count; i++) { 
68                                 if (problems[i] != null) {
69                                         if (problemCount == 0)
70                                                 buffer.append("----------\n");
71                                         problemCount++;
72                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
73                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
74                                         try {
75                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
76                                                 buffer.append("\n");
77                                                 buffer.append(problems[i].getMessage());
78                                                 buffer.append("\n");
79                                         } catch (Exception e) {
80                                                 StringWriter stringWriter = new StringWriter();
81                                                 e.printStackTrace(new PrintWriter(stringWriter));
82                                                 buffer.append(stringWriter.getBuffer());
83                                         }
84                                         buffer.append("----------\n");
85                                 }
86                         }
87                 }
88                 String computedSyntaxErrorDiagnosis = buffer.toString();
89                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
90                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
91                 }
92                 assertEquals(
93                         "Invalid syntax error diagnosis" + testName,
94                         expectedSyntaxErrorDiagnosis,
95                         computedSyntaxErrorDiagnosis);
96         }
97         
98         public void test01() {
99                 String s = 
100                         "class class {                                                      \n"+
101                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
102                         "       {                                                               \n"+
103                         "       }                                                                               \n"+
104                         "}                                                                                      \n";    
105
106                 String expectedSyntaxErrorDiagnosis =
107                         "----------\n" + 
108                         "1. ERROR in <test1> (at line 1)\n" + 
109                         "       class class {                                                       \n" + 
110                         "             ^^^^^\n" + 
111                         "Parse error \"Don\'t use keyword for class declaration [class].\"\n" + 
112                         "----------\n" + 
113                         "2. ERROR in <test1> (at line 1)\n" + 
114                         "       class class {                                                       \n" + 
115                         "                   ^\n" + 
116                         "Parse error \"Class name expected after keyword \'class\'.\"\n" + 
117                         "----------\n";
118
119                 String testName = "<test1>";
120                 checkParse(
121                         s.toCharArray(),
122                         expectedSyntaxErrorDiagnosis,
123                         testName);
124         }
125         
126         public void test02() {
127                 String s = 
128                         "if(!$result = mysql_query($sql)) return(array());\n";  
129
130                 String expectedSyntaxErrorDiagnosis =
131                         "";
132
133                 String testName = "<test2>";
134                 checkParse(
135                         s.toCharArray(),
136                         expectedSyntaxErrorDiagnosis,
137                         testName);
138         }
139         
140         public void test03() {
141                 String s = 
142                         "class test {                                                       \n"+
143                         "   murks;                                        \n"+
144                         "       function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
145                         "       {                                                               \n"+
146                         "       }                                                                               \n"+
147                         "}                                                                                      \n";    
148
149                 String expectedSyntaxErrorDiagnosis =
150                         "";
151
152                 String testName = "<test3>";
153                 checkParse(
154                         s.toCharArray(),
155                         expectedSyntaxErrorDiagnosis,
156                         testName);
157         }
158 }