Search in sources :

Example 1 with TestSuite

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

the class TestDiscoveryHelper method addOrCreateSuite.

private TestSuite addOrCreateSuite(N4JSWorkspaceConfigSnapshot ws, TClass clazz, Map<String, TestSuite> suites) {
    String suiteKey = getTestCatalogNameFor(ws, clazz);
    TestSuite suite = suites.get(suiteKey);
    if (suite == null) {
        suite = new TestSuite(suiteKey);
        suites.put(suiteKey, suite);
    }
    return suite;
}
Also used : TestSuite(org.eclipse.n4js.tooling.tester.model.TestSuite)

Example 2 with TestSuite

use of org.eclipse.n4js.tooling.tester.model.TestSuite 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 3 with TestSuite

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

the class TestDiscoveryHelper method collectTestCasesAndSuitesForModule.

/**
 * Collect test cases for all all top level non-abstract exported classes, exclude everything else.
 */
private void collectTestCasesAndSuitesForModule(N4JSWorkspaceConfigSnapshot ws, TModule module, Map<String, TestSuite> suites) {
    for (final TClass clazz : from(module.getTypes()).filter(TClass.class).filter(c -> !c.isAbstract() && c.isExported())) {
        Iterable<TMethod> testMethods = getAllTestMethodsOfClass(clazz);
        if (testMethods.iterator().hasNext()) {
            TestSuite testSuite = addOrCreateSuite(ws, clazz, suites);
            for (final TMethod method : testMethods) {
                final TestCase testCase = createTestCase(method, module, getTestCatalogNameFor(ws, clazz));
                testSuite.add(testCase);
            }
        }
    }
}
Also used : TMethod(org.eclipse.n4js.ts.types.TMethod) TestSuite(org.eclipse.n4js.tooling.tester.model.TestSuite) TestCase(org.eclipse.n4js.tooling.tester.model.TestCase) TClass(org.eclipse.n4js.ts.types.TClass)

Example 4 with TestSuite

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

the class TestDiscoveryHelper method collectTestSuiteAndTestCaseForMethod.

private void collectTestSuiteAndTestCaseForMethod(N4JSWorkspaceConfigSnapshot ws, URI uri, TModule module, Map<String, TestSuite> suites) {
    final EObject object = module.eResource().getResourceSet().getEObject(uri, true);
    final Type type = (object instanceof N4MethodDeclaration) ? ((N4MethodDeclaration) object).getDefinedType() : (object instanceof TMethod) ? (TMethod) object : null;
    if (type instanceof TMethod) {
        final TMethod method = (TMethod) type;
        TestSuite testSuite = addOrCreateSuite(ws, (TClass) method.getContainingType(), suites);
        testSuite.add(createTestCase(ws, method, module));
    }
}
Also used : ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) EcoreUtil2.getContainerOfType(org.eclipse.xtext.EcoreUtil2.getContainerOfType) FileExtensionType(org.eclipse.n4js.fileextensions.FileExtensionType) TMethod(org.eclipse.n4js.ts.types.TMethod) TestSuite(org.eclipse.n4js.tooling.tester.model.TestSuite) EObject(org.eclipse.emf.ecore.EObject) N4MethodDeclaration(org.eclipse.n4js.n4JS.N4MethodDeclaration)

Aggregations

TestSuite (org.eclipse.n4js.tooling.tester.model.TestSuite)4 TMethod (org.eclipse.n4js.ts.types.TMethod)2 LinkedHashMap (java.util.LinkedHashMap)1 UUID.randomUUID (java.util.UUID.randomUUID)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 FileExtensionType (org.eclipse.n4js.fileextensions.FileExtensionType)1 N4MethodDeclaration (org.eclipse.n4js.n4JS.N4MethodDeclaration)1 ID (org.eclipse.n4js.tooling.tester.model.ID)1 TestCase (org.eclipse.n4js.tooling.tester.model.TestCase)1 TestTree (org.eclipse.n4js.tooling.tester.model.TestTree)1 ContainerType (org.eclipse.n4js.ts.types.ContainerType)1 TClass (org.eclipse.n4js.ts.types.TClass)1 TModule (org.eclipse.n4js.ts.types.TModule)1 Type (org.eclipse.n4js.ts.types.Type)1 EcoreUtil2.getContainerOfType (org.eclipse.xtext.EcoreUtil2.getContainerOfType)1