Cleaned up the unit tests

This commit is contained in:
Mike Cifelli 2016-12-12 14:57:34 -05:00
parent 6b6f349c29
commit 1596503936
6 changed files with 135 additions and 140 deletions

View File

@ -8,10 +8,18 @@ import org.junit.Test;
public class FilePositionTrackerTester { public class FilePositionTrackerTester {
public static final String FILE_NAME = "testFileName"; public static final String FILE_NAME = "testFile";
private FilePositionTracker trackerUnderTest; private FilePositionTracker trackerUnderTest;
private FilePosition createFilePosition(int lineNumber, int columnNumber) {
FilePosition position = new FilePosition(FILE_NAME);
position.setLineNumber(lineNumber);
position.setColumnNumber(columnNumber);
return position;
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
trackerUnderTest = new FilePositionTracker(FILE_NAME); trackerUnderTest = new FilePositionTracker(FILE_NAME);
@ -89,12 +97,4 @@ public class FilePositionTrackerTester {
assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition())); assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition()));
} }
private FilePosition createFilePosition(int lineNumber, int columnNumber) {
FilePosition position = new FilePosition(FILE_NAME);
position.setLineNumber(lineNumber);
position.setColumnNumber(columnNumber);
return position;
}
} }

View File

@ -5,7 +5,6 @@ import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import error.ErrorManager; import error.ErrorManager;
@ -15,11 +14,31 @@ import testutils.TestUtilities;
public class LispCommentRemovingInputStreamTester { public class LispCommentRemovingInputStreamTester {
private StringBuilder charactersRead; private String getLispCommentRemovingInputStreamResult(String inputString) {
return readInputStreamIntoString(createLispInputStream(inputString));
}
@Before private LispInputStream createLispInputStream(String inputString) {
public void setUp() { InputStream stringInputStream = TestUtilities.createInputStreamFromString(inputString);
charactersRead = new StringBuilder();
return new LispCommentRemovingInputStream(stringInputStream);
}
private String readInputStreamIntoString(LispInputStream inputStream) {
StringBuilder charactersRead = new StringBuilder();
for (int c = inputStream.read(); c != -1; c = inputStream.read())
charactersRead.append((char) c);
return charactersRead.toString();
}
private InputStream createIOExceptionThrowingInputStream() {
return new InputStream() {
public int read() throws IOException {
throw new IOException("test IOException");
}
};
} }
@Test @Test
@ -218,34 +237,4 @@ public class LispCommentRemovingInputStreamTester {
} }
} }
private String getLispCommentRemovingInputStreamResult(String inputString) {
return readInputStreamIntoString(createLispInputStream(inputString));
}
private LispInputStream createLispInputStream(String inputString) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(inputString);
return new LispCommentRemovingInputStream(stringInputStream);
}
private String readInputStreamIntoString(LispInputStream inputStream) {
int c = inputStream.read();
while (c != -1) {
charactersRead.append((char) c);
c = inputStream.read();
}
return charactersRead.toString();
}
private InputStream createIOExceptionThrowingInputStream() {
return new InputStream() {
public int read() throws IOException {
throw new IOException("test IOException");
}
};
}
} }

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue;
import java.io.InputStream; import java.io.InputStream;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import testutils.TestUtilities; import testutils.TestUtilities;
@ -12,8 +11,32 @@ import token.Token;
public class LispScannerLineColumnTester { public class LispScannerLineColumnTester {
@Before private static class LineColumn {
public void setUp() throws Exception {
private int line;
private int column;
public static LineColumn create(int line, int column) {
LineColumn lineColumn = new LineColumn();
lineColumn.line = line;
lineColumn.column = column;
return lineColumn;
}
public boolean isEqual(Token token) {
return (this.line == token.getLine()) && (this.column == token.getColumn());
}
}
private void assertTokenLineAndColumnsMatch(String input, LineColumn[] expectedLineColumnList) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "testFile");
for (LineColumn lineColumn : expectedLineColumnList) {
Token nextToken = lispScanner.getNextToken();
assertTrue(lineColumn.isEqual(nextToken));
}
} }
@Test @Test
@ -111,32 +134,4 @@ public class LispScannerLineColumnTester {
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
} }
private void assertTokenLineAndColumnsMatch(String input, LineColumn[] expectedLineColumnList) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
for (LineColumn lineColumn : expectedLineColumnList) {
Token nextToken = lispScanner.getNextToken();
assertTrue(lineColumn.isEqual(nextToken));
}
}
private static class LineColumn {
private int line;
private int column;
public static LineColumn create(int line, int column) {
LineColumn lineColumn = new LineColumn();
lineColumn.line = line;
lineColumn.column = column;
return lineColumn;
}
public boolean isEqual(Token token) {
return (this.line == token.getLine()) && (this.column == token.getColumn());
}
}
} }

View File

@ -7,10 +7,35 @@ import java.io.InputStream;
import org.junit.Test; import org.junit.Test;
import testutils.TestUtilities; import testutils.TestUtilities;
import token.Token;
public class LispScannerTextTester { public class LispScannerTextTester {
private void assertTokenTextMatches(String input, String[] expectedTextList) {
LispScanner lispScanner = createLispScanner(input);
for (String expectedText : expectedTextList)
assertEquals(expectedText, lispScanner.getNextToken().getText());
}
private void assertTokenTextMatches(String input, String expectedText) {
LispScanner lispScanner = createLispScanner(input);
assertEquals(expectedText, lispScanner.getNextToken().getText());
}
private LispScanner createLispScanner(String input) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
return new LispScanner(stringInputStream, "testFile");
}
private void assertInputFileNameMatches(String input, String expectedInputFileName) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, expectedInputFileName);
assertEquals(expectedInputFileName, lispScanner.getNextToken().getFileName());
}
@Test @Test
public void givenEmptyStream_RecordsCorrectFileName() { public void givenEmptyStream_RecordsCorrectFileName() {
String input = ""; String input = "";
@ -80,26 +105,4 @@ public class LispScannerTextTester {
assertTokenTextMatches(input, expected); assertTokenTextMatches(input, expected);
} }
private void assertTokenTextMatches(String input, String[] expectedTextList) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
for (String expectedText : expectedTextList)
assertEquals(expectedText, lispScanner.getNextToken().getText());
}
private void assertTokenTextMatches(String input, String expectedText) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
assertEquals(expectedText, lispScanner.getNextToken().getText());
}
private void assertInputFileNameMatches(String input, String expectedInputFileName) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, expectedInputFileName);
assertEquals(expectedInputFileName, lispScanner.getNextToken().getFileName());
}
} }

View File

@ -9,17 +9,25 @@ import org.junit.Test;
import error.ErrorManager; import error.ErrorManager;
import scanner.LispScanner.UnterminatedStringException; import scanner.LispScanner.UnterminatedStringException;
import testutils.TestUtilities; import testutils.TestUtilities;
import token.Token;
import token.Token.Type; import token.Token.Type;
import token.TokenFactory;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.BadCharacterException;
public class LispScannerTypeTester { public class LispScannerTypeTester {
private void assertTokenTypesMatch(String input, Type[] expectedTypeList) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "testFile");
for (Type expectedType : expectedTypeList)
assertEquals(expectedType, lispScanner.getNextToken().getType());
assertEquals(Type.EOF, lispScanner.getNextToken().getType());
}
@Test @Test
public void givenEmptyFile_ReturnsCorrectTypes() { public void givenEmptyFile_ReturnsCorrectTypes() {
String input = ""; String input = "";
Token.Type[] expectedTypes = {}; Type[] expectedTypes = {};
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -27,7 +35,7 @@ public class LispScannerTypeTester {
@Test(expected = BadCharacterException.class) @Test(expected = BadCharacterException.class)
public void givenBadCharacter_ThrowsException() { public void givenBadCharacter_ThrowsException() {
String input = "["; String input = "[";
Token.Type[] expectedTypes = {}; Type[] expectedTypes = {};
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -35,7 +43,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenBadCharacter_ExceptionContainsCorrectSeverity() { public void givenBadCharacter_ExceptionContainsCorrectSeverity() {
String input = "abc\ndef["; String input = "abc\ndef[";
Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER };
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
@ -47,7 +55,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenBadCharacter_ExceptionContainsMessage() { public void givenBadCharacter_ExceptionContainsMessage() {
String input = "abc\ndef["; String input = "abc\ndef[";
Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER };
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
@ -61,7 +69,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenNil_ReturnsCorrectTypes() { public void givenNil_ReturnsCorrectTypes() {
String input = "()"; String input = "()";
Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN }; Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -69,7 +77,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenListOfNumbers_ReturnsCorrectTypes() { public void givenListOfNumbers_ReturnsCorrectTypes() {
String input = "(1 2)"; String input = "(1 2)";
Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.NUMBER, Type.NUMBER, Type.RIGHT_PAREN }; Type[] expectedTypes = { Type.LEFT_PAREN, Type.NUMBER, Type.NUMBER, Type.RIGHT_PAREN };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -77,7 +85,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenString_ReturnsCorrectTypes() { public void givenString_ReturnsCorrectTypes() {
String input = "\"string\""; String input = "\"string\"";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -85,7 +93,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() { public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() {
String input = "\"string \n hi \\\" bye\""; String input = "\"string \n hi \\\" bye\"";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -93,7 +101,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() { public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() {
String input = "\"string \n hi \\\" ; bye\""; String input = "\"string \n hi \\\" ; bye\"";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -101,7 +109,7 @@ public class LispScannerTypeTester {
@Test(expected = UnterminatedStringException.class) @Test(expected = UnterminatedStringException.class)
public void givenUnterminatedString_ThrowsException() { public void givenUnterminatedString_ThrowsException() {
String input = "\"oh no!"; String input = "\"oh no!";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -109,7 +117,7 @@ public class LispScannerTypeTester {
@Test() @Test()
public void givenUnterminatedString_ExceptionHasCorrectSeverity() { public void givenUnterminatedString_ExceptionHasCorrectSeverity() {
String input = "\"oh no!"; String input = "\"oh no!";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
@ -121,7 +129,7 @@ public class LispScannerTypeTester {
@Test() @Test()
public void givenUnterminatedString_ExceptionContainsMessage() { public void givenUnterminatedString_ExceptionContainsMessage() {
String input = "\"oh no!"; String input = "\"oh no!";
Token.Type[] expectedTypes = { Type.STRING }; Type[] expectedTypes = { Type.STRING };
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
@ -135,7 +143,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenIdentifier_ReturnsCorrectTypes() { public void givenIdentifier_ReturnsCorrectTypes() {
String input = "abcdefgHIJKLMNOP1234"; String input = "abcdefgHIJKLMNOP1234";
Token.Type[] expectedTypes = { Type.IDENTIFIER }; Type[] expectedTypes = { Type.IDENTIFIER };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -143,7 +151,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenSingleDigitNumber_ReturnsCorrectTypes() { public void givenSingleDigitNumber_ReturnsCorrectTypes() {
String input = "1"; String input = "1";
Token.Type[] expectedTypes = { Type.NUMBER }; Type[] expectedTypes = { Type.NUMBER };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -151,7 +159,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenMultipleDigitNumber_ReturnsCorrectTypes() { public void givenMultipleDigitNumber_ReturnsCorrectTypes() {
String input = "1234567890"; String input = "1234567890";
Token.Type[] expectedTypes = { Type.NUMBER }; Type[] expectedTypes = { Type.NUMBER };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -159,7 +167,7 @@ public class LispScannerTypeTester {
@Test @Test
public void givenQuote_ReturnsCorrectTypes() { public void givenQuote_ReturnsCorrectTypes() {
String input = "'"; String input = "'";
Token.Type[] expectedTypes = { Type.QUOTE_MARK }; Type[] expectedTypes = { Type.QUOTE_MARK };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -167,8 +175,8 @@ public class LispScannerTypeTester {
@Test @Test
public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() { public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() {
String input = "xxx\"hi\"999()'aaa"; String input = "xxx\"hi\"999()'aaa";
Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN, Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN,
Type.QUOTE_MARK, Type.IDENTIFIER }; Type.QUOTE_MARK, Type.IDENTIFIER };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
@ -176,21 +184,11 @@ public class LispScannerTypeTester {
@Test @Test
public void givenFunctionCall_ReturnsCorrectTypes() { public void givenFunctionCall_ReturnsCorrectTypes() {
String input = "(defun myFunction (x)\n (print x))"; String input = "(defun myFunction (x)\n (print x))";
Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, Type.IDENTIFIER,
Type.IDENTIFIER, Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.RIGHT_PAREN,
Type.IDENTIFIER, Type.RIGHT_PAREN, Type.RIGHT_PAREN }; Type.RIGHT_PAREN };
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input, expectedTypes);
} }
private void assertTokenTypesMatch(String input, Token.Type[] expectedTypeList) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
for (Token.Type expectedType : expectedTypeList)
assertEquals(expectedType, lispScanner.getNextToken().getType());
assertEquals(Token.Type.EOF, lispScanner.getNextToken().getType());
}
} }

View File

@ -6,7 +6,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import file.FilePosition; import file.FilePosition;
import token.Token.Type;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.BadCharacterException;
public class TokenFactoryTester { public class TokenFactoryTester {
@ -22,45 +22,55 @@ public class TokenFactoryTester {
testPosition.setColumnNumber(0); testPosition.setColumnNumber(0);
} }
private Type getCreatedTokenType(String text) {
return tokenFactory.createToken(text, testPosition).getType();
}
@Test @Test
public void testEOFTokenCreation() { public void testEOFTokenCreation() {
assertEquals(Token.Type.EOF, tokenFactory.createEOFToken(testPosition).getType()); assertEquals(Type.EOF, tokenFactory.createEOFToken(testPosition).getType());
}
@Test(expected = RuntimeException.class)
public void testEmptyString_ThrowsException() {
String text = "";
tokenFactory.createToken(text, testPosition);
} }
@Test @Test
public void testLeftParenthesisCreation() { public void testLeftParenthesisCreation() {
String text = "("; String text = "(";
assertEquals(Token.Type.LEFT_PAREN, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.LEFT_PAREN, getCreatedTokenType(text));
} }
@Test @Test
public void testRightParenthesisCreation() { public void testRightParenthesisCreation() {
String text = ")"; String text = ")";
assertEquals(Token.Type.RIGHT_PAREN, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.RIGHT_PAREN, getCreatedTokenType(text));
} }
@Test @Test
public void testQuoteMarkCreation() { public void testQuoteMarkCreation() {
String text = "'"; String text = "'";
assertEquals(Token.Type.QUOTE_MARK, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.QUOTE_MARK, getCreatedTokenType(text));
} }
@Test @Test
public void testNumberCreation() { public void testNumberCreation() {
String text = "987"; String text = "987";
assertEquals(Token.Type.NUMBER, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.NUMBER, getCreatedTokenType(text));
} }
@Test @Test
public void testIdentifierCreation() { public void testIdentifierCreation() {
String text = "identifier"; String text = "identifier";
assertEquals(Token.Type.IDENTIFIER, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.IDENTIFIER, getCreatedTokenType(text));
} }
@Test @Test
public void testStringCreation() { public void testStringCreation() {
String text = "\"string\""; String text = "\"string\"";
assertEquals(Token.Type.STRING, tokenFactory.createToken(text, testPosition).getType()); assertEquals(Type.STRING, getCreatedTokenType(text));
} }
@Test(expected = BadCharacterException.class) @Test(expected = BadCharacterException.class)