aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/couscous/tests/frontends
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-15 14:23:47 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-15 14:23:47 +0100
commitbc06840e00425db342bc5a021c0900d9e63b71f0 (patch)
tree0aa45a4ea475804a36a5ee33f9a726656f45bc1f /src/test/java/org/zwobble/couscous/tests/frontends
parent3be68289f34a37ac7f9e1e95fa0d95dfc1bb3836 (diff)
Split switch handling off to desugar step
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/frontends')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java31
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/NodeMatchers.java37
2 files changed, 46 insertions, 22 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java
index d455301..acda60c 100644
--- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java
@@ -179,25 +179,18 @@ public class JavaReaderTests {
}
@Test
- public void switchStatementWithoutFallThroughIsReadAsIfElseStatement() {
- assertEquals(
- readStatements(
- "int",
- "String _couscous_tmp_0 = \"one\";" +
- "if (_couscous_tmp_0.equals(\"one\")) { return 0; }" +
- "else if (_couscous_tmp_0.equals(\"two\")) { return 0; }" +
- "else { return 1; }"),
- readStatements("int", "switch (\"one\") { case \"one\": case \"two\": return 0; default: return 1; }"));
- }
-
- @Test
- public void switchStatementWithoutDefaultIsReadAsIfStatementWithoutElseStatement() {
- assertEquals(
- readStatements(
- "int",
- "String _couscous_tmp_0 = \"one\";" +
- "if (_couscous_tmp_0.equals(\"one\")) { return 0; } return 1;"),
- readStatements("int", "switch (\"one\") { case \"one\": return 0; } return 1;"));
+ public void canReadSwitchStatements() {
+ assertThat(
+ readStatement("int", "switch (\"one\") { case \"one\": case \"two\": return 0; default: return 1; }"),
+ isSwitch(
+ hasSwitchValue(equalTo(literal("one"))),
+ hasSwitchCases(
+ isCase(literal("one"), list()),
+ isCase(literal("two"), list(returns(literal(0)))),
+ isDefaultCase(list(returns(literal(1))))
+ )
+ )
+ );
}
@Test
diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/NodeMatchers.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/NodeMatchers.java
index e9c77f0..5815e68 100644
--- a/src/test/java/org/zwobble/couscous/tests/frontends/java/NodeMatchers.java
+++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/NodeMatchers.java
@@ -3,13 +3,15 @@ package org.zwobble.couscous.tests.frontends.java;
import org.hamcrest.Matcher;
import org.zwobble.couscous.ast.*;
import org.zwobble.couscous.ast.identifiers.Identifier;
+import org.zwobble.couscous.ast.sugar.SwitchCaseNode;
+import org.zwobble.couscous.ast.sugar.SwitchNode;
import org.zwobble.couscous.types.Type;
import java.util.List;
+import java.util.Optional;
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.Matchers.contains;
-import static org.hamcrest.Matchers.equalTo;
+import static java.util.Arrays.asList;
+import static org.hamcrest.Matchers.*;
import static org.zwobble.couscous.tests.util.ExtraMatchers.hasFeature;
import static org.zwobble.couscous.tests.util.ExtraMatchers.isInstance;
@@ -86,4 +88,33 @@ public class NodeMatchers {
public static Matcher<TryNode> hasFinally(StatementNode... statements) {
return hasFeature("finally clause", TryNode::getFinallyBody, contains(statements));
}
+
+ @SafeVarargs
+ public static Matcher<StatementNode> isSwitch(Matcher<SwitchNode>... matchers) {
+ return isInstance(SwitchNode.class, allOf(matchers));
+ }
+
+ public static Matcher<SwitchNode> hasSwitchValue(Matcher<ExpressionNode> matcher) {
+ return hasFeature("switch value", SwitchNode::getValue, matcher);
+ }
+
+ @SafeVarargs
+ public static Matcher<SwitchNode> hasSwitchCases(Matcher<SwitchCaseNode>... matchers) {
+ return hasFeature("switch cases", SwitchNode::getCases, contains(asList(matchers)));
+ }
+
+ public static Matcher<SwitchCaseNode> isCase(ExpressionNode value, List<StatementNode> statements) {
+ return isCase(Optional.of(value), statements);
+ }
+
+ public static Matcher<SwitchCaseNode> isDefaultCase(List<StatementNode> statements) {
+ return isCase(Optional.empty(), statements);
+ }
+
+ private static Matcher<SwitchCaseNode> isCase(Optional<ExpressionNode> value, List<StatementNode> statements) {
+ return allOf(
+ hasFeature("value", SwitchCaseNode::getValue, equalTo(value)),
+ hasFeature("statements", SwitchCaseNode::getStatements, equalTo(statements))
+ );
+ }
}