From 1596503936a770ade7ae36641e97e1f6c564cf69 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Mon, 12 Dec 2016 14:57:34 -0500 Subject: [PATCH] Cleaned up the unit tests --- test/file/FilePositionTrackerTester.java | 18 +++--- .../LispCommentRemovingInputStreamTester.java | 59 +++++++---------- test/scanner/LispScannerLineColumnTester.java | 57 ++++++++--------- test/scanner/LispScannerTextTester.java | 51 ++++++++------- test/scanner/LispScannerTypeTester.java | 64 +++++++++---------- test/token/TokenFactoryTester.java | 26 +++++--- 6 files changed, 135 insertions(+), 140 deletions(-) diff --git a/test/file/FilePositionTrackerTester.java b/test/file/FilePositionTrackerTester.java index d75af87..5af16f2 100644 --- a/test/file/FilePositionTrackerTester.java +++ b/test/file/FilePositionTrackerTester.java @@ -8,10 +8,18 @@ import org.junit.Test; public class FilePositionTrackerTester { - public static final String FILE_NAME = "testFileName"; + public static final String FILE_NAME = "testFile"; 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 public void setUp() throws Exception { trackerUnderTest = new FilePositionTracker(FILE_NAME); @@ -89,12 +97,4 @@ public class FilePositionTrackerTester { 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; - } - } diff --git a/test/scanner/LispCommentRemovingInputStreamTester.java b/test/scanner/LispCommentRemovingInputStreamTester.java index e157407..10e6667 100644 --- a/test/scanner/LispCommentRemovingInputStreamTester.java +++ b/test/scanner/LispCommentRemovingInputStreamTester.java @@ -5,7 +5,6 @@ import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; -import org.junit.Before; import org.junit.Test; import error.ErrorManager; @@ -15,11 +14,31 @@ import testutils.TestUtilities; public class LispCommentRemovingInputStreamTester { - private StringBuilder charactersRead; + private String getLispCommentRemovingInputStreamResult(String inputString) { + return readInputStreamIntoString(createLispInputStream(inputString)); + } - @Before - public void setUp() { - charactersRead = new StringBuilder(); + private LispInputStream createLispInputStream(String inputString) { + InputStream stringInputStream = TestUtilities.createInputStreamFromString(inputString); + + 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 @@ -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"); - } - }; - } - } diff --git a/test/scanner/LispScannerLineColumnTester.java b/test/scanner/LispScannerLineColumnTester.java index 2a9700c..29d4e81 100644 --- a/test/scanner/LispScannerLineColumnTester.java +++ b/test/scanner/LispScannerLineColumnTester.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue; import java.io.InputStream; -import org.junit.Before; import org.junit.Test; import testutils.TestUtilities; @@ -12,8 +11,32 @@ import token.Token; public class LispScannerLineColumnTester { - @Before - public void setUp() throws Exception { + 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()); + } + } + + 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 @@ -111,32 +134,4 @@ public class LispScannerLineColumnTester { 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()); - } - } - } diff --git a/test/scanner/LispScannerTextTester.java b/test/scanner/LispScannerTextTester.java index d9053ea..967a8a9 100644 --- a/test/scanner/LispScannerTextTester.java +++ b/test/scanner/LispScannerTextTester.java @@ -7,10 +7,35 @@ import java.io.InputStream; import org.junit.Test; import testutils.TestUtilities; -import token.Token; 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 public void givenEmptyStream_RecordsCorrectFileName() { String input = ""; @@ -34,7 +59,7 @@ public class LispScannerTextTester { assertTokenTextMatches(input, expected); } - + @Test public void givenEOF_ReordsCorrectText() { String input = ""; @@ -80,26 +105,4 @@ public class LispScannerTextTester { 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()); - } - } diff --git a/test/scanner/LispScannerTypeTester.java b/test/scanner/LispScannerTypeTester.java index 1f00fc5..e459d18 100644 --- a/test/scanner/LispScannerTypeTester.java +++ b/test/scanner/LispScannerTypeTester.java @@ -9,17 +9,25 @@ import org.junit.Test; import error.ErrorManager; import scanner.LispScanner.UnterminatedStringException; import testutils.TestUtilities; -import token.Token; import token.Token.Type; -import token.TokenFactory; import token.TokenFactory.BadCharacterException; 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 public void givenEmptyFile_ReturnsCorrectTypes() { String input = ""; - Token.Type[] expectedTypes = {}; + Type[] expectedTypes = {}; assertTokenTypesMatch(input, expectedTypes); } @@ -27,7 +35,7 @@ public class LispScannerTypeTester { @Test(expected = BadCharacterException.class) public void givenBadCharacter_ThrowsException() { String input = "["; - Token.Type[] expectedTypes = {}; + Type[] expectedTypes = {}; assertTokenTypesMatch(input, expectedTypes); } @@ -35,7 +43,7 @@ public class LispScannerTypeTester { @Test public void givenBadCharacter_ExceptionContainsCorrectSeverity() { String input = "abc\ndef["; - Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; + Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; try { assertTokenTypesMatch(input, expectedTypes); @@ -47,7 +55,7 @@ public class LispScannerTypeTester { @Test public void givenBadCharacter_ExceptionContainsMessage() { String input = "abc\ndef["; - Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; + Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; try { assertTokenTypesMatch(input, expectedTypes); @@ -61,7 +69,7 @@ public class LispScannerTypeTester { @Test public void givenNil_ReturnsCorrectTypes() { String input = "()"; - Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN }; + Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN }; assertTokenTypesMatch(input, expectedTypes); } @@ -69,7 +77,7 @@ public class LispScannerTypeTester { @Test public void givenListOfNumbers_ReturnsCorrectTypes() { 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); } @@ -77,7 +85,7 @@ public class LispScannerTypeTester { @Test public void givenString_ReturnsCorrectTypes() { String input = "\"string\""; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; assertTokenTypesMatch(input, expectedTypes); } @@ -85,7 +93,7 @@ public class LispScannerTypeTester { @Test public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() { String input = "\"string \n hi \\\" bye\""; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; assertTokenTypesMatch(input, expectedTypes); } @@ -93,7 +101,7 @@ public class LispScannerTypeTester { @Test public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() { String input = "\"string \n hi \\\" ; bye\""; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; assertTokenTypesMatch(input, expectedTypes); } @@ -101,7 +109,7 @@ public class LispScannerTypeTester { @Test(expected = UnterminatedStringException.class) public void givenUnterminatedString_ThrowsException() { String input = "\"oh no!"; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; assertTokenTypesMatch(input, expectedTypes); } @@ -109,7 +117,7 @@ public class LispScannerTypeTester { @Test() public void givenUnterminatedString_ExceptionHasCorrectSeverity() { String input = "\"oh no!"; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; try { assertTokenTypesMatch(input, expectedTypes); @@ -121,7 +129,7 @@ public class LispScannerTypeTester { @Test() public void givenUnterminatedString_ExceptionContainsMessage() { String input = "\"oh no!"; - Token.Type[] expectedTypes = { Type.STRING }; + Type[] expectedTypes = { Type.STRING }; try { assertTokenTypesMatch(input, expectedTypes); @@ -135,7 +143,7 @@ public class LispScannerTypeTester { @Test public void givenIdentifier_ReturnsCorrectTypes() { String input = "abcdefgHIJKLMNOP1234"; - Token.Type[] expectedTypes = { Type.IDENTIFIER }; + Type[] expectedTypes = { Type.IDENTIFIER }; assertTokenTypesMatch(input, expectedTypes); } @@ -143,7 +151,7 @@ public class LispScannerTypeTester { @Test public void givenSingleDigitNumber_ReturnsCorrectTypes() { String input = "1"; - Token.Type[] expectedTypes = { Type.NUMBER }; + Type[] expectedTypes = { Type.NUMBER }; assertTokenTypesMatch(input, expectedTypes); } @@ -151,7 +159,7 @@ public class LispScannerTypeTester { @Test public void givenMultipleDigitNumber_ReturnsCorrectTypes() { String input = "1234567890"; - Token.Type[] expectedTypes = { Type.NUMBER }; + Type[] expectedTypes = { Type.NUMBER }; assertTokenTypesMatch(input, expectedTypes); } @@ -159,7 +167,7 @@ public class LispScannerTypeTester { @Test public void givenQuote_ReturnsCorrectTypes() { String input = "'"; - Token.Type[] expectedTypes = { Type.QUOTE_MARK }; + Type[] expectedTypes = { Type.QUOTE_MARK }; assertTokenTypesMatch(input, expectedTypes); } @@ -167,8 +175,8 @@ public class LispScannerTypeTester { @Test public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() { String input = "xxx\"hi\"999()'aaa"; - Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN, - Type.QUOTE_MARK, Type.IDENTIFIER }; + Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN, + Type.QUOTE_MARK, Type.IDENTIFIER }; assertTokenTypesMatch(input, expectedTypes); } @@ -176,21 +184,11 @@ public class LispScannerTypeTester { @Test public void givenFunctionCall_ReturnsCorrectTypes() { String input = "(defun myFunction (x)\n (print x))"; - Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, - Type.IDENTIFIER, Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, - Type.IDENTIFIER, Type.RIGHT_PAREN, Type.RIGHT_PAREN }; + Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, Type.IDENTIFIER, + Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.RIGHT_PAREN, + Type.RIGHT_PAREN }; 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()); - } - } diff --git a/test/token/TokenFactoryTester.java b/test/token/TokenFactoryTester.java index 74b9c9a..364d440 100644 --- a/test/token/TokenFactoryTester.java +++ b/test/token/TokenFactoryTester.java @@ -6,7 +6,7 @@ import org.junit.Before; import org.junit.Test; import file.FilePosition; - +import token.Token.Type; import token.TokenFactory.BadCharacterException; public class TokenFactoryTester { @@ -22,45 +22,55 @@ public class TokenFactoryTester { testPosition.setColumnNumber(0); } + private Type getCreatedTokenType(String text) { + return tokenFactory.createToken(text, testPosition).getType(); + } + @Test 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 public void testLeftParenthesisCreation() { String text = "("; - assertEquals(Token.Type.LEFT_PAREN, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.LEFT_PAREN, getCreatedTokenType(text)); } @Test public void testRightParenthesisCreation() { String text = ")"; - assertEquals(Token.Type.RIGHT_PAREN, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.RIGHT_PAREN, getCreatedTokenType(text)); } @Test public void testQuoteMarkCreation() { String text = "'"; - assertEquals(Token.Type.QUOTE_MARK, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.QUOTE_MARK, getCreatedTokenType(text)); } @Test public void testNumberCreation() { String text = "987"; - assertEquals(Token.Type.NUMBER, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.NUMBER, getCreatedTokenType(text)); } @Test public void testIdentifierCreation() { String text = "identifier"; - assertEquals(Token.Type.IDENTIFIER, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.IDENTIFIER, getCreatedTokenType(text)); } @Test public void testStringCreation() { String text = "\"string\""; - assertEquals(Token.Type.STRING, tokenFactory.createToken(text, testPosition).getType()); + assertEquals(Type.STRING, getCreatedTokenType(text)); } @Test(expected = BadCharacterException.class)