transcendental-lisp/test/token/TokenFactoryTest.java

128 lines
3.4 KiB
Java
Raw Normal View History

package token;
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;
2017-11-12 09:42:25 -05:00
import org.junit.Before;
import org.junit.Test;
import file.FilePosition;
2017-11-12 09:42:25 -05:00
import token.TokenFactory.BadCharacterException;
import token.TokenFactory.EmptyTokenTextException;
2017-03-15 13:37:39 -04:00
public class TokenFactoryTest {
private TokenFactory tokenFactory;
private FilePosition testPosition;
@Before
2017-03-24 09:36:44 -04:00
public void setUp() {
tokenFactory = new TokenFactoryImpl();
testPosition = new FilePosition("testFile");
testPosition.setLineNumber(0);
testPosition.setColumnNumber(0);
}
private Token createToken(String text) {
return tokenFactory.createToken(text, testPosition);
2016-12-12 14:57:34 -05:00
}
@Test
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
}
@Test
public void leftParenthesisCreation() {
String text = "(";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(LeftParenthesis.class));
}
@Test
public void rightParenthesisCreation() {
String text = ")";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(RightParenthesis.class));
}
@Test
public void quoteMarkCreation() {
String text = "'";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(QuoteMark.class));
}
@Test
public void numberCreation() {
String text = "987";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(Number.class));
}
@Test
public void prefixedNumberCreation() {
String text = "-987";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(Number.class));
}
@Test
public void identifierCreation() {
String text = "identifier";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(Identifier.class));
}
@Test
public void prefixedIdentifierCreation() {
String text = "-identifier";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(Identifier.class));
}
@Test
public void stringCreation() {
String text = "\"string\"";
2017-11-23 11:35:28 -05:00
assertThat(createToken(text), instanceOf(QuotedString.class));
}
@Test(expected = EmptyTokenTextException.class)
public void emptyString_ThrowsException() {
createToken("");
}
@Test
2017-03-11 15:41:07 -05:00
public void emptyTokenTextException_ContainsCorrectAttributes() {
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));
}
}
@Test(expected = BadCharacterException.class)
public void badCharacter_ThrowsException() {
createToken("[abc]");
}
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
}
}