Skip to content

Commit 46bbd1d

Browse files
committed
test: support source-version guarded hint rules
1 parent 0de1fe0 commit 46bbd1d

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

sandbox_common_core/src/test/java/org/sandbox/jdt/triggerpattern/test/HintRuleTestSupport.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract class HintRuleTestSupport {
4545

4646
private static final String BUNDLED_HINT_PREFIX =
4747
"org/sandbox/jdt/triggerpattern/internal/"; //$NON-NLS-1$
48+
private static final String DEFAULT_SOURCE_VERSION = "21"; //$NON-NLS-1$
4849

4950
protected HintFile loadBundledHint(String resourceName) throws Exception {
5051
String resourcePath = BUNDLED_HINT_PREFIX + resourceName;
@@ -61,8 +62,12 @@ protected HintFile parseHint(String hintContent) throws Exception {
6162
}
6263

6364
protected List<TransformationResult> process(HintFile hintFile, String code) {
65+
return process(hintFile, code, DEFAULT_SOURCE_VERSION);
66+
}
67+
68+
protected List<TransformationResult> process(HintFile hintFile, String code, String sourceVersion) {
6469
BatchTransformationProcessor processor = new BatchTransformationProcessor(hintFile);
65-
return processor.process(parseCode(code));
70+
return processor.process(parseCode(code, sourceVersion), Map.of(JavaCore.COMPILER_SOURCE, sourceVersion));
6671
}
6772

6873
/**
@@ -86,7 +91,12 @@ protected void assertReplacementFragment(HintFile hintFile, String beforeCode, S
8691
* replacement fragment.
8792
*/
8893
protected void assertFullReplacement(HintFile hintFile, String beforeCode, String expectedCode) {
89-
List<TransformationResult> results = process(hintFile, beforeCode);
94+
assertFullReplacement(hintFile, beforeCode, expectedCode, DEFAULT_SOURCE_VERSION);
95+
}
96+
97+
protected void assertFullReplacement(HintFile hintFile, String beforeCode, String expectedCode,
98+
String sourceVersion) {
99+
List<TransformationResult> results = process(hintFile, beforeCode, sourceVersion);
90100
assertFalse(results.isEmpty(), "Expected at least one match"); //$NON-NLS-1$
91101
TransformationResult result = results.stream()
92102
.filter(TransformationResult::hasReplacement)
@@ -96,8 +106,19 @@ protected void assertFullReplacement(HintFile hintFile, String beforeCode, Strin
96106
assertEquals(normalizeSource(expectedCode), normalizeSource(actualCode));
97107
}
98108

99-
protected void assertNoMatch(HintFile hintFile, String code) {
109+
protected void assertHintOnlyMatch(HintFile hintFile, String code) {
100110
List<TransformationResult> results = process(hintFile, code);
111+
assertFalse(results.isEmpty(), "Expected at least one hint-only match"); //$NON-NLS-1$
112+
assertTrue(results.stream().noneMatch(TransformationResult::hasReplacement),
113+
"Expected hint-only match, but at least one replacement was produced: " + results); //$NON-NLS-1$
114+
}
115+
116+
protected void assertNoMatch(HintFile hintFile, String code) {
117+
assertNoMatch(hintFile, code, DEFAULT_SOURCE_VERSION);
118+
}
119+
120+
protected void assertNoMatch(HintFile hintFile, String code, String sourceVersion) {
121+
List<TransformationResult> results = process(hintFile, code, sourceVersion);
101122
assertTrue(results.isEmpty(), "Expected no match but got: " + results); //$NON-NLS-1$
102123
}
103124

@@ -117,12 +138,12 @@ private String normalizeSource(String source) {
117138
return source.replaceAll("\\s+", " ").trim(); //$NON-NLS-1$ //$NON-NLS-2$
118139
}
119140

120-
private CompilationUnit parseCode(String code) {
141+
private CompilationUnit parseCode(String code, String sourceVersion) {
121142
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
122143
astParser.setSource(code.toCharArray());
123144
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
124145
Map<String, String> options = JavaCore.getOptions();
125-
options.put(JavaCore.COMPILER_SOURCE, "17"); //$NON-NLS-1$
146+
options.put(JavaCore.COMPILER_SOURCE, sourceVersion);
126147
astParser.setCompilerOptions(options);
127148
return (CompilationUnit) astParser.createAST(null);
128149
}

0 commit comments

Comments
 (0)