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