Package org.erlide.engine.model.erlang

Source Code of org.erlide.engine.model.erlang.ErlModelTest

package org.erlide.engine.model.erlang;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.net.URI;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IPathVariableManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.erlide.engine.ErlangEngine;
import org.erlide.engine.internal.model.root.ErlProject;
import org.erlide.engine.model.IErlModel;
import org.erlide.engine.model.IErlModelChangeListener;
import org.erlide.engine.model.root.IErlElement;
import org.erlide.engine.model.root.IErlElementLocator;
import org.erlide.engine.model.root.IErlProject;
import org.erlide.testing.utils.WorkspaceTest;
import org.erlide.util.Util;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.google.common.collect.Lists;

public class ErlModelTest extends WorkspaceTest {

    private static final String XX_ERLIDEX = "xx.erlidex";
    private static final String PV = "XXYYZZ";
    private IErlModel model;
    private IErlModule module;
    private IErlProject project;
    private ErlProject project2;

    @Before
    public void setup() throws Exception {
        setUpProject("testproject1");
        setUpProject("testproject2");
        project = getErlProject("testproject1");
        project2 = (ErlProject) getErlProject("testproject2");
        module = getErlModule("xx.erl");
        model = getModel();
    }

    @After
    public void teardown() throws Exception {
        ResourcesPlugin.getWorkspace().getPathVariableManager().setURIValue(PV, null);
    }

    // void addModelChangeListener(IErlModelChangeListener listener);
    // void removeModelChangeListener(IErlModelChangeListener listener);
    @Test
    public void addModelChangeListener() throws Exception {
        final List<IErlElement> changed = Lists.newArrayList();
        final IErlModelChangeListener listener = new IErlModelChangeListener() {

            @Override
            public void elementChanged(final IErlElement element) {
                changed.add(element);
            }
        };
        final IErlModule module2 = createModule(project, "zz6.erl", "-module(zz6).\n");
        final IErlModule module3 = createModule(project, "tt.erl", "-module(tt).\n");
        model.addModelChangeListener(listener);
        module2.open(null);
        model.removeModelChangeListener(listener);
        module3.open(null);
        final List<IErlElement> changed2 = Lists.newArrayList(changed);
        changed.clear();
        final List<IErlElement> changed3 = Lists.newArrayList(changed);
        changed.clear();
        assertEquals(1, changed2.size());
        assertEquals(0, changed3.size());
    }

    // IErlElement findElement(IResource resource);
    @Test
    public void findElement() throws Exception {
        final IProject workspaceProject = project.getWorkspaceProject();
        final IErlElement findElement = model.findElement(workspaceProject);
        final IErlElement findElement2 = model.findElement(module
                .getCorrespondingResource());
        final IErlElement findElement3 = model.findElement(workspaceProject
                .getFolder("no_way"));
        final IErlElement findElement4 = model.findElement(workspaceProject.getParent());
        final IErlElement parent = module.getParent();
        final IFolder folder = (IFolder) parent.getResource();
        final IErlElement findElement5 = model.findElement(folder);
        project.open(null);
        final IErlElement srcFolder = project.getChildNamed("src");
        assertEquals(project, findElement);
        assertEquals(module, findElement2);
        assertNull(findElement3);
        assertNull(findElement4);
        assertEquals(srcFolder, findElement5);
    }

    // IErlElement findElement(IResource resource, boolean openElements);

    // IErlProject findProject(IProject project);
    @Test
    public void findProject() throws Exception {
        final IProject workspaceProject = project.getWorkspaceProject();
        final IProject workspaceProject2 = project2.getWorkspaceProject();
        final IErlProject findProject = model.findProject(workspaceProject);
        final IErlProject findProject2 = model.findProject(workspaceProject2);
        assertEquals(project, findProject);
        assertEquals(project2, findProject2);
    }

    // IErlModule findModule(IFile file);
    @Test
    public void findModule() throws Exception {
        final IErlModule module2 = createModule(project, "zz5.erl", "-module(z5z).\n");
        final IFile file = (IFile) module2.getResource();
        final IErlModule findModule = model.findModule(file);
        final IErlElement parent = module2.getParent();
        final IFolder folder = (IFolder) parent.getResource();
        IFile createFile = null;
        try {
            createFile = createFile(folder, "tt.erl", "-module(tt).\n");
            model.findModule(createFile);
            assertEquals(module2, findModule);
        } finally {
            if (createFile != null && createFile.exists()) {
                createFile.delete(true, null);
            }
        }
    }

    // IErlModule findModule(String name) throws ErlModelException;
    @Test
    public void findModuleByName() throws Exception {
        createModule(project2, "zz4.hrl", "-define(X, zz).\n");
        final IErlModule findModule = model.findModule("xx");
        final IErlModule findModule2 = model.findModule("xx.erl");
        final IErlModule findModule3 = model.findModule("yy.hrl");
        final IErlModule findModule4 = model.findModule("zz4");
        final IErlModule findModule5 = model.findModule("zz4.hrl");
        final IErlModule findModule6 = model.findModule("XX");
        assertEquals(module, findModule);
        assertEquals(module, findModule2);
        assertNull(findModule3);
        assertNull(findModule4);
        assertNull(findModule5);
        assertNull(findModule6);
    }

    // IErlModule findModule(String moduleName, String modulePath)
    // throws ErlModelException;
    @Test
    public void findModule_namePath() throws Exception {
        final IErlModule findModule = model.findModule("xx", null);
        final IErlModule findModule2 = model.findModule("xx.erl", null);
        final String filePath = module.getFilePath();
        final IErlModule findModule3 = model.findModule(null, filePath);
        final IErlModule findModule4 = model.findModule("apa", filePath);
        assertEquals(module, findModule);
        assertEquals(module, findModule2);
        assertEquals(module, findModule3);
        assertEquals(module, findModule4);
    }

    // IErlModule findInclude(final String includeName, final String
    // includePath)
    @Test
    public void findInclude() throws Exception {
        final IErlModule module2 = createModule(project2, "zz5.hrl", "-define(X, zz).\n");
        final IErlModule include = createInclude(project2, "xx5.hrl", "-define(X, xx).\n");
        final IErlModule findInclude = model.findInclude("zz5", null);
        assertNull(findInclude);
        final IErlModule findInclude2 = model.findInclude("xx5", null);
        assertEquals(include, findInclude2);
        final String filePath = include.getFilePath();
        final IErlModule findInclude3 = model.findInclude(null, filePath);
        assertEquals(include, findInclude3);
        final IErlModule findInclude4 = model.findInclude("apa", filePath);
        assertEquals(include, findInclude4);
        final IErlModule findInclude5 = model.findInclude(null, module2.getFilePath());
        assertNull(findInclude5);
    }

    // IErlElement innermostThat(final IErlElement el,
    // final IErlangFirstThat firstThat);

    // OtpErlangList getPathVars();
    @Test
    public void getPathVars() throws Exception {
        final OtpErlangList pathVars = model.getPathVars(project.getWorkspaceProject());
        final IPathVariableManager pvm = project.getCorrespondingResource()
                .getPathVariableManager();
        final String[] pathVariableNames = pvm.getPathVariableNames();
        final int n = pathVariableNames.length;
        assertEquals(n, pathVars.arity());

        final URI path = getTmpURIPath("");
        pvm.setURIValue(PV, path);
        final OtpErlangList pathVars2 = model.getPathVars(project.getWorkspaceProject());
        assertEquals(n + 1, pathVars2.arity());

        final OtpErlangTuple t = (OtpErlangTuple) pathVars2.elementAt(0);
        final String name = Util.stringValue(t.elementAt(0));
        final String value = pvm.getURIValue(name).getPath();
        String value2 = Util.stringValue(t.elementAt(1));
        if (!value2.startsWith("/")) {
            value2 = "/" + value2;
        }
        assertEquals(value, value2);
    }

    // IErlFunction findFunction(FunctionRef r) throws ErlModelException;
    @Test
    public void findFunction() throws Exception {
        final FunctionRef functionRef = new FunctionRef("xx", "f", 1);
        final FunctionRef functionRef2 = new FunctionRef("xx", "f",
                ErlangFunction.ANY_ARITY);
        final FunctionRef functionRef3 = new FunctionRef("xx", "f", 0);
        final IErlFunction findFunction = model.findFunction(functionRef);
        final IErlFunction findFunction2 = model.findFunction(functionRef2);
        final IErlFunction findFunction3 = model.findFunction(functionRef3);
        assertNotNull(findFunction);
        assertNotNull(findFunction2);
        assertNull(findFunction3);
    }

    // IErlModule findIncludeFromModule(IErlModule module, String includeName,
    // String includePath, Scope scope) throws ErlModelException;
    @Test
    public void findIncludeFromModule() throws Exception {
        File externalIncludeFile = null;
        final IErlProject myProject = project;
        final IProject workspaceProject = myProject.getWorkspaceProject();
        final IProject[] referencedProjects = workspaceProject.getReferencedProjects();
        final Collection<IPath> includeDirs = myProject.getProperties().getIncludeDirs();
        // given
        // a project with an external include and an internal include and a
        // referenced project with an include and an include in the same
        // directory as the module
        try {
            final String xxHrl = "xx.hrl";
            externalIncludeFile = createTmpFile(xxHrl,
                    "-record(rec2, {field, another=def}.");
            final String externalIncludePath = externalIncludeFile.getAbsolutePath();
            final IPath p = new Path(externalIncludePath).removeLastSegments(1);
            final List<IPath> newIncludeDirs = Lists.newArrayList(includeDirs);
            newIncludeDirs.add(p);
            ((ErlProject) myProject).setIncludeDirs(newIncludeDirs);
            final IErlModule include = createInclude(myProject, "yy8.hrl",
                    "-define(Y, include).\n");
            final IErlProject project1 = project2;
            final IErlModule referencedInclude = createInclude(project1, "zz8.hrl",
                    "-define(Z, referenced).\n");
            final IErlModule includeInModuleDir = createModule(myProject, "ww.hrl",
                    "-define(WW, x).\n");
            myProject.open(null);
            // when
            // looking for includes
            final String xx = "xx";
            final IErlModule x1 = model.findIncludeFromModule(module, xx, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule x2 = model.findIncludeFromModule(module, xx, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule x3 = model.findIncludeFromModule(module, xx, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String yy = "yy8";
            final IErlModule y1 = model.findIncludeFromModule(module, yy, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule y2 = model.findIncludeFromModule(module, yy, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule y3 = model.findIncludeFromModule(module, yy, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String zz = "zz8";
            final IErlModule z1 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z2 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z3 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(new IProject[] { project1
                    .getWorkspaceProject() });
            workspaceProject.setDescription(description, null);
            myProject.open(null);
            final IErlModule z4 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z5 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z6 = model.findIncludeFromModule(module, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String ww = "ww";
            final IErlModule w1 = model.findIncludeFromModule(module, ww, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlElementLocator mymodel = ErlangEngine.getInstance().getModel();
            final IErlModule w2 = mymodel.findIncludeFromProject(myProject, ww, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            // then
            // scope should be respected
            assertNotNull(x1);
            assertEquals(xxHrl, x1.getName());
            assertNotNull(x2);
            assertEquals(xxHrl, x2.getName());
            assertNotNull(x3);
            assertEquals(xxHrl, x3.getName());
            assertEquals(include, y1);
            assertEquals(include, y2);
            assertEquals(include, y3);
            assertNull(z1);
            assertEquals(referencedInclude, z2);
            assertNull(z3);
            assertNull(z4);
            assertEquals(referencedInclude, z5);
            assertEquals(referencedInclude, z6);
            assertEquals(includeInModuleDir, w1);
            assertNull(w2);
        } finally {
            if (externalIncludeFile != null && externalIncludeFile.exists()) {
                externalIncludeFile.delete();
            }
            ((ErlProject) myProject).setIncludeDirs(includeDirs);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(referencedProjects);
            workspaceProject.setDescription(description, null);
        }
    }

    // enum Scope {
    // PROJECT_ONLY, REFERENCED_PROJECTS, ALL_PROJECTS
    // }
    // IErlModule findModuleFromProject(IErlProject project, String moduleName,
    // String modulePath, Scope scope) throws ErlModelException;
    @Test
    public void findModuleFromProject() throws Exception {
        File externalModuleFile = null;
        File externalsFile = null;
        final IErlProject aProject = project;
        final IProject workspaceProject = aProject.getWorkspaceProject();
        final IProject[] referencedProjects = workspaceProject.getReferencedProjects();
        final String externalModulesString = aProject.getProperties()
                .getExternalModules();
        // given
        // a project with an external module and an internal module and a
        // referenced project with a module
        try {
            final String xxErl = "xx6.erl";
            externalModuleFile = createTmpFile(xxErl, "-module(xx6).\n");
            final String externalModulePath = externalModuleFile.getAbsolutePath();
            externalsFile = createTmpFile(XX_ERLIDEX, externalModulePath);
            ((ErlProject) aProject).setExternalModulesFile(externalsFile
                    .getAbsolutePath());
            final IErlModule aModule = createModule(aProject, "yy6.erl",
                    "-module(yy6).\n");
            final IErlProject project1 = project2;
            final IErlModule referencedModule = createModule(project1, "zz6.erl",
                    "-module(zz6).\n");
            aProject.open(null);
            // when
            // looking for modules
            final String xx = "xx6";
            final IErlModule x1 = model.findModuleFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule x2 = model.findModuleFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule x3 = model.findModuleFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String yy = "yy6";
            final IErlModule y1 = model.findModuleFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule y2 = model.findModuleFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule y3 = model.findModuleFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IErlModule y4 = model.findModuleFromProject(project1, yy, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule y5 = model.findModuleFromProject(project1, yy, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule y6 = model.findModuleFromProject(project1, yy, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String zz = "zz6";
            final IErlModule z1 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z2 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z3 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(new IProject[] { project1
                    .getWorkspaceProject() });
            workspaceProject.setDescription(description, null);
            aProject.open(null);
            final IErlModule z4 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z5 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z6 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            // then
            // scope should be respected
            assertNotNull(x1);
            assertEquals(xxErl, x1.getName());
            assertNotNull(x2);
            assertEquals(xxErl, x2.getName());
            assertNotNull(x3);
            assertEquals(xxErl, x3.getName());
            assertEquals(aModule, y1);
            assertEquals(aModule, y2);
            assertEquals(aModule, y3);
            assertNull(y4);
            assertEquals(aModule, y5);
            assertNull(y6);
            assertNull(z1);
            assertEquals(referencedModule, z2);
            assertNull(z3);
            assertNull(z4);
            assertEquals(referencedModule, z5);
            assertEquals(referencedModule, z6);
        } finally {
            if (externalModuleFile != null && externalModuleFile.exists()) {
                externalModuleFile.delete();
            }
            if (externalsFile != null && externalsFile.exists()) {
                externalsFile.delete();
            }
            ((ErlProject) aProject).setExternalModulesFile(externalModulesString);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(referencedProjects);
            workspaceProject.setDescription(description, null);
        }
    }

    @Test
    public void findModuleFromProject_preferProjectFile() throws Exception {
        File externalModuleFile = null;
        File externalsFile = null;
        final IErlProject aProject = project;
        final IProject workspaceProject = aProject.getWorkspaceProject();
        final IProject[] referencedProjects = workspaceProject.getReferencedProjects();
        final String externalModulesString = aProject.getProperties()
                .getExternalModules();
        // given
        // a project with an external include and a
        // referenced project with an include, both have same name
        try {
            final String zzErl = "zz9.erl";
            final String xxxContents = "-module(zz9).\n";
            externalModuleFile = createTmpFile(zzErl, xxxContents);
            final String externalModulePath = externalModuleFile.getAbsolutePath();
            externalsFile = createTmpFile(XX_ERLIDEX, externalModulePath);
            ((ErlProject) aProject).setExternalModulesFile(externalsFile
                    .getAbsolutePath());
            final IErlProject project1 = project2;
            final IErlModule referencedModule = createModule(project1, zzErl, xxxContents);
            aProject.open(null);
            // when
            // looking for module
            final String zz = "zz9";
            final IErlModule zz1 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule zz2 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule zz3 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(new IProject[] { project1
                    .getWorkspaceProject() });
            workspaceProject.setDescription(description, null);
            aProject.open(null);
            final IErlModule zz4 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule zz5 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule zz6 = model.findModuleFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            // then
            // the non-external should be preferred
            assertNotNull(zz1);
            assertEquals(zzErl, zz1.getName());
            assertNotSame(referencedModule, zz1);
            assertEquals(referencedModule, zz2);
            assertNotNull(zz3);
            assertEquals(zzErl, zz3.getName());
            assertNotSame(referencedModule, zz3);
            assertNotNull(zz4);
            assertNotSame(referencedModule, zz4);
            assertEquals(referencedModule, zz5);
            assertEquals(referencedModule, zz6);
        } finally {
            if (externalModuleFile != null && externalModuleFile.exists()) {
                externalModuleFile.delete();
            }
            if (externalsFile != null && externalsFile.exists()) {
                externalsFile.delete();
            }
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(referencedProjects);
            workspaceProject.setDescription(description, null);
            ((ErlProject) aProject).setExternalModulesFile(externalModulesString);
        }
    }

    // IErlModule findIncludeFromProject(IErlProject project, String
    // includeName, String includePath, Scope scope) throws ErlModelException;
    @Test
    public void findIncludeFromProject() throws Exception {
        File externalIncludeFile = null;
        final IErlProject aProject = project;
        final IProject workspaceProject = aProject.getWorkspaceProject();
        final IProject[] referencedProjects = workspaceProject.getReferencedProjects();
        final Collection<IPath> includeDirs = aProject.getProperties().getIncludeDirs();
        // given
        // a project with an external include and an internal include and a
        // referenced project with an include
        try {
            final String xxHrl = "xx.hrl";
            externalIncludeFile = createTmpFile(xxHrl,
                    "-record(rec2, {field, another=def}.");
            final String externalIncludePath = externalIncludeFile.getAbsolutePath();
            final IPath p = new Path(externalIncludePath).removeLastSegments(1);
            final List<IPath> newIncludeDirs = Lists.newArrayList(includeDirs);
            newIncludeDirs.add(p);
            ((ErlProject) aProject).setIncludeDirs(newIncludeDirs);
            final IErlModule include = createInclude(aProject, "yy5.hrl",
                    "-define(Y, include).\n");
            final IErlProject project1 = project2;
            final IErlModule referencedInclude = createInclude(project1, "zz5.hrl",
                    "-define(Z, referenced).\n");
            aProject.open(null);
            // when
            // looking for includes
            final String xx = "xx";
            final IErlModule x1 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule x2 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule x3 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String yy = "yy5";
            final IErlModule y1 = model.findIncludeFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule y2 = model.findIncludeFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule y3 = model.findIncludeFromProject(aProject, yy, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IErlModule y4 = model.findIncludeFromProject(project1, yy, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule y5 = model.findIncludeFromProject(project1, yy, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule y6 = model.findIncludeFromProject(project1, yy, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final String zz = "zz5";
            final IErlModule z1 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z2 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z3 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(new IProject[] { project1
                    .getWorkspaceProject() });
            workspaceProject.setDescription(description, null);
            aProject.open(null);
            final IErlModule z4 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule z5 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule z6 = model.findIncludeFromProject(aProject, zz, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            // then
            // scope should be respected
            assertNotNull(x1);
            assertEquals(xxHrl, x1.getName());
            assertNotNull(x2);
            assertEquals(xxHrl, x2.getName());
            assertNotNull(x3);
            assertEquals(xxHrl, x3.getName());
            assertEquals(include, y1);
            assertEquals(include, y2);
            assertEquals(include, y3);
            assertNull(y4);
            assertEquals(include, y5);
            assertNull(y6);
            assertNull(z1);
            assertEquals(referencedInclude, z2);
            assertNull(z3);
            assertNull(z4);
            assertEquals(referencedInclude, z5);
            assertEquals(referencedInclude, z6);
        } finally {
            if (externalIncludeFile != null && externalIncludeFile.exists()) {
                externalIncludeFile.delete();
            }
            ((ErlProject) aProject).setIncludeDirs(includeDirs);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(referencedProjects);
            workspaceProject.setDescription(description, null);
        }
    }

    @Test
    public void findInclude_preferProjectFile() throws Exception {
        File externalIncludeFile = null;
        final IErlProject aProject = project;
        final IProject workspaceProject = aProject.getWorkspaceProject();
        final IProject[] referencedProjects = workspaceProject.getReferencedProjects();
        final Collection<IPath> includeDirs = aProject.getProperties().getIncludeDirs();
        // given
        // a project with an external include and a
        // referenced project with an include, both have same name
        try {
            final String xxHrl = "xx.hrl";
            externalIncludeFile = createTmpFile(xxHrl,
                    "-record(rec2, {field, another=def}.");
            final String externalIncludePath = externalIncludeFile.getAbsolutePath();
            final IPath p = new Path(externalIncludePath).removeLastSegments(1);
            final List<IPath> newIncludeDirs = Lists.newArrayList(includeDirs);
            newIncludeDirs.add(p);
            ((ErlProject) aProject).setIncludeDirs(newIncludeDirs);
            final IErlProject project1 = project2;
            final IErlModule referencedInclude = createInclude(project1, xxHrl,
                    "-define(Z, referenced).\n");
            aProject.open(null);
            // when
            // looking for includes
            final String xx = "xx";
            final IErlModule x1 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule x2 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule x3 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(new IProject[] { project1
                    .getWorkspaceProject() });
            workspaceProject.setDescription(description, null);
            aProject.open(null);
            final IErlModule x4 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.PROJECT_ONLY);
            final IErlModule x5 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.ALL_PROJECTS);
            final IErlModule x6 = model.findIncludeFromProject(aProject, xx, null,
                    IErlElementLocator.Scope.REFERENCED_PROJECTS);
            // then
            // the non-external should be preferred
            assertNotNull(x1);
            assertEquals(xxHrl, x1.getName());
            assertNotSame(referencedInclude, x1);
            assertEquals(referencedInclude, x2);
            assertNotNull(x3);
            assertEquals(xxHrl, x3.getName());
            assertNotSame(referencedInclude, x3);
            assertNotNull(x4);
            assertNotSame(referencedInclude, x4);
            assertEquals(referencedInclude, x5);
            assertEquals(referencedInclude, x6);
        } finally {
            if (externalIncludeFile != null && externalIncludeFile.exists()) {
                externalIncludeFile.delete();
            }
            ((ErlProject) aProject).setIncludeDirs(includeDirs);
            final IProjectDescription description = workspaceProject.getDescription();
            description.setReferencedProjects(referencedProjects);
            workspaceProject.setDescription(description, null);
        }
    }

}
TOP

Related Classes of org.erlide.engine.model.erlang.ErlModelTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.