using System; using System.Collections.Generic; using JetBrains.ProjectModel; using JetBrains.ReSharper.Psi; using JetBrains.ReSharper.Psi.Caches; using JetBrains.ReSharper.Psi.Tree; using JetBrains.ReSharper.UnitTestExplorer; using JetBrains.Shell; using JetBrains.Util; namespace MbUnit.ReSharperRunner.Elements { public abstract class MbUnitTestElementBase : UnitTestElement, IEquatable { private readonly ProjectModelElementEnvoy currentProject; private readonly string currentTypeName; public MbUnitTestElementBase(IUnitTestProvider provider, UnitTestElement parent, IProjectModelElement project, string typeName) : base(provider, parent) { if ((project == null) && !Shell.Instance.IsTestShell) { throw new ArgumentNullException("project"); } if (typeName == null) { throw new ArgumentNullException("typeName"); } if (project != null) { currentProject = new ProjectModelElementEnvoy(project); } currentTypeName = typeName; } #region IEquatable Members public bool Equals(MbUnitTestElementBase mbUnitTestElementBase) { if (mbUnitTestElementBase == null) { return false; } if (!base.Equals(mbUnitTestElementBase)) { return false; } return Equals(currentProject, mbUnitTestElementBase.currentProject) && Equals(currentTypeName, mbUnitTestElementBase.currentTypeName); } #endregion public override IProject GetProject() { return (currentProject.GetValidProjectElement() as IProject); } public override string GetTypeClrName() { return currentTypeName; } public override UnitTestNamespace GetNamespace() { return new UnitTestNamespace(new CLRTypeName(currentTypeName).NamespaceName); } public override IList GetProjectItems() { ITypeElement declaredType = GetDeclaredType(); if (declaredType == null) { return EmptyArray.Instance; } return declaredType.GetProjectFiles(); } protected ITypeElement GetDeclaredType() { ITypeElement typeElement; IProject project = GetProject(); if (project == null) { return null; } PsiManager manager = PsiManager.GetInstance(project.GetSolution()); using (ReadLockCookie.Create()) { typeElement = manager.GetDeclarationsCache(DeclarationsCacheScope.ProjectScope(project, true), true). GetTypeElementByCLRName(currentTypeName); } return typeElement; } public override UnitTestElementDisposition GetDisposition() { IDeclaredElement element = GetDeclaredElement(); if ((element == null) || !element.IsValid()) { return UnitTestElementDisposition.ourInvalidDisposition; } List locations = new List(); foreach (IDeclaration declaration in element.GetDeclarations()) { IFile file = declaration.GetContainingFile(); if (file != null) { locations.Add( new UnitTestElementLocation(file.ProjectItem, declaration.GetNameRange(), declaration.GetDocumentRange().TextRange)); } } return new UnitTestElementDisposition(locations, this); } public override bool Equals(object obj) { if (ReferenceEquals(this, obj)) { return true; } return Equals(obj as MbUnitTestElementBase); } public override int GetHashCode() { int result = base.GetHashCode(); result = 29 * result + (currentProject != null ? currentProject.GetHashCode() : 0); result = 29 * result + (currentTypeName != null ? currentTypeName.GetHashCode() : 0); return result; } } }