Commit dbf48761 authored by Alexander Weigl's avatar Alexander Weigl

test facilities

just a parsable expression in goodexpr.txt
and not parsable in badexpr.txt and they are tested

similar for complete scripts
parent 51bc8891
package edu.kit.formal.proofscriptparser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
@RunWith(Parameterized.class)
public class BadExpressionTest {
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> getBadExpressions() throws IOException {
return TestHelper.loadLines("badexpr.txt");
}
@Parameterized.Parameter
public String testExpression;
@Test
public void test() throws IOException {
ScriptLanguageParser slp = TestHelper.getParser(testExpression);
slp.expression();
Assert.assertNotEquals(0, slp.getNumberOfSyntaxErrors());
}
}
\ No newline at end of file
package edu.kit.formal.proofscriptparser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
@RunWith(Parameterized.class)
public class GoodExpressionTest {
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> getGoodExpressions() throws IOException {
return TestHelper.loadLines("goodexpr.txt");
}
@Parameterized.Parameter
public String testExpression;
@Test
public void test() throws IOException {
ScriptLanguageParser slp = TestHelper.getParser(testExpression);
slp.expression();
Assert.assertEquals(0, slp.getNumberOfSyntaxErrors());
}
}
\ No newline at end of file
package edu.kit.formal.proofscriptparser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
@RunWith(Parameterized.class)
public class ScriptTest {
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> getStructuredTextFiles() throws IOException {
return TestHelper.getResourcesAsParameters("edu/kit/formal/proofscriptparser/scripts");
}
@Parameterized.Parameter
public File f;
@Test public void parse() throws IOException {
ScriptLanguageParser slp = TestHelper.getParser(f);
slp.start();
Assert.assertEquals(0, slp.getNumberOfSyntaxErrors());
}
}
package edu.kit.formal.proofscriptparser;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.junit.AssumptionViolatedException;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
public class TestHelper {
public static Iterable<Object[]> getResourcesAsParameters(String folder) {
File[] files = getResources(folder);
return Arrays.stream(files).map(f -> new Object[] { f }).collect(Collectors.toList());
}
public static final File[] getResources(String folder) {
URL f = TestHelper.class.getClassLoader().getResource(folder);
if (f == null) {
System.err.format("Could not find %s%n", folder);
return new File[0];
}
File file = new File(f.getFile());
return file.listFiles();
}
public static Iterable<Object[]> loadLines(String filename) throws IOException {
List<Object[]> validExpression = new ArrayList<>(4096);
InputStream ve = TestHelper.class.getResourceAsStream(filename);
if (ve == null) {
System.err.println("Could not find: " + filename);
return validExpression;
}
BufferedReader stream = new BufferedReader(new InputStreamReader(ve));
String tmp = "";
while ((tmp = stream.readLine()) != null) {
if (tmp.startsWith("#"))
continue;
validExpression.add(new Object[] { tmp });
}
System.out.println("Found: " + filename + " with " + validExpression.size() + " lines!");
return validExpression;
}
public static Collection<Object[]> asParameters(String[] cases) {
return Arrays.stream(cases).map(s -> new Object[] { s }).collect(Collectors.toList());
}
public static ScriptLanguageParser getParser(String input) {
return new ScriptLanguageParser(new CommonTokenStream(new ScriptLanguageLexer(CharStreams.fromString(input))));
}
public static ScriptLanguageParser getParser(File f) throws IOException {
org.antlr.v4.runtime.CharStream stream = CharStreams.fromFileName(f.getAbsolutePath());
ScriptLanguageLexer lexer = new ScriptLanguageLexer(stream);
return new ScriptLanguageParser(new CommonTokenStream(lexer));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment