2016-12-12 10:15:20 -05:00
|
|
|
package token;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.CRITICAL;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
import file.FilePosition;
|
2017-11-12 09:42:25 -05:00
|
|
|
import token.TokenFactory.BadCharacterException;
|
|
|
|
import token.TokenFactory.EmptyTokenTextException;
|
2016-12-12 10:15:20 -05:00
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class TokenFactoryTest {
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
private TokenFactory tokenFactory;
|
|
|
|
private FilePosition testPosition;
|
|
|
|
|
|
|
|
@Before
|
2017-03-24 09:36:44 -04:00
|
|
|
public void setUp() {
|
2016-12-11 15:09:48 -05:00
|
|
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void eofTokenCreation() {
|
2017-02-27 14:10:20 -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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void leftParenthesisCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
String text = "(";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof LeftParenthesis);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void rightParenthesisCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
String text = ")";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof RightParenthesis);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void quoteMarkCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
String text = "'";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof QuoteMark);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void numberCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
String text = "987";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof Number);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 13:37:56 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void prefixedNumberCreation() {
|
2017-02-27 13:37:56 -05:00
|
|
|
String text = "-987";
|
|
|
|
assertTrue(createToken(text) instanceof Number);
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void identifierCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
String text = "identifier";
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTrue(createToken(text) instanceof Identifier);
|
2017-02-27 13:37:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void prefixedIdentifierCreation() {
|
2017-02-27 13:37:56 -05:00
|
|
|
String text = "-identifier";
|
|
|
|
assertTrue(createToken(text) instanceof Identifier);
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void stringCreation() {
|
2016-12-11 15:09:48 -05:00
|
|
|
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)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void emptyString_ThrowsException() {
|
2017-02-09 12:09:51 -05:00
|
|
|
createToken("");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-11 15:41:07 -05:00
|
|
|
public void emptyTokenTextException_ContainsCorrectAttributes() {
|
2017-02-09 12:09:51 -05:00
|
|
|
try {
|
|
|
|
createToken("");
|
|
|
|
} catch (EmptyTokenTextException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
2017-03-11 15:41:07 -05:00
|
|
|
assertEquals(CRITICAL, e.getSeverity());
|
2017-02-09 12:09:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void badCharacter_ThrowsException() {
|
2017-02-09 12:09:51 -05:00
|
|
|
createToken("[abc]");
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
@Test
|
|
|
|
public void backTickCreation() {
|
|
|
|
String text = "`";
|
2017-03-12 12:19:45 -04:00
|
|
|
assertTrue(createToken(text) instanceof Backquote);
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void commaCreation() {
|
|
|
|
String text = ",";
|
|
|
|
assertTrue(createToken(text) instanceof Comma);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void atSignCreation() {
|
|
|
|
String text = "@";
|
|
|
|
assertTrue(createToken(text) instanceof AtSign);
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|