package interpreter;

import interpreter.Environment;
import interpreter.UnboundVariableException;
import interpreter.type.TestObject;
import interpreter.type.Symbol;

import static org.junit.Assert.assertSame;

import org.junit.Before;
import org.junit.Test;

public class EnvironmentTest {

    private Environment testEnv;
    private TestObject obj;
    private Symbol key;

    @Before
    public void setUp() {
        testEnv = new Environment();
        obj = new TestObject();
        key = new Symbol("test");
        testEnv.define(key, obj);
    }

    @Test
    public void lookUpWhenVariableBoundShouldReturnObject() {
        assertSame(testEnv.lookUp(key), obj);
    }

    @Test
    public void lookUpWhenVariableBoundInParentEnvironmentShouldReturnObject() {
        Environment env = new Environment(testEnv, null, null);
        assertSame(env.lookUp(key), obj);
    }

    @Test(expected= UnboundVariableException.class)
    public void lookUpWhenVariableNotBoundInEnvironmentShouldThrowUnboundVariableException() {
        testEnv.lookUp(new Symbol("foo"));
    }

    @Test(expected= UnboundVariableException.class)
    public void lookUpWhenVariableNotBoundInEnvironmentAndParentEnvironmentShouldThrowUnboundVariableException() {
        Environment env = new Environment(testEnv, null, null);
        testEnv.lookUp(new Symbol("foo"));
    }

    @Test
    public void setWhenVariableBoundInEnvironmentShouldSetToNewObject() {
        TestObject newObj = new TestObject();
        testEnv.set(key, newObj);
        assertSame(testEnv.lookUp(key), newObj);
    }

    @Test
    public void setWhenVariableBoundInParentEnvironmentShouldSetToNewObjectInParentEnvironment() {
        TestObject newObj = new TestObject();
        Environment env = new Environment(testEnv, null, null);
        env.set(key, newObj);
        assertSame(testEnv.lookUp(key), newObj);
    }

    @Test(expected= UnboundVariableException.class)
    public void setWhenVariableNotBoundInEnvironmentShouldThrowUnboundVariableException() {
        TestObject newObj = new TestObject();
        testEnv.set(new Symbol("foo"), newObj);
    }
}

@@ -159,16 +159,46 @@
 .Symbol;
+%0Aimport interpreter.type.Null;
 %0A%0Aimport
@@ -848,33 +848,45 @@
 t(testEnv, n
+ew N
 ull
+()
 , n
+ew N
 ull
+()
 );%0A        a
@@ -1337,33 +1337,45 @@
 t(testEnv, n
+ew N
 ull
+()
 , n
+ew N
 ull
+()
 );%0A        t
@@ -1846,17 +1846,29 @@
 v, n
+ew N
 ull
+()
 , n
+ew N
 ull
+()
 );%0A 

Fix Environment tests