2016-12-19 13:05:53 -05:00
|
|
|
package function;
|
2016-12-19 11:38:14 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2016-12-19 11:38:14 -05:00
|
|
|
import static org.junit.Assert.*;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
|
|
|
import static sexpression.Symbol.T;
|
2016-12-19 11:38:14 -05:00
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
|
2016-12-19 13:05:53 -05:00
|
|
|
import function.ArgumentValidator.*;
|
2016-12-19 11:38:14 -05:00
|
|
|
import sexpression.*;
|
|
|
|
|
|
|
|
public class ArgumentValidatorTester {
|
|
|
|
|
|
|
|
private ArgumentValidator validator;
|
|
|
|
|
|
|
|
private Cons makeArgumentListOfSize(int size) {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = NIL;
|
2016-12-19 11:38:14 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < size; i++)
|
2017-03-02 09:54:23 -05:00
|
|
|
argumentList = new Cons(NIL, argumentList);
|
2016-12-19 11:38:14 -05:00
|
|
|
|
|
|
|
return argumentList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
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
|
2017-03-02 11:39:07 -05:00
|
|
|
public void tooManyArgumentsException_HasCorrectAttributes() {
|
2017-03-02 09:54:23 -05:00
|
|
|
TooManyArgumentsException e = new TooManyArgumentsException("TEST", NIL);
|
2016-12-19 11:38:14 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-19 11:38:14 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 11:39:07 -05:00
|
|
|
public void tooFewArgumentsException_HasCorrectAttributes() {
|
2017-03-02 09:54:23 -05:00
|
|
|
TooFewArgumentsException e = new TooFewArgumentsException("TEST", NIL);
|
2016-12-19 11:38:14 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-19 11:38:14 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 11:39:07 -05:00
|
|
|
public void badArgumentTypeException_HasCorrectAttributes() {
|
2017-03-02 09:54:23 -05:00
|
|
|
BadArgumentTypeException e = new BadArgumentTypeException("TEST", NIL, SExpression.class);
|
2016-12-19 11:38:14 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-19 11:38:14 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void correctArgumentType_DoesNotThrowException() {
|
2016-12-22 16:55:25 -05:00
|
|
|
validator.setEveryArgumentExpectedType(Nil.class);
|
2016-12-19 11:38:14 -05:00
|
|
|
validator.validate(makeArgumentListOfSize(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void badArgumentType_ThrowsException() {
|
2016-12-22 16:55:25 -05:00
|
|
|
validator.setEveryArgumentExpectedType(LispString.class);
|
2016-12-19 11:38:14 -05:00
|
|
|
validator.validate(makeArgumentListOfSize(1));
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:55:25 -05:00
|
|
|
@Test
|
|
|
|
public void correctFirstAndRestArgumentTypes_DoesNotThrowException() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
|
2016-12-22 16:55:25 -05:00
|
|
|
|
|
|
|
validator.setFirstArgumentExpectedType(Symbol.class);
|
|
|
|
validator.setTrailingArgumentExpectedType(Cons.class);
|
|
|
|
validator.validate(argumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void badFirstArgumentType_ThrowsException() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
|
2016-12-22 16:55:25 -05:00
|
|
|
|
|
|
|
validator.setFirstArgumentExpectedType(Cons.class);
|
|
|
|
validator.setTrailingArgumentExpectedType(Cons.class);
|
|
|
|
validator.validate(argumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void badTrailingArgumentType_ThrowsException() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
|
2016-12-22 16:55:25 -05:00
|
|
|
|
|
|
|
validator.setFirstArgumentExpectedType(Symbol.class);
|
|
|
|
validator.setTrailingArgumentExpectedType(Symbol.class);
|
|
|
|
validator.validate(argumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void expectedTypeWithNoDisplayName_DoesNotCauseNPE() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
|
2016-12-22 16:55:25 -05:00
|
|
|
SExpression withoutDisplayName = new SExpression() {};
|
|
|
|
|
|
|
|
validator.setEveryArgumentExpectedType(withoutDisplayName.getClass());
|
|
|
|
|
|
|
|
try {
|
|
|
|
validator.validate(argumentList);
|
|
|
|
} catch (BadArgumentTypeException e) {
|
2016-12-24 13:16:03 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
2016-12-22 16:55:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void givenDottedArgumentList_ThrowsException() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, T);
|
2016-12-22 16:55:25 -05:00
|
|
|
|
|
|
|
validator.validate(argumentList);
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:53:11 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void givenLargeDottedArgumentList_ThrowsException() {
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(T, new Cons(T, T));
|
2016-12-23 10:53:11 -05:00
|
|
|
|
|
|
|
validator.validate(argumentList);
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:16:03 -05:00
|
|
|
@Test
|
2017-03-03 12:07:23 -05:00
|
|
|
public void dottedArgumentListException_HasCorrectAttributes() {
|
2017-03-02 09:54:23 -05:00
|
|
|
DottedArgumentListException e = new DottedArgumentListException("TEST", NIL);
|
2016-12-24 13:16:03 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-22 16:55:25 -05:00
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:39:07 -05:00
|
|
|
@Test
|
|
|
|
public void excludedFirstArgumentType_DoesNotAffectTrailingArguments() {
|
|
|
|
validator.setFirstArgumentExcludedType(Nil.class);
|
2017-03-02 09:54:23 -05:00
|
|
|
validator.validate(new Cons(T, new Cons(NIL, NIL)));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 11:39:07 -05:00
|
|
|
public void excludedTrailingArgumentType_DoesNotAffectFirstArgument() {
|
|
|
|
validator.setTrailingArgumentExcludedType(Nil.class);
|
|
|
|
validator.validate(new Cons(NIL, new Cons(T, NIL)));
|
2016-12-25 12:49:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:39:07 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void excludedFirstArgumentType_ThrowsException() {
|
|
|
|
validator.setFirstArgumentExcludedType(Nil.class);
|
|
|
|
validator.validate(new Cons(NIL, new Cons(T, NIL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void excludedTrailingArgumentType_ThrowsException() {
|
|
|
|
validator.setTrailingArgumentExcludedType(Nil.class);
|
|
|
|
validator.validate(new Cons(T, new Cons(NIL, NIL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void excludedArgumentType_ThrowsExceptionOnFirstArgument() {
|
|
|
|
validator.setEveryArgumentExcludedType(Nil.class);
|
|
|
|
validator.validate(new Cons(NIL, new Cons(T, NIL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
|
|
public void excludedArgumentType_ThrowsExceptionOnTrailingArgument() {
|
|
|
|
validator.setEveryArgumentExcludedType(Nil.class);
|
|
|
|
validator.validate(new Cons(T, new Cons(NIL, NIL)));
|
2016-12-24 13:16:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-19 11:38:14 -05:00
|
|
|
}
|