From 6f4afccfe077fa305d5cabe24c1a133bc8c20fcc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 22:31:24 +0100 Subject: Add proper description of function type --- .../shedlang/compiler/types/FunctionTypeTests.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java b/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java new file mode 100644 index 00000000..d182ce19 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java @@ -0,0 +1,90 @@ +package org.shedlang.compiler.types; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class FunctionTypeTests { + @Test + public void descriptionIncludesReturnType() { + var functionType = new FunctionType( + List.of(), + List.of(), + Effects.NONE, + Types.STRING + ); + + var result = functionType.describe(); + + assertThat(result, equalTo("fn () -> String")); + } + + @Test + public void descriptionIncludesPositionalParams() { + var functionType = new FunctionType( + List.of( + Types.INT, + Types.BOOL + ), + List.of(), + Effects.NONE, + Types.STRING + ); + + var result = functionType.describe(); + + assertThat(result, equalTo("fn (Int, Bool) -> String")); + } + + @Test + public void descriptionIncludesNamedParams() { + var functionType = new FunctionType( + List.of(), + List.of( + new NamedParam("a", Types.INT), + new NamedParam("b", Types.BOOL) + ), + Effects.NONE, + Types.STRING + ); + + var result = functionType.describe(); + + assertThat(result, equalTo("fn (.a: Int, .b: Bool) -> String")); + } + + @Test + public void positionalParamsAndNamedParamsAreCommaSeparated() { + var functionType = new FunctionType( + List.of( + Types.INT + ), + List.of( + new NamedParam("b", Types.BOOL) + ), + Effects.NONE, + Types.STRING + ); + + var result = functionType.describe(); + + assertThat(result, equalTo("fn (Int, .b: Bool) -> String")); + } + + @Test + public void descriptionIncludesEffect() { + var functionType = new FunctionType( + List.of(), + List.of(), + Effects.IO, + Types.STRING + ); + + var result = functionType.describe(); + + assertThat(result, equalTo("fn () ! Io -> String")); + } +} -- cgit v1.2.3