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