using System; using System.Collections.Generic; using JetBrains.Metadata.Reader.API; using JetBrains.ProjectModel; using JetBrains.ReSharper.Psi; using JetBrains.ReSharper.UnitTestExplorer; using MbUnit.ReSharperRunner.Elements; namespace MbUnit.ReSharperRunner.Explorer { internal class MbUnitAssemblyExplorer { private CLRTypeName ignoreAttribute = new CLRTypeName(AttributesNames.IgnoreAttributeName); private CLRTypeName categoryAttribute = new CLRTypeName(AttributesNames.TestCategoryAttributeName); private readonly AssemblyHelper helper = new AssemblyHelper(); public void ExploreAssembly(IUnitTestProvider provider, IMetadataAssembly assembly, IProject project, UnitTestElementConsumer consumer) { if (provider == null) { throw new ArgumentNullException("provider"); } if (assembly == null) { throw new ArgumentNullException("assembly"); } if (project == null) { throw new ArgumentNullException("project"); } if (consumer == null) { throw new ArgumentNullException("consumer"); } foreach (IMetadataTypeInfo type in assembly.GetTypes()) { if (helper.IsTestFixture(type)) { List fixtureCategories = new List(); helper.CollectTestFixtureCategories(type, ref fixtureCategories); MbUnitTestFixtureElement fixture = new MbUnitTestFixtureElement(provider, project, type.FullyQualifiedName, assembly.Location, helper.GetTestFixtureName(type)); fixture.AssignCategories(fixtureCategories); fixture.SetExplicit(GetExplicitString(type)); consumer(fixture); foreach (IMetadataMethod method in helper.GetAllTestMethods(type)) { if (helper.IsTestMethod(method)) { List methodCategories = new List(fixtureCategories); helper.CollectTestMethodCategories(method, ref methodCategories); MbUnitTestElement testElement = new MbUnitTestElement( provider, fixture, project, method.DeclaringType.FullyQualifiedName, helper.GetMethodName(method), helper.GetTestName(method)); if (helper.HasChildTest(method)) { foreach (string childName in helper.GetChildNames(method)) { MbUnitChildTestElement childTest = new MbUnitChildTestElement(testElement, provider, project, method.DeclaringType.FullyQualifiedName, childName, helper.GetTestName(method)); childTest.AssignCategories(methodCategories); consumer(childTest); } } // testElement.SetExplicit(GetExplicitString(method)); testElement.AssignCategories(methodCategories); consumer(testElement); } } } } } private string GetExplicitString(IMetadataEntity method) { IMetadataCustomAttribute[] attributes = method.GetCustomAttributes(ignoreAttribute.ClrName); string reason = null; for (int i = 0; i < attributes.Length; i++) { object[] arguments = attributes[i].ConstructorArguments; if (arguments.Length == 1) { string r = arguments[0] as string; if (r == null) { if (reason == null) { reason = ""; } } else { reason = r; } } else if (reason == null) { reason = ""; } } return reason; } } }