public void checkParse(char[] source, String expectedSyntaxErrorDiagnosis, String testName) {

    /* using regular parser in DIET mode */
    Parser parser =
        new Parser(
            new ProblemReporter(
                DefaultErrorHandlingPolicies.proceedWithAllProblems(),
                new CompilerOptions(getCompilerOptions()),
                new DefaultProblemFactory(Locale.getDefault())),
            optimizeStringLiterals);
    ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null);
    CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);

    parser.parse(sourceUnit, compilationResult);

    StringBuffer buffer = new StringBuffer(100);
    if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
      CategorizedProblem[] problems = compilationResult.getAllProblems();
      int count = problems.length;
      int problemCount = 0;
      char[] unitSource = compilationResult.compilationUnit.getContents();
      for (int i = 0; i < count; i++) {
        if (problems[i] != null) {
          if (problemCount == 0) buffer.append("----------\n");
          problemCount++;
          buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
          buffer.append(
              " in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
          try {
            buffer.append(((DefaultProblem) problems[i]).errorReportSource(unitSource));
            buffer.append("\n");
            buffer.append(problems[i].getMessage());
            buffer.append("\n");
          } catch (Exception e) {
          }
          buffer.append("----------\n");
        }
      }
    }
    String computedSyntaxErrorDiagnosis = buffer.toString();
    // System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
    assertEquals(
        "Invalid syntax error diagnosis" + testName,
        Util.convertToIndependantLineDelimiter(expectedSyntaxErrorDiagnosis),
        Util.convertToIndependantLineDelimiter(computedSyntaxErrorDiagnosis));
  }
Example #2
0
 public void runConformTest(
     String fileName, String fileContents, Parser parser, ASTCollector visitor, String expected) {
   CompilationUnit source = new CompilationUnit(fileContents.toCharArray(), fileName, null);
   CompilationResult compilationResult = new CompilationResult(source, 1, 1, 10);
   CompilationUnitDeclaration unit = parser.parse(source, compilationResult);
   assertEquals(0, compilationResult.problemCount);
   unit.traverse(visitor, unit.scope);
   String result = visitor.result();
   if (!expected.equals(result)) {
     System.out.println(getClass().getName() + '#' + getName());
     System.out.println("Expected:");
     System.out.println(expected);
     System.out.println("But was:");
     System.out.println(result);
     System.out.println("Cut and paste:");
     System.out.println(Util.displayString(result, INDENT, SHIFT));
   }
   assertEquals(expected, result);
 }