improved parser string handling
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / util / AbstractCompilerTest.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
12
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.ArrayList;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import net.sourceforge.phpdt.core.compiler.IProblem;
25 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
26 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
27 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
28 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
29 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
30 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
31 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
32 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
33 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
34 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
35
36 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
37 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
38
39 public class AbstractCompilerTest extends TestCase {
40
41 //      public static final String COMPLIANCE_1_3 = "1.3";
42 //      public static final String COMPLIANCE_1_4 = "1.4";
43 //      public static final String COMPLIANCE_1_5 = "1.5";
44 //
45 //      public static final int F_1_3 = 0x1;
46 //      public static final int F_1_4 = 0x2;
47 //      public static final int F_1_5 = 0x4;
48
49         private static int possibleComplianceLevels = -1;
50
51         protected String complianceLevel;
52         public void checkParsePHP(
53                 char[] source, 
54                 String expectedSyntaxErrorDiagnosis) {
55 //              String testName) {
56
57                 UnitParser parser = 
58                         new UnitParser(
59                                 new ProblemReporter(
60                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
61                                         new CompilerOptions(getCompilerOptions()), 
62                                         new DefaultProblemFactory(Locale.getDefault())));
63
64                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
65                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
66                 
67                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
68 //              if (computedUnit.types != null) {
69 //                      for (int i = computedUnit.types.size(); --i >= 0;){
70 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
71 //                      }
72 //              }
73
74                 StringBuffer buffer = new StringBuffer(100);
75                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
76                         IProblem[] problems = compilationResult.getAllProblems();
77                         int count = problems.length;
78                         int problemCount = 0;
79                         char[] unitSource = compilationResult.compilationUnit.getContents();
80                         for (int i = 0; i < count; i++) { 
81                                 if (problems[i] != null) {
82                                         if (problemCount == 0)
83                                                 buffer.append("----------\n");
84                                         problemCount++;
85                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
86                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
87                                         try {
88                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
89                                                 buffer.append("\n");
90                                                 buffer.append(problems[i].getMessage());
91                                                 buffer.append("\n");
92                                         } catch (Exception e) {
93                                                 StringWriter stringWriter = new StringWriter();
94                                                 e.printStackTrace(new PrintWriter(stringWriter));
95                                                 buffer.append(stringWriter.getBuffer());
96                                         }
97                                         buffer.append("----------\n");
98                                 }
99                         }
100                 }
101                 String computedSyntaxErrorDiagnosis = buffer.toString();
102                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
103                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
104                 }
105                 assertEquals(
106                         "Invalid syntax error diagnosis",
107                         expectedSyntaxErrorDiagnosis,
108                         computedSyntaxErrorDiagnosis);
109         }
110         public void checkParseHTML(
111                 char[] source, 
112                 String expectedSyntaxErrorDiagnosis) {
113 //              String testName) {
114
115                 UnitParser parser = 
116                         new UnitParser(
117                                 new ProblemReporter(
118                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
119                                         new CompilerOptions(getCompilerOptions()), 
120                                         new DefaultProblemFactory(Locale.getDefault())));
121
122                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
123                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
124                 
125                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
126 //              if (computedUnit.types != null) {
127 //                      for (int i = computedUnit.types.size(); --i >= 0;){
128 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
129 //                      }
130 //              }
131
132                 StringBuffer buffer = new StringBuffer(100);
133                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
134                         IProblem[] problems = compilationResult.getAllProblems();
135                         int count = problems.length;
136                         int problemCount = 0;
137                         char[] unitSource = compilationResult.compilationUnit.getContents();
138                         for (int i = 0; i < count; i++) { 
139                                 if (problems[i] != null) {
140                                         if (problemCount == 0)
141                                                 buffer.append("----------\n");
142                                         problemCount++;
143                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
144                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
145                                         try {
146                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
147                                                 buffer.append("\n");
148                                                 buffer.append(problems[i].getMessage());
149                                                 buffer.append("\n");
150                                         } catch (Exception e) {
151                                                 StringWriter stringWriter = new StringWriter();
152                                                 e.printStackTrace(new PrintWriter(stringWriter));
153                                                 buffer.append(stringWriter.getBuffer());
154                                         }
155                                         buffer.append("----------\n");
156                                 }
157                         }
158                 }
159                 String computedSyntaxErrorDiagnosis = buffer.toString();
160                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
161                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
162                 }
163                 assertEquals(
164                         "Invalid syntax error diagnosis",
165                         expectedSyntaxErrorDiagnosis,
166                         computedSyntaxErrorDiagnosis);
167         }
168         /*
169          * Returns the possible compliance levels this VM instance can run.
170          */
171 //      public static int getPossibleComplianceLevels() {
172 //              if (possibleComplianceLevels == -1) {
173 //                      String compliance = System.getProperty("compliance");
174 //                      if (compliance != null) {
175 //                              if (COMPLIANCE_1_3.equals(compliance)) {
176 //                                      possibleComplianceLevels = F_1_3;
177 //                              } else if (COMPLIANCE_1_4.equals(compliance)) {
178 //                                      possibleComplianceLevels = F_1_4;
179 //                              } else if (COMPLIANCE_1_5.equals(compliance)) {
180 //                                      possibleComplianceLevels = F_1_5;
181 //                              } else {
182 //                                      System.out.println("Invalid compliance specified (" + compliance + ")");
183 //                                      System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
184 //                                      System.out.println("Defaulting to all possible compliances");
185 //                              }
186 //                      }
187 //                      if (possibleComplianceLevels == -1) {
188 //                              possibleComplianceLevels = F_1_3;
189 //                              String specVersion = System.getProperty("java.specification.version");
190 //                              boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
191 //                              if (canRun1_4) {
192 //                                      possibleComplianceLevels |= F_1_4;
193 //                              }
194 //                              boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
195 //                              if (canRun1_5) {
196 //                                      possibleComplianceLevels |= F_1_5;
197 //                              }
198 //                      }
199 //              }
200 //              return possibleComplianceLevels;
201 //      }
202
203         /*
204          * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
205          * and using the given setup class (CompilerTestSetup or a subclass)
206          */
207 //      public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
208 //              TestSuite all = new TestSuite(suiteName);
209 //              int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
210 //              if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
211 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
212 //              }
213 //              if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
214 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
215 //              }
216 //              if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
217 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
218 //              }
219 //              return all;
220 //      }
221
222         /*
223          * Returns a test suite including the tests defined by the given classes for the given complianceLevel 
224          * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
225          */
226         public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
227                 TestSuite suite;
228                 if (testClasses.size() == 1) {
229                         suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
230                 } else {
231                         suite = new TestSuite(complianceLevel);
232                         for (int i = 0, length = testClasses.size(); i < length; i++) {
233                                 Class testClass = (Class)testClasses.get(i);
234                                 TestSuite innerSuite = new TestSuite(testClass);
235                                 suite.addTest(innerSuite);
236                         }
237                 }
238
239                 // call the setup constructor with the suite and compliance level
240                 try {
241                         Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
242                         Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
243                         return setUp;
244                 } catch (IllegalAccessException e) {
245                         e.printStackTrace();
246                 } catch (InstantiationException e) {
247                         e.printStackTrace();
248                 } catch (InvocationTargetException e) {
249                         e.getTargetException().printStackTrace();
250                 } catch (NoSuchMethodException e) {
251                         e.printStackTrace();
252                 }
253
254                 return null;
255         }
256
257         public AbstractCompilerTest(String name) {
258                 super(name);
259         }
260
261         protected Map getCompilerOptions() {
262                 Map options = new CompilerOptions().getMap();
263 //              if (COMPLIANCE_1_3.equals(this.complianceLevel)) {
264 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_3);
265 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
266 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
267 //              } else if (COMPLIANCE_1_4.equals(this.complianceLevel)) {
268 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
269 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
270 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
271 //              } else if (COMPLIANCE_1_5.equals(this.complianceLevel)) {
272 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
273 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
274 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
275 //              }
276                 return options;
277         }
278
279         public String getName() {
280                 String name = super.getName();
281                 if (this.complianceLevel != null) {
282                         name = this.complianceLevel + " - " + name;
283                 }
284                 return name;
285         }
286
287 //      public void initialize(CompilerTestSetup setUp) {
288 //              this.complianceLevel = setUp.complianceLevel;
289 //      }
290 }