diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-19 16:30:17 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-19 16:30:17 +0000 |
| commit | 4f8135071ae6faf7abfbc2f4765101ce9c0bb287 (patch) | |
| tree | 466ce9540538e5f3a193b0b82fde30796651de0f /src/test/java | |
| parent | cbe45599f7b46d7a0674a62d424b9485e7735d45 (diff) | |
Define methods directly on struct
Diffstat (limited to 'src/test/java')
9 files changed, 75 insertions, 109 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedExtendInlineNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedExtendInlineNodeMatcher.java deleted file mode 100644 index 1c82f937..00000000 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedExtendInlineNodeMatcher.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.shedlang.compiler.ast.untyped; - -import org.pcollections.PVector; -import org.shedlang.compiler.testing.MatcherBuilder; -import org.shedlang.compiler.util.P; -import org.zwobble.precisely.Matcher; - -import static org.zwobble.precisely.Matchers.has; - -public class UntypedExtendInlineNodeMatcher extends MatcherBuilder< - UntypedNamespaceStatementNode, - UntypedExtendInlineNode, - UntypedExtendInlineNodeMatcher -> { - public static UntypedExtendInlineNodeMatcher empty() { - return new UntypedExtendInlineNodeMatcher(P.vector()); - } - - public UntypedExtendInlineNodeMatcher(PVector<Matcher<? super UntypedExtendInlineNode>> submatchers) { - super(submatchers); - } - - @Override - protected Class<UntypedExtendInlineNode> expectedType() { - return UntypedExtendInlineNode.class; - } - - @Override - protected UntypedExtendInlineNodeMatcher create( - PVector<Matcher<? super UntypedExtendInlineNode>> submatchers - ) { - return new UntypedExtendInlineNodeMatcher(submatchers); - } - - public UntypedExtendInlineNodeMatcher withExtendedType( - Matcher<UntypedTypeLevelExpressionNode> extendedType - ) { - return with(has( - "extendedType", - x -> x.extendedType(), - extendedType - )); - } - - public UntypedExtendInlineNodeMatcher withMethods( - Matcher<Iterable<? extends UntypedFunctionDefinitionNode>> methods - ) { - return with(has( - "methods", - x -> x.methods(), - methods - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java index 6e98a788..e0597387 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -141,10 +141,6 @@ public class UntypedNodeMatchers { ); } - public static UntypedExtendInlineNodeMatcher isUntypedExtendInlineNode() { - return UntypedExtendInlineNodeMatcher.empty(); - } - public static Matcher<UntypedExpressionNode> isUntypedFieldAccessNode( Matcher<UntypedExpressionNode> receiver, String fieldName diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java index 0c1090b2..acdcff50 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java @@ -57,4 +57,10 @@ public class UntypedStructDefinitionNodeMatcher extends MatcherBuilder<UntypedNa public UntypedStructDefinitionNodeMatcher withVisibility(Visibility visibility) { return with(has("visibility", x -> x.visibility(), equalTo(visibility))); } + + public UntypedStructDefinitionNodeMatcher withMethods( + Matcher<Iterable<? extends UntypedFunctionDefinitionNode>> methods + ) { + return with(has("methods", x -> x.methods(), methods)); + } } diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java index cd4694ec..bd754c3f 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java @@ -23,7 +23,7 @@ public class Amd64ExampleTests extends ExampleTests { } private static final Set<String> DISABLED = Set.of( - "35-extend-struct-inline" + "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java index 085de25d..9de1ff27 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java @@ -23,7 +23,7 @@ public class I686ExampleTests extends ExampleTests { } private static final Set<String> DISABLED = Set.of( - "35-extend-struct-inline" + "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java index 30b2ea8a..1e56b989 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java @@ -23,7 +23,7 @@ public class Wasm32ExampleTests extends ExampleTests { } private static final Set<String> DISABLED = Set.of( - "35-extend-struct-inline" + "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java index 2f3f2195..972da7b7 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java @@ -23,7 +23,7 @@ public class Wasm64ExampleTests extends ExampleTests { } private static final Set<String> DISABLED = Set.of( - "35-extend-struct-inline" + "35-struct-methods" ); @Override 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") + )) + ); + } } |
