Convert more tests to kotlin

This commit is contained in:
Mike Cifelli 2018-09-30 14:10:11 -04:00
parent ba6418a977
commit 1a6d888ca9
11 changed files with 123 additions and 145 deletions

View File

@ -96,7 +96,6 @@ class LispMain constructor(var configuration: TerminalConfiguration = TerminalCo
val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp") val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp")
@JvmStatic
fun main(arguments: Array<String>) { fun main(arguments: Array<String>) {
val lispMain = LispMain() val lispMain = LispMain()

View File

@ -5,11 +5,8 @@ import sexpression.SExpression
abstract class LispFunction { abstract class LispFunction {
open val isArgumentListEvaluated: Boolean open val isArgumentListEvaluated = true
get() = true open val isMacro = false
open val isMacro: Boolean
get() = false
abstract fun call(argumentList: Cons): SExpression abstract fun call(argumentList: Cons): SExpression
} }

View File

@ -2,6 +2,5 @@ package function
abstract class LispSpecialFunction : LispFunction() { abstract class LispSpecialFunction : LispFunction() {
override val isArgumentListEvaluated: Boolean override val isArgumentListEvaluated = false
get() = false
} }

View File

@ -2,9 +2,9 @@ package function
import sexpression.Cons import sexpression.Cons
class UserDefinedMacro(name: String, lambdaList: Cons, body: Cons) class UserDefinedMacro(name: String,
: UserDefinedSpecialFunction(name, lambdaList, body) { lambdaList: Cons,
body: Cons) : UserDefinedSpecialFunction(name, lambdaList, body) {
override val isMacro: Boolean override val isMacro = true
get() = true
} }

View File

@ -2,9 +2,9 @@ package function
import sexpression.Cons import sexpression.Cons
open class UserDefinedSpecialFunction(name: String, lambdaList: Cons, body: Cons) open class UserDefinedSpecialFunction(name: String,
: UserDefinedFunction(name, lambdaList, body) { lambdaList: Cons,
body: Cons) : UserDefinedFunction(name, lambdaList, body) {
override val isArgumentListEvaluated: Boolean override val isArgumentListEvaluated = false
get() = false
} }

View File

@ -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());
}
}

View File

@ -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()
}
}

View File

@ -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());
}
}

View File

@ -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()
}
}

View File

@ -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());
}
}

View File

@ -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))
}
}