2016-12-12 10:15:20 -05:00
|
|
|
package token;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2017-02-09 12:09:51 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2017-02-06 13:44:35 -05:00
|
|
|
import org.junit.*;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2017-02-09 12:09:51 -05:00
|
|
|
import error.ErrorManager;
|
2016-12-11 15:09:48 -05:00
|
|
|
import file.FilePosition;
|
2017-02-09 12:09:51 -05:00
|
|
|
import token.TokenFactory.*;
|
2016-12-12 10:15:20 -05:00
|
|
|
|
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-13 10:41:14 -05:00
|
|
|
private Token createToken(String text) {
|
|
|
|
return tokenFactory.createToken(text, testPosition);
|
2016-12-12 14:57:34 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void testEOFTokenCreation() {
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof);
|
2016-12-12 14:57:34 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void testLeftParenthesisCreation() {
|
|
|
|
String text = "(";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof LeftParenthesis);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRightParenthesisCreation() {
|
|
|
|
String text = ")";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof RightParenthesis);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testQuoteMarkCreation() {
|
|
|
|
String text = "'";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof QuoteMark);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNumberCreation() {
|
|
|
|
String text = "987";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof Number);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIdentifierCreation() {
|
|
|
|
String text = "identifier";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof Identifier);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStringCreation() {
|
|
|
|
String text = "\"string\"";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof QuotedString);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 12:09:51 -05:00
|
|
|
@Test(expected = EmptyTokenTextException.class)
|
|
|
|
public void testEmptyString_ThrowsException() {
|
|
|
|
createToken("");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void EmptyTokenTextException_ContainsCorrectSeverity() {
|
|
|
|
try {
|
|
|
|
createToken("");
|
|
|
|
} catch (EmptyTokenTextException e) {
|
|
|
|
assertTrue(e.getSeverity() == ErrorManager.CRITICAL_LEVEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void EmptyTokenTextException_ContainsMessage() {
|
|
|
|
try {
|
|
|
|
createToken("");
|
|
|
|
} catch (EmptyTokenTextException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2017-02-09 12:09:51 -05:00
|
|
|
public void testBadCharacter_ThrowsException() {
|
|
|
|
createToken("[abc]");
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|