summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-03 15:48:13 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-03 15:51:22 +0100
commitcffab4d76d6358dde9b708691d96852d39f9e977 (patch)
treeb00ae9ed3fb4ca742e017f173dd69db000729c8d /src/test/java
parent7311b7638de76ba68f197f891248b426e656e730 (diff)
Add subsections for Rust items
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java208
1 files changed, 108 insertions, 100 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java
index fe82fb9..3ed6e3e 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java
@@ -38,6 +38,113 @@ public class RustWriterTests {
// == Items ==
+ // === Functions ===
+
+ @Test
+ public void emptyFunction() throws IOException {
+ var rust = RustFunction.arbitrary()
+ .withName(new RustIdentifier("width"))
+ .build();
+
+ var string = write(writer -> writer.writeAssociatedItem(rust));
+
+ assertThat(string, equalTo("""
+ pub fn width();"""));
+ }
+
+ @Test
+ public void functionWithParams() throws IOException {
+ var rust = RustFunction.arbitrary()
+ .withName(new RustIdentifier("area"))
+ .addParam(new RustFunctionParam(new RustIdentifier("width"), RustTypePath.of("i32")))
+ .addParam(new RustFunctionParam(new RustIdentifier("height"), RustTypePath.of("u64")))
+ .build();
+
+ var string = write(writer -> writer.writeAssociatedItem(rust));
+
+ assertThat(string, equalTo("""
+ pub fn area(width: i32, height: u64);"""));
+ }
+
+ @Test
+ public void functionWithReturnType() throws IOException {
+ var rust = RustFunction.arbitrary()
+ .withName(new RustIdentifier("area"))
+ .withReturnType(Optional.of(RustTypePath.of("i32")))
+ .build();
+
+ var string = write(writer -> writer.writeAssociatedItem(rust));
+
+ assertThat(string, equalTo("""
+ pub fn area() -> i32;"""));
+ }
+
+ @Test
+ public void functionWithBody() throws IOException {
+ var rust = RustFunction.arbitrary()
+ .withName(new RustIdentifier("predicate"))
+ .withBody(RustBlockExpression.arbitrary()
+ .withFinalOperand(RustBoolLiteral.arbitrary().withValue(false).build())
+ .build())
+ .build();
+
+ var string = write(writer -> writer.writeAssociatedItem(rust));
+
+ assertThat(string, equalTo("""
+ pub fn predicate() {
+ false
+ }"""));
+ }
+
+ // === Structs ===
+
+ @Test
+ public void emptyStruct() throws IOException {
+ var rust = RustStructStruct.arbitrary().withName(new RustIdentifier("Rectangle")).build();
+
+ var string = write(writer -> writer.writeItem(rust));
+
+ assertThat(string, equalTo("""
+ pub struct Rectangle {
+ }"""));
+ }
+
+ @Test
+ public void structWithFields() throws IOException {
+ var rust = RustStructStruct.arbitrary()
+ .withName(new RustIdentifier("Rectangle"))
+ .withFields(List.of(
+ new RustStructField(new RustIdentifier("width"), RustTypePath.of("i32")),
+ new RustStructField(new RustIdentifier("height"), RustTypePath.of("i32"))
+ ))
+ .build();
+
+ var string = write(writer -> writer.writeItem(rust));
+
+ assertThat(string, equalTo("""
+ pub struct Rectangle {
+ pub width: i32,
+ pub height: i32,
+ }"""));
+ }
+
+ @Test
+ public void structWithDocComment() throws IOException {
+ var rust = RustStructStruct.arbitrary()
+ .withName(new RustIdentifier("Rectangle"))
+ .withDocComment(new DocComment("A 2D rectangle."))
+ .build();
+
+ var string = write(writer -> writer.writeItem(rust));
+
+ assertThat(string, equalTo("""
+ /// A 2D rectangle.
+ pub struct Rectangle {
+ }"""));
+ }
+
+ // === Enumerations ===
+
@Test
public void emptyEnum() throws IOException {
var rust = RustEnum.arbitrary().withName(new RustIdentifier("Shape")).build();
@@ -127,61 +234,7 @@ public class RustWriterTests {
}"""));
}
- @Test
- public void emptyFunction() throws IOException {
- var rust = RustFunction.arbitrary()
- .withName(new RustIdentifier("width"))
- .build();
-
- var string = write(writer -> writer.writeAssociatedItem(rust));
-
- assertThat(string, equalTo("""
- pub fn width();"""));
- }
-
- @Test
- public void functionWithParams() throws IOException {
- var rust = RustFunction.arbitrary()
- .withName(new RustIdentifier("area"))
- .addParam(new RustFunctionParam(new RustIdentifier("width"), RustTypePath.of("i32")))
- .addParam(new RustFunctionParam(new RustIdentifier("height"), RustTypePath.of("u64")))
- .build();
-
- var string = write(writer -> writer.writeAssociatedItem(rust));
-
- assertThat(string, equalTo("""
- pub fn area(width: i32, height: u64);"""));
- }
-
- @Test
- public void functionWithReturnType() throws IOException {
- var rust = RustFunction.arbitrary()
- .withName(new RustIdentifier("area"))
- .withReturnType(Optional.of(RustTypePath.of("i32")))
- .build();
-
- var string = write(writer -> writer.writeAssociatedItem(rust));
-
- assertThat(string, equalTo("""
- pub fn area() -> i32;"""));
- }
-
- @Test
- public void functionWithBody() throws IOException {
- var rust = RustFunction.arbitrary()
- .withName(new RustIdentifier("predicate"))
- .withBody(RustBlockExpression.arbitrary()
- .withFinalOperand(RustBoolLiteral.arbitrary().withValue(false).build())
- .build())
- .build();
-
- var string = write(writer -> writer.writeAssociatedItem(rust));
-
- assertThat(string, equalTo("""
- pub fn predicate() {
- false
- }"""));
- }
+ // === Implementations ===
@Test
public void emptyInherentImpl() throws IOException {
@@ -212,51 +265,6 @@ public class RustWriterTests {
}"""));
}
- @Test
- public void emptyStruct() throws IOException {
- var rust = RustStructStruct.arbitrary().withName(new RustIdentifier("Rectangle")).build();
-
- var string = write(writer -> writer.writeItem(rust));
-
- assertThat(string, equalTo("""
- pub struct Rectangle {
- }"""));
- }
-
- @Test
- public void structWithFields() throws IOException {
- var rust = RustStructStruct.arbitrary()
- .withName(new RustIdentifier("Rectangle"))
- .withFields(List.of(
- new RustStructField(new RustIdentifier("width"), RustTypePath.of("i32")),
- new RustStructField(new RustIdentifier("height"), RustTypePath.of("i32"))
- ))
- .build();
-
- var string = write(writer -> writer.writeItem(rust));
-
- assertThat(string, equalTo("""
- pub struct Rectangle {
- pub width: i32,
- pub height: i32,
- }"""));
- }
-
- @Test
- public void structWithDocComment() throws IOException {
- var rust = RustStructStruct.arbitrary()
- .withName(new RustIdentifier("Rectangle"))
- .withDocComment(new DocComment("A 2D rectangle."))
- .build();
-
- var string = write(writer -> writer.writeItem(rust));
-
- assertThat(string, equalTo("""
- /// A 2D rectangle.
- pub struct Rectangle {
- }"""));
- }
-
// == Statements ==
@Test