|
| 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 | +} |
0 commit comments