From cffab4d76d6358dde9b708691d96852d39f9e977 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Jul 2026 15:48:13 +0100 Subject: Add subsections for Rust items --- .../compiler/output/lang/rust/RustWriterTests.java | 208 +++++++++++---------- 1 file changed, 108 insertions(+), 100 deletions(-) (limited to 'src/test/java/org/zwobble') 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 -- cgit v1.2.3