Search in sources :

Example 1 with ID

use of org.eclipse.n4js.tooling.tester.model.ID 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)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 UUID.randomUUID (java.util.UUID.randomUUID)1 URI (org.eclipse.emf.common.util.URI)1 ID (org.eclipse.n4js.tooling.tester.model.ID)1 TestSuite (org.eclipse.n4js.tooling.tester.model.TestSuite)1 TestTree (org.eclipse.n4js.tooling.tester.model.TestTree)1 TModule (org.eclipse.n4js.ts.types.TModule)1