transcendental-lisp/test/file/FilePositionTrackerTest.java

67 lines
1.9 KiB
Java

package file;
import static org.junit.Assert.assertTrue;
import java.util.Objects;
import org.junit.Before;
import org.junit.Test;
public class FilePositionTrackerTest {
public static final String FILE_NAME = "testFile";
private FilePositionTracker trackerUnderTest;
private FilePosition createFilePosition(int lineNumber, int columnNumber) {
FilePosition position = new FilePosition(FILE_NAME);
position.setLineNumber(lineNumber);
position.setColumnNumber(columnNumber);
return position;
}
private void assertTrackerPositionEquals(FilePosition expectedPosition) {
assertTrue(arePositionsEqual(expectedPosition, trackerUnderTest.getCurrentPosition()));
}
private boolean arePositionsEqual(FilePosition position1, FilePosition position2) {
return Objects.equals(position1.getFileName(), position2.getFileName())
&& Objects.equals(position1.getLineNumber(), position2.getLineNumber())
&& Objects.equals(position1.getColumnNumber(), position2.getColumnNumber());
}
@Before
public void setUp() {
trackerUnderTest = new FilePositionTracker(FILE_NAME);
}
@Test
public void noMovement_ReturnsInitialPosition() {
assertTrackerPositionEquals(createFilePosition(1, 0));
}
@Test
public void advanceOneColumn_ReturnsCorrectPosition() {
trackerUnderTest.incrementColumn();
assertTrackerPositionEquals(createFilePosition(1, 1));
}
@Test
public void advanceOneLine_ReturnsCorrectPosition() {
trackerUnderTest.incrementLine();
assertTrackerPositionEquals(createFilePosition(2, 0));
}
@Test
public void advanceOneLine_ResetsColumn() {
trackerUnderTest.incrementColumn();
trackerUnderTest.incrementLine();
assertTrackerPositionEquals(createFilePosition(2, 0));
}
}