Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
implemented reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Umbranecis committed Apr 17, 2024
1 parent b3f5476 commit 740fb7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,10 @@

public class LexerTest {

private final Path filePath = Path.of("lexer-test.txt");

private void init() {
try {
Files.deleteIfExists(filePath);
Files.createFile(filePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void cleanUp() {
try {
Files.delete(filePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
public void testLexer() throws Exception {
init();
Reader reader = new Reader(new File("src/test/resources/test.txt"));
Lexer lexer = new Lexer(reader);

Expand Down Expand Up @@ -59,7 +41,5 @@ public void testLexer() throws Exception {
lexer.expect(TokenType.TOK_EOF);

assert lexer.isEOF();

cleanUp();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.auberer.compilerdesignlectureproject.reader;

import lombok.Locked;
import org.junit.Test;

import java.io.File;
Expand All @@ -12,31 +13,43 @@ public class ReaderTest {
@Test
public void testGetChar() throws FileNotFoundException {
Reader reader = new Reader(testFile);
reader.getChar();
reader.advance();
assert reader.getChar() == 'i';

}

@Test
public void testGetCodeLoc() throws FileNotFoundException {
Reader reader = new Reader(testFile);
reader.getCodeLoc();
assertCodeLocCorrect(reader, 1L, 1L);
}

@Test
public void testAdvance() throws FileNotFoundException {
Reader reader = new Reader(testFile);
reader.advance();
assertCodeLocCorrect(reader, 1L ,2L);
reader.advance();
assertCodeLocCorrect(reader, 1L, 3L);
}

@Test
public void testExpect() throws FileNotFoundException {
Reader reader = new Reader(testFile);
reader.expect('a');
reader.advance();
reader.expect('i');
}

@Test
public void testIsEOF() throws FileNotFoundException {
Reader reader = new Reader(testFile);
reader.isEOF();
do{reader.advance();}
while (reader.getChar() != (char) -1);
assert reader.isEOF();
}

private static void assertCodeLocCorrect(Reader reader, Long line, Long column){
assert reader.getCodeLoc().toString().equals(new CodeLoc(line, column).toString());
}

}

0 comments on commit 740fb7e

Please sign in to comment.