new tests

This commit is contained in:
ChUrl
2020-12-08 00:57:18 +01:00
parent 6e1cdbc86c
commit 7a746902f9
3 changed files with 96 additions and 47 deletions

View File

@ -1,26 +1,20 @@
package parser; package parser;
import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import parser.grammar.Grammar; import parser.grammar.Grammar;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ -30,24 +24,6 @@ class LL1ParserTest {
private static ILL1ParsingTable table0; private static ILL1ParsingTable table0;
private static ILL1ParsingTable table1; private static ILL1ParsingTable table1;
private Lexer initLexer(String program) {
try {
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) {
ignore.printStackTrace();
}
return null;
}
private List<String> getSymbols(Lexer lex) {
return lex.getAllTokens().stream()
.map(tok -> lex.getVocabulary().getSymbolicName(tok.getType()))
.collect(Collectors.toUnmodifiableList());
}
@BeforeAll @BeforeAll
static void setUp() { static void setUp() {
table0 = initTable0(); table0 = initTable0();
@ -216,26 +192,17 @@ class LL1ParserTest {
assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class); assertThatThrownBy(() -> parser.parse(Arrays.asList(token1))).isInstanceOf(MyParseException.class);
} }
@Disabled
@Test @Test
void testDanglingElse() throws URISyntaxException, IOException { void testDanglingElse() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/DanglingElse.grammar").toURI()); Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/DanglingElse.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path); LL1Parser parser = LL1Parser.fromGrammar(path);
// String[] token1 = {"if", "expr", "then", "other"}; String[] token1 = {"if", "expr", "then", "other"};
// String[] token2 = {"if", "expr", "then", "other", "else", "other"}; String[] token2 = {"if", "expr", "then", "other", "else", "other"};
// assertThat(parser.parse(Arrays.asList(token1))).isTrue(); assertThat(parser.parse(Arrays.asList(token1))).isTrue();
// assertThat(parser.parse(Arrays.asList(token2))).isTrue(); assertThat(parser.parse(Arrays.asList(token2))).isTrue();
} }
@Test
void testGrammarEmptyMain() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyMain.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue();
}
} }

View File

@ -0,0 +1,82 @@
package parser;
import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class LexerParserGrammarTest {
private Lexer initLexer(String program) {
try {
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) {
ignore.printStackTrace();
}
return null;
}
private List<String> getSymbols(Lexer lex) {
return lex.getAllTokens().stream()
.map(tok -> lex.getVocabulary().getSymbolicName(tok.getType()))
.collect(Collectors.toUnmodifiableList());
}
@Test
void testEmptyFile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyFile.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue();
}
@Test
void testEmptyMain() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyMain.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue();
}
@Test
void testGeneralComment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralComment.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue();
}
@Test
void testGeneralWhile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralWhile.stups");
List<String> token = this.getSymbols(lex);
assertThat(parser.parse(token)).isTrue();
}
}

View File

@ -1,14 +1,14 @@
class WhileClass { class WhileClass {
public static void main(String[] args) { public static void main(String[] args) {
int a = 1; // int a = 1;
int b = 1; // int b = 1;
int temp = 0; // int temp = 0;
while (a < 144) { while (a < b) { // (a < 144)
temp = b; // temp = b;
b = a + b; // b = a + b;
a = temp; // a = temp;
System.out.println(a); // System.out.println(a);
} }
} }
} }