Search in sources :

Example 51 with Description

use of com.google.errorprone.matchers.Description 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();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 52 with Description

use of com.google.errorprone.matchers.Description 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();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 53 with Description

use of com.google.errorprone.matchers.Description 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();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 54 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DescriptionBasedDiffTest method twoDiffsWithImport.

@Test
public void twoDiffsWithImport() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(124, 127, "longer").replace(137, 140, "bar").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.longer.println(\"bar\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 55 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DescriptionBasedDiffTest method removeImport.

@Test
public void removeImport() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().removeImport("com.foo.Bar").removeImport("org.bar.Baz").build(), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "", "", "class Foo {", "  public static void main(String[] args) {", "    System.out.println(\"foo\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Aggregations

Description (com.google.errorprone.matchers.Description)56 Tree (com.sun.source.tree.Tree)23 VisitorState (com.google.errorprone.VisitorState)22 BugPattern (com.google.errorprone.BugPattern)21 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)20 ASTHelpers (com.google.errorprone.util.ASTHelpers)20 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)17 ExpressionTree (com.sun.source.tree.ExpressionTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 JDK (com.google.errorprone.BugPattern.Category.JDK)16 Symbol (com.sun.tools.javac.code.Symbol)16 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)14 Type (com.sun.tools.javac.code.Type)14 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)11 VariableTree (com.sun.source.tree.VariableTree)11 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)11 NO_MATCH (com.google.errorprone.matchers.Description.NO_MATCH)10 ClassTree (com.sun.source.tree.ClassTree)10 Optional (java.util.Optional)10 MethodTree (com.sun.source.tree.MethodTree)9