From 08049732add4245e0a54174ab9f51008229a3e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Wed, 9 Dec 2020 11:04:13 +0100 Subject: [PATCH] fix(sniper): Fix rounding error in indentation detection on single type member (#3722) --- .../sniper/internal/IndentationDetector.java | 4 +-- .../test/prettyprinter/TestSniperPrinter.java | 25 ++++++++++++++++++- .../singletypemember/FourSpaces.java | 5 ++++ .../indentation/singletypemember/Tabs.java | 5 ++++ .../singletypemember/TwoSpaces.java | 5 ++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/indentation/singletypemember/FourSpaces.java create mode 100644 src/test/resources/indentation/singletypemember/Tabs.java create mode 100644 src/test/resources/indentation/singletypemember/TwoSpaces.java diff --git a/src/main/java/spoon/support/sniper/internal/IndentationDetector.java b/src/main/java/spoon/support/sniper/internal/IndentationDetector.java index 93cd0696507..a3e859e1328 100644 --- a/src/main/java/spoon/support/sniper/internal/IndentationDetector.java +++ b/src/main/java/spoon/support/sniper/internal/IndentationDetector.java @@ -79,9 +79,9 @@ private static Pair guessIndentationStyle(List 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); } diff --git a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java index 99f906010ae..c52f2653b4b 100644 --- a/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java +++ b/src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java @@ -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> addElements = type -> { Factory fact = type.getFactory(); @@ -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> 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, String> assertTabs = (type, result) -> + assertThat(result, containsString("\n\t" + newField)); + BiConsumer, String> assertTwoSpaces = (type, result) -> + assertThat(result, containsString("\n " + newField)); + BiConsumer, 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 diff --git a/src/test/resources/indentation/singletypemember/FourSpaces.java b/src/test/resources/indentation/singletypemember/FourSpaces.java new file mode 100644 index 00000000000..50761ee8d42 --- /dev/null +++ b/src/test/resources/indentation/singletypemember/FourSpaces.java @@ -0,0 +1,5 @@ +package indentation.singletypemember; + +public class FourSpaces { + int x; +} \ No newline at end of file diff --git a/src/test/resources/indentation/singletypemember/Tabs.java b/src/test/resources/indentation/singletypemember/Tabs.java new file mode 100644 index 00000000000..2cc1d5f515e --- /dev/null +++ b/src/test/resources/indentation/singletypemember/Tabs.java @@ -0,0 +1,5 @@ +package indentation.singletypemember; + +public class Tabs { + int x; +} \ No newline at end of file diff --git a/src/test/resources/indentation/singletypemember/TwoSpaces.java b/src/test/resources/indentation/singletypemember/TwoSpaces.java new file mode 100644 index 00000000000..8dfe991fb47 --- /dev/null +++ b/src/test/resources/indentation/singletypemember/TwoSpaces.java @@ -0,0 +1,5 @@ +package indentation.singletypemember; + +public class TwoSpaces { + int x; +} \ No newline at end of file