// I always have to think about what this means... // Based on Type.IsAssignableFrom(c) // "Determines whether an instance of the current Type can be assigned from an instance of the specified Type" // True if c and the current type are the same type, or if the current type is in the inheritance hierarchy of // the type c // In other words, type.IsAssignableFrom(c) is true if I can assign an instance of c to a variable of type "type" // e.g. typeof(IUnitTestProvider).IsAssignableFrom(typeof(UnitTestProvider)) public static bool IsAssignableFrom(this Type type, IMetadataTypeInfo c) { // TODO: Can this cause an infinite loop/stack overflow? // I think maybe not, because we're dealing with metadata, which means we've successfully compiled // and it'll be very hard to compile circular inheritance chains return type.FullName == c.FullyQualifiedName || (c.Base != null && type.IsAssignableFrom(c.Base.Type)); }
static IEnumerable <IMetadataField> GetPrivateFieldsWith(this IMetadataTypeInfo type, Type fieldType) { return(type.GetPrivateFields() .Where(x => x.Type is IMetadataClassType) .Where(x => new CLRTypeName(((IMetadataClassType)x.Type).Type.FullyQualifiedName) == new CLRTypeName(fieldType.FullName))); }
public static string GetSubjectString(this IMetadataTypeInfo type) { var attribute = type.AndAllBaseTypes() .SelectMany(x => x.GetCustomAttributes(typeof(SubjectAttribute).FullName)) .FirstOrDefault(); if (attribute == null) { if (type.DeclaringType == null) { return(null); } return(type.DeclaringType.GetSubjectString()); } var parameters = attribute.ConstructorArguments.Select(x => { var typeArgument = x as IMetadataClassType; if (typeArgument != null) { return(new CLRTypeName(typeArgument.Type.FullyQualifiedName).ShortName); } return((string)x); }) .ToArray(); return(String.Join(" ", parameters)); }
private List <IMetadataMethod> GetAllTestMethods(IMetadataTypeInfo type) { myTempMethods.Clear(); myTempMethodsNames.Clear(); var currentType = type; while (currentType != null) { foreach (var method in currentType.GetMethods()) { if (method.IsVirtual) { if (myTempMethodsNames.Contains(method.Name)) { continue; } if (!method.IsNewSlot) { myTempMethodsNames.Add(method.Name); } } if (IsTestMethod(method)) { myTempMethods.Add(method); } } var baseOfCurrentType = currentType.Base; currentType = (baseOfCurrentType != null) ? baseOfCurrentType.Type : null; } return(myTempMethods); }
// I always have to think about what this means... // Based on Type.IsAssignableFrom(c) // "Determines whether an instance of the current Type can be assigned from an instance of the specified Type" // True if c and the current type are the same type, or if the current type is in the inheritance hierarchy of // the type c // In other words, type.IsAssignableFrom(c) is true if I can assign an instance of c to a variable of type "type" // e.g. typeof(IUnitTestProvider).IsAssignableFrom(typeof(UnitTestProvider)) public static bool IsAssignableFrom(this Type type, IMetadataTypeInfo c) { // TODO: Can this cause an infinite loop/stack overflow? // I think maybe not, because we're dealing with metadata, which means we've successfully compiled // and it'll be very hard to compile circular inheritance chains return(type.FullName == c.FullyQualifiedName || (c.Base != null && type.IsAssignableFrom(c.Base.Type))); }
static IEnumerable <IMetadataField> GetInstanceFieldsOfType(this IMetadataTypeInfo type, Type fieldType) { var metadataFields = type.GetInstanceFields(); var fields = metadataFields.Where(x => x.Type is IMetadataClassType); return(fields.Where(x => (((IMetadataClassType)x.Type).Type.FullyQualifiedName == fieldType.FullName))); }
private static IEnumerable <IMetadataField> GetInstanceFieldsOfType(this IMetadataTypeInfo type, string fullyQualifiedName) { var metadataFields = type.GetInstanceFields(); var fields = metadataFields.Where(x => x.Type is IMetadataClassType); return(fields.Where(x => (((IMetadataClassType)x.Type).Type.HasCustomAttribute(fullyQualifiedName)))); }
public bool IsValidTestClass(IProject project, IMetadataTypeInfo typeInfo) { if (project == null || typeInfo == null) return false; return IsValidTestClass(project, typeInfo.FullyQualifiedName); }
public void Explore(IProject project, IMetadataAssembly assembly, UnitTestElementConsumer consumer, IMetadataTypeInfo metadataTypeInfo) { if (!metadataTypeInfo.IsContext()) { return; } var contextElement = _factories.Contexts.CreateContext(project, assembly.Location.FullPath, metadataTypeInfo); consumer(contextElement); metadataTypeInfo.GetSpecifications() .ForEach(x => consumer(_factories.ContextSpecifications.CreateContextSpecification(contextElement, x))); metadataTypeInfo.GetBehaviors().ForEach(x => { var behaviorElement = _factories.Behaviors.CreateBehavior(contextElement, x); consumer(behaviorElement); _factories.BehaviorSpecifications .CreateBehaviorSpecificationsFromBehavior(behaviorElement, x) .ForEach(y => consumer(y)); }); }
public ITestMetadata GetTestMetadata(IMetadataTypeInfo type) { var isCompilerGenerated = type.GetAttributeData <CompilerGeneratedAttribute>() != null; if (isCompilerGenerated) { return(null); } if (!IsSuite(type)) { return(null); } var identity = _assemblyIdentity.CreateChildIdentity(type.FullyQualifiedName); var categories = type.GetAttributeData <CategoriesAttribute>().GetValueOrDefault( x => x.ConstructorArguments[0].ValuesArray.Select(y => (string)y.Value), () => new string[0]).NotNull(); var text = GetText(type); var fieldTests = type.GetFields() .TakeWhile(_notInterrupted) .Select(x => GetFieldTest(x, identity)) .WhereNotNull(); return(new TypeTestMetadata(identity, _project, categories, text, fieldTests, type)); }
static bool IsStruct(this IMetadataTypeInfo type) { if (type.Base != null) { return(type.Base.Type.FullyQualifiedName == typeof(ValueType).FullName); } return(false); }
public TypeTestMetadata(IIdentity identity, IProject project, IEnumerable<string> categories, string text, IMetadataTypeInfo metadataTypeInfo) : base(metadataTypeInfo) { _identity = identity; _project = project; _text = text; _categories = categories; }
public ContextElement CreateContext(IProject project, string assemblyPath, IMetadataTypeInfo type) { return GetOrCreateContext(assemblyPath, project, _reflectionTypeNameCache.GetClrName(type), type.GetSubjectString(), type.GetTags(), type.IsIgnored()); }
private XunitTestClassElement GetParentClassElement(IMetadataTypeInfo type) { if (!type.IsNested) { return null; } return factory.GetElementById(project, type.DeclaringType.FullyQualifiedName) as XunitTestClassElement; }
public ContextElement CreateContext(IProject project, string assemblyPath, IMetadataTypeInfo type) { return(this.GetOrCreateContext(assemblyPath, project, this._reflectionTypeNameCache.GetClrName(type), type.GetSubjectString(), type.GetTags(), type.IsIgnored())); }
// IsGenericParameter indicates that this type is a type representing a generic // "placeholder", e.g. T. A generic argument is the value that populates a generic // parameter. So for IEnumerable<T>, T is a generic parameter. For IEnumerable<string>, // string is a generic argument private MetadataTypeInfoAdapter2(IMetadataClassType metadataType, IMetadataTypeInfo metadataTypeInfo) { // Assertion.Assert(metadataTypeInfo.IsResolved, "Unresolved type: {0}", metadataType); Assertion.Assert(metadataTypeInfo != null, "metadataTypeInfo cannot be null"); this.metadataType = metadataType; this.metadataTypeInfo = metadataTypeInfo; }
private static bool IsTestFixture(IMetadataTypeInfo typeInfo) { if (!typeInfo.IsAbstract && (typeInfo.IsPublic || typeInfo.IsNestedPublic) && typeInfo.GenericParameters.Length == 0) { return(HasTestFixtureAttribute(typeInfo)); } return(false); }
public static bool IsContext(this IMetadataTypeInfo type) { return(!type.IsAbstract && !type.IsStruct() && type.GenericParameters.Length == 0 && !type.HasCustomAttribute(FullNames.BehaviorsAttribute) && (type.GetSpecifications().Any() || type.GetBehaviors().Any())); }
public bool IsValidTestClass(IProject project, IMetadataTypeInfo typeInfo) { if (project == null || typeInfo == null) { return(false); } return(IsValidTestClass(project, typeInfo.FullyQualifiedName)); }
public ContextElement CreateContext(IProject project, string assemblyPath, IMetadataTypeInfo contextType) { return(this.GetOrCreateContext(assemblyPath, project, new ClrTypeName(contextType.FullyQualifiedName), contextType.GetSubjectString(), contextType.GetTags(), contextType.IsIgnored())); }
public ITestMetadata GetTestMetadata(IMetadataTypeInfo type) { var isCompilerGenerated = type.GetAttributeData <CompilerGeneratedAttribute>() != null; if (isCompilerGenerated) { return(null); } var hasBecauseField = type.GetFields().Any(x => { var metadataClassType = x.Type as IMetadataClassType; if (metadataClassType == null) { return(false); } var fullyQualifiedName = metadataClassType.Type.FullyQualifiedName; return(fullyQualifiedName == "Machine.Specifications.Because"); }); if (!hasBecauseField) { return(null); } var text = type.DescendantsAndSelf(x => x.DeclaringType) .Select( x => { var subjectAttributeData = x.GetAttributeData("Machine.Specifications.SubjectAttribute"); if (subjectAttributeData == null) { return(null); } var subjectType = subjectAttributeData.ConstructorArguments.First().Type.NotNull().ToCommon(); return(subjectType.Name + ", " + type.ToCommon().Name.Replace(oldChar: '_', newChar: ' ')); }).WhereNotNull().FirstOrDefault(); if (text == null) { return(null); } var identity = _assemblyIdentity.CreateChildIdentity(type.FullyQualifiedName); var categories = type.GetAttributeData <CategoriesAttribute>().GetValueOrDefault( x => x.ConstructorArguments[0].ValuesArray.Select(y => (string)y.Value), () => new string[0]).NotNull(); var fieldTests = type.GetFields().SelectMany(Flatten) .TakeWhile(_notInterrupted) .Select(x => GetFieldTest(x, identity)) .WhereNotNull(); return(new TypeTestMetadata(identity, _project, categories, text, fieldTests, type)); }
private string GetText(IMetadataTypeInfo type) { var subjectAttribute = type.DescendantsAndSelf(x => x.DeclaringType) .Select(x => x.GetAttributeData(MSpecUtility.SubjectAttributeFullName)).WhereNotNull().First(); var subjectTypes = subjectAttribute.ConstructorArguments.Select(x => x.Value as IMetadataType).WhereNotNull(); var subjectText = subjectAttribute.ConstructorArguments.Select(x => x.Value as string).WhereNotNull().FirstOrDefault(); return(MSpecUtility.CreateText(type.ToCommon(), subjectTypes.SingleOrDefault()?.ToCommon(), subjectText)); }
// typeof(Base).IsAssignableFrom(typeof(Derived)) == true // typeof(Interface).IsAssignableFrom(typeof(Implementor)) == true public static bool IsAssignableFrom(this Type type, IMetadataTypeInfo c) { if (type.FullName == c.FullyQualifiedName) return true; if (type.IsInterface) return c.Interfaces.Any(i => type.FullName == i.Type.FullyQualifiedName); return c.Base != null && type.IsAssignableFrom((Type) c.Base.Type); }
public ContextElement CreateContext(IMetadataTypeInfo type) { return new ContextElement(_provider, _project, type.FullyQualifiedName, _assemblyPath, type.GetSubjectString(), type.GetTags(), type.IsIgnored()); }
private bool IsSuite(IMetadataTypeInfo type) { if (type.GetAttributeData(MSpecUtility.BehaviorsAttributeFullName) != null) { return(false); } return(type.GetFields().Select(x => x.Type).OfType <IMetadataClassType>().Select(x => x.Type.FullyQualifiedName) .Any(x => x == MSpecUtility.ItDelegateFullName || x == MSpecUtility.BehavesLikeDelegateFullName)); }
public ContextElement CreateContext(IMetadataTypeInfo type) { return(new ContextElement(_provider, _projectEnvoy, type.FullyQualifiedName, _assemblyPath, type.GetSubjectString(), type.GetTags(), type.IsIgnored())); }
public string Present(IMetadataTypeInfo type) { var subjectAttributeData = type.GetAttributeData<SuiteAttributeBase>(); if (subjectAttributeData == null) return null; var subjectAttribute = subjectAttributeData.ToCommon(); var displayFormatAttribute = subjectAttributeData.UsedConstructor.GetAttributeData<DisplayFormatAttribute>().ToCommon(); return _introspectionPresenter.Present(displayFormatAttribute, type.ToCommon(), subjectAttribute); }
public IEnumerable <BehaviorSpecificationElement> CreateBehaviorSpecificationsFromBehavior( BehaviorElement behavior, IMetadataField behaviorSpecification) { IMetadataTypeInfo typeContainingBehaviorSpecifications = behaviorSpecification.GetFirstGenericArgument(); foreach (var specification in typeContainingBehaviorSpecifications.GetSpecifications()) { yield return(CreateBehaviorSpecification(behavior, specification)); } }
public TypeTestMetadata( IIdentity identity, IProject project, string text, IMetadataTypeInfo metadataTypeInfo) : base(metadataTypeInfo) { _identity = identity; _project = project; _text = text; }
private void ExploreType(IProject project, IMetadataAssembly assembly, UnitTestElementConsumer consumer, IMetadataTypeInfo metadataTypeInfo) { // It would be nice to use TestClassCommandFactory.Make(...), but that doesn't work // with RunWith, since Make ends up calling TypeUtility.GetRunWith, which tries to // call IAttributeInfo.GetInstance<RunWithAttribute>, and we can't support that. // So we'll break down Make and do it ourselves. If the runner finds any methods // that we don't find, it will create them at runtime var typeInfo = metadataTypeInfo.AsTypeInfo(); if (TypeUtility.IsTestClass(typeInfo)) ExploreTestClass(project, assembly, consumer, typeInfo, metadataTypeInfo.FullyQualifiedName); }
public BehaviorElement CreateBehavior(ContextElement context, IMetadataField behavior) { IMetadataTypeInfo typeContainingBehaviorSpecifications = behavior.GetFirstGenericArgument(); return(new BehaviorElement(_provider, context, _project, behavior.DeclaringType.FullyQualifiedName, behavior.Name, behavior.IsIgnored() || typeContainingBehaviorSpecifications.IsIgnored())); }
public static IEnumerable <IMetadataField> GetBehaviors(this IMetadataTypeInfo type) { IEnumerable <IMetadataField> behaviorFields = type.GetInstanceFieldsOfType(typeof(Behaves_like <>)); foreach (IMetadataField field in behaviorFields) { if (field.GetFirstGenericArgument().HasCustomAttribute(typeof(BehaviorsAttribute).FullName)) { yield return(field); } } }
private ITestMetadata VisitType(IMetadataTypeInfo type, IIdentity parentIdentity) { var text = _metadataPresenter.Present(type); if (text == null) return null; var identity = parentIdentity.CreateChildIdentity(type.FullyQualifiedName); var categories = type.GetAttributeData<CategoriesAttribute>().GetValueOrDefault( x => x.ConstructorArguments[0].ValuesArray.Select(y => (string) y.Value), () => new string[0]); return new TypeTestMetadata(identity, _project, categories, text, type); }
private void ExploreSpecificationContainer(IProject project, SpecificationContainerElement container, UnitTestElementConsumer consumer, IMetadataTypeInfo metadataTypeInfo) { foreach (var field in metadataTypeInfo.GetFields()) { if (!field.IsPublic) continue; var x = field.Name; var methodElement = _elementFactory.GetOrCreateSpecificationElement(project, container, new ClrTypeName(metadataTypeInfo.FullyQualifiedName), x, string.Empty); consumer(methodElement); } }
public static IEnumerable <IMetadataField> GetBehaviors(this IMetadataTypeInfo type) { IEnumerable <IMetadataField> behaviorFields = type.GetInstanceFieldsOfType(new BehaviorDelegateAttributeFullName()); foreach (IMetadataField field in behaviorFields) { if (field.GetFirstGenericArgument().HasCustomAttribute(new BehaviorAttributeFullName())) { yield return(field); } } }
private void ProcessTypeInfo(IMetadataTypeInfo metadataTypeInfo) { ITypeInfo typeInfo = metadataTypeInfo.AsTypeInfo(); // TODO: What about HasRunWith support? Not supported in previous R# versions if (!UnitTestElementMetadataIdentifier.IsUnitTestContainer(metadataTypeInfo)) return; ITestClassCommand testClassCommand = TestClassCommandFactory.Make(typeInfo); if (testClassCommand == null) return; ProcessTestClass(new ClrTypeName(metadataTypeInfo.FullyQualifiedName), testClassCommand.EnumerateTestMethods(), GetParentClassElement(metadataTypeInfo)); }
public ContextElement CreateContext(IMetadataTypeInfo type) { return GetOrCreateContextElement(_provider, #if RESHARPER_61 _manager, _psiModuleManager, _cacheManager, #endif _project, _projectEnvoy, type.FullyQualifiedName, _assemblyPath); }
private ITestMetadata VisitType(IMetadataTypeInfo type, IIdentity parentIdentity) { var text = _metadataPresenter.Present(type); if (text == null) { return(null); } var identity = parentIdentity.CreateChildIdentity(type.FullyQualifiedName); return(new TypeTestMetadata(identity, _project, text, type)); }
public ContextElement CreateContext(IMetadataTypeInfo type) { return(GetOrCreateContext(_provider, _manager, _psiModuleManager, _cacheManager, _project, _projectEnvoy, _reflectionTypeNameCache.GetClrName(type), _assemblyPath, type.GetSubjectString(), type.GetTags(), type.IsIgnored())); }
public string Present(IMetadataTypeInfo type) { var subjectAttributeData = type.GetAttributeData <SubjectAttributeBase>(); if (subjectAttributeData == null) { return(null); } var subjectAttribute = subjectAttributeData.ToCommon(); var displayFormatAttribute = subjectAttributeData.UsedConstructor.GetAttributeData <DisplayFormatAttribute>().ToCommon(); return(_introspectionPresenter.Present(displayFormatAttribute, subjectAttribute)); }
public ContextElement CreateContext(IMetadataTypeInfo type) { if (type.IsNested || !type.IsPublic) { return null; } return new ContextElement(_provider, _project, type.FullyQualifiedName, _assemblyPath, type.GetTags(), type.IsIgnored()); }
public ContextElement CreateContext(IMetadataTypeInfo type) { return GetOrCreateContext(_provider, _manager, _psiModuleManager, _cacheManager, _project, _projectEnvoy, _reflectionTypeNameCache.GetClrName(type), _assemblyPath, type.GetSubjectString(), type.GetTags(), type.IsIgnored()); }
public ITestMetadata GetTestMetadata(IMetadataTypeInfo type) { var text = _metadataPresenter.Present(type, suiteAttributeType: "TestFx.SpecK.SubjectAttribute"); if (text == null) { return(null); } var identity = _assemblyIdentity.CreateChildIdentity(type.FullyQualifiedName); var categories = type.GetAttributeData <CategoriesAttribute>().GetValueOrDefault( x => x.ConstructorArguments[0].ValuesArray.Select(y => (string)y.Value), () => new string[0]).NotNull(); return(new TypeTestMetadata(identity, _project, categories, text, new ITestMetadata[0], type)); }
private ITestMetadata VisitType(IMetadataTypeInfo type, IIdentity parentIdentity) { var text = _metadataPresenter.Present(type); if (text == null) { return(null); } var identity = parentIdentity.CreateChildIdentity(type.FullyQualifiedName); var categories = type.GetAttributeData <CategoriesAttribute>().GetValueOrDefault( x => x.ConstructorArguments[0].ValuesArray.Select(y => (string)y.Value), () => new string[0]); return(new TypeTestMetadata(identity, _project, categories, text, type)); }
private static bool HasTestFixtureAttribute(IMetadataTypeInfo typeInfo) { if (typeInfo.HasCustomAttribute(TestFixtureAttribute.ClrName)) { return(true); } IMetadataClassType baseType = typeInfo.Base; if (baseType != null) { return(HasTestFixtureAttribute(baseType.Type)); } return(false); }
public static IEnumerable <IMetadataField> GetBehaviors(this IMetadataTypeInfo type) { IEnumerable <IMetadataField> behaviorFields = type.GetPrivateFieldsWith(typeof(Behaves_like <>)); foreach (IMetadataField field in behaviorFields) { if (field.GetFirstGenericArgument().HasCustomAttribute(typeof(BehaviorsAttribute).FullName) #if !RESHARPER_5 && field.GetFirstGenericArgument().GenericParameters.Length == 0 #endif ) { yield return(field); } } }
public ContextElement CreateContext(IMetadataTypeInfo type) { return GetOrCreateContextElement(_provider, #if RESHARPER_61 _manager, _psiModuleManager, _cacheManager, #endif _project, _projectEnvoy, #if RESHARPER_61 _reflectionTypeNameCache.GetClrName(type), #else new ClrTypeName(type.FullyQualifiedName), // may work incorrect in ReSharper 6.0 #endif _assemblyPath, type.GetSubjectString(), type.GetTags(), type.IsIgnored()); }
public void Explore(IProject project, IMetadataAssembly assembly, IUnitTestElementsObserver consumer, IMetadataTypeInfo metadataTypeInfo) { if (!metadataTypeInfo.IsContext()) { return; } var contextElement = this._factories.Contexts.CreateContext(project, assembly.Location.FullPath, metadataTypeInfo); consumer.OnUnitTestElement(contextElement); metadataTypeInfo.GetSpecifications() .ForEach(x => { var element = this._factories.ContextSpecifications.CreateContextSpecification(contextElement, x); consumer.OnUnitTestElement(element); consumer.OnUnitTestElementChanged(element); }); metadataTypeInfo.GetBehaviors().ForEach(x => { var behaviorElement = this._factories.Behaviors.CreateBehavior(contextElement, x); consumer.OnUnitTestElement(behaviorElement); consumer.OnUnitTestElementChanged(behaviorElement); this._factories.BehaviorSpecifications .CreateBehaviorSpecificationsFromBehavior(behaviorElement, x) .ForEach(y => { consumer.OnUnitTestElement(y); consumer.OnUnitTestElementChanged(y); }); }); }
public static bool IsPublic(IMetadataTypeInfo type) { // Hmmm. This seems a little odd. Resharper reports public nested types with IsNestedPublic, // while IsPublic is false return type.IsPublic || (type.IsNested && type.IsNestedPublic); }
protected MetadataTypeInfoBase(IMetadataTypeInfo metadataTypeInfo) : base(metadataTypeInfo) { _metadataTypeInfo = metadataTypeInfo; }
public static bool IsUnitTestContainer(IMetadataTypeInfo metadataTypeInfo) { return IsDirectUnitTestClass(metadataTypeInfo); }
public MetadataTypeInfoAdapter2(IMetadataTypeInfo metadataTypeInfo) : this(null, metadataTypeInfo) { this.metadataTypeInfo = metadataTypeInfo; }
private static bool IsDirectUnitTestClass(IMetadataTypeInfo metadataTypeInfo) { return IsExportedType(metadataTypeInfo) && TypeUtility.IsTestClass(metadataTypeInfo.AsTypeInfo()); }
public MetadataTypeInfoWrapper(IMetadataTypeInfo metadataTypeInfo) { this.metadataTypeInfo = metadataTypeInfo; }
private static bool IsPublic(IMetadataTypeInfo type) { return (type.IsNested && type.IsNestedPublic) || type.IsPublic; }
private void ExploreType(IProject project, IMetadataAssembly metadataAssembly, UnitTestElementConsumer consumer, IMetadataTypeInfo typeInfo) { if (conventionCheck.IsValidTestClass(project, typeInfo)) { var classUnitTestElement = unitTestElementFactory.GetOrCreateTestClass(project, new ClrTypeName(typeInfo.FullyQualifiedName), metadataAssembly.Location.FullPath); consumer(classUnitTestElement); } }
public static bool IsUnitTestContainer(IMetadataTypeInfo metadataTypeInfo) { return IsPublic(metadataTypeInfo) && TypeUtility.IsTestClass(metadataTypeInfo.AsTypeInfo()); }
private static bool IsExportedType(IMetadataTypeInfo metadataTypeInfo) { return metadataTypeInfo.IsPublic || metadataTypeInfo.IsNestedPublic; }