Added unit tests

This commit is contained in:
Mike Cifelli 2016-12-19 13:29:31 -05:00
parent 089e3bd520
commit f925993093
2 changed files with 87 additions and 2 deletions

View File

@ -2,10 +2,10 @@ package sexpression;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Before; import org.junit.*;
import org.junit.Test;
import error.ErrorManager; import error.ErrorManager;
import function.UserDefinedFunction;
import sexpression.LispNumber.InvalidNumberException; import sexpression.LispNumber.InvalidNumberException;
public class SExpressionTester { public class SExpressionTester {
@ -78,6 +78,33 @@ public class SExpressionTester {
assertSExpressionMatchesString(expected, list); assertSExpressionMatchesString(expected, list);
} }
@Test
public void testLambdaExpressionToString() {
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()), null);
assertSExpressionMatchesString(expected, lambda);
}
@Test
public void testLambdaExpressionGetLambdaExpression() {
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()), null);
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
}
@Test
public void testLambdaExpressionGetFunction() {
String expected = "(LAMBDA)";
UserDefinedFunction function = new UserDefinedFunction(expected, Nil.getUniqueInstance(),
Nil.getUniqueInstance());
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()),
function);
assertEquals(function, lambda.getFunction());
}
@Test @Test
public void testCarOfNilIsNil() { public void testCarOfNilIsNil() {
assertEquals(Nil.getUniqueInstance().getCar(), Nil.getUniqueInstance()); assertEquals(Nil.getUniqueInstance().getCar(), Nil.getUniqueInstance());

View File

@ -0,0 +1,58 @@
package table;
import static org.junit.Assert.*;
import org.junit.*;
import sexpression.*;
public class SymbolTableTester {
private SymbolTable symbolTable;
@Before
public void setUp() throws Exception {
symbolTable = new SymbolTable();
}
@Test
public void lookupSymbolNotInTable() {
assertFalse(symbolTable.contains("symbol"));
}
@Test
public void lookupSymbolInTable() {
symbolTable.put("symbol", Symbol.T);
assertTrue(symbolTable.contains("symbol"));
}
@Test
public void retrieveSymbolValue() {
symbolTable.put("symbol", Symbol.T);
assertEquals(symbolTable.get("symbol"), Symbol.T);
}
@Test
public void redefineSymbolValue() {
symbolTable.put("symbol", Symbol.T);
symbolTable.put("symbol", Nil.getUniqueInstance());
assertEquals(symbolTable.get("symbol"), Nil.getUniqueInstance());
}
@Test
public void checkParentTableIsCorrect() {
SymbolTable childTable = new SymbolTable(symbolTable);
assertEquals(childTable.getParent(), symbolTable);
}
@Test
public void lookupSymbolInParentTable() {
symbolTable.put("symbol", Symbol.T);
SymbolTable childTable = new SymbolTable(symbolTable);
SymbolTable parentTable = childTable.getParent();
assertEquals(parentTable.get("symbol"), Symbol.T);
}
}