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-23 11:35:28 -05:00
|
|
|
import static org.hamcrest.Matchers.greaterThan;
|
|
|
|
import static org.hamcrest.Matchers.instanceOf;
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
|
import static org.junit.Assert.assertThat;
|
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-11-23 11:35:28 -05:00
|
|
|
assertThat(tokenFactory.createEofToken(testPosition), instanceOf(Eof.class));
|
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 = "(";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(LeftParenthesis.class));
|
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 = ")";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(RightParenthesis.class));
|
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 = "'";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(QuoteMark.class));
|
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";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Number.class));
|
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";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Number.class));
|
2017-02-27 13:37:56 -05:00
|
|
|
}
|
|
|
|
|
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";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Identifier.class));
|
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";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Identifier.class));
|
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\"";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(QuotedString.class));
|
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();
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(message, is(notNullValue()));
|
|
|
|
assertThat(message.length(), greaterThan(0));
|
|
|
|
assertThat(e.getSeverity(), is(CRITICAL));
|
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-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Backquote.class));
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void commaCreation() {
|
|
|
|
String text = ",";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(Comma.class));
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void atSignCreation() {
|
|
|
|
String text = "@";
|
2017-11-23 11:35:28 -05:00
|
|
|
assertThat(createToken(text), instanceOf(AtSign.class));
|
2017-03-11 15:41:07 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|