Skip to content

Commit

Permalink
Add FindCompileErrorsTest & move away from deprecated print()
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 18, 2024
1 parent c8afbc5 commit 8a1d9c9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Erroneous visitErroneous(J.Erroneous erroneous, ExecutionContext ctx) {

J.CompilationUnit cu = getCursor().firstEnclosing(J.CompilationUnit.class);
String sourceFile = cu != null ? cu.getSourcePath().toString() : "Unknown source file";

String code = erroneous.print();

String code = erroneous.print(getCursor());
report.insertRow(ctx, new CompileErrors.Row(
sourceFile,
code
));

return SearchResult.found(erroneous);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.search;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.table.CompileErrors;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;

class FindCompileErrorsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipes(new FindCompileErrors());
}

@DocumentExample
@Test
void javaVisitorHandlesErroneousNodes() {
rewriteRun(
spec -> spec.dataTable(CompileErrors.Row.class,
rows -> assertThat(rows).singleElement().satisfies(row -> {
assertThat(row.getSourceFile()).isEqualTo("A.java");
assertThat(row.getCode()).isEqualTo("\n owner");
})),
java(
"""
class A {
void test() {
owner
}
}
""",
"""
class A {
void test() {
/*~~>*/owner
}
}
"""
)
);
}
}

0 comments on commit 8a1d9c9

Please sign in to comment.