add test cases for from file generated parsers

This commit is contained in:
Christoph
2020-12-04 14:30:30 +01:00
parent 1652d17528
commit f60eef549e
3 changed files with 91 additions and 3 deletions

View File

@ -13,12 +13,11 @@ import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class LexerTest {
private Lexer initLexer(String program) {
try {
Path path = Paths.get(this.getClass().getClassLoader().getResource("examples/" + program).toURI());
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
return new StupsLexer(CharStreams.fromString(programCode));
} catch (Exception ignore) {

View File

@ -4,6 +4,10 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import parser.grammar.Grammar;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.HashMap;
@ -122,6 +126,20 @@ class LL1ParserTest {
assertThat(parser.parse(Arrays.asList(token3))).isTrue();
}
@Test
void testIfThenElseFromFile() throws MyParseException, IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/SimpleGrammar0.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
String[] token1 = {"i", "b", "t", "a"};
String[] token2 = {"i", "b", "t", "i", "b", "t", "a"};
String[] token3 = {"i", "b", "t", "i", "b", "t", "i", "b", "t", "a"};
assertThat(parser.parse(Arrays.asList(token1))).isTrue();
assertThat(parser.parse(Arrays.asList(token2))).isTrue();
assertThat(parser.parse(Arrays.asList(token3))).isTrue();
}
@Test
void testException0() {
LL1Parser parser = new LL1Parser(table0);
@ -146,8 +164,22 @@ class LL1ParserTest {
String[] token2 = {"id", "*", "id", "*", "id"};
String[] token3 = {"id", "+", "id"};
assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token2))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token3))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token3))).isInstanceOf(MyParseException.class);
}
@Test
void testArithExpressionFromFile() throws MyParseException, IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/SimpleGrammar1.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
String[] token1 = {"id", "+", "id", "*", "id"};
String[] token2 = {"id", "*", "id", "*", "id"};
String[] token3 = {"id", "+", "id"};
assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token2))).isInstanceOf(MyParseException.class);
assertThatThrownBy(() -> parser.parse(Arrays.asList(token3))).isInstanceOf(MyParseException.class);
}

View File

@ -0,0 +1,57 @@
package parser.grammar;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.assertj.core.api.Assertions.assertThat;
class GrammarTest {
private Path getPath(String name) {
try {
return Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/" + name).toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
@Test
void testSimpleGrammar0() throws IOException {
Path path = this.getPath("SimpleGrammar0.grammar");
Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
assertThat(grammar.getStartSymbol()).isEqualTo("S");
assertThat(grammar.getTerminals()).containsOnly("a", "i", "t", "b");
assertThat(grammar.getNonterminals()).containsOnly("S", "E");
assertThat(grammar.getRules()).containsOnly(new GrammarRule("S", "a"),
new GrammarRule("S", "i", "E", "t", "S"),
new GrammarRule("E", "b"));
}
@Test
void testSimpleGrammar1() throws IOException {
Path path = this.getPath("SimpleGrammar1.grammar");
Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
System.out.println(grammar.getRules());
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
assertThat(grammar.getStartSymbol()).isEqualTo("E");
assertThat(grammar.getTerminals()).containsOnly("id", "+", "*", "(", ")");
assertThat(grammar.getNonterminals()).containsOnly("E", "E2", "T", "T2", "F");
assertThat(grammar.getRules()).contains(new GrammarRule("E", "T", "E2"),
new GrammarRule("E2", "+", "T", "E2"),
new GrammarRule("E2", "epsilon"));
}
}