8659637e4a60c8ab15157be83b6c36ce82d87238
[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.ast.CompilationUnitDeclaration;
28 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
29 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
30 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
31 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
32 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
33 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
34 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
35 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
36
37 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
38 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
39
40 public class AbstractCompilerTest extends TestCase {
41
42         private static int possibleComplianceLevels = -1;
43
44         protected String complianceLevel;
45         public void checkParsePHP(
46                 char[] source,
47                 String expectedSyntaxErrorDiagnosis) {
48 //              String testName) {
49
50                 UnitParser parser =
51                         new UnitParser(
52                                 new ProblemReporter(
53                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(),
54                                         new CompilerOptions(getCompilerOptions()),
55                                         new DefaultProblemFactory(Locale.getDefault())));
56
57                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
58                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
59
60                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
61 //              if (computedUnit.types != null) {
62 //                      for (int i = computedUnit.types.size(); --i >= 0;){
63 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
64 //                      }
65 //              }
66
67                 StringBuffer buffer = new StringBuffer(100);
68                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
69                         IProblem[] problems = compilationResult.getAllProblems();
70                         int count = problems.length;
71                         int problemCount = 0;
72                         char[] unitSource = compilationResult.compilationUnit.getContents();
73                         for (int i = 0; i < count; i++) {
74                                 if (problems[i] != null) {
75                                         if (problemCount == 0)
76                                                 buffer.append("----------\n");
77                                         problemCount++;
78                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
79                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
80                                         try {
81                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
82                                                 buffer.append("\n");
83                                                 buffer.append(problems[i].getMessage());
84                                                 buffer.append("\n");
85                                         } catch (Exception e) {
86                                                 StringWriter stringWriter = new StringWriter();
87                                                 e.printStackTrace(new PrintWriter(stringWriter));
88                                                 buffer.append(stringWriter.getBuffer());
89                                         }
90                                         buffer.append("----------\n");
91                                 }
92                         }
93                 }
94                 String computedSyntaxErrorDiagnosis = buffer.toString();
95                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
96                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
97                 }
98                 assertEquals(
99                         "Invalid syntax error diagnosis",
100                         expectedSyntaxErrorDiagnosis,
101                         computedSyntaxErrorDiagnosis);
102         }
103         public void checkParseHTML(
104                 char[] source,
105                 String expectedSyntaxErrorDiagnosis) {
106 //              String testName) {
107
108                 UnitParser parser =
109                         new UnitParser(
110                                 new ProblemReporter(
111                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(),
112                                         new CompilerOptions(getCompilerOptions()),
113                                         new DefaultProblemFactory(Locale.getDefault())));
114
115                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
116                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
117
118                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
119 //              if (computedUnit.types != null) {
120 //                      for (int i = computedUnit.types.size(); --i >= 0;){
121 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
122 //                      }
123 //              }
124
125                 StringBuffer buffer = new StringBuffer(100);
126                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
127                         IProblem[] problems = compilationResult.getAllProblems();
128                         int count = problems.length;
129                         int problemCount = 0;
130                         char[] unitSource = compilationResult.compilationUnit.getContents();
131                         for (int i = 0; i < count; i++) {
132                                 if (problems[i] != null) {
133                                         if (problemCount == 0)
134                                                 buffer.append("----------\n");
135                                         problemCount++;
136                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
137                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
138                                         try {
139                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
140                                                 buffer.append("\n");
141                                                 buffer.append(problems[i].getMessage());
142                                                 buffer.append("\n");
143                                         } catch (Exception e) {
144                                                 StringWriter stringWriter = new StringWriter();
145                                                 e.printStackTrace(new PrintWriter(stringWriter));
146                                                 buffer.append(stringWriter.getBuffer());
147                                         }
148                                         buffer.append("----------\n");
149                                 }
150                         }
151                 }
152                 String computedSyntaxErrorDiagnosis = buffer.toString();
153                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
154                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
155                 }
156                 assertEquals(
157                         "Invalid syntax error diagnosis",
158                         expectedSyntaxErrorDiagnosis,
159                         computedSyntaxErrorDiagnosis);
160         }
161         /*
162          * Returns the possible compliance levels this VM instance can run.
163          */
164 //      public static int getPossibleComplianceLevels() {
165 //              if (possibleComplianceLevels == -1) {
166 //                      String compliance = System.getProperty("compliance");
167 //                      if (compliance != null) {
168 //                              if (COMPLIANCE_1_3.equals(compliance)) {
169 //                                      possibleComplianceLevels = F_1_3;
170 //                              } else if (COMPLIANCE_1_4.equals(compliance)) {
171 //                                      possibleComplianceLevels = F_1_4;
172 //                              } else if (COMPLIANCE_1_5.equals(compliance)) {
173 //                                      possibleComplianceLevels = F_1_5;
174 //                              } else {
175 //                                      System.out.println("Invalid compliance specified (" + compliance + ")");
176 //                                      System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
177 //                                      System.out.println("Defaulting to all possible compliances");
178 //                              }
179 //                      }
180 //                      if (possibleComplianceLevels == -1) {
181 //                              possibleComplianceLevels = F_1_3;
182 //                              String specVersion = System.getProperty("java.specification.version");
183 //                              boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
184 //                              if (canRun1_4) {
185 //                                      possibleComplianceLevels |= F_1_4;
186 //                              }
187 //                              boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
188 //                              if (canRun1_5) {
189 //                                      possibleComplianceLevels |= F_1_5;
190 //                              }
191 //                      }
192 //              }
193 //              return possibleComplianceLevels;
194 //      }
195
196         /*
197          * Returns a test suite including the tests defined by the given classes for the given complianceLevel
198          * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
199          */
200         public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
201                 TestSuite suite;
202                 if (testClasses.size() == 1) {
203                         suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
204                 } else {
205                         suite = new TestSuite(complianceLevel);
206                         for (int i = 0, length = testClasses.size(); i < length; i++) {
207                                 Class testClass = (Class)testClasses.get(i);
208                                 TestSuite innerSuite = new TestSuite(testClass);
209                                 suite.addTest(innerSuite);
210                         }
211                 }
212
213                 // call the setup constructor with the suite and compliance level
214                 try {
215                         Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
216                         Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
217                         return setUp;
218                 } catch (IllegalAccessException e) {
219                         e.printStackTrace();
220                 } catch (InstantiationException e) {
221                         e.printStackTrace();
222                 } catch (InvocationTargetException e) {
223                         e.getTargetException().printStackTrace();
224                 } catch (NoSuchMethodException e) {
225                         e.printStackTrace();
226                 }
227
228                 return null;
229         }
230
231         public AbstractCompilerTest(String name) {
232                 super(name);
233         }
234
235         protected Map getCompilerOptions() {
236                 Map options = new CompilerOptions().getMap();
237                 // ignore warnings
238                 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning, CompilerOptions.IGNORE);
239                 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning, CompilerOptions.IGNORE);
240                 options.put(CompilerOptions.OPTION_PHPBadStyleUppercaseIdentifierWarning, CompilerOptions.IGNORE);
241                 options.put(CompilerOptions.OPTION_PHPIncludeNotExistWarning, CompilerOptions.IGNORE);
242                 options.put(CompilerOptions.OPTION_UninitializedLocalVariableWarning, CompilerOptions.IGNORE);
243                 options.put(CompilerOptions.OPTION_CodeCannotBeReachedWarning, CompilerOptions.IGNORE);
244                 return options;
245         }
246
247         public String getName() {
248                 String name = super.getName();
249                 if (this.complianceLevel != null) {
250                         name = this.complianceLevel + " - " + name;
251                 }
252                 return name;
253         }
254         protected void checkHTML(String strEval) {
255             if (Scanner.DEBUG) {
256               System.out.println("\n------------------------------------");
257               System.out.println(strEval);
258             }
259             checkParseHTML(
260                 strEval.toCharArray(),
261                         "");
262         //    parser.phpParserTester(strEval, 1);
263           }
264         //  private void checkHTML(String strEval) {
265         //    if (Scanner.DEBUG) {
266         //      System.out.println("\n------------------------------------");
267         //      System.out.println(strEval);
268         //    }
269         //    parser.parse(strEval);
270         //  }
271         //  /**
272         //   * The JUnit setup method
273         //   */
274         //  protected void setUp() {
275         //    parser = new Parser(null);
276         //  }
277
278 //      public void initialize(CompilerTestSetup setUp) {
279 //              this.complianceLevel = setUp.complianceLevel;
280 //      }
281 }