From ae48d9c2ca32c5d729de45b007fa5f7664c0bad6 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 30 Sep 2018 17:32:01 -0400 Subject: [PATCH] Convert argument validator tests to kotlin --- .../function/ArgumentValidatorTest.java | 223 ---------------- .../kotlin/function/ArgumentValidatorTest.kt | 240 ++++++++++++++++++ 2 files changed, 240 insertions(+), 223 deletions(-) delete mode 100644 src/test/kotlin/function/ArgumentValidatorTest.java create mode 100644 src/test/kotlin/function/ArgumentValidatorTest.kt diff --git a/src/test/kotlin/function/ArgumentValidatorTest.java b/src/test/kotlin/function/ArgumentValidatorTest.java deleted file mode 100644 index 2aefbb0..0000000 --- a/src/test/kotlin/function/ArgumentValidatorTest.java +++ /dev/null @@ -1,223 +0,0 @@ -package function; - -import function.ArgumentValidator.BadArgumentTypeException; -import function.ArgumentValidator.DottedArgumentListException; -import function.ArgumentValidator.TooFewArgumentsException; -import function.ArgumentValidator.TooManyArgumentsException; -import org.junit.Before; -import org.junit.Test; -import sexpression.Cons; -import sexpression.LispString; -import sexpression.Nil; -import sexpression.SExpression; -import sexpression.Symbol; - -import static error.Severity.ERROR; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class ArgumentValidatorTest { - - private ArgumentValidator validator; - - private Cons makeArgumentListOfSize(int size) { - Cons argumentList = Nil.INSTANCE; - - for (int i = 0; i < size; i++) - argumentList = new Cons(Nil.INSTANCE, argumentList); - - return argumentList; - } - - @Before - public void setUp() { - validator = new ArgumentValidator("TEST"); - } - - @Test - public void noConstraints_DoesNotThrowExceptionWithNoArguments() { - validator.validate(makeArgumentListOfSize(0)); - } - - @Test - public void noConstraints_DoesNotThrowExceptionWithOneArgument() { - validator.validate(makeArgumentListOfSize(1)); - } - - @Test - public void noConstraints_DoesNotThrowExceptionWithManyArguments() { - validator.validate(makeArgumentListOfSize(20)); - } - - @Test(expected = TooFewArgumentsException.class) - public void tooFewArgumentsWithMinimumSet_ThrowsException() { - validator.setMinimumNumberOfArguments(1); - validator.validate(makeArgumentListOfSize(0)); - } - - @Test(expected = TooManyArgumentsException.class) - public void tooManyArgumentsWithMaximumSet_ThrowsException() { - validator.setMaximumNumberOfArguments(1); - validator.validate(makeArgumentListOfSize(2)); - } - - @Test - public void exactNumberOfArguments_DoesNotThrowException() { - validator.setExactNumberOfArguments(5); - validator.validate(makeArgumentListOfSize(5)); - } - - @Test(expected = TooFewArgumentsException.class) - public void tooFewArgumentsWithExactSet_ThrowsException() { - validator.setExactNumberOfArguments(3); - validator.validate(makeArgumentListOfSize(2)); - } - - @Test(expected = TooManyArgumentsException.class) - public void tooManyArgumentsWithExactSet_ThrowsException() { - validator.setExactNumberOfArguments(3); - validator.validate(makeArgumentListOfSize(4)); - } - - @Test - public void tooManyArgumentsException_HasCorrectAttributes() { - TooManyArgumentsException e = new TooManyArgumentsException("TEST", Nil.INSTANCE); - - assertEquals(ERROR, e.getSeverity()); - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - } - - @Test - public void tooFewArgumentsException_HasCorrectAttributes() { - TooFewArgumentsException e = new TooFewArgumentsException("TEST", Nil.INSTANCE); - - assertEquals(ERROR, e.getSeverity()); - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - } - - @Test - public void badArgumentTypeException_HasCorrectAttributes() { - BadArgumentTypeException e = new BadArgumentTypeException("TEST", Nil.INSTANCE, SExpression.class); - - assertEquals(ERROR, e.getSeverity()); - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - } - - @Test - public void correctArgumentType_DoesNotThrowException() { - validator.setEveryArgumentExpectedType(Nil.class); - validator.validate(makeArgumentListOfSize(1)); - } - - @Test(expected = BadArgumentTypeException.class) - public void badArgumentType_ThrowsException() { - validator.setEveryArgumentExpectedType(LispString.class); - validator.validate(makeArgumentListOfSize(1)); - } - - @Test - public void correctFirstAndRestArgumentTypes_DoesNotThrowException() { - Cons argumentList = new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE)); - - validator.setFirstArgumentExpectedType(Symbol.class); - validator.setTrailingArgumentExpectedType(Cons.class); - validator.validate(argumentList); - } - - @Test(expected = BadArgumentTypeException.class) - public void badFirstArgumentType_ThrowsException() { - Cons argumentList = new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE)); - - validator.setFirstArgumentExpectedType(Cons.class); - validator.setTrailingArgumentExpectedType(Cons.class); - validator.validate(argumentList); - } - - @Test(expected = BadArgumentTypeException.class) - public void badTrailingArgumentType_ThrowsException() { - Cons argumentList = new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE)); - - validator.setFirstArgumentExpectedType(Symbol.class); - validator.setTrailingArgumentExpectedType(Symbol.class); - validator.validate(argumentList); - } - - @Test - public void expectedTypeWithNoDisplayName_DoesNotCauseNPE() { - Cons argumentList = new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE)); - SExpression withoutDisplayName = new SExpression() {}; - - validator.setEveryArgumentExpectedType(withoutDisplayName.getClass()); - - try { - validator.validate(argumentList); - } catch (BadArgumentTypeException e) { - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - } - } - - @Test(expected = DottedArgumentListException.class) - public void givenDottedArgumentList_ThrowsException() { - Cons argumentList = new Cons(Symbol.Companion.getT(), Symbol.Companion.getT()); - - validator.validate(argumentList); - } - - @Test(expected = DottedArgumentListException.class) - public void givenLargeDottedArgumentList_ThrowsException() { - Cons argumentList = - new Cons(Symbol.Companion.getT(), new Cons(Symbol.Companion.getT(), Symbol.Companion.getT())); - - validator.validate(argumentList); - } - - @Test - public void dottedArgumentListException_HasCorrectAttributes() { - DottedArgumentListException e = new DottedArgumentListException("TEST", Nil.INSTANCE); - - assertEquals(ERROR, e.getSeverity()); - assertNotNull(e.getMessage()); - assertTrue(e.getMessage().length() > 0); - } - - @Test - public void excludedFirstArgumentType_DoesNotAffectTrailingArguments() { - validator.setFirstArgumentExcludedType(Nil.class); - validator.validate(new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE))); - } - - @Test - public void excludedTrailingArgumentType_DoesNotAffectFirstArgument() { - validator.setTrailingArgumentExcludedType(Nil.class); - validator.validate(new Cons(Nil.INSTANCE, new Cons(Symbol.Companion.getT(), Nil.INSTANCE))); - } - - @Test(expected = BadArgumentTypeException.class) - public void excludedFirstArgumentType_ThrowsException() { - validator.setFirstArgumentExcludedType(Nil.class); - validator.validate(new Cons(Nil.INSTANCE, new Cons(Symbol.Companion.getT(), Nil.INSTANCE))); - } - - @Test(expected = BadArgumentTypeException.class) - public void excludedTrailingArgumentType_ThrowsException() { - validator.setTrailingArgumentExcludedType(Nil.class); - validator.validate(new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE))); - } - - @Test(expected = BadArgumentTypeException.class) - public void excludedArgumentType_ThrowsExceptionOnFirstArgument() { - validator.setEveryArgumentExcludedType(Nil.class); - validator.validate(new Cons(Nil.INSTANCE, new Cons(Symbol.Companion.getT(), Nil.INSTANCE))); - } - - @Test(expected = BadArgumentTypeException.class) - public void excludedArgumentType_ThrowsExceptionOnTrailingArgument() { - validator.setEveryArgumentExcludedType(Nil.class); - validator.validate(new Cons(Symbol.Companion.getT(), new Cons(Nil.INSTANCE, Nil.INSTANCE))); - } -} diff --git a/src/test/kotlin/function/ArgumentValidatorTest.kt b/src/test/kotlin/function/ArgumentValidatorTest.kt new file mode 100644 index 0000000..43060bf --- /dev/null +++ b/src/test/kotlin/function/ArgumentValidatorTest.kt @@ -0,0 +1,240 @@ +package function + +import function.ArgumentValidator.BadArgumentTypeException +import function.ArgumentValidator.DottedArgumentListException +import function.ArgumentValidator.TooFewArgumentsException +import function.ArgumentValidator.TooManyArgumentsException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import sexpression.Cons +import sexpression.LispString +import sexpression.Nil +import sexpression.SExpression +import sexpression.Symbol +import sexpression.Symbol.Companion.T +import testutil.TestUtilities.assertIsErrorWithMessage + +class ArgumentValidatorTest { + + companion object { + private const val FUNCTION_NAME = "TEST" + } + + private lateinit var validator: ArgumentValidator + + private fun makeArgumentListOfSize(size: Int) = + (0 until size).fold(Nil as Cons) { list, _ -> Cons(Nil, list) } + + @BeforeEach + fun setUp() { + validator = ArgumentValidator(FUNCTION_NAME) + } + + @Test + fun noConstraints_DoesNotThrowExceptionWithNoArguments() { + validator.validate(makeArgumentListOfSize(0)) + } + + @Test + fun noConstraints_DoesNotThrowExceptionWithOneArgument() { + validator.validate(makeArgumentListOfSize(1)) + } + + @Test + fun noConstraints_DoesNotThrowExceptionWithManyArguments() { + validator.validate(makeArgumentListOfSize(20)) + } + + @Test + fun tooFewArgumentsWithMinimumSet_ThrowsException() { + validator.setMinimumNumberOfArguments(1) + + assertThrows(TooFewArgumentsException::class.java) { + validator.validate(makeArgumentListOfSize(0)) + } + } + + @Test + fun tooManyArgumentsWithMaximumSet_ThrowsException() { + validator.setMaximumNumberOfArguments(1) + + assertThrows(TooManyArgumentsException::class.java) { + validator.validate(makeArgumentListOfSize(2)) + } + } + + @Test + fun exactNumberOfArguments_DoesNotThrowException() { + validator.setExactNumberOfArguments(5) + validator.validate(makeArgumentListOfSize(5)) + } + + @Test + fun tooFewArgumentsWithExactSet_ThrowsException() { + validator.setExactNumberOfArguments(3) + + assertThrows(TooFewArgumentsException::class.java) { + validator.validate(makeArgumentListOfSize(2)) + } + } + + @Test + fun tooManyArgumentsWithExactSet_ThrowsException() { + validator.setExactNumberOfArguments(3) + + assertThrows(TooManyArgumentsException::class.java) { + validator.validate(makeArgumentListOfSize(4)) + } + } + + @Test + fun tooManyArgumentsException_HasCorrectAttributes() { + assertIsErrorWithMessage(TooManyArgumentsException(FUNCTION_NAME, Nil)) + } + + @Test + fun tooFewArgumentsException_HasCorrectAttributes() { + assertIsErrorWithMessage(TooFewArgumentsException(FUNCTION_NAME, Nil)) + } + + @Test + fun badArgumentTypeException_HasCorrectAttributes() { + assertIsErrorWithMessage(BadArgumentTypeException(FUNCTION_NAME, Nil, SExpression::class.java)) + } + + @Test + fun correctArgumentType_DoesNotThrowException() { + validator.setEveryArgumentExpectedType(Nil::class.java) + validator.validate(makeArgumentListOfSize(1)) + } + + @Test + fun badArgumentType_ThrowsException() { + validator.setEveryArgumentExpectedType(LispString::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(makeArgumentListOfSize(1)) + } + } + + @Test + fun correctFirstAndRestArgumentTypes_DoesNotThrowException() { + val argumentList = Cons(T, Cons(Nil, Nil)) + + validator.setFirstArgumentExpectedType(Symbol::class.java) + validator.setTrailingArgumentExpectedType(Cons::class.java) + validator.validate(argumentList) + } + + @Test + fun badFirstArgumentType_ThrowsException() { + val argumentList = Cons(T, Cons(Nil, Nil)) + + validator.setFirstArgumentExpectedType(Cons::class.java) + validator.setTrailingArgumentExpectedType(Cons::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(argumentList) + } + } + + @Test + fun badTrailingArgumentType_ThrowsException() { + val argumentList = Cons(T, Cons(Nil, Nil)) + + validator.setFirstArgumentExpectedType(Symbol::class.java) + validator.setTrailingArgumentExpectedType(Symbol::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(argumentList) + } + } + + @Test + fun expectedTypeWithNoDisplayName_DoesNotCauseNPE() { + val argumentList = Cons(T, Cons(Nil, Nil)) + val withoutDisplayName = object : SExpression() { + + } + + validator.setEveryArgumentExpectedType(withoutDisplayName.javaClass) + + try { + validator.validate(argumentList) + } catch (e: BadArgumentTypeException) { + assertIsErrorWithMessage(e) + } + } + + @Test + fun givenDottedArgumentList_ThrowsException() { + val argumentList = Cons(T, T) + + assertThrows(DottedArgumentListException::class.java) { + validator.validate(argumentList) + } + } + + @Test + fun givenLargeDottedArgumentList_ThrowsException() { + val argumentList = Cons(T, Cons(T, T)) + + assertThrows(DottedArgumentListException::class.java) { + validator.validate(argumentList) + } + } + + @Test + fun dottedArgumentListException_HasCorrectAttributes() { + assertIsErrorWithMessage(DottedArgumentListException(FUNCTION_NAME, Nil)) + } + + @Test + fun excludedFirstArgumentType_DoesNotAffectTrailingArguments() { + validator.setFirstArgumentExcludedType(Nil::class.java) + validator.validate(Cons(T, Cons(Nil, Nil))) + } + + @Test + fun excludedTrailingArgumentType_DoesNotAffectFirstArgument() { + validator.setTrailingArgumentExcludedType(Nil::class.java) + validator.validate(Cons(Nil, Cons(T, Nil))) + } + + @Test + fun excludedFirstArgumentType_ThrowsException() { + validator.setFirstArgumentExcludedType(Nil::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(Cons(Nil, Cons(T, Nil))) + } + } + + @Test + fun excludedTrailingArgumentType_ThrowsException() { + validator.setTrailingArgumentExcludedType(Nil::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(Cons(T, Cons(Nil, Nil))) + } + } + + @Test + fun excludedArgumentType_ThrowsExceptionOnFirstArgument() { + validator.setEveryArgumentExcludedType(Nil::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(Cons(Nil, Cons(T, Nil))) + } + } + + @Test + fun excludedArgumentType_ThrowsExceptionOnTrailingArgument() { + validator.setEveryArgumentExcludedType(Nil::class.java) + + assertThrows(BadArgumentTypeException::class.java) { + validator.validate(Cons(T, Cons(Nil, Nil))) + } + } +}