use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.
the class DescriptionBasedDiffTest method addImport.
@Test
public void addImport() {
DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit);
diff.onDescribed(new Description(null, "message", SuggestedFix.builder().addImport("com.google.foo.Bar").build(), SeverityLevel.SUGGESTION));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import com.foo.Bar;", "import com.google.foo.Bar;", "", "class Foo {", " public static void main(String[] args) {", " System.out.println(\"foo\");", " }", "}").inOrder();
}
use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.
the class BugCheckerRefactoringTestHelper method applyDiff.
private JavaFileObject applyDiff(JavaFileObject sourceFileObject, Context context, JCCompilationUnit tree) throws IOException {
ImportOrganizer importOrganizer = ImportOrderParser.getImportOrganizer(importOrder);
final DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, importOrganizer);
transformer(refactoringBugChecker).apply(new TreePath(tree), context, new DescriptionListener() {
@Override
public void onDescribed(Description description) {
if (!description.fixes.isEmpty()) {
diff.handleFix(fixChooser.choose(description.fixes));
}
}
});
SourceFile sourceFile = SourceFile.create(sourceFileObject);
diff.applyDifferences(sourceFile);
JavaFileObject transformed = JavaFileObjects.forSourceString(getFullyQualifiedName(tree), sourceFile.getSourceText());
return transformed;
}
use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.
the class DescriptionBasedDiffTest method overlappingDiffs_throws.
@Test
public void overlappingDiffs_throws() {
DescriptionBasedDiff diff = createDescriptionBasedDiff();
assertThrows(IllegalArgumentException.class, () -> diff.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "baz").replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION)));
DescriptionBasedDiff diff2 = createDescriptionBasedDiff();
diff2.onDescribed(new Description(null, "bah", SuggestedFix.builder().replace(137, 140, "baz").build(), SeverityLevel.SUGGESTION));
assertThrows(IllegalArgumentException.class, () -> diff2.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION)));
DescriptionBasedDiff diff3 = DescriptionBasedDiff.createIgnoringOverlaps(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER);
diff3.onDescribed(new Description(null, "bah", SuggestedFix.builder().replace(137, 140, "baz").build(), SeverityLevel.SUGGESTION));
// No throw, since it's lenient. Refactors to the first "baz" replacement and ignores this.
diff3.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION));
diff3.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import org.bar.Baz;", "import com.foo.Bar;", "", "class Foo {", " public static void main(String[] args) {", " System.out.println(\"baz\");", " }", "}").inOrder();
}
use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.
the class DescriptionBasedDiffTest method twoDiffs.
@Test
public void twoDiffs() {
DescriptionBasedDiff diff = createDescriptionBasedDiff();
diff.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(124, 127, "longer").replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import org.bar.Baz;", "import com.foo.Bar;", "", "class Foo {", " public static void main(String[] args) {", " System.longer.println(\"bar\");", " }", "}").inOrder();
}
use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.
the class DescriptionBasedDiffTest method applyDifferences_addsImportAndSorts_whenAddingNewImport.
@Test
public void applyDifferences_addsImportAndSorts_whenAddingNewImport() {
DescriptionBasedDiff diff = createDescriptionBasedDiff();
diff.onDescribed(new Description(null, "message", SuggestedFix.builder().addImport("com.google.foo.Bar").build(), SeverityLevel.SUGGESTION));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import com.foo.Bar;", "import com.google.foo.Bar;", "import org.bar.Baz;", "", "class Foo {", " public static void main(String[] args) {", " System.out.println(\"foo\");", " }", "}").inOrder();
}
Aggregations