Convert table tests to kotlin
This commit is contained in:
		
							parent
							
								
									426b130e3b
								
							
						
					
					
						commit
						d172c62e01
					
				@ -1,102 +0,0 @@
 | 
				
			|||||||
package table;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.junit.After;
 | 
					 | 
				
			||||||
import org.junit.Before;
 | 
					 | 
				
			||||||
import org.junit.Test;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import static org.hamcrest.Matchers.is;
 | 
					 | 
				
			||||||
import static org.hamcrest.Matchers.not;
 | 
					 | 
				
			||||||
import static org.hamcrest.Matchers.nullValue;
 | 
					 | 
				
			||||||
import static org.junit.Assert.assertThat;
 | 
					 | 
				
			||||||
import static sexpression.Nil.NIL;
 | 
					 | 
				
			||||||
import static sexpression.Symbol.T;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
public class ExecutionContextTest {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private ExecutionContext executionContext;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ExecutionContextTest() {
 | 
					 | 
				
			||||||
        this.executionContext = ExecutionContext.getInstance();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Before
 | 
					 | 
				
			||||||
    public void setUp() {
 | 
					 | 
				
			||||||
        executionContext.clearContext();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @After
 | 
					 | 
				
			||||||
    public void tearDown() {
 | 
					 | 
				
			||||||
        executionContext.clearContext();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void assignANewScope() {
 | 
					 | 
				
			||||||
        SymbolTable scope = new SymbolTable();
 | 
					 | 
				
			||||||
        executionContext.setScope(scope);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope(), is(scope));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void clearContext() {
 | 
					 | 
				
			||||||
        SymbolTable scope = new SymbolTable();
 | 
					 | 
				
			||||||
        executionContext.setScope(scope);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope(), is(scope));
 | 
					 | 
				
			||||||
        executionContext.clearContext();
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope(), is(not(scope)));
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope().getParent(), is(nullValue()));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void lookupVariable() {
 | 
					 | 
				
			||||||
        executionContext.getScope().put("test", T);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.lookupSymbolValue("test"), is(T));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void lookupLocalVariable() {
 | 
					 | 
				
			||||||
        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        scope.put("local", T);
 | 
					 | 
				
			||||||
        executionContext.setScope(scope);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.lookupSymbolValue("local"), is(T));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void lookupGlobalVariable() {
 | 
					 | 
				
			||||||
        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        executionContext.getScope().put("global", T);
 | 
					 | 
				
			||||||
        executionContext.setScope(scope);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.lookupSymbolValue("global"), is(T));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void lookupShadowedVariable() {
 | 
					 | 
				
			||||||
        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        scope.put("shadowed", NIL);
 | 
					 | 
				
			||||||
        executionContext.getScope().put("shadowed", T);
 | 
					 | 
				
			||||||
        executionContext.setScope(scope);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.lookupSymbolValue("shadowed"), is(NIL));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Test
 | 
					 | 
				
			||||||
    public void restoreGlobalContext() {
 | 
					 | 
				
			||||||
        SymbolTable global = executionContext.getScope();
 | 
					 | 
				
			||||||
        SymbolTable scope1 = new SymbolTable(global);
 | 
					 | 
				
			||||||
        SymbolTable scope2 = new SymbolTable(scope1);
 | 
					 | 
				
			||||||
        SymbolTable scope3 = new SymbolTable(scope2);
 | 
					 | 
				
			||||||
        executionContext.setScope(scope3);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope().isGlobal(), is(false));
 | 
					 | 
				
			||||||
        executionContext.restoreGlobalScope();
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope().isGlobal(), is(true));
 | 
					 | 
				
			||||||
        assertThat(executionContext.getScope(), is(global));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										97
									
								
								src/test/java/table/ExecutionContextTest.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/test/java/table/ExecutionContextTest.kt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,97 @@
 | 
				
			|||||||
 | 
					package table
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.assertj.core.api.Assertions.assertThat
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.AfterEach
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.BeforeEach
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.Test
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.TestInstance
 | 
				
			||||||
 | 
					import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
 | 
				
			||||||
 | 
					import sexpression.Nil.NIL
 | 
				
			||||||
 | 
					import sexpression.Symbol.T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@TestInstance(PER_CLASS)
 | 
				
			||||||
 | 
					class ExecutionContextTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private val executionContext: ExecutionContext = ExecutionContext.getInstance()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @BeforeEach
 | 
				
			||||||
 | 
					    fun setUp() {
 | 
				
			||||||
 | 
					        executionContext.clearContext()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AfterEach
 | 
				
			||||||
 | 
					    fun tearDown() {
 | 
				
			||||||
 | 
					        executionContext.clearContext()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `assign a new scope`() {
 | 
				
			||||||
 | 
					        val scope = SymbolTable()
 | 
				
			||||||
 | 
					        executionContext.scope = scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope).isEqualTo(scope)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `clear the context`() {
 | 
				
			||||||
 | 
					        val scope = SymbolTable()
 | 
				
			||||||
 | 
					        executionContext.scope = scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope).isEqualTo(scope)
 | 
				
			||||||
 | 
					        executionContext.clearContext()
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope).isNotEqualTo(scope)
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope.parent).isNull()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `lookup a variable`() {
 | 
				
			||||||
 | 
					        executionContext.scope.put("test", T)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.lookupSymbolValue("test")).isEqualTo(T)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `lookup a local variable`() {
 | 
				
			||||||
 | 
					        val scope = SymbolTable(executionContext.scope)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        scope.put("local", T)
 | 
				
			||||||
 | 
					        executionContext.scope = scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.lookupSymbolValue("local")).isEqualTo(T)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `lookup a global variable`() {
 | 
				
			||||||
 | 
					        val scope = SymbolTable(executionContext.scope)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        executionContext.scope.put("global", T)
 | 
				
			||||||
 | 
					        executionContext.scope = scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.lookupSymbolValue("global")).isEqualTo(T)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `lookup a shadowed variable`() {
 | 
				
			||||||
 | 
					        val scope = SymbolTable(executionContext.scope)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        scope.put("shadowed", NIL)
 | 
				
			||||||
 | 
					        executionContext.scope.put("shadowed", T)
 | 
				
			||||||
 | 
					        executionContext.scope = scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.lookupSymbolValue("shadowed")).isEqualTo(NIL)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    fun `restore the global context`() {
 | 
				
			||||||
 | 
					        val global = executionContext.scope
 | 
				
			||||||
 | 
					        val scope1 = SymbolTable(global)
 | 
				
			||||||
 | 
					        val scope2 = SymbolTable(scope1)
 | 
				
			||||||
 | 
					        val scope3 = SymbolTable(scope2)
 | 
				
			||||||
 | 
					        executionContext.scope = scope3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope.isGlobal).isFalse()
 | 
				
			||||||
 | 
					        executionContext.restoreGlobalScope()
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope.isGlobal).isTrue()
 | 
				
			||||||
 | 
					        assertThat(executionContext.scope).isEqualTo(global)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -43,7 +43,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @BeforeEach
 | 
					    @BeforeEach
 | 
				
			||||||
    fun initialize() {
 | 
					    fun setUp() {
 | 
				
			||||||
        resetFunctionTable()
 | 
					        resetFunctionTable()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -53,27 +53,27 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun builtInFunctionIsDefined() {
 | 
					    fun `built-in function is defined`() {
 | 
				
			||||||
        assertThat(isAlreadyDefined("CONS")).isTrue()
 | 
					        assertThat(isAlreadyDefined("CONS")).isTrue()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun undefinedFunctionIsNotDefined() {
 | 
					    fun `undefined function is not defined`() {
 | 
				
			||||||
        assertThat(isAlreadyDefined("undefined")).isFalse()
 | 
					        assertThat(isAlreadyDefined("undefined")).isFalse()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun lookupBuiltInFunction_ReturnsFunction() {
 | 
					    fun `lookup a built-in function`() {
 | 
				
			||||||
        assertThat(lookupFunction("CONS")).isNotNull()
 | 
					        assertThat(lookupFunction("CONS")).isNotNull()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun lookupUndefinedFunction_ReturnsNull() {
 | 
					    fun `look up an undefined function`() {
 | 
				
			||||||
        assertThat(lookupFunction("undefined")).isNull()
 | 
					        assertThat(lookupFunction("undefined")).isNull()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun defineFunctionWorks() {
 | 
					    fun `function is defined`() {
 | 
				
			||||||
        val functionName = "testFunction"
 | 
					        val functionName = "testFunction"
 | 
				
			||||||
        val testFunction = createLispFunction()
 | 
					        val testFunction = createLispFunction()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,7 +87,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun resetFunctionTableWorks() {
 | 
					    fun `function table is reset`() {
 | 
				
			||||||
        val functionName = "testFunction"
 | 
					        val functionName = "testFunction"
 | 
				
			||||||
        val testFunction = createLispFunction()
 | 
					        val testFunction = createLispFunction()
 | 
				
			||||||
        defineFunction(functionName, testFunction)
 | 
					        defineFunction(functionName, testFunction)
 | 
				
			||||||
@ -99,7 +99,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun resetWithCustomBuitIns() {
 | 
					    fun `reset function table with custom built-ins`() {
 | 
				
			||||||
        val goodBuiltIns = HashSet<Class<out LispFunction>>()
 | 
					        val goodBuiltIns = HashSet<Class<out LispFunction>>()
 | 
				
			||||||
        goodBuiltIns.add(GoodFunction::class.java)
 | 
					        goodBuiltIns.add(GoodFunction::class.java)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -110,7 +110,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun unableToInitializeBuiltIn() {
 | 
					    fun `unable to initialize a built-in function`() {
 | 
				
			||||||
        val badBuiltIns = HashSet<Class<out LispFunction>>()
 | 
					        val badBuiltIns = HashSet<Class<out LispFunction>>()
 | 
				
			||||||
        badBuiltIns.add(BadFunction::class.java)
 | 
					        badBuiltIns.add(BadFunction::class.java)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -118,7 +118,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun lispFunctionInstantiationException_HasCorrectAttributes() {
 | 
					    fun `lispFunctionInstantiationException is cool`() {
 | 
				
			||||||
        val e = LispFunctionInstantiationException("Bad")
 | 
					        val e = LispFunctionInstantiationException("Bad")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assertThat(e.message).isNotEmpty()
 | 
					        assertThat(e.message).isNotEmpty()
 | 
				
			||||||
@ -126,7 +126,7 @@ class FunctionTableTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun namelessBuiltIn_DoesNotCauseNPE() {
 | 
					    fun `built-in without a name doesn't throw an exception`() {
 | 
				
			||||||
        val namelessBuiltins = HashSet<Class<out LispFunction>>()
 | 
					        val namelessBuiltins = HashSet<Class<out LispFunction>>()
 | 
				
			||||||
        namelessBuiltins.add(UglyFunction::class.java)
 | 
					        namelessBuiltins.add(UglyFunction::class.java)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,7 @@ class SymbolTableTest {
 | 
				
			|||||||
    private lateinit var symbolTable: SymbolTable
 | 
					    private lateinit var symbolTable: SymbolTable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @BeforeEach
 | 
					    @BeforeEach
 | 
				
			||||||
    fun initialize() {
 | 
					    fun setUp() {
 | 
				
			||||||
        symbolTable = SymbolTable()
 | 
					        symbolTable = SymbolTable()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user