Refactor unit tests

This commit is contained in:
Mike Cifelli 2017-11-23 11:35:28 -05:00
parent f3923cadc0
commit c4e6de88e1
8 changed files with 161 additions and 155 deletions

View File

@ -1,8 +1,8 @@
package terminal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static terminal.ControlSequenceHandler.isEscape;
import static terminal.SelectGraphicRendition.GREEN;
import static terminal.SelectGraphicRendition.PURPLE;
@ -42,95 +42,95 @@ public class ControlSequenceHandlerTest {
@Test
public void isEscapeDetectsNonEscapeCharacter() {
assertFalse(isEscape('x'));
assertThat(isEscape('x'), is(false));
}
@Test
public void isEscapeDetectsEscapeCharacter() {
assertTrue(isEscape('\u001b'));
assertThat(isEscape('\u001b'), is(true));
}
@Test
public void correctlyParsesControlSequence_LeavesRestOfStreamIntact() {
SafeInputStream input = createSafeInputStream("[32mdata");
handler.parse(input);
assertEquals("data", readRemaining(input));
assertThat(readRemaining(input), is("data"));
}
@Test
public void unterminatedControlSequence_OnlyConsumesFirstNonSequenceCharacter() {
SafeInputStream input = createSafeInputStream("[32data");
handler.parse(input);
assertEquals("ata", readRemaining(input));
assertThat(readRemaining(input), is("ata"));
}
@Test
public void malformedControlSequence_OnlyConsumesOneCharacter() {
SafeInputStream input = createSafeInputStream("32mdata");
handler.parse(input);
assertEquals("2mdata", readRemaining(input));
assertThat(readRemaining(input), is("2mdata"));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF() {
SafeInputStream input = createSafeInputStream("");
assertTrue(handler.parse(input) instanceof NullControlSequence);
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_EOF_AfterFirstCharacter() {
SafeInputStream input = createSafeInputStream("[");
assertTrue(handler.parse(input) instanceof NullControlSequence);
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_UnterminatedControlSequence() {
SafeInputStream input = createSafeInputStream("[data");
assertTrue(handler.parse(input) instanceof NullControlSequence);
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_MalformedControlSequence() {
SafeInputStream input = createSafeInputStream("32mdata");
assertTrue(handler.parse(input) instanceof NullControlSequence);
assertThat(handler.parse(input), instanceOf(NullControlSequence.class));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Reset() {
SafeInputStream input = createSafeInputStream("[0m");
assertEquals(RESET, handler.parse(input));
assertThat(handler.parse(input), is(RESET));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Red() {
SafeInputStream input = createSafeInputStream("[31m");
assertEquals(RED, handler.parse(input));
assertThat(handler.parse(input), is(RED));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Green() {
SafeInputStream input = createSafeInputStream("[32m");
assertEquals(GREEN, handler.parse(input));
assertThat(handler.parse(input), is(GREEN));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Yellow() {
SafeInputStream input = createSafeInputStream("[33m");
assertEquals(YELLOW, handler.parse(input));
assertThat(handler.parse(input), is(YELLOW));
}
@Test
public void parsedControlSequenceIsCorrectType_SGR_Purple() {
SafeInputStream input = createSafeInputStream("[35m");
assertEquals(PURPLE, handler.parse(input));
assertThat(handler.parse(input), is(PURPLE));
}
@Test
public void parseMultipleControlSequences() {
SafeInputStream input = createSafeInputStream("[35m[32m[0m");
assertEquals(PURPLE, handler.parse(input));
assertEquals(GREEN, handler.parse(input));
assertEquals(RESET, handler.parse(input));
assertThat(handler.parse(input), is(PURPLE));
assertThat(handler.parse(input), is(GREEN));
assertThat(handler.parse(input), is(RESET));
}
}

View File

@ -1,7 +1,9 @@
package terminal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.isEmptyString;
import static org.junit.Assert.assertThat;
import static terminal.SelectGraphicRendition.GREEN;
import static terminal.SelectGraphicRendition.PURPLE;
import static terminal.SelectGraphicRendition.RED;
@ -49,48 +51,48 @@ public class ControlSequenceTest {
ControlSequence nullControlSequence = new NullControlSequence();
VirtualTerminal terminal = createTerminalWithIndicators();
nullControlSequence.applyTo(terminal);
assertTrue(indicatorSet.isEmpty());
assertThat(indicatorSet, empty());
}
@Test
public void controlSequenceUpdatesTerminal_SGR_Reset() {
VirtualTerminal terminal = createTerminalWithIndicators();
RESET.applyTo(terminal);
assertTrue(indicatorSet.contains("RESET"));
assertThat(indicatorSet, contains("RESET"));
}
@Test
public void controlSequenceUpdatesTerminal_SGR_Red() {
VirtualTerminal terminal = createTerminalWithIndicators();
RED.applyTo(terminal);
assertTrue(indicatorSet.contains("RED"));
assertThat(indicatorSet, contains("RED"));
}
@Test
public void controlSequenceUpdatesTerminal_SGR_Green() {
VirtualTerminal terminal = createTerminalWithIndicators();
GREEN.applyTo(terminal);
assertTrue(indicatorSet.contains("GREEN"));
assertThat(indicatorSet, contains("GREEN"));
}
@Test
public void controlSequenceUpdatesTerminal_SGR_Yellow() {
VirtualTerminal terminal = createTerminalWithIndicators();
YELLOW.applyTo(terminal);
assertTrue(indicatorSet.contains("YELLOW"));
assertThat(indicatorSet, contains("YELLOW"));
}
@Test
public void controlSequenceUpdatesTerminal_SGR_Purple() {
VirtualTerminal terminal = createTerminalWithIndicators();
PURPLE.applyTo(terminal);
assertTrue(indicatorSet.contains("MAGENTA"));
assertThat(indicatorSet, contains("MAGENTA"));
}
@Test
public void nullControlSequenceHasCorrectCode() {
ControlSequence nullControlSequence = new NullControlSequence();
assertEquals("", nullControlSequence.getCode());
assertThat(nullControlSequence.getCode(), isEmptyString());
}
@Test

View File

@ -1,7 +1,7 @@
package terminal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.junit.After;
import org.junit.Before;
@ -12,19 +12,19 @@ public class TerminalHistoryTest {
private TerminalHistory history;
private void assertAtBeginning() {
assertTrue(history.isBeginning());
assertThat(history.isBeginning(), is(true));
}
private void assertAtEnd() {
assertTrue(history.isEnd());
assertThat(history.isEnd(), is(true));
}
private void assertPrevious(String expected) {
assertEquals(expected, history.getPreviousLine());
assertThat(history.getPreviousLine(), is(expected));
}
private void assertNext(String expected) {
assertEquals(expected, history.getNextLine());
assertThat(history.getNextLine(), is(expected));
}
@Before

View File

@ -1,6 +1,7 @@
package terminal;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static terminal.LispTerminal.END_OF_SEGMENT;
@ -136,8 +137,8 @@ public class VirtualTerminalInteractor {
}
public void assertCursorPosition(int column, int row) {
assertEquals(column, virtualTerminal.getCursorPosition().getColumn());
assertEquals(row, virtualTerminal.getCursorPosition().getRow());
assertThat(virtualTerminal.getCursorPosition().getColumn(), is(column));
assertThat(virtualTerminal.getCursorPosition().getRow(), is(row));
}
public void assertScreenText(String... rows) {
@ -150,7 +151,7 @@ public class VirtualTerminalInteractor {
TerminalPosition position = new TerminalPosition(column, row);
String expected = String.valueOf(character);
String actual = String.valueOf(virtualTerminal.getCharacter(position).getCharacter());
assertEquals(expected, actual);
assertThat(actual, is(expected));
}
public void assertInputWritten(String expected) {
@ -163,7 +164,7 @@ public class VirtualTerminalInteractor {
actual += (char) c;
} catch (IOException ignored) {}
assertEquals(expected, actual);
assertThat(actual, is(expected));
}
public void assertInputStreamClosed() {

View File

@ -1,8 +1,7 @@
package testutil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
@ -11,121 +10,121 @@ import sexpression.SExpression;
public final class TypeAssertions {
public static void assertList(SExpression sExpression) {
assertFalse(sExpression.isAtom());
assertTrue(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertTrue(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(false));
assertThat(sExpression.isCons(), is(true));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(true));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertNil(SExpression sExpression) {
assertEquals(NIL, sExpression);
assertThat(sExpression, is(NIL));
assertTrue(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertTrue(sExpression.isList());
assertTrue(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertTrue(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(true));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(true));
assertThat(sExpression.isNull(), is(true));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(true));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertNumber(SExpression sExpression) {
assertTrue(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertTrue(sExpression.isNumber());
assertFalse(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(true));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(true));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertString(SExpression sExpression) {
assertTrue(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertTrue(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(true));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(true));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertSymbol(SExpression sExpression) {
assertTrue(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertTrue(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(true));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(true));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertT(SExpression sExpression) {
assertEquals(T, sExpression);
assertThat(sExpression, is(T));
}
public static void assertBackTickExpression(SExpression sExpression) {
assertFalse(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertTrue(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(false));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(true));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertCommaExpression(SExpression sExpression) {
assertFalse(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertTrue(sExpression.isComma());
assertFalse(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(false));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(true));
assertThat(sExpression.isAtSign(), is(false));
}
public static void assertAtSignExpression(SExpression sExpression) {
assertFalse(sExpression.isAtom());
assertFalse(sExpression.isCons());
assertFalse(sExpression.isFunction());
assertFalse(sExpression.isList());
assertFalse(sExpression.isNull());
assertFalse(sExpression.isNumber());
assertFalse(sExpression.isString());
assertFalse(sExpression.isSymbol());
assertFalse(sExpression.isBackquote());
assertFalse(sExpression.isComma());
assertTrue(sExpression.isAtSign());
assertThat(sExpression.isAtom(), is(false));
assertThat(sExpression.isCons(), is(false));
assertThat(sExpression.isFunction(), is(false));
assertThat(sExpression.isList(), is(false));
assertThat(sExpression.isNull(), is(false));
assertThat(sExpression.isNumber(), is(false));
assertThat(sExpression.isString(), is(false));
assertThat(sExpression.isSymbol(), is(false));
assertThat(sExpression.isBackquote(), is(false));
assertThat(sExpression.isComma(), is(false));
assertThat(sExpression.isAtSign(), is(true));
}
}

View File

@ -1,9 +1,11 @@
package token;
import static error.ErrorManager.Severity.CRITICAL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
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;
import org.junit.Before;
import org.junit.Test;
@ -31,55 +33,55 @@ public class TokenFactoryTest {
@Test
public void eofTokenCreation() {
assertTrue(tokenFactory.createEofToken(testPosition) instanceof Eof);
assertThat(tokenFactory.createEofToken(testPosition), instanceOf(Eof.class));
}
@Test
public void leftParenthesisCreation() {
String text = "(";
assertTrue(createToken(text) instanceof LeftParenthesis);
assertThat(createToken(text), instanceOf(LeftParenthesis.class));
}
@Test
public void rightParenthesisCreation() {
String text = ")";
assertTrue(createToken(text) instanceof RightParenthesis);
assertThat(createToken(text), instanceOf(RightParenthesis.class));
}
@Test
public void quoteMarkCreation() {
String text = "'";
assertTrue(createToken(text) instanceof QuoteMark);
assertThat(createToken(text), instanceOf(QuoteMark.class));
}
@Test
public void numberCreation() {
String text = "987";
assertTrue(createToken(text) instanceof Number);
assertThat(createToken(text), instanceOf(Number.class));
}
@Test
public void prefixedNumberCreation() {
String text = "-987";
assertTrue(createToken(text) instanceof Number);
assertThat(createToken(text), instanceOf(Number.class));
}
@Test
public void identifierCreation() {
String text = "identifier";
assertTrue(createToken(text) instanceof Identifier);
assertThat(createToken(text), instanceOf(Identifier.class));
}
@Test
public void prefixedIdentifierCreation() {
String text = "-identifier";
assertTrue(createToken(text) instanceof Identifier);
assertThat(createToken(text), instanceOf(Identifier.class));
}
@Test
public void stringCreation() {
String text = "\"string\"";
assertTrue(createToken(text) instanceof QuotedString);
assertThat(createToken(text), instanceOf(QuotedString.class));
}
@Test(expected = EmptyTokenTextException.class)
@ -93,9 +95,9 @@ public class TokenFactoryTest {
createToken("");
} catch (EmptyTokenTextException e) {
String message = e.getMessage();
assertNotNull(message);
assertTrue(message.length() > 0);
assertEquals(CRITICAL, e.getSeverity());
assertThat(message, is(notNullValue()));
assertThat(message.length(), greaterThan(0));
assertThat(e.getSeverity(), is(CRITICAL));
}
}
@ -107,19 +109,19 @@ public class TokenFactoryTest {
@Test
public void backTickCreation() {
String text = "`";
assertTrue(createToken(text) instanceof Backquote);
assertThat(createToken(text), instanceOf(Backquote.class));
}
@Test
public void commaCreation() {
String text = ",";
assertTrue(createToken(text) instanceof Comma);
assertThat(createToken(text), instanceOf(Comma.class));
}
@Test
public void atSignCreation() {
String text = "@";
assertTrue(createToken(text) instanceof AtSign);
assertThat(createToken(text), instanceOf(AtSign.class));
}
}

View File

@ -1,6 +1,7 @@
package util;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -13,7 +14,7 @@ public class CharactersTest {
public void constructorIsPrivate() throws Exception {
Constructor<Characters> constructor = Characters.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
assertThat(Modifier.isPrivate(constructor.getModifiers()), is(true));
constructor.setAccessible(true);
constructor.newInstance();
}

View File

@ -1,6 +1,7 @@
package util;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -13,7 +14,7 @@ public class PathTest {
public void constructorIsPrivate() throws Exception {
Constructor<Path> constructor = Path.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
assertThat(Modifier.isPrivate(constructor.getModifiers()), is(true));
constructor.setAccessible(true);
constructor.newInstance();
}