diff --git a/src/test/java/table/ExecutionContextTest.java b/src/test/java/table/ExecutionContextTest.java deleted file mode 100644 index ec950b4..0000000 --- a/src/test/java/table/ExecutionContextTest.java +++ /dev/null @@ -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)); - } -} diff --git a/src/test/java/table/ExecutionContextTest.kt b/src/test/java/table/ExecutionContextTest.kt new file mode 100644 index 0000000..ea99313 --- /dev/null +++ b/src/test/java/table/ExecutionContextTest.kt @@ -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) + } +} diff --git a/src/test/java/table/FunctionTableTest.kt b/src/test/java/table/FunctionTableTest.kt index a2aa680..27ec5dd 100644 --- a/src/test/java/table/FunctionTableTest.kt +++ b/src/test/java/table/FunctionTableTest.kt @@ -43,7 +43,7 @@ class FunctionTableTest { } @BeforeEach - fun initialize() { + fun setUp() { resetFunctionTable() } @@ -53,27 +53,27 @@ class FunctionTableTest { } @Test - fun builtInFunctionIsDefined() { + fun `built-in function is defined`() { assertThat(isAlreadyDefined("CONS")).isTrue() } @Test - fun undefinedFunctionIsNotDefined() { + fun `undefined function is not defined`() { assertThat(isAlreadyDefined("undefined")).isFalse() } @Test - fun lookupBuiltInFunction_ReturnsFunction() { + fun `lookup a built-in function`() { assertThat(lookupFunction("CONS")).isNotNull() } @Test - fun lookupUndefinedFunction_ReturnsNull() { + fun `look up an undefined function`() { assertThat(lookupFunction("undefined")).isNull() } @Test - fun defineFunctionWorks() { + fun `function is defined`() { val functionName = "testFunction" val testFunction = createLispFunction() @@ -87,7 +87,7 @@ class FunctionTableTest { } @Test - fun resetFunctionTableWorks() { + fun `function table is reset`() { val functionName = "testFunction" val testFunction = createLispFunction() defineFunction(functionName, testFunction) @@ -99,7 +99,7 @@ class FunctionTableTest { } @Test - fun resetWithCustomBuitIns() { + fun `reset function table with custom built-ins`() { val goodBuiltIns = HashSet>() goodBuiltIns.add(GoodFunction::class.java) @@ -110,7 +110,7 @@ class FunctionTableTest { } @Test - fun unableToInitializeBuiltIn() { + fun `unable to initialize a built-in function`() { val badBuiltIns = HashSet>() badBuiltIns.add(BadFunction::class.java) @@ -118,7 +118,7 @@ class FunctionTableTest { } @Test - fun lispFunctionInstantiationException_HasCorrectAttributes() { + fun `lispFunctionInstantiationException is cool`() { val e = LispFunctionInstantiationException("Bad") assertThat(e.message).isNotEmpty() @@ -126,7 +126,7 @@ class FunctionTableTest { } @Test - fun namelessBuiltIn_DoesNotCauseNPE() { + fun `built-in without a name doesn't throw an exception`() { val namelessBuiltins = HashSet>() namelessBuiltins.add(UglyFunction::class.java) diff --git a/src/test/java/table/SymbolTableTest.kt b/src/test/java/table/SymbolTableTest.kt index 334fb77..8af0072 100644 --- a/src/test/java/table/SymbolTableTest.kt +++ b/src/test/java/table/SymbolTableTest.kt @@ -14,7 +14,7 @@ class SymbolTableTest { private lateinit var symbolTable: SymbolTable @BeforeEach - fun initialize() { + fun setUp() { symbolTable = SymbolTable() }