Skip to content

Commit 50d407b

Browse files
4.x: Codegen Unit Testing (helidon-io#11283)
* Codegen Unit Testing Add a new module helidon-codegen-testing to facilitate writing using tests for code generators. The module contains a unit test that demonstrates how to use it. * Review feedback * Updates to builder to align with Helidon builder style. * Add checks for non-null. --------- Co-authored-by: Tomas Langer <tomas.langer@oracle.com>
1 parent 350b540 commit 50d407b

7 files changed

Lines changed: 944 additions & 0 deletions

File tree

codegen/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<module>apt</module>
4545
<module>scan</module>
4646
<module>compiler</module>
47+
<module>testing</module>
4748
</modules>
4849

4950
<build>

codegen/testing/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2026 Oracle and/or its affiliates.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>io.helidon.codegen</groupId>
24+
<artifactId>helidon-codegen-project</artifactId>
25+
<version>4.4.0-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>helidon-codegen-testing</artifactId>
28+
<name>Helidon Codegen Testing</name>
29+
<description>
30+
Tools for testing codegen extensions
31+
</description>
32+
33+
<properties>
34+
<helidon.services.skip>true</helidon.services.skip>
35+
<maven.deploy.skip>true</maven.deploy.skip>
36+
<maven.sources.skip>true</maven.sources.skip>
37+
<maven.javadoc.skip>true</maven.javadoc.skip>
38+
<spotbugs.skip>true</spotbugs.skip>
39+
<dependency-check.skip>true</dependency-check.skip>
40+
<checkstyle.skip>true</checkstyle.skip>
41+
<dependency-plugin-check-dependencies.skip>true</dependency-plugin-check-dependencies.skip>
42+
</properties>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>io.helidon.codegen</groupId>
47+
<artifactId>helidon-codegen</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.hamcrest</groupId>
51+
<artifactId>hamcrest-all</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.jetbrains</groupId>
55+
<artifactId>annotations</artifactId>
56+
<optional>true</optional>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-api</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2026 Oracle and/or its affiliates.
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 io.helidon.codegen.testing;
17+
18+
import java.util.regex.Pattern;
19+
import java.util.stream.Collectors;
20+
21+
import org.hamcrest.Description;
22+
import org.hamcrest.Matcher;
23+
import org.hamcrest.TypeSafeMatcher;
24+
import org.intellij.lang.annotations.Language;
25+
26+
/**
27+
* Hamcrest matchers for codegen tests.
28+
* <p>
29+
* <b>This is NOT part of any supported API.
30+
* If you write code that depends on this, you do so at your own risk.
31+
* This code and its internal interfaces are subject to change or deletion without notice.</b>
32+
* </p>
33+
*/
34+
public final class CodegenMatchers {
35+
36+
private CodegenMatchers() {
37+
}
38+
39+
/**
40+
* Regex match that treats {@code //...} as multiline wildcards.
41+
*
42+
* @param expected expected string
43+
* @return matcher
44+
*/
45+
public static Matcher<String> matches(@Language("java") String expected) {
46+
return new RegexMatcher(expected);
47+
}
48+
49+
private static final class RegexMatcher extends TypeSafeMatcher<String> {
50+
51+
private final String regex;
52+
53+
RegexMatcher(String expected) {
54+
var regex = expected.lines().map(it -> {
55+
if (it.trim().startsWith("//...")) {
56+
return ".*";
57+
} else {
58+
return "\\Q" + it + "\\E";
59+
}
60+
}).collect(Collectors.joining("\n"));
61+
this.regex = expected.endsWith("\n") ? regex + "\n" : regex;
62+
}
63+
64+
@Override
65+
public void describeTo(Description description) {
66+
description.appendText(regex);
67+
}
68+
69+
@Override
70+
protected boolean matchesSafely(String item) {
71+
var pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
72+
return pattern.matcher(item).matches();
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)