|
| 1 | +/* |
| 2 | + * Copyright 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.spotless; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import org.assertj.core.api.AbstractBooleanAssert; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import com.diffplug.spotless.generic.EndWithNewlineStep; |
| 29 | + |
| 30 | +public class LintSuppressionTest { |
| 31 | + private LintState dummyLintState() { |
| 32 | + var lints = new ArrayList<Lint>(); |
| 33 | + lints.add(Lint.atLine(2, "66", "Order 66")); |
| 34 | + var perStep = new ArrayList<List<Lint>>(); |
| 35 | + perStep.add(lints); |
| 36 | + return new LintState(DirtyState.clean(), perStep); |
| 37 | + } |
| 38 | + |
| 39 | + private Formatter formatter() { |
| 40 | + return Formatter.builder() |
| 41 | + .lineEndingsPolicy(LineEnding.UNIX.createPolicy()) |
| 42 | + .encoding(StandardCharsets.UTF_8) |
| 43 | + .steps(List.of(EndWithNewlineStep.create())) |
| 44 | + .build(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testMatchSingle() { |
| 49 | + var noSuppressions = dummyLintState(); |
| 50 | + noSuppressions.removeSuppressedLints(formatter(), "file", List.of()); |
| 51 | + assertThat(noSuppressions.isHasLints()).isTrue(); |
| 52 | + removesLint(s -> s.setStep("blah")).isFalse(); |
| 53 | + removesLint(s -> s.setStep("endWithNewline")).isTrue(); |
| 54 | + removesLint(s -> s.setFile("blah")).isFalse(); |
| 55 | + removesLint(s -> s.setFile("testFile")).isTrue(); |
| 56 | + removesLint(s -> s.setShortCode("blah")).isFalse(); |
| 57 | + removesLint(s -> s.setShortCode("66")).isTrue(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testMatchDouble() { |
| 62 | + removesLint(s -> { |
| 63 | + s.setStep("endWithNewline"); |
| 64 | + s.setShortCode("blah"); |
| 65 | + }).isFalse(); |
| 66 | + removesLint(s -> { |
| 67 | + s.setStep("blah"); |
| 68 | + s.setShortCode("66"); |
| 69 | + }).isFalse(); |
| 70 | + removesLint(s -> { |
| 71 | + s.setStep("endWithNewline"); |
| 72 | + s.setShortCode("66"); |
| 73 | + }).isTrue(); |
| 74 | + } |
| 75 | + |
| 76 | + private AbstractBooleanAssert<?> removesLint(Consumer<LintSuppression> suppression) { |
| 77 | + var s = new LintSuppression(); |
| 78 | + suppression.accept(s); |
| 79 | + |
| 80 | + var ls = dummyLintState(); |
| 81 | + ls.removeSuppressedLints(formatter(), "testFile", List.of(s)); |
| 82 | + return assertThat(!ls.isHasLints()); |
| 83 | + } |
| 84 | +} |
0 commit comments