-
Notifications
You must be signed in to change notification settings - Fork 435
/
FenceStepTest.java
161 lines (147 loc) · 4.69 KB
/
FenceStepTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.generic;
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.StepHarness;
class FenceStepTest extends ResourceHarness {
@Test
void single() {
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
StepHarness harness = StepHarness.forStepNoRoundtrip(fence);
harness.test(
StringPrinter.buildStringFromLines(
"A B C",
"spotless:off",
"D E F",
"spotless:on",
"G H I"),
StringPrinter.buildStringFromLines(
"a b c",
"spotless:off",
"D E F",
"spotless:on",
"g h i"));
}
@Test
void multiple() {
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
StepHarness harness = StepHarness.forStepNoRoundtrip(fence);
harness.test(
StringPrinter.buildStringFromLines(
"A B C",
"spotless:off",
"D E F",
"spotless:on",
"G H I",
"spotless:off J K L spotless:on",
"M N O",
"P Q R",
"S T U spotless:off V W",
" X ",
" Y spotless:on Z",
"1 2 3"),
StringPrinter.buildStringFromLines(
"a b c",
"spotless:off",
"D E F",
"spotless:on",
"g h i",
"spotless:off J K L spotless:on",
"m n o",
"p q r",
"s t u spotless:off V W",
" X ",
" Y spotless:on z",
"1 2 3"));
}
@Test
void broken() {
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("uppercase", String::toUpperCase)));
// this fails because uppercase turns spotless:off into SPOTLESS:OFF, etc
StepHarness.forStepNoRoundtrip(fence).testExceptionMsg(StringPrinter.buildStringFromLines("A B C",
"spotless:off",
"D E F",
"spotless:on",
"G H I")).isEqualTo("An intermediate step removed a match of spotless:off spotless:on");
}
@Test
void andApply() {
FormatterStep fence = FenceStep.named("fence").openClose("<lower>", "</lower>")
.applyWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
StepHarness.forStepNoRoundtrip(fence).test(
StringPrinter.buildStringFromLines(
"A B C",
"<lower>",
"D E F",
"</lower>",
"G H I"),
StringPrinter.buildStringFromLines(
"A B C",
"<lower>",
"d e f",
"</lower>",
"G H I"));
}
/**
* @param name
* The name of the formatter step
* @param function
* The function used by the formatter step
* @return A FormatterStep which will never report that it is up-to-date, because
* it is not equal to the serialized representation of itself.
*/
static <T extends FormatterFunc & Serializable> FormatterStep createNeverUpToDateSerializable(
String name,
T function) {
Objects.requireNonNull(function, "function");
return new NeverUpToDateSerializable(name, function);
}
static class NeverUpToDateSerializable<T extends FormatterFunc & Serializable> implements FormatterStep, Serializable {
private final String name;
private final T formatterFunc;
private NeverUpToDateSerializable(String name, T formatterFunc) {
this.name = name;
this.formatterFunc = formatterFunc;
}
@Override
public String getName() {
return name;
}
@Nullable
@Override
public String format(String rawUnix, File file) throws Exception {
return formatterFunc.apply(rawUnix, file);
}
@Override
public void close() throws Exception {
if (formatterFunc instanceof FormatterFunc.Closeable) {
((FormatterFunc.Closeable) formatterFunc).close();
}
}
}
}