generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use consistent code writer api. - Add extra indentation to record parameters.
- Loading branch information
1 parent
11b220e
commit 36b03dc
Showing
3 changed files
with
87 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...n-generator-java/src/test/java/io/micronaut/sourcegen/javapoet/write/RecordWriteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.micronaut.sourcegen.javapoet.write; | ||
|
||
import io.micronaut.sourcegen.JavaPoetSourceGenerator; | ||
import io.micronaut.sourcegen.model.AnnotationDef; | ||
import io.micronaut.sourcegen.model.ClassTypeDef; | ||
import io.micronaut.sourcegen.model.PropertyDef; | ||
import io.micronaut.sourcegen.model.RecordDef; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public class RecordWriteTest { | ||
|
||
|
||
@Test | ||
public void writeSimpleRecord() throws IOException { | ||
RecordDef recordDef = RecordDef.builder("test.TestRecord") | ||
.addProperty(PropertyDef.builder("name").ofType(String.class).build()) | ||
.addProperty(PropertyDef.builder("age").ofType(Integer.class) | ||
.addAnnotation(AnnotationDef.builder( | ||
ClassTypeDef.of("jakarta.validation.constraints.Min")) | ||
.addMember("value", 1).build()) | ||
.build() | ||
) | ||
.addProperty(PropertyDef.builder("description").ofType(String.class) | ||
.addAnnotation("jakarta.validation.constraints.NotBlank") | ||
.build() | ||
) | ||
.build(); | ||
var result = writeRecord(recordDef); | ||
|
||
var expected = """ | ||
record TestRecord( | ||
String name, | ||
@Min(1) Integer age, | ||
@NotBlank String description | ||
) { | ||
} | ||
"""; | ||
assertEquals(expected.strip(), result.strip()); | ||
} | ||
|
||
private String writeRecord(RecordDef recordDef) throws IOException { | ||
JavaPoetSourceGenerator generator = new JavaPoetSourceGenerator(); | ||
String result; | ||
try (StringWriter writer = new StringWriter()) { | ||
generator.write(recordDef, writer); | ||
result = writer.toString(); | ||
} | ||
|
||
// The regex will skip the imports and make sure it is a record | ||
final Pattern RECORD_REGEX = Pattern.compile("package [^;]+;[\\s\\S]+" + | ||
"(record \\S+[\\s\\S]+})\\s*"); | ||
Matcher matcher = RECORD_REGEX.matcher(result); | ||
if (!matcher.matches()) { | ||
fail("Expected record to match regex: \n" + RECORD_REGEX + "\nbut is: \n" + result); | ||
} | ||
return matcher.group(1); | ||
} | ||
|
||
} |