Removed references to the Token.Type enum in test cases

This commit is contained in:
Mike Cifelli 2016-12-13 10:41:14 -05:00
parent 1596503936
commit be986ea5cf
2 changed files with 91 additions and 57 deletions

View File

@ -1,52 +1,69 @@
package scanner; package scanner;
import static org.junit.Assert.*; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test; 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.Type; import token.Eof;
import token.Identifier;
import token.LeftParenthesis;
import token.Number;
import token.QuoteMark;
import token.QuotedString;
import token.RightParenthesis;
import token.Token;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.BadCharacterException;
public class LispScannerTypeTester { public class LispScannerTypeTester {
private void assertTokenTypesMatch(String input, Type[] expectedTypeList) { private List<Class<? extends Token>> expectedTypes;
private void assertTokenTypesMatch(String input) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input); InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "testFile"); LispScanner lispScanner = new LispScanner(stringInputStream, "testFile");
for (Type expectedType : expectedTypeList) for (Class<? extends Token> type : expectedTypes)
assertEquals(expectedType, lispScanner.getNextToken().getType()); assertTrue(type.isInstance(lispScanner.getNextToken()));
assertEquals(Type.EOF, lispScanner.getNextToken().getType()); assertTrue(lispScanner.getNextToken() instanceof Eof);
}
@Before
public void setUp() {
expectedTypes = new ArrayList<>();
} }
@Test @Test
public void givenEmptyFile_ReturnsCorrectTypes() { public void givenEmptyFile_ReturnsCorrectTypes() {
String input = ""; String input = "";
Type[] expectedTypes = {};
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test(expected = BadCharacterException.class) @Test(expected = BadCharacterException.class)
public void givenBadCharacter_ThrowsException() { public void givenBadCharacter_ThrowsException() {
String input = "["; String input = "[";
Type[] expectedTypes = {};
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenBadCharacter_ExceptionContainsCorrectSeverity() { public void givenBadCharacter_ExceptionContainsCorrectSeverity() {
String input = "abc\ndef["; String input = "abc\ndef[";
Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; expectedTypes.add(Identifier.class);
expectedTypes.add(Identifier.class);
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} catch (BadCharacterException e) { } catch (BadCharacterException e) {
assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL); assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL);
} }
@ -55,10 +72,11 @@ public class LispScannerTypeTester {
@Test @Test
public void givenBadCharacter_ExceptionContainsMessage() { public void givenBadCharacter_ExceptionContainsMessage() {
String input = "abc\ndef["; String input = "abc\ndef[";
Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER }; expectedTypes.add(Identifier.class);
expectedTypes.add(Identifier.class);
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} catch (BadCharacterException e) { } catch (BadCharacterException e) {
String message = e.getMessage(); String message = e.getMessage();
assertNotNull(message); assertNotNull(message);
@ -69,58 +87,62 @@ public class LispScannerTypeTester {
@Test @Test
public void givenNil_ReturnsCorrectTypes() { public void givenNil_ReturnsCorrectTypes() {
String input = "()"; String input = "()";
Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN }; expectedTypes.add(LeftParenthesis.class);
expectedTypes.add(RightParenthesis.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenListOfNumbers_ReturnsCorrectTypes() { public void givenListOfNumbers_ReturnsCorrectTypes() {
String input = "(1 2)"; String input = "(1 2)";
Type[] expectedTypes = { Type.LEFT_PAREN, Type.NUMBER, Type.NUMBER, Type.RIGHT_PAREN }; expectedTypes.add(LeftParenthesis.class);
expectedTypes.add(Number.class);
expectedTypes.add(Number.class);
expectedTypes.add(RightParenthesis.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenString_ReturnsCorrectTypes() { public void givenString_ReturnsCorrectTypes() {
String input = "\"string\""; String input = "\"string\"";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() { public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() {
String input = "\"string \n hi \\\" bye\""; String input = "\"string \n hi \\\" bye\"";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() { public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() {
String input = "\"string \n hi \\\" ; bye\""; String input = "\"string \n hi \\\" ; bye\"";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test(expected = UnterminatedStringException.class) @Test(expected = UnterminatedStringException.class)
public void givenUnterminatedString_ThrowsException() { public void givenUnterminatedString_ThrowsException() {
String input = "\"oh no!"; String input = "\"oh no!";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test() @Test()
public void givenUnterminatedString_ExceptionHasCorrectSeverity() { public void givenUnterminatedString_ExceptionHasCorrectSeverity() {
String input = "\"oh no!"; String input = "\"oh no!";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} catch (LispScanner.UnterminatedStringException e) { } catch (LispScanner.UnterminatedStringException e) {
assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL); assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL);
} }
@ -129,10 +151,10 @@ public class LispScannerTypeTester {
@Test() @Test()
public void givenUnterminatedString_ExceptionContainsMessage() { public void givenUnterminatedString_ExceptionContainsMessage() {
String input = "\"oh no!"; String input = "\"oh no!";
Type[] expectedTypes = { Type.STRING }; expectedTypes.add(QuotedString.class);
try { try {
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} catch (LispScanner.UnterminatedStringException e) { } catch (LispScanner.UnterminatedStringException e) {
String message = e.getMessage(); String message = e.getMessage();
assertNotNull(message); assertNotNull(message);
@ -143,52 +165,65 @@ public class LispScannerTypeTester {
@Test @Test
public void givenIdentifier_ReturnsCorrectTypes() { public void givenIdentifier_ReturnsCorrectTypes() {
String input = "abcdefgHIJKLMNOP1234"; String input = "abcdefgHIJKLMNOP1234";
Type[] expectedTypes = { Type.IDENTIFIER }; expectedTypes.add(Identifier.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenSingleDigitNumber_ReturnsCorrectTypes() { public void givenSingleDigitNumber_ReturnsCorrectTypes() {
String input = "1"; String input = "1";
Type[] expectedTypes = { Type.NUMBER }; expectedTypes.add(Number.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenMultipleDigitNumber_ReturnsCorrectTypes() { public void givenMultipleDigitNumber_ReturnsCorrectTypes() {
String input = "1234567890"; String input = "1234567890";
Type[] expectedTypes = { Type.NUMBER }; expectedTypes.add(Number.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenQuote_ReturnsCorrectTypes() { public void givenQuote_ReturnsCorrectTypes() {
String input = "'"; String input = "'";
Type[] expectedTypes = { Type.QUOTE_MARK }; expectedTypes.add(QuoteMark.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@Test @Test
public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() { public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() {
String input = "xxx\"hi\"999()'aaa"; String input = "xxx\"hi\"999()'aaa";
Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN, expectedTypes.add(Identifier.class);
Type.QUOTE_MARK, Type.IDENTIFIER }; expectedTypes.add(QuotedString.class);
expectedTypes.add(Number.class);
expectedTypes.add(LeftParenthesis.class);
expectedTypes.add(RightParenthesis.class);
expectedTypes.add(QuoteMark.class);
expectedTypes.add(Identifier.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
@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))";
Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, Type.IDENTIFIER, expectedTypes.add(LeftParenthesis.class);
Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.RIGHT_PAREN, expectedTypes.add(Identifier.class);
Type.RIGHT_PAREN }; expectedTypes.add(Identifier.class);
expectedTypes.add(LeftParenthesis.class);
expectedTypes.add(Identifier.class);
expectedTypes.add(RightParenthesis.class);
expectedTypes.add(LeftParenthesis.class);
expectedTypes.add(Identifier.class);
expectedTypes.add(Identifier.class);
expectedTypes.add(RightParenthesis.class);
expectedTypes.add(RightParenthesis.class);
assertTokenTypesMatch(input, expectedTypes); assertTokenTypesMatch(input);
} }
} }

View File

@ -1,12 +1,11 @@
package token; package token;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue;
import org.junit.Before; 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,55 +21,55 @@ public class TokenFactoryTester {
testPosition.setColumnNumber(0); testPosition.setColumnNumber(0);
} }
private Type getCreatedTokenType(String text) { private Token createToken(String text) {
return tokenFactory.createToken(text, testPosition).getType(); return tokenFactory.createToken(text, testPosition);
} }
@Test @Test
public void testEOFTokenCreation() { public void testEOFTokenCreation() {
assertEquals(Type.EOF, tokenFactory.createEOFToken(testPosition).getType()); assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof);
} }
@Test(expected = RuntimeException.class) @Test(expected = RuntimeException.class)
public void testEmptyString_ThrowsException() { public void testEmptyString_ThrowsException() {
String text = ""; String text = "";
tokenFactory.createToken(text, testPosition); createToken(text);
} }
@Test @Test
public void testLeftParenthesisCreation() { public void testLeftParenthesisCreation() {
String text = "("; String text = "(";
assertEquals(Type.LEFT_PAREN, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof LeftParenthesis);
} }
@Test @Test
public void testRightParenthesisCreation() { public void testRightParenthesisCreation() {
String text = ")"; String text = ")";
assertEquals(Type.RIGHT_PAREN, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof RightParenthesis);
} }
@Test @Test
public void testQuoteMarkCreation() { public void testQuoteMarkCreation() {
String text = "'"; String text = "'";
assertEquals(Type.QUOTE_MARK, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof QuoteMark);
} }
@Test @Test
public void testNumberCreation() { public void testNumberCreation() {
String text = "987"; String text = "987";
assertEquals(Type.NUMBER, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof Number);
} }
@Test @Test
public void testIdentifierCreation() { public void testIdentifierCreation() {
String text = "identifier"; String text = "identifier";
assertEquals(Type.IDENTIFIER, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof Identifier);
} }
@Test @Test
public void testStringCreation() { public void testStringCreation() {
String text = "\"string\""; String text = "\"string\"";
assertEquals(Type.STRING, getCreatedTokenType(text)); assertTrue(createToken(text) instanceof QuotedString);
} }
@Test(expected = BadCharacterException.class) @Test(expected = BadCharacterException.class)