Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: fix(sniper): Fix rounding error in indentation detection on single type member #3722

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}