Search in sources :

Example 1 with TestTree

use of org.eclipse.n4js.tooling.tester.model.TestTree in project n4js by eclipse.

the class TestDiscoveryHelper method collectTests.

/**
 * Collects all test methods available at the given location and returns a {@link TestTree} in which each test
 * method is represented by a {@link TestCase}. The location may point to an N4JS project or a folder or file within
 * an N4JS project.
 *
 * @param resourceSetAccess
 *            the accessor for the resource set
 *
 * @param locations
 *            the locations referencing to a resource. Can be an N4JS project, or a folder or a file within an N4JS
 *            project.
 * @return The test tree representing one or more test cases. Never returns with {@code null}, but the returned test
 *         tree may be empty.
 */
private TestTree collectTests(N4JSWorkspaceConfigSnapshot ws, Function<? super URI, ? extends ResourceSet> resourceSetAccess, final List<URI> locations) {
    // create test cases (aggregated in test suites)
    final Map<String, TestSuite> suites = new LinkedHashMap<>();
    // create MultiMap from trimmed(!) URI -> original URIs for this prefix URI
    final List<URI> testLocations = collectDistinctTestLocations(ws, resourceSetAccess, locations);
    // module URI --> test case URIs
    final HashMultimap<URI, URI> testLocationMapping = createTestLocationMapping(testLocations);
    // module URI --> module
    final Map<URI, TModule> moduleUri2Modules = loadModules(testLocationMapping.asMap().keySet(), resourceSetAccess);
    for (final URI moduleLocation : testLocationMapping.keySet()) {
        final TModule module = moduleUri2Modules.get(moduleLocation);
        if (null == module) {
            // Assuming broken AST.
            continue;
        }
        // make sure trimmed URIs processed before non-trimmed ones, in other words we collects all tests for
        // test modules first, then later we can skip individual test collecting for test methods.
        // so we can make sure, iff there are more than one elements in the list, and the first one equals to
        // the module location URI (with any fragments), we can skip the processing, since the whole module
        // contains the individual test locations
        final List<URI> testLocationsForModule = newArrayList(testLocationMapping.get(moduleLocation));
        testLocationsForModule.sort(URI_COMPARATOR);
        if (testLocationsForModule.get(0).equals(moduleLocation)) {
            // The first item is referencing the entire module
            // --> collect all test methods in module and ignore remaining URIs
            collectTestCasesAndSuitesForModule(ws, module, suites);
        } else {
            // we have URIs pointing to individual test methods -> collect all URIs
            for (final URI uri : testLocationsForModule) {
                collectTestSuiteAndTestCaseForMethod(ws, uri, module, suites);
            }
        }
    }
    // if test cases are selected, name of tree is first test method and number of more tests
    final ID sessionId = createTestSessionId();
    String name = valueOf(sessionId);
    if (!locations.isEmpty()) {
        final URI uri = locations.get(0);
        name = valueOf(uri.trimFragment()).replaceFirst("platform:/resource/", "");
        if (name.lastIndexOf('.') > 0) {
            name = name.substring(0, name.lastIndexOf('.'));
        }
        // Assuming one single test case.
        if (uri.hasFragment() && !suites.isEmpty() && !suites.values().iterator().next().getTestCases().isEmpty()) {
            name += "#" + suites.values().iterator().next().getTestCases().get(0).getDisplayName();
        }
        if (locations.size() > 1) {
            name += " and " + (locations.size() - 1) + " more";
        }
    }
    return new TestTree(sessionId, suites.values(), name).sort();
}
Also used : TestSuite(org.eclipse.n4js.tooling.tester.model.TestSuite) ID(org.eclipse.n4js.tooling.tester.model.ID) UUID.randomUUID(java.util.UUID.randomUUID) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI) LinkedHashMap(java.util.LinkedHashMap) TestTree(org.eclipse.n4js.tooling.tester.model.TestTree)

Example 2 with TestTree

use of org.eclipse.n4js.tooling.tester.model.TestTree in project n4js by eclipse.

the class TestDiscoveryHelper method collectAllTestsFromProjects.

/**
 * Collects all test cases from the given projects and returns with a new test tree instance representing those test
 * cases.
 *
 * @param resourceSetAccess
 *            the accessor for the resource set
 *
 * @param projects
 *            the projects that are searched for tests
 *
 * @return a test tree representing all test cases in the workspace.
 */
public TestTree collectAllTestsFromProjects(N4JSWorkspaceConfigSnapshot ws, Function<? super URI, ? extends ResourceSet> resourceSetAccess, Iterable<? extends N4JSProjectConfigSnapshot> projects) {
    List<URI> testableProjectURIs = new ArrayList<>();
    for (N4JSProjectConfigSnapshot project : projects) {
        URI location = project.getPathAsFileURI().toURI();
        if (isTestable(ws, location)) {
            testableProjectURIs.add(location);
        }
    }
    TestTree collectedTests = collectTests(ws, resourceSetAccess, testableProjectURIs);
    return collectedTests;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI) TestTree(org.eclipse.n4js.tooling.tester.model.TestTree)

Aggregations

URI (org.eclipse.emf.common.util.URI)2 TestTree (org.eclipse.n4js.tooling.tester.model.TestTree)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 UUID.randomUUID (java.util.UUID.randomUUID)1 ID (org.eclipse.n4js.tooling.tester.model.ID)1 TestSuite (org.eclipse.n4js.tooling.tester.model.TestSuite)1 TModule (org.eclipse.n4js.ts.types.TModule)1 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)1