2016-12-12 10:15:20 -05:00
|
|
|
package token;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import file.FilePosition;
|
2016-12-12 14:57:34 -05:00
|
|
|
import token.Token.Type;
|
2016-12-12 10:15:20 -05:00
|
|
|
import token.TokenFactory.BadCharacterException;
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
public class TokenFactoryTester {
|
|
|
|
|
|
|
|
private TokenFactory tokenFactory;
|
|
|
|
private FilePosition testPosition;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
tokenFactory = new TokenFactoryImpl();
|
|
|
|
testPosition = new FilePosition("testFile");
|
|
|
|
testPosition.setLineNumber(0);
|
|
|
|
testPosition.setColumnNumber(0);
|
|
|
|
}
|
|
|
|
|
2016-12-12 14:57:34 -05:00
|
|
|
private Type getCreatedTokenType(String text) {
|
|
|
|
return tokenFactory.createToken(text, testPosition).getType();
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void testEOFTokenCreation() {
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.EOF, tokenFactory.createEOFToken(testPosition).getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = RuntimeException.class)
|
|
|
|
public void testEmptyString_ThrowsException() {
|
|
|
|
String text = "";
|
|
|
|
tokenFactory.createToken(text, testPosition);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLeftParenthesisCreation() {
|
|
|
|
String text = "(";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.LEFT_PAREN, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRightParenthesisCreation() {
|
|
|
|
String text = ")";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.RIGHT_PAREN, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testQuoteMarkCreation() {
|
|
|
|
String text = "'";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.QUOTE_MARK, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNumberCreation() {
|
|
|
|
String text = "987";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.NUMBER, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIdentifierCreation() {
|
|
|
|
String text = "identifier";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.IDENTIFIER, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStringCreation() {
|
|
|
|
String text = "\"string\"";
|
2016-12-12 14:57:34 -05:00
|
|
|
assertEquals(Type.STRING, getCreatedTokenType(text));
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2016-12-11 15:09:48 -05:00
|
|
|
public void testBadCharacter() {
|
|
|
|
String text = "[abc]";
|
|
|
|
tokenFactory.createToken(text, testPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|