Search in sources :

Example 11 with FixReplacement

use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreterTest method replacementIsTranslatedToTreeModification.

@Test
public void replacementIsTranslatedToTreeModification() throws Exception {
    FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(1, 1, 3, 2), "Modified content");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
    CommitModification commitModification = toCommitModification(fixReplacement);
    ImmutableList<TreeModification> treeModifications = commitModification.treeModifications();
    assertThatList(treeModifications).onlyElement().asChangeFileContentModification().filePaths().containsExactly(filePath1);
    assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("FModified contentird line\n");
}
Also used : CommitModification(com.google.gerrit.server.edit.CommitModification) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 12 with FixReplacement

use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.

the class FixCalculatorVariousTest method multipleChanges.

@Test
public void multipleChanges() throws Exception {
    String str = "First line\nSecond line\nThird line\nFourth line\nFifth line\nSixth line" + "\nSeventh line\nEighth line\nNinth line\nTenth line\n";
    Text content = new Text(str.getBytes(UTF_8));
    FixReplacement multiLineReplace = new FixReplacement("path", new Range(1, 2, 3, 3), "AB\nC\nDEFG\nQ\n");
    FixReplacement multiLineDelete = new FixReplacement("path", new Range(4, 8, 5, 8), "");
    FixReplacement singleLineInsert = new FixReplacement("path", new Range(5, 10, 5, 10), "QWERTY");
    FixReplacement singleLineReplace = new FixReplacement("path", new Range(7, 3, 7, 7), "XY");
    FixReplacement multiLineInsert = new FixReplacement("path", new Range(8, 7, 8, 7), "KLMNO\nASDF");
    FixReplacement singleLineDelete = new FixReplacement("path", new Range(10, 3, 10, 7), "");
    FixResult result = FixCalculator.calculateFix(content, ImmutableList.of(multiLineReplace, multiLineDelete, singleLineInsert, singleLineReplace, multiLineInsert, singleLineDelete));
    assertThat(result).text().isEqualTo("FiAB\n" + "C\n" + "DEFG\n" + "Q\n" + "rd line\n" + "Fourth lneQWERTY\n" + "Sixth line\n" + "SevXY line\n" + "Eighth KLMNO\n" + "ASDFline\n" + "Ninth line\n" + "Tenine\n");
    assertThat(result).edits().hasSize(3);
    assertThat(result).edits().element(0).isReplace(0, 5, 0, 6);
    assertThat(result).edits().element(0).internalEdits().containsExactly(new Edit(2, 26, 2, 14), new Edit(42, 54, 30, 30), new Edit(56, 56, 32, 38));
    assertThat(result).edits().element(1).isReplace(6, 2, 7, 3);
    assertThat(result).edits().element(1).internalEdits().containsExactly(new Edit(3, 7, 3, 5), new Edit(20, 20, 18, 28));
    assertThat(result).edits().element(2).isReplace(9, 1, 11, 1);
    assertThat(result).edits().element(2).internalEdits().onlyElement().isDelete(3, 4, 3);
}
Also used : FixResult(com.google.gerrit.server.fixes.FixCalculator.FixResult) Text(com.google.gerrit.server.patch.Text) Edit(org.eclipse.jgit.diff.Edit) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 13 with FixReplacement

use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.

the class FixCalculatorVariousTest method severalChangesInConsecutiveLines.

@Test
public void severalChangesInConsecutiveLines() throws Exception {
    FixReplacement replace = new FixReplacement("path", new Range(2, 1, 2, 3), "ABC");
    FixReplacement insert = new FixReplacement("path", new Range(3, 5, 3, 5), "DEFG");
    FixReplacement delete = new FixReplacement("path", new Range(4, 7, 4, 9), "");
    FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(replace, insert, delete));
    assertThat(result).text().isEqualTo("First line\nSABCond line\nThirdDEFG line\nFourth ne\nFifth line\n");
    assertThat(result).edits().hasSize(1);
    Edit edit = result.edits.get(0);
    assertThat(edit).isReplace(1, 3, 1, 3);
    assertThat(edit).internalEdits().hasSize(3);
    assertThat(edit).internalEdits().element(0).isReplace(1, 2, 1, 3);
    assertThat(edit).internalEdits().element(1).isInsert(17, 18, 4);
    assertThat(edit).internalEdits().element(2).isDelete(30, 2, 35);
}
Also used : FixResult(com.google.gerrit.server.fixes.FixCalculator.FixResult) Edit(org.eclipse.jgit.diff.Edit) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 14 with FixReplacement

use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.

the class FixCalculatorVariousTest method severalChangesInTheSameLineNonSorted.

@Test
public void severalChangesInTheSameLineNonSorted() throws Exception {
    FixReplacement replace = new FixReplacement("path", new Range(2, 1, 2, 3), "ABC");
    FixReplacement insert = new FixReplacement("path", new Range(2, 5, 2, 5), "DEFG");
    FixReplacement delete = new FixReplacement("path", new Range(2, 7, 2, 9), "");
    FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(replace, delete, insert));
    assertThat(result).text().isEqualTo("First line\nSABConDEFGd ne\nThird line\nFourth line\nFifth line\n");
    assertThat(result).edits().hasSize(1);
    Edit edit = result.edits.get(0);
    assertThat(edit).isReplace(1, 1, 1, 1);
    assertThat(edit).internalEdits().hasSize(3);
    assertThat(edit).internalEdits().element(0).isReplace(1, 2, 1, 3);
    assertThat(edit).internalEdits().element(1).isInsert(5, 6, 4);
    assertThat(edit).internalEdits().element(2).isDelete(7, 2, 12);
}
Also used : FixResult(com.google.gerrit.server.fixes.FixCalculator.FixResult) Edit(org.eclipse.jgit.diff.Edit) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Aggregations

FixReplacement (com.google.gerrit.entities.FixReplacement)14 Range (com.google.gerrit.entities.Comment.Range)10 Test (org.junit.Test)8 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 FixResult (com.google.gerrit.server.fixes.FixCalculator.FixResult)5 CommitModification (com.google.gerrit.server.edit.CommitModification)4 TreeModification (com.google.gerrit.server.edit.tree.TreeModification)4 Edit (org.eclipse.jgit.diff.Edit)4 ImmutableList (com.google.common.collect.ImmutableList)3 ProjectState (com.google.gerrit.server.project.ProjectState)3 Inject (com.google.inject.Inject)3 Singleton (com.google.inject.Singleton)3 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 Change (com.google.gerrit.entities.Change)2 Patch (com.google.gerrit.entities.Patch)2 PatchSet (com.google.gerrit.entities.PatchSet)2