Allow TestCompiler SourceFile to work with records

Update `SourceFile` to try a regex replace to make `record` files look
like regular classes.

Closes gh-29236
This commit is contained in:
Phillip Webb
2022-09-30 11:18:43 -07:00
parent 8c24e8c034
commit 7fd8e081bb
3 changed files with 37 additions and 1 deletions

View File

@@ -119,6 +119,33 @@ class SourceFileTests {
assertThat(sourceFile.getContent()).isEqualTo(HELLO_WORLD);
}
@Test
void getClassNameFromSimpleRecord() {
SourceFile sourceFile = SourceFile.of("""
package com.example.helloworld;
record HelloWorld(String name) {
}
""");
assertThat(sourceFile.getClassName()).isEqualTo("com.example.helloworld.HelloWorld");
}
@Test
void getClassNameFromMoreComplexRecord() {
SourceFile sourceFile = SourceFile.of("""
package com.example.helloworld;
public record HelloWorld(String name) {
String getFoo() {
return name();
}
}
""");
assertThat(sourceFile.getClassName()).isEqualTo("com.example.helloworld.HelloWorld");
}
/**
* JavaPoet style API with a {@code writeTo} method.
*/