implements PR014
This commit is contained in:
+9
-1
@@ -48,7 +48,7 @@ class PbsExprParserTest {
|
||||
final var source = """
|
||||
fn flow(cond: bool, state: int) -> int {
|
||||
let a: int = if cond { 1 } else { 2 };
|
||||
let b: int = switch state { default: { 0 } };
|
||||
let b: int = switch state { default: { 0 }, _: { 1 } };
|
||||
let c: int = handle run apply () { Err.fail -> Other.fail, _ -> { 1 } };
|
||||
return 0;
|
||||
}
|
||||
@@ -69,7 +69,15 @@ class PbsExprParserTest {
|
||||
|
||||
final var switchExpr = assertInstanceOf(PbsAst.SwitchExpr.class,
|
||||
assertInstanceOf(PbsAst.LetStatement.class, body.get(1)).initializer());
|
||||
assertEquals(2, switchExpr.arms().size());
|
||||
final var defaultPattern = assertInstanceOf(PbsAst.WildcardSwitchPattern.class,
|
||||
switchExpr.arms().getFirst().pattern());
|
||||
assertEquals(PbsAst.WildcardSwitchKind.DEFAULT, defaultPattern.kind());
|
||||
assertInstanceOf(PbsAst.IntLiteralExpr.class, switchExpr.arms().getFirst().block().tailExpression());
|
||||
final var underscorePattern = assertInstanceOf(PbsAst.WildcardSwitchPattern.class,
|
||||
switchExpr.arms().get(1).pattern());
|
||||
assertEquals(PbsAst.WildcardSwitchKind.UNDERSCORE, underscorePattern.kind());
|
||||
assertInstanceOf(PbsAst.IntLiteralExpr.class, switchExpr.arms().get(1).block().tailExpression());
|
||||
|
||||
final var handle = assertInstanceOf(PbsAst.HandleExpr.class,
|
||||
assertInstanceOf(PbsAst.LetStatement.class, body.get(2)).initializer());
|
||||
|
||||
+43
@@ -5,6 +5,7 @@ import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsSemanticsControlFlowTest {
|
||||
@@ -39,4 +40,46 @@ class PbsSemanticsControlFlowTest {
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_BARE_METHOD_EXTRACTION.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateSwitchWildcardArms() {
|
||||
final var source = """
|
||||
fn flow(state: int) -> int {
|
||||
let value: int = switch state {
|
||||
default: { 1 },
|
||||
default: { 2 },
|
||||
};
|
||||
return value;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
final var duplicateWildcardCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_SWITCH_DUPLICATE_PATTERN.name()))
|
||||
.count();
|
||||
assertEquals(1, duplicateWildcardCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectMixedSwitchWildcardSpellings() {
|
||||
final var source = """
|
||||
fn flow(state: int) -> int {
|
||||
let value: int = switch state {
|
||||
default: { 1 },
|
||||
_: { 2 },
|
||||
};
|
||||
return value;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
final var duplicateWildcardCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_SWITCH_DUPLICATE_PATTERN.name()))
|
||||
.count();
|
||||
assertEquals(1, duplicateWildcardCount);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user