Search in sources :

Example 1 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class AbstractStructuredIdeTest method test.

/**
 * Single entry method to perform tests.
 * <p>
 * Note: At this point, neither projects nor modules have a '*' appended.
 *
 * @param projectsModulesContents
 *            map that maps directories to module names which map to their contents.
 * @param projectPath
 *            project that contains the module with the name 'moduleName'
 * @param moduleName
 *            one moduleName of the {@code moduleNameToContents}. Will be opened during the test.
 * @param t
 *            will be passed to {@link #performTest(Project, String, Object)}.
 */
protected Project test(Map<String, Map<String, String>> projectsModulesContents, String projectPath, String moduleName, T t) {
    Project project = testWorkspaceManager.createTestOnDisk(projectsModulesContents);
    startAndWaitForLspServer();
    openFile(moduleName);
    try {
        performTest(project, moduleName, t);
    } catch (AssertionError ae) {
        throw ae;
    } catch (Throwable th) {
        throw new AssertionError("exception/error in #performTest() method", th);
    }
    return project;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project)

Example 2 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class AbstractOrganizeImportsTest method performTest.

@Override
protected void performTest(Project project, String moduleName, TestOrganizeImportsConfiguration config) throws Exception {
    FileURI uri = getFileURIFromModuleName(moduleName);
    if (config.expectedIssues.isEmpty()) {
        assertNoIssues();
    } else {
        assertIssues(Collections.singletonMap(uri, config.expectedIssues));
    }
    TextDocumentIdentifier id = new TextDocumentIdentifier(uri.toString());
    Range range = new Range(new Position(0, 0), new Position(0, 0));
    CodeActionContext context = new CodeActionContext();
    CodeActionParams params = new CodeActionParams(id, range, context);
    CompletableFuture<List<Either<Command, CodeAction>>> codeActionFuture = languageServer.codeAction(params);
    List<Either<Command, CodeAction>> result = codeActionFuture.join();
    Command organizeImportsCommand = result.stream().map(e -> e.isLeft() ? e.getLeft() : e.getRight().getCommand()).filter(cmd -> cmd != null && Objects.equals(cmd.getCommand(), N4JSCommandService.N4JS_ORGANIZE_IMPORTS)).findFirst().orElse(null);
    Assert.assertNotNull("code action for organize imports not found", organizeImportsCommand);
    ExecuteCommandParams execParams = new ExecuteCommandParams(organizeImportsCommand.getCommand(), organizeImportsCommand.getArguments());
    CompletableFuture<Object> execFuture = languageServer.executeCommand(execParams);
    execFuture.join();
    joinServerRequests();
    assertContentOfFileOnDisk(uri, config.expectedCode);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Iterables(com.google.common.collect.Iterables) IssueCodes(org.eclipse.n4js.validation.IssueCodes) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Lists(com.google.common.collect.Lists) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) N4JSLanguageConstants(org.eclipse.n4js.N4JSLanguageConstants) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) N4JSCommandService(org.eclipse.n4js.ide.server.commands.N4JSCommandService) CodeAction(org.eclipse.lsp4j.CodeAction) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Set(java.util.Set) Project(org.eclipse.n4js.tests.codegen.Project) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Strings(org.eclipse.n4js.utils.Strings) List(java.util.List) Command(org.eclipse.lsp4j.Command) TestOrganizeImportsConfiguration(org.eclipse.n4js.ide.tests.helper.server.AbstractOrganizeImportsTest.TestOrganizeImportsConfiguration) Pair(org.eclipse.xtext.xbase.lib.Pair) Assert(org.junit.Assert) Collections(java.util.Collections) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) Range(org.eclipse.lsp4j.Range) FileURI(org.eclipse.n4js.workspace.locations.FileURI) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) List(java.util.List)

Example 3 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class TestWorkspaceManager method createYarnProject.

private Project createYarnProject(Map<String, ? extends Map<String, ? extends CharSequence>> projectsModulesContents) {
    YarnWorkspaceProject yarnProject = new YarnWorkspaceProject(YARN_TEST_PROJECT, VENDOR, VENDOR_NAME);
    Multimap<String, String> dependencies = HashMultimap.create();
    for (String projectNameWithSelector : projectsModulesContents.keySet()) {
        Map<String, ? extends CharSequence> moduleContents = projectsModulesContents.get(projectNameWithSelector);
        String prjName = projectNameWithSelector;
        if (prjName.startsWith(CFG_NODE_MODULES)) {
            prjName = prjName.substring(CFG_NODE_MODULES.length());
            Project project = createSimpleProject(prjName, moduleContents, dependencies, ProjectKind.NodeModule);
            yarnProject.addNodeModuleProject(project);
        } else if (prjName.startsWith(CFG_WORKSPACES_FOLDER)) {
            int idxCFG = CFG_WORKSPACES_FOLDER.length();
            int idxSlash = prjName.indexOf("/");
            String wsFolder = prjName.substring(idxCFG, idxSlash);
            prjName = prjName.substring(idxSlash + 1);
            Project project = createSimpleProject(prjName, moduleContents, dependencies, ProjectKind.Member);
            yarnProject.addMemberProject(wsFolder, project);
        } else if (prjName.equals(CFG_YARN_PROJECT)) {
            if (moduleContents.containsKey(N4JSGlobals.PACKAGE_JSON)) {
                yarnProject.setProjectDescriptionContent(moduleContents.get(N4JSGlobals.PACKAGE_JSON).toString());
            }
        } else {
            Project project = createSimpleProject(prjName, moduleContents, dependencies, ProjectKind.Member);
            yarnProject.addMemberProject(project);
        }
    }
    setDependencies(yarnProject, dependencies);
    // apply default values
    boolean haveMemberRequiringN4jsRuntime = IterableExtensions.exists(yarnProject.getMemberProjects(), p -> N4JSGlobals.PROJECT_TYPES_REQUIRING_N4JS_RUNTIME.contains(p.getType()));
    if (haveMemberRequiringN4jsRuntime) {
        if (yarnProject.getNodeModuleProject(N4JS_RUNTIME) == null) {
            yarnProject.addNodeModuleProject(N4JS_RUNTIME_FAKE);
        }
    }
    return yarnProject;
}
Also used : YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)

Example 4 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class TestWorkspaceManager method createTestOnDisk.

private Project createTestOnDisk(Path destination, Map<String, ? extends Map<String, ? extends CharSequence>> projectsModulesContents, boolean forceYarnWorkspace) {
    final Project project;
    if (!forceYarnWorkspace && projectsModulesContents.size() == 1) {
        Entry<String, ? extends Map<String, ? extends CharSequence>> singleProject = projectsModulesContents.entrySet().iterator().next();
        String projectName = singleProject.getKey();
        Map<String, ? extends CharSequence> modulesContents = singleProject.getValue();
        project = createSimpleProject(projectName, modulesContents, HashMultimap.create(), ProjectKind.TopLevel);
    } else {
        project = createYarnProject(projectsModulesContents);
    }
    Workspace workspace = new Workspace();
    workspace.addProject(project);
    createTestOnDisk(destination, workspace);
    return project;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) Workspace(org.eclipse.n4js.tests.codegen.Workspace)

Example 5 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class TestWorkspaceManager method getProjectRootFailSafe.

private File getProjectRootFailSafe(String projectName) {
    if (!isCreated()) {
        return null;
    }
    Path projectNamePath = projectNameToRelativePath(projectName);
    Path createdProjectNamePath = projectNameToRelativePath(createdProject.getName());
    Path createdProjectPath = getRoot().toPath().resolve(createdProjectNamePath);
    if (projectName.equals(createdProject.getName())) {
        return createdProjectPath.toFile();
    }
    if (createdProject instanceof YarnWorkspaceProject) {
        Project containedProject = ((YarnWorkspaceProject) createdProject).getMemberProject(projectName);
        if (containedProject != null) {
            return createdProjectPath.resolve(YarnWorkspaceProject.PACKAGES).resolve(projectNamePath).toFile();
        }
    }
    Project nodeModuleProject = createdProject.getNodeModuleProject(projectName);
    if (nodeModuleProject != null) {
        return createdProjectPath.resolve(N4JSGlobals.NODE_MODULES).resolve(projectNamePath).toFile();
    }
    if (createdProject instanceof YarnWorkspaceProject) {
        for (Project containedProject : ((YarnWorkspaceProject) createdProject).getMemberProjects()) {
            Project containedNodeModuleProject = createdProject.getNodeModuleProject(projectName);
            if (containedNodeModuleProject != null) {
                Path containedProjectNamePath = projectNameToRelativePath(containedProject.getName());
                return createdProjectPath.resolve(YarnWorkspaceProject.PACKAGES).resolve(containedProjectNamePath).resolve(N4JSGlobals.NODE_MODULES).resolve(projectNamePath).toFile();
            }
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)

Aggregations

Project (org.eclipse.n4js.tests.codegen.Project)16 YarnWorkspaceProject (org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)8 File (java.io.File)3 Path (java.nio.file.Path)3 Folder (org.eclipse.n4js.tests.codegen.Folder)3 FileURI (org.eclipse.n4js.workspace.locations.FileURI)3 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)2 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)2 Command (org.eclipse.lsp4j.Command)2 Position (org.eclipse.lsp4j.Position)2 Range (org.eclipse.lsp4j.Range)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 Module (org.eclipse.n4js.tests.codegen.Module)2 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1