aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-11-24 21:06:25 +0000
committerMichael Williamson <mike@zwobble.org>2015-11-24 21:06:25 +0000
commitc5f612e695f9fdc1773048899483f447507d3462 (patch)
tree4fc2ea068ab9e8175d04d8d1c4c18e1a0c190140
parent18c40acb1945c640205c1091cc6775b07699ce4e (diff)
Add tests for FreeVariables
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/FreeVariables.java2
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java34
2 files changed, 35 insertions, 1 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/FreeVariables.java b/src/main/java/org/zwobble/couscous/frontends/java/FreeVariables.java
index 9c5d557..3e3c7c6 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/FreeVariables.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/FreeVariables.java
@@ -12,7 +12,7 @@ import static org.zwobble.couscous.util.Casts.tryCast;
import static org.zwobble.couscous.util.ExtraStreams.toStream;
public class FreeVariables {
- public static List<VariableDeclaration> findFreeVariables(List<FormalArgumentNode> formalArguments, List<StatementNode> body) {
+ public static List<VariableDeclaration> findFreeVariables(List<FormalArgumentNode> formalArguments, List<? extends Node> body) {
Stream<VariableDeclaration> referencedDeclarations = body.stream()
.flatMap(FreeVariables::findReferencedDeclarations);
// TODO: consider nested arguments
diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java
new file mode 100644
index 0000000..68cfda7
--- /dev/null
+++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java
@@ -0,0 +1,34 @@
+package org.zwobble.couscous.tests.frontends.java;
+
+import org.junit.Test;
+import org.zwobble.couscous.ast.FormalArgumentNode;
+import org.zwobble.couscous.ast.VariableDeclaration;
+import org.zwobble.couscous.values.StringValue;
+
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static org.junit.Assert.assertEquals;
+import static org.zwobble.couscous.ast.FormalArgumentNode.formalArg;
+import static org.zwobble.couscous.ast.VariableDeclaration.var;
+import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
+import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables;
+
+public class FreeVariablesTests {
+ @Test
+ public void referenceIsFreeIfThereIsNoAssociatedDeclaration() {
+ VariableDeclaration declaration = var("[id]", "[name]", StringValue.REF);
+ List<VariableDeclaration> freeVariables = findFreeVariables(emptyList(), asList(reference(declaration)));
+ assertEquals(asList(declaration), freeVariables);
+ }
+
+ @Test
+ public void referenceIsNotFreeIfIsDeclaredAsArgument() {
+ VariableDeclaration declaration = var("[id]", "[name]", StringValue.REF);
+ List<VariableDeclaration> freeVariables = findFreeVariables(
+ asList(formalArg(declaration)),
+ asList(reference(declaration)));
+ assertEquals(emptyList(), freeVariables);
+ }
+}