Search in sources :

Example 1 with TestCase

use of com.oracle.truffle.sl.test.SLTestRunner.TestCase in project graal by oracle.

the class SLTestRunner method createTests.

protected static List<TestCase> createTests(final Class<?> c) throws IOException, InitializationError {
    SLTestSuite suite = c.getAnnotation(SLTestSuite.class);
    if (suite == null) {
        throw new InitializationError(String.format("@%s annotation required on class '%s' to run with '%s'.", SLTestSuite.class.getSimpleName(), c.getName(), SLTestRunner.class.getSimpleName()));
    }
    String[] paths = suite.value();
    Class<?> testCaseDirectory = c;
    if (suite.testCaseDirectory() != SLTestSuite.class) {
        testCaseDirectory = suite.testCaseDirectory();
    }
    Path root = getRootViaResourceURL(testCaseDirectory, paths);
    if (root == null) {
        for (String path : paths) {
            Path candidate = FileSystems.getDefault().getPath(path);
            if (Files.exists(candidate)) {
                root = candidate;
                break;
            }
        }
    }
    if (root == null && paths.length > 0) {
        throw new FileNotFoundException(paths[0]);
    }
    final Path rootPath = root;
    final List<TestCase> foundCases = new ArrayList<>();
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
            String sourceName = sourceFile.getFileName().toString();
            if (sourceName.endsWith(SOURCE_SUFFIX)) {
                String baseName = sourceName.substring(0, sourceName.length() - SOURCE_SUFFIX.length());
                Path inputFile = sourceFile.resolveSibling(baseName + INPUT_SUFFIX);
                String testInput = "";
                if (Files.exists(inputFile)) {
                    testInput = readAllLines(inputFile);
                }
                Path outputFile = sourceFile.resolveSibling(baseName + OUTPUT_SUFFIX);
                String expectedOutput = "";
                if (Files.exists(outputFile)) {
                    expectedOutput = readAllLines(outputFile);
                }
                foundCases.add(new TestCase(c, baseName, sourceName, sourceFile, testInput, expectedOutput));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return foundCases;
}
Also used : Path(java.nio.file.Path) InitializationError(org.junit.runners.model.InitializationError) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) TestCase(com.oracle.truffle.sl.test.SLTestRunner.TestCase) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

TestCase (com.oracle.truffle.sl.test.SLTestRunner.TestCase)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 InitializationError (org.junit.runners.model.InitializationError)1