-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRemediatorTestMixin.java
More file actions
89 lines (75 loc) · 3.02 KB
/
RemediatorTestMixin.java
File metadata and controls
89 lines (75 loc) · 3.02 KB
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
package io.codemodder;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import io.codemodder.codetf.DetectorRule;
import io.codemodder.remediation.Remediator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
/** A mixin which provides basic structure for testing remediators. */
public interface RemediatorTestMixin<T> {
@TestFactory
default Stream<DynamicTest> it_fixes_the_code() {
Stream<FixableSample> fixableSamples = createFixableSamples();
return fixableSamples.map(
sample -> {
String beforeCode = sample.beforeCode();
String afterCode = sample.afterCode();
int line = sample.line();
return DynamicTest.dynamicTest(
beforeCode,
() -> {
CompilationUnit cu = StaticJavaParser.parse(beforeCode);
LexicalPreservingPrinter.setup(cu);
Remediator<Object> remediator = createRemediator();
DetectorRule rule = new DetectorRule("rule-123", "my-remediation-rule", null);
CodemodFileScanningResult result =
remediator.remediateAll(
cu,
"foo",
rule,
List.of(new Object()),
f -> "123",
f -> line,
x -> Optional.empty(),
x -> Optional.empty());
assertThat(result.unfixedFindings()).isEmpty();
assertThat(result.changes()).hasSize(1);
CodemodChange change = result.changes().get(0);
assertThat(change.lineNumber()).isEqualTo(line);
String actualCode = LexicalPreservingPrinter.print(cu);
assertThat(actualCode).isEqualToIgnoringCase(afterCode);
});
});
}
/** Build the remediator to test. */
Remediator<Object> createRemediator();
/** Create samples to test. */
Stream<FixableSample> createFixableSamples();
/** Create unfixable samples. */
Stream<UnfixableSample> createUnfixableSamples();
/** Represents a finding that can be fixed. */
record FixableSample(String beforeCode, String afterCode, int line) {
public FixableSample {
Objects.requireNonNull(beforeCode);
Objects.requireNonNull(afterCode);
if (line < 0) {
throw new IllegalArgumentException("Line number must be non-negative");
}
}
}
/** Represents a finding that can't be fixed for some reason. */
record UnfixableSample(String code, int line) {
public UnfixableSample {
Objects.requireNonNull(code);
if (line < 0) {
throw new IllegalArgumentException("Line number must be non-negative");
}
}
}
}