Skip to content

Commit

Permalink
fix(sniper): Fix rounding error in indentation detection on single ty…
Browse files Browse the repository at this point in the history
…pe member (INRIA#3722)
  • Loading branch information
slarse authored Dec 9, 2020
1 parent aa7f0a4 commit 0804973
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ private static Pair<Integer, Boolean> guessIndentationStyle(List<String> wsPrece
indentationSize = 1;
}

boolean usesTabs = wsPrecedingTypeMembers.stream()
boolean usesTabs = (double) wsPrecedingTypeMembers.stream()
.filter(s -> s.contains("\t"))
.count() >= wsPrecedingTypeMembers.size() / 2;
.count() >= (double) wsPrecedingTypeMembers.size() / 2;
return Pair.of(indentationSize, usesTabs);
}

Expand Down
25 changes: 24 additions & 1 deletion src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public void testAddedImportStatementPlacedOnSeparateLineInFileWithPackageStateme
@Test
public void testAddedElementsIndentedWithAppropriateIndentationStyle() {
// contract: added elements in a source file should be indented with the same style of
// indentation as the rest of the file
// indentation as in the rest of the file

Consumer<CtType<?>> addElements = type -> {
Factory fact = type.getFactory();
Expand All @@ -446,6 +446,29 @@ public void testAddedElementsIndentedWithAppropriateIndentationStyle() {
testSniper("indentation.FourSpaces", addElements, assertFourSpaces);
}

@Test
public void testAddedElementsIndentedWithAppropriateIndentationStyleWhenOnlyOneTypeMemberExists() {
// contract: added elements in a source file should be indented with the same style of
// indentation as the single type member, when there is only one type member.

Consumer<CtType<?>> addElement = type -> {
Factory fact = type.getFactory();
fact.createField(type, new HashSet<>(), fact.Type().INTEGER_PRIMITIVE, "z", fact.createLiteral(2));
};
final String newField = "int z = 2;";

BiConsumer<CtType<?>, String> assertTabs = (type, result) ->
assertThat(result, containsString("\n\t" + newField));
BiConsumer<CtType<?>, String> assertTwoSpaces = (type, result) ->
assertThat(result, containsString("\n " + newField));
BiConsumer<CtType<?>, String> assertFourSpaces = (type, result) ->
assertThat(result, containsString("\n " + newField));

testSniper("indentation.singletypemember.Tabs", addElement, assertTabs);
testSniper("indentation.singletypemember.TwoSpaces", addElement, assertTwoSpaces);
testSniper("indentation.singletypemember.FourSpaces", addElement, assertFourSpaces);
}

@Test
public void testDefaultsToSingleTabIndentationWhenThereAreNoTypeMembers() {
// contract: if there are no type members in a compilation unit, the sniper printer defaults
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package indentation.singletypemember;

public class FourSpaces {
int x;
}
5 changes: 5 additions & 0 deletions src/test/resources/indentation/singletypemember/Tabs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package indentation.singletypemember;

public class Tabs {
int x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package indentation.singletypemember;

public class TwoSpaces {
int x;
}

0 comments on commit 0804973

Please sign in to comment.