From 4f8135071ae6faf7abfbc2f4765101ce9c0bb287 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 19 Dec 2024 16:30:17 +0000 Subject: Define methods directly on struct --- .../compiler/parser/ParserExtendInlineTests.java | 47 ---------------- .../parser/ParserStructDefinitionTests.java | 65 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 47 deletions(-) delete mode 100644 src/test/java/org/shedlang/compiler/parser/ParserExtendInlineTests.java (limited to 'src/test/java/org/shedlang/compiler/parser') diff --git a/src/test/java/org/shedlang/compiler/parser/ParserExtendInlineTests.java b/src/test/java/org/shedlang/compiler/parser/ParserExtendInlineTests.java deleted file mode 100644 index 6652a46e..00000000 --- a/src/test/java/org/shedlang/compiler/parser/ParserExtendInlineTests.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.shedlang.compiler.parser; - -import org.junit.jupiter.api.Test; - -import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; -import static org.shedlang.compiler.parser.ParserTesting.parseString; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class ParserExtendInlineTests { - @Test - public void canParseExtendInlineWithNoMethods() { - var source = """ - extend Count {} - """; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedExtendInlineNode() - .withExtendedType(isUntypedTypeLevelReferenceNode("Count")) - .withMethods(isSequence()) - ); - } - - @Test - public void canParseExtendInlineWithSingleMethod() { - var source = """ - extend Count { - fun message(this) { - } - } - """; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedExtendInlineNode() - .withExtendedType(isUntypedTypeLevelReferenceNode("Count")) - .withMethods(isSequence( - isUntypedFunctionDefinitionNode().withName("message") - )) - ); - } -} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java index b450506e..9380c64a 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -178,4 +178,69 @@ public class ParserStructDefinitionTests { ))) ); } + + + @Test + public void canParseStructWithNoMethods() { + var source = """ + struct X() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withMethods(isSequence()) + ); + } + + @Test + public void canParseExtendInlineWithSingleMethod() { + var source = """ + struct X() { + fun a(this) { + } + } + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withMethods(isSequence( + isUntypedFunctionDefinitionNode().withName("a") + )) + ); + } + + @Test + public void canParseExtendInlineWithMultipleMethods() { + var source = """ + struct X() { + fun a(this) { + } + + fun b(this) { + } + + fun c(this) { + } + + } + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withMethods(isSequence( + isUntypedFunctionDefinitionNode().withName("a"), + isUntypedFunctionDefinitionNode().withName("b"), + isUntypedFunctionDefinitionNode().withName("c") + )) + ); + } } -- cgit v1.2.3