Search in sources :

Example 11 with Project

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

the class AbstractCodeActionTest method performTest.

@Override
protected void performTest(Project project, String moduleName, N4JSTestCodeActionConfiguration tcac) throws InterruptedException, ExecutionException {
    CodeActionParams codeActionParams = new CodeActionParams();
    Range range = new Range();
    Position posStart = new Position(tcac.getLine(), tcac.getColumn());
    Position posEnd = tcac.getEndLine() >= 0 && tcac.getEndColumn() >= 0 ? new Position(tcac.getEndLine(), tcac.getEndColumn()) : posStart;
    range.setStart(posStart);
    range.setEnd(posEnd);
    codeActionParams.setRange(range);
    CodeActionContext context = new CodeActionContext();
    FileURI uri = getFileURIFromModuleName(moduleName);
    context.setDiagnostics(Lists.newArrayList(getIssuesInFile(uri)));
    codeActionParams.setContext(context);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(uri.toString());
    codeActionParams.setTextDocument(textDocument);
    CompletableFuture<List<Either<Command, CodeAction>>> future = languageServer.codeAction(codeActionParams);
    List<Either<Command, CodeAction>> result = future.get();
    if (tcac.getAssertCodeActions() != null) {
        tcac.getAssertCodeActions().apply(result);
    } else {
        String resultStr = result.stream().map(cmdOrAction -> getStringLSP4J().toString3(cmdOrAction)).collect(Collectors.joining("\n-----\n"));
        assertEquals(tcac.getExpectedCodeActions().trim(), resultStr.trim());
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeAction(org.eclipse.lsp4j.CodeAction) FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSTestCodeActionConfiguration(org.eclipse.n4js.ide.tests.helper.server.AbstractCodeActionTest.N4JSTestCodeActionConfiguration) TestCodeActionConfiguration(org.eclipse.xtext.testing.AbstractLanguageServerTest.TestCodeActionConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) Project(org.eclipse.n4js.tests.codegen.Project) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Collectors(java.util.stream.Collectors) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ExecutionException(java.util.concurrent.ExecutionException) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) List(java.util.List) Lists(com.google.common.collect.Lists) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) Assert.assertEquals(org.junit.Assert.assertEquals) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) 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 12 with Project

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

the class XtFileDataParser method createDefaultWorkspace.

static XtWorkspace createDefaultWorkspace(String fileName, String xtFileContent, XtSetupParseResult setupParseResult) {
    String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
    String moduleName = fileName.substring(0, fileName.length() - 1 - extension.length());
    Module xtFileModule = new Module(moduleName, extension);
    xtFileModule.setContents(xtFileContent);
    Folder srcFolder = new Folder(DEFAULT_SOURCE_FOLDER);
    srcFolder.addModule(xtFileModule);
    Project project = new Project(DEFAULT_PROJECT_NAME, VENDOR, VENDOR_NAME);
    project.addSourceFolder(srcFolder);
    project.setGenerateDts(setupParseResult.generateDts);
    for (String otherSrcFileName : setupParseResult.files.keySet()) {
        String otherExt = URIUtils.fileExtension(URIUtils.toFileUri(otherSrcFileName));
        String otherContent = setupParseResult.files.get(otherSrcFileName);
        String otherModuleName = otherSrcFileName.substring(0, otherSrcFileName.length() - otherExt.length() - 1);
        Module otherModule = new Module(otherModuleName, otherExt);
        otherModule.setContents(otherContent);
        srcFolder.addModule(otherModule);
    }
    XtWorkspace workspace = new XtWorkspace();
    workspace.addProject(project);
    workspace.moduleNameOfXtFile = fileName;
    return workspace;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) XtWorkspace(org.eclipse.n4js.ide.tests.helper.server.xt.XtSetupParser.XtWorkspace) Module(org.eclipse.n4js.tests.codegen.Module) Folder(org.eclipse.n4js.tests.codegen.Folder)

Example 13 with Project

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

the class TestWorkspaceManager method getProjectRootContaining.

/**
 * For this method, it is sufficient if the given file is located somewhere inside a project; strict
 * {@link SourceFolderSnapshot#contains(URI) containment in a source folder} is *not* required.
 *
 * @return the root folder of the project containing the given file or <code>null</code>.
 */
public File getProjectRootContaining(File file) {
    List<Project> allProjects = new ArrayList<>();
    if (createdProject instanceof YarnWorkspaceProject) {
        allProjects.addAll(((YarnWorkspaceProject) createdProject).getMemberProjects());
    } else {
        allProjects.add(createdProject);
    }
    Path filePath = file.toPath();
    Path resultPath = null;
    for (Project project : allProjects) {
        File projectRoot = getProjectRoot(project.getName());
        if (projectRoot != null) {
            Path projectRootPath = projectRoot.toPath();
            if (filePath.startsWith(projectRootPath)) {
                if (resultPath == null || projectRootPath.getNameCount() > resultPath.getNameCount()) {
                    resultPath = projectRootPath;
                }
            }
        }
    }
    return resultPath != null ? resultPath.toFile() : null;
}
Also used : Path(java.nio.file.Path) Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) ArrayList(java.util.ArrayList) File(java.io.File)

Example 14 with Project

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

the class TestWorkspaceManager method setDependencies.

private void setDependencies(YarnWorkspaceProject yarnProject, Multimap<String, String> dependencies) {
    for (String projectName : dependencies.keySet()) {
        Collection<String> projectDependencies = dependencies.get(projectName);
        Project project = yarnProject.getMemberProject(projectName);
        if (project == null) {
            project = yarnProject.getNodeModuleProject(projectName);
            if (project == null) {
                throw new IllegalStateException("unknown project: " + projectName);
            }
        }
        for (String projectDependency : projectDependencies) {
            Project dependency = yarnProject.getMemberProject(projectDependency);
            if (dependency == null) {
                dependency = yarnProject.getNodeModuleProject(projectDependency);
                if (dependency == null) {
                    // -> this might be a valid test case, so ignore this problem
                    continue;
                }
            }
            project.addProjectDependency(dependency.getName());
        }
    }
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)

Example 15 with Project

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

the class ScenarioGenerator method generateScenario.

/**
 * Generates a test scenario according to the parameters specified in the test specification.
 *
 * @param destination
 *            the path to generate the scenario in
 * @return a list files representing the root directories of the created projects
 */
public List<File> generateScenario(Path destination) {
    List<File> result = new LinkedList<>();
    // Create the required classifiers for the scenario.
    ScenarioResult scenario = createScenario();
    Classifier<?> supplier = scenario.supplier;
    Classifier<?> client = scenario.client;
    Class factory = scenario.factory;
    Class implementer = scenario.implementer;
    // Create a member for the supplier according the specified visibility and "abstract-ness".
    switch(specification.getSupplierType()) {
        case CLASS:
        case DEFAULT_INTERFACE:
            supplier.addMember(createMember("member", specification.getMemberVisibility()));
            break;
        case ABSTRACT_CLASS:
        case INTERFACE:
            supplier.addMember(createMember("member", specification.getMemberVisibility()).makeAbstract());
            break;
    }
    // or it may attempt to access it by reading or writing it or calling it if it is a method.
    switch(specification.getScenario()) {
        case EXTENDS:
        case IMPLEMENTS:
            {
                switch(specification.getUsageType()) {
                    case ACCESS:
                        switch(specification.getMemberStatic()) {
                            case YES:
                                client.addMember(createAccess("member", "S"));
                                break;
                            case NO:
                                client.addMember(createAccess("member", "this"));
                                break;
                        }
                        if (specification.getSupplierType() != ClassifierType.INTERFACE && specification.getSupplierType() != ClassifierType.ABSTRACT_CLASS)
                            break;
                        if (// Fields cannot be abstract
                        memberType == MemberType.FIELD)
                            break;
                    // $FALL-THROUGH$
                    case OVERRIDE:
                        client.addMember(createMember("member", specification.getMemberVisibility()).makeOverride());
                        break;
                    default:
                        throw new IllegalArgumentException("Unexpected usage type: " + specification.getUsageType());
                }
                break;
            }
        case REFERENCES:
            {
                if (specification.getUsageType() == UsageType.OVERRIDE)
                    throw new IllegalArgumentException("Cannot override in reference scenario");
                switch(specification.getSupplierType()) {
                    case CLASS:
                    case DEFAULT_INTERFACE:
                        break;
                    case ABSTRACT_CLASS:
                    case INTERFACE:
                        implementer.addMember(createMember("member", specification.getMemberVisibility()).makeOverride());
                        break;
                }
                // Create a method that accesses the supplier's member via an instance created by the factory.
                client.addMember(createAccess("member", "new GetS().getS()"));
                break;
            }
    }
    // created, according to the client location in the test specification.
    switch(specification.getClientLocation()) {
        case SAME_TYPE:
        case SAME_MODULE:
            {
                Module module = new Module("SameModule");
                module.addClassifier(supplier);
                if (implementer != null)
                    module.addClassifier(implementer);
                if (factory != null)
                    module.addClassifier(factory);
                module.addClassifier(client);
                Project project = new Project("SameModule", "sameVendor", "SameVendor", PROJECT_TYPE);
                project.createSourceFolder("src").addModule(module);
                result.add(project.create(destination));
                break;
            }
        case SAME_PROJECT:
            {
                Module supplierModule = createSupplierModule(supplier, factory, implementer);
                Module clientModule = createClientModule(client, supplier, factory, supplierModule);
                Project project = new Project("SameProject", "sameVendor", "SameVendor", PROJECT_TYPE);
                project.createSourceFolder("src").addModule(supplierModule).addModule(clientModule);
                result.add(project.create(destination));
                break;
            }
        case SAME_VENDOR:
            {
                Module supplierModule = createSupplierModule(supplier, factory, implementer);
                Module clientModule = createClientModule(client, supplier, factory, supplierModule);
                Project supplierProject = createSupplierProject(supplierModule, "sameVendor");
                Project clientProject = createClientProject(clientModule, "sameVendor", supplierProject);
                result.add(supplierProject.create(destination));
                result.add(clientProject.create(destination));
                break;
            }
        case OTHER:
            {
                Module supplierModule = createSupplierModule(supplier, factory, implementer);
                Module clientModule = createClientModule(client, supplier, factory, supplierModule);
                Project supplierProject = createSupplierProject(supplierModule, "vendorA");
                Project clientProject = createClientProject(clientModule, "vendorB", supplierProject);
                result.add(supplierProject.create(destination));
                result.add(clientProject.create(destination));
                break;
            }
        default:
            break;
    }
    return result;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) Class(org.eclipse.n4js.tests.codegen.Class) Module(org.eclipse.n4js.tests.codegen.Module) File(java.io.File) LinkedList(java.util.LinkedList)

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