Skip to content

Commit 8dde43f

Browse files
committed
feat: test new api for indentation spaces vs. tabs
- check for deprecation warnings but retained functionality of old api - check functionality of new api Refs: #794, #2311
1 parent 8a11dd5 commit 8dde43f

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2021-2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.io.IOException;
21+
import java.util.stream.Stream;
22+
23+
import org.gradle.testkit.runner.BuildResult;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
import org.junit.jupiter.params.provider.ValueSource;
28+
29+
class IndentIntegrationTest extends GradleIntegrationHarness {
30+
31+
@ParameterizedTest
32+
@ValueSource(strings = {"indentWithSpaces", "indentWithTabs"})
33+
void oldIndentApiLogsDeprecationWarning(String indentationMethodName) throws IOException {
34+
BuildResult result = runIndentFormatter(indentationMethodName);
35+
assertThat(result.getOutput()).containsPattern(".*" + indentationMethodName + ".*deprecated.*");
36+
}
37+
38+
@ParameterizedTest
39+
@ValueSource(strings = {"leadingTabsToSpaces", "leadingSpacesToTabs"})
40+
void newIndentApiDoesNotLogDeprecationWarning(String indentationMethodName) throws IOException {
41+
BuildResult result = runIndentFormatter(indentationMethodName);
42+
assertThat(result.getOutput()).doesNotContainPattern(".*" + indentationMethodName + ".*deprecated.*");
43+
}
44+
45+
@ParameterizedTest(name = "{0}")
46+
@MethodSource("indentationCombinations")
47+
void indentationCombinations(String testName, String indentationMethodName, String actualResource, String expectedResultResource) throws IOException {
48+
runIndentFormatter(indentationMethodName, actualResource);
49+
assertFile("test.txt").sameAsResource(expectedResultResource);
50+
}
51+
52+
private static Stream<Arguments> indentationCombinations() {
53+
return Stream.of(
54+
// new API
55+
Arguments.of("tabsToTabs", "leadingSpacesToTabs", "indent/IndentedWithTab.test", "indent/IndentedWithTab.test"),
56+
Arguments.of("spacesToSpaces", "leadingTabsToSpaces", "indent/IndentedWithSpace.test", "indent/IndentedWithSpace.test"),
57+
Arguments.of("spacesToTabs", "leadingSpacesToTabs", "indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"),
58+
Arguments.of("tabsToSpaces", "leadingTabsToSpaces", "indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"),
59+
Arguments.of("mixedToTabs", "leadingSpacesToTabs", "indent/IndentedMixed.test", "indent/IndentedWithTab.test"),
60+
Arguments.of("mixedToSpaces", "leadingTabsToSpaces", "indent/IndentedMixed.test", "indent/IndentedWithSpace.test"),
61+
// legacy API
62+
Arguments.of("legacy: tabsToTabs", "indentWithTabs", "indent/IndentedWithTab.test", "indent/IndentedWithTab.test"),
63+
Arguments.of("legacy: spacesToSpaces", "indentWithSpaces", "indent/IndentedWithSpace.test", "indent/IndentedWithSpace.test"),
64+
Arguments.of("legacy: spacesToTabs", "indentWithTabs", "indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"),
65+
Arguments.of("legacy: tabsToSpaces", "indentWithSpaces", "indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"),
66+
Arguments.of("legacy: mixedToTabs", "indentWithTabs", "indent/IndentedMixed.test", "indent/IndentedWithTab.test"),
67+
Arguments.of("legacy: mixedToSpaces", "indentWithSpaces", "indent/IndentedMixed.test", "indent/IndentedWithSpace.test"));
68+
}
69+
70+
private BuildResult runIndentFormatter(String indentationMethodName) throws IOException {
71+
return runIndentFormatter(indentationMethodName, "indent/IndentedMixed.test");
72+
}
73+
74+
private BuildResult runIndentFormatter(String indentationMethodName, String resourceFile) throws IOException {
75+
setFile("build.gradle").toLines(
76+
"plugins {",
77+
" id 'com.diffplug.spotless'",
78+
"}",
79+
"spotless {",
80+
" format 'test', {",
81+
" target '**/*.txt'",
82+
" " + indentationMethodName + "()",
83+
" }",
84+
"}");
85+
setFile("test.txt").toResource(resourceFile);
86+
BuildResult result = gradleRunner().withArguments("spotlessApply").build();
87+
return result;
88+
}
89+
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.youribonnaffe.gradle.format;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* Test class.
7+
*/
8+
public class Java8Test {
9+
/**
10+
* Test method.
11+
*/
12+
public void doStuff() throws Exception {
13+
Function<String, Integer> example = Integer::parseInt;
14+
example.andThen(val -> {
15+
return val + 2;
16+
} );
17+
SimpleEnum val = SimpleEnum.A;
18+
switch (val) {
19+
case A:
20+
break;
21+
case B:
22+
break;
23+
case C:
24+
break;
25+
default:
26+
throw new Exception();
27+
}
28+
}
29+
30+
/** Test enum
31+
* with weirdly formatted javadoc
32+
* which IndentStep should not change
33+
*/
34+
public enum SimpleEnum {
35+
A, B, C;
36+
}
37+
}

0 commit comments

Comments
 (0)