diff --git a/src/main/kotlin/application/LispMain.kt b/src/main/kotlin/application/LispMain.kt index f16bdfb..70bcdd5 100644 --- a/src/main/kotlin/application/LispMain.kt +++ b/src/main/kotlin/application/LispMain.kt @@ -96,7 +96,6 @@ class LispMain constructor(var configuration: TerminalConfiguration = TerminalCo val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp") - @JvmStatic fun main(arguments: Array) { val lispMain = LispMain() diff --git a/src/main/kotlin/function/LispFunction.kt b/src/main/kotlin/function/LispFunction.kt index 159bdc3..224d114 100644 --- a/src/main/kotlin/function/LispFunction.kt +++ b/src/main/kotlin/function/LispFunction.kt @@ -5,11 +5,8 @@ import sexpression.SExpression abstract class LispFunction { - open val isArgumentListEvaluated: Boolean - get() = true - - open val isMacro: Boolean - get() = false + open val isArgumentListEvaluated = true + open val isMacro = false abstract fun call(argumentList: Cons): SExpression } diff --git a/src/main/kotlin/function/LispSpecialFunction.kt b/src/main/kotlin/function/LispSpecialFunction.kt index ad55f88..85adad6 100644 --- a/src/main/kotlin/function/LispSpecialFunction.kt +++ b/src/main/kotlin/function/LispSpecialFunction.kt @@ -2,6 +2,5 @@ package function abstract class LispSpecialFunction : LispFunction() { - override val isArgumentListEvaluated: Boolean - get() = false + override val isArgumentListEvaluated = false } diff --git a/src/main/kotlin/function/UserDefinedMacro.kt b/src/main/kotlin/function/UserDefinedMacro.kt index 33671bb..2d1df95 100644 --- a/src/main/kotlin/function/UserDefinedMacro.kt +++ b/src/main/kotlin/function/UserDefinedMacro.kt @@ -2,9 +2,9 @@ package function import sexpression.Cons -class UserDefinedMacro(name: String, lambdaList: Cons, body: Cons) - : UserDefinedSpecialFunction(name, lambdaList, body) { +class UserDefinedMacro(name: String, + lambdaList: Cons, + body: Cons) : UserDefinedSpecialFunction(name, lambdaList, body) { - override val isMacro: Boolean - get() = true + override val isMacro = true } diff --git a/src/main/kotlin/function/UserDefinedSpecialFunction.kt b/src/main/kotlin/function/UserDefinedSpecialFunction.kt index 12d82cc..227c715 100644 --- a/src/main/kotlin/function/UserDefinedSpecialFunction.kt +++ b/src/main/kotlin/function/UserDefinedSpecialFunction.kt @@ -2,9 +2,9 @@ package function import sexpression.Cons -open class UserDefinedSpecialFunction(name: String, lambdaList: Cons, body: Cons) - : UserDefinedFunction(name, lambdaList, body) { +open class UserDefinedSpecialFunction(name: String, + lambdaList: Cons, + body: Cons) : UserDefinedFunction(name, lambdaList, body) { - override val isArgumentListEvaluated: Boolean - get() = false + override val isArgumentListEvaluated = false } diff --git a/src/test/kotlin/function/LispFunctionTest.java b/src/test/kotlin/function/LispFunctionTest.java deleted file mode 100644 index 7ca00c0..0000000 --- a/src/test/kotlin/function/LispFunctionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package function; - -import org.junit.Test; -import sexpression.Cons; -import sexpression.SExpression; - -import static org.junit.Assert.assertTrue; - -public class LispFunctionTest { - - @Test - public void evaluateArguments() { - LispFunction lispFunction = new LispFunction() { - - @Override - public SExpression call(Cons argumentList) { - return null; - } - }; - - assertTrue(lispFunction.isArgumentListEvaluated()); - } -} diff --git a/src/test/kotlin/function/LispFunctionTest.kt b/src/test/kotlin/function/LispFunctionTest.kt new file mode 100644 index 0000000..975f140 --- /dev/null +++ b/src/test/kotlin/function/LispFunctionTest.kt @@ -0,0 +1,18 @@ +package function + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import sexpression.Cons +import sexpression.Nil + +class LispFunctionTest { + + @Test + fun `arguments are evaluated`() { + val lispFunction = object : LispFunction() { + override fun call(argumentList: Cons) = Nil + } + + assertThat(lispFunction.isArgumentListEvaluated).isTrue() + } +} diff --git a/src/test/kotlin/function/LispSpecialFunctionTest.java b/src/test/kotlin/function/LispSpecialFunctionTest.java deleted file mode 100644 index 4e064bd..0000000 --- a/src/test/kotlin/function/LispSpecialFunctionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package function; - -import org.junit.Test; -import sexpression.Cons; -import sexpression.SExpression; - -import static org.junit.Assert.assertFalse; - -public class LispSpecialFunctionTest { - - @Test - public void evaluateArguments() { - LispFunction lispFunction = new LispSpecialFunction() { - - @Override - public SExpression call(Cons argumentList) { - return null; - } - }; - - assertFalse(lispFunction.isArgumentListEvaluated()); - } -} diff --git a/src/test/kotlin/function/LispSpecialFunctionTest.kt b/src/test/kotlin/function/LispSpecialFunctionTest.kt new file mode 100644 index 0000000..6ee4c77 --- /dev/null +++ b/src/test/kotlin/function/LispSpecialFunctionTest.kt @@ -0,0 +1,18 @@ +package function + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import sexpression.Cons +import sexpression.Nil + +class LispSpecialFunctionTest { + + @Test + fun `arguments are not evaluated`() { + val lispFunction = object : LispSpecialFunction() { + override fun call(argumentList: Cons) = Nil + } + + assertThat(lispFunction.isArgumentListEvaluated).isFalse() + } +} diff --git a/src/test/kotlin/function/UserDefinedFunctionTest.java b/src/test/kotlin/function/UserDefinedFunctionTest.java deleted file mode 100644 index 2078e9c..0000000 --- a/src/test/kotlin/function/UserDefinedFunctionTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package function; - -import error.Severity; -import function.ArgumentValidator.TooFewArgumentsException; -import function.ArgumentValidator.TooManyArgumentsException; -import function.UserDefinedFunction.IllegalKeywordRestPositionException; -import org.junit.Test; -import sexpression.Cons; -import sexpression.LispNumber; -import sexpression.Nil; -import sexpression.SExpression; -import sexpression.Symbol; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static testutil.TestUtilities.assertSExpressionsMatch; - -public class UserDefinedFunctionTest { - - private static final String FUNCTION_NAME = "TEST"; - - private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() { - return new UserDefinedFunction(FUNCTION_NAME, - Nil.INSTANCE, new Cons(Nil.INSTANCE, Nil.INSTANCE)); - } - - private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() { - return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.INSTANCE), new Cons(new Symbol("X"), - Nil.INSTANCE)); - } - - @Test - public void nilFunctionCall() { - UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil(); - - assertEquals(Nil.INSTANCE, function.call(Nil.INSTANCE)); - } - - @Test - public void nilFunctionToString() { - UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil(); - Cons expected = new Cons(new Symbol(FUNCTION_NAME), new Cons(Nil.INSTANCE, new Cons(Nil.INSTANCE, - Nil.INSTANCE))); - - assertSExpressionsMatch(expected, function.getLambdaExpression()); - } - - @Test - public void oneArgumentFunction_ReturnsCorrectValue() { - UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); - SExpression argument = new LispNumber("23"); - Cons argumentList = new Cons(argument, Nil.INSTANCE); - - assertSExpressionsMatch(argument, function.call(argumentList)); - } - - @Test(expected = TooManyArgumentsException.class) - public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() { - UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); - SExpression argument = new LispNumber("23"); - Cons argumentList = new Cons(argument, new Cons(argument, Nil.INSTANCE)); - - function.call(argumentList); - } - - @Test(expected = TooFewArgumentsException.class) - public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() { - UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); - - function.call(Nil.INSTANCE); - } - - @Test - public void illegalKeywordRestPositionException_HasCorrectAttributes() { - IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME, - Nil.INSTANCE); - - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - assertEquals(Severity.ERROR, e.getSeverity()); - } -} diff --git a/src/test/kotlin/function/UserDefinedFunctionTest.kt b/src/test/kotlin/function/UserDefinedFunctionTest.kt new file mode 100644 index 0000000..fd4992f --- /dev/null +++ b/src/test/kotlin/function/UserDefinedFunctionTest.kt @@ -0,0 +1,76 @@ +package function + +import function.ArgumentValidator.TooFewArgumentsException +import function.ArgumentValidator.TooManyArgumentsException +import function.UserDefinedFunction.IllegalKeywordRestPositionException +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.Cons +import sexpression.LispNumber +import sexpression.Nil +import sexpression.Symbol +import testutil.TestUtilities.assertIsErrorWithMessage +import testutil.TestUtilities.assertSExpressionsMatch + +class UserDefinedFunctionTest { + + companion object { + private const val FUNCTION_NAME = "TEST" + } + + private fun createNoArgumentFunctionThatReturnsNil() = + UserDefinedFunction(FUNCTION_NAME, Nil, Cons(Nil, Nil)) + + private fun createOneArgumentFunctionThatReturnsArgument() = + UserDefinedFunction(FUNCTION_NAME, Cons(Symbol("X"), Nil), Cons(Symbol("X"), Nil)) + + @Test + fun `call function that returns nil`() { + val function = createNoArgumentFunctionThatReturnsNil() + + assertThat(function.call(Nil)).isEqualTo(Nil) + } + + @Test + fun `function has correct lambda expression`() { + val function = createNoArgumentFunctionThatReturnsNil() + val expected = Cons(Symbol(FUNCTION_NAME), Cons(Nil, Cons(Nil, Nil))) + + assertSExpressionsMatch(expected, function.lambdaExpression) + } + + @Test + fun `function returns correct value`() { + val function = createOneArgumentFunctionThatReturnsArgument() + val argument = LispNumber("23") + val argumentList = Cons(argument, Nil) + + assertSExpressionsMatch(argument, function.call(argumentList)) + } + + @Test + fun `function throws exception with too many arguments`() { + val function = createOneArgumentFunctionThatReturnsArgument() + val argument = LispNumber("23") + val argumentList = Cons(argument, Cons(argument, Nil)) + + assertThrows(TooManyArgumentsException::class.java) { + function.call(argumentList) + } + } + + @Test + fun `function throws exception with too few arguments`() { + val function = createOneArgumentFunctionThatReturnsArgument() + + assertThrows(TooFewArgumentsException::class.java) { + function.call(Nil) + } + } + + @Test + fun `IllegalKeywordRestPositionException is cool`() { + assertIsErrorWithMessage(IllegalKeywordRestPositionException(FUNCTION_NAME, Nil)) + } +}