Beispiel #1
0
        private void ProcessBehaviorField(IProject project, IFieldInfo field, IDeclaration declaration = null)
        {
            var behaviorType   = field.FieldType.GetGenericArguments().FirstOrDefault();
            var containingType = new ClrTypeName(field.DeclaringType);

            if (_contexts.TryGetValue(containingType, out var context))
            {
                var behavior = _factory.GetOrCreateBehavior(
                    project,
                    context,
                    containingType,
                    field.ShortName,
                    field.IsIgnored());

                OnUnitTestElement(behavior, declaration);

                if (behaviorType != null && behaviorType.IsBehaviorContainer())
                {
                    var specFields = behaviorType.GetFields()
                                     .Where(x => x.IsSpecification());

                    foreach (var specField in specFields)
                    {
                        var specification = _factory.GetOrCreateBehaviorSpecification(
                            project,
                            behavior,
                            new ClrTypeName(specField.DeclaringType),
                            specField.ShortName,
                            specField.IsIgnored());

                        OnUnitTestElement(specification, declaration);
                    }
                }
            }
        }
        public static IClassDeclaration GetContractClassDeclaration(this IClassLikeDeclaration classLikeDeclaration)
        {
            Contract.Requires(classLikeDeclaration != null);
            Contract.Requires(classLikeDeclaration.DeclaredElement != null);

            var contractClassAttributeType     = new ClrTypeName(typeof(ContractClassAttribute).FullName);
            var contractClassAttributeInstance =
                classLikeDeclaration.DeclaredElement.GetAttributeInstances(contractClassAttributeType, true)
                .FirstOrDefault();

            if (contractClassAttributeInstance == null)
            {
                return(null);
            }

            Contract.Assert(contractClassAttributeInstance.PositionParameterCount == 1);

            return(contractClassAttributeInstance.PositionParameters()
                   .FirstOrDefault()
                   .With(x => x.TypeValue)
                   .With(x => x.GetScalarType())
                   .With(x => x.Resolve())
                   .With(x => x.DeclaredElement)
                   .With(x => x.GetDeclarations().FirstOrDefault())
                   .Return(x => x as IClassDeclaration));
        }
Beispiel #3
0
        public override IUnitTestElement Map(Test test)
        {
            using (ReadLockCookie.Create())
            {
                var assemblyPath  = _project?.GetOutputFilePath(_targetFrameworkId);
                var typeName      = GetType(test);
                var fieldName     = GetField(test);
                var subject       = GetTraits(test, "Subject").FirstOrDefault();
                var tags          = GetTraits(test, "Tag");
                var behaviorField = GetTraits(test, "BehaviorField").FirstOrDefault();
                var behaviorType  = GetTraits(test, "BehaviorType").FirstOrDefault();

                var type = new ClrTypeName(typeName);

                var context = _factory.GetOrCreateContext(_project, type, assemblyPath, subject, tags.ToArray(), false);

                if (!string.IsNullOrEmpty(behaviorField) && !string.IsNullOrEmpty(behaviorType))
                {
                    var behavior = _factory.GetOrCreateBehavior(_project, context, type, behaviorField, false);

                    return(_factory.GetOrCreateBehaviorSpecification(_project, behavior, new ClrTypeName(behaviorType), fieldName, false));
                }

                return(_factory.GetOrCreateContextSpecification(_project, context, type, fieldName, false));
            }
        }
        private static ContractStatement CreateSpecialInvocationContractStatement(ICSharpStatement statement)
        {
            var method = statement.GetInvokedMethod();

            if (method == null)
            {
                return(null);
            }

            var validatorAttribute = new ClrTypeName("System.Diagnostics.Contracts.ContractArgumentValidatorAttribute");

            if (method.HasAttributeInstance(validatorAttribute, false))
            {
                return(ContractValidatorStatement.TryCreate(statement, method));
            }

            var abbreviationAttribute = new ClrTypeName("System.Diagnostics.Contracts.ContractAbbreviatorAttribute");

            if (method.HasAttributeInstance(abbreviationAttribute, false))
            {
                return(ContractAbbreviationStatement.TryCreate(statement, method));
            }

            return(null);
        }
Beispiel #5
0
 private IClrTypeName GetInternedClrTypeName(string typeName)
 {
     if (!myTypeNames.TryGetValue(typeName, out var clrTypeName))
     {
         clrTypeName = new ClrTypeName(typeName);
         myTypeNames.Add(typeName, clrTypeName);
     }
     return(clrTypeName);
 }
Beispiel #6
0
 private IClrTypeName GetClrTypeName(string typeName)
 {
     IClrTypeName clrTypeName;
     if (!myTypeNames.TryGetValue(typeName, out clrTypeName))
     {
         clrTypeName = new ClrTypeName(typeName);
         myTypeNames.Add(typeName, clrTypeName);
     }
     return clrTypeName;
 }
Beispiel #7
0
        private IClrTypeName GetClrTypeName(string typeName)
        {
            IClrTypeName clrTypeName;

            if (!myTypeNames.TryGetValue(typeName, out clrTypeName))
            {
                clrTypeName = new ClrTypeName(typeName);
                myTypeNames.Add(typeName, clrTypeName);
            }
            return(clrTypeName);
        }
        public static bool HasAttribute(this IClassLikeDeclaration classLikeDeclaration, Type attributeType)
        {
            Contract.Requires(classLikeDeclaration != null);
            Contract.Requires(classLikeDeclaration.DeclaredElement != null);
            Contract.Requires(attributeType != null);
            Contract.Requires(attributeType.IsAttribute());

            var clrAttribute = new ClrTypeName(attributeType.FullName);
            return classLikeDeclaration.DeclaredElement.HasAttributeInstance(
                new ClrTypeName(attributeType.FullName), inherit: true);
        }
        public static bool HasAttribute(this IClassLikeDeclaration classLikeDeclaration, Type attributeType)
        {
            Contract.Requires(classLikeDeclaration != null);
            Contract.Requires(classLikeDeclaration.DeclaredElement != null);
            Contract.Requires(attributeType != null);
            Contract.Requires(attributeType.IsAttribute());

            var clrAttribute = new ClrTypeName(attributeType.FullName);

            return(classLikeDeclaration.DeclaredElement.HasAttributeInstance(
                       new ClrTypeName(attributeType.FullName), inherit: true));
        }
Beispiel #10
0
        public void ContextWithNoSubjectPresentationIsValid()
        {
            With(() =>
            {
                var id   = CreateId("id");
                var type = new ClrTypeName("Namespace.Elements.my_class_name");

                var element = new ContextElement(id, type, ServiceProvider, null, false);

                Assert.That(element.GetPresentation(null, false), Is.EqualTo("my class name"));
            });
        }
        public void SameTypeNamesCanBeAKey()
        {
            var name1 = new ClrTypeName("Namespace.Name1");
            var name2 = new ClrTypeName("Namespace.Name1");

            var dictionary = new Dictionary <ClrTypeName, int>();

            dictionary[name1] = 1;
            dictionary[name2] = 2;

            Assert.That(dictionary.Count, Is.EqualTo(1));
            Assert.That(name1, Is.EqualTo(name2));
        }
        public void DifferentTypeNamesAreUnique()
        {
            var name1 = new ClrTypeName("Namespace.Name1");
            var name2 = new ClrTypeName("Namespace.Name2");

            var dictionary = new Dictionary <ClrTypeName, int>();

            dictionary[name1] = 1;
            dictionary[name2] = 2;

            Assert.That(dictionary.Count, Is.EqualTo(2));
            Assert.That(name1, Is.Not.EqualTo(name2));
        }
Beispiel #13
0
        private void ProcessSpecificationField(IProject project, IFieldInfo field, IDeclaration declaration = null)
        {
            var containingType = new ClrTypeName(field.DeclaringType);

            if (_contexts.TryGetValue(containingType, out var context))
            {
                var specification = _factory.GetOrCreateContextSpecification(
                    project,
                    context,
                    containingType,
                    field.ShortName,
                    field.IsIgnored());

                OnUnitTestElement(specification, declaration);
            }
        }
        private static ContractStatement CreateSpecialInvocationContractStatement(ICSharpStatement statement)
        {
            var method = statement.GetInvokedMethod();

            if (method == null)
                return null;

            var validatorAttribute = new ClrTypeName("System.Diagnostics.Contracts.ContractArgumentValidatorAttribute");
            if (method.HasAttributeInstance(validatorAttribute, false))
                return ContractValidatorStatement.TryCreate(statement, method);

            var abbreviationAttribute = new ClrTypeName("System.Diagnostics.Contracts.ContractAbbreviatorAttribute");
            if (method.HasAttributeInstance(abbreviationAttribute, false))
                return ContractAbbreviationStatement.TryCreate(statement, method);

            return null;
        }
 public static IAttribute GetAttibuteOfCLRType(this IEnumerable<IAttribute> attributes, ClrTypeName clrTypeName)
 {
     return attributes.FirstOrDefault(a =>
     {
         var typeReference = a.TypeReference;
         if (typeReference != null)
         {
             var typeElement =
                 typeReference.Resolve().DeclaredElement as ITypeElement;
             if (typeElement != null && Equals(typeElement.GetClrName(), clrTypeName))
             {
                 return true;
             }
         }
         return false;
     });
 }
        protected override void Run(IDllImportMethodDeclaration importMethod, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var element = importMethod.MethodDeclaration;
            var libraryName = importMethod.ImportedDll;
            var factory = new LibraryFactory();
            var library = factory.LoadLibrary(libraryName);
            var export = library[element.NameIdentifier.Name];
            if (export == null || export.Parameters.Count != element.ParameterDeclarations.Count)
            {
                return;
            }

            for (var i = 0; i < element.ParameterDeclarations.Count; i++ )
            {
                var parameter = element.ParameterDeclarations[i];
                var knownParameter = export.Parameters[i];
                var exportedType = new ClrTypeName(knownParameter.CLRType.FullName);
                if (parameter.Type.IsInt() && exportedType.Equals(IntPtrClrType))
                {
                    consumer.AddHighlighting(new DllImportInt32ForIntPtrHighlighting(parameter));
                }
                var marshalAs = parameter.Attributes.GetAttibuteOfCLRType(MarshalAsAttribute);
                if (knownParameter.UnmanagedType.HasValue && marshalAs != null && marshalAs.ConstructorArgumentExpressions.Count == 1)
                {
                    var argumentExpression = marshalAs.ConstructorArgumentExpressions[0];
                    if (!argumentExpression.IsConstantValue())
                    {
                        continue;
                    }
                    var shortType = argumentExpression.ConstantValue.IsShort();
                    UnmanagedType unmanagedType;
                    if (shortType)
                    {
                        unmanagedType = (UnmanagedType)(short)argumentExpression.ConstantValue.Value;
                    }
                    else
                    {
                        unmanagedType = (UnmanagedType)argumentExpression.ConstantValue.Value;
                    }
                    if (knownParameter.UnmanagedType.Value != unmanagedType)
                    {
                        consumer.AddHighlighting(new DllImportIncorrectParameterMarshalHighlighting(parameter, argumentExpression, knownParameter.UnmanagedType.Value));
                    }
                }
            }
        }
        public static bool Is([NotNull] this IType type, [NotNull] Type reflecationType)
        {
            if (reflecationType.IsArray && !type.IsArrayOf(reflecationType.GetElementType(), reflecationType.GetArrayRank()))
            {
                return(false);
            }

            if (!(type is IDeclaredType declaredType))
            {
                return(false);
            }

            Type[] genericArguments = reflecationType.GetGenericArguments();
            if (genericArguments.Length == 0 || reflecationType.ContainsGenericParameters)
            {
                var clrTypeName = new ClrTypeName(reflecationType.FullName);
                return(declaredType.GetClrName().Equals(clrTypeName));
            }

            using (IEnumerator <IType> underlyingTypesEnumerator = GetGenericUnderlyingTypes(type).GetEnumerator())
            {
                foreach (Type genericArgumentType in genericArguments)
                {
                    if (!underlyingTypesEnumerator.MoveNext())
                    {
                        return(false);
                    }

                    if (!underlyingTypesEnumerator.Current.Is(genericArgumentType))
                    {
                        return(false);
                    }
                }

                if (underlyingTypesEnumerator.MoveNext())
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #18
0
        public ITypeElement GetClassForSearch(IParametersOwner originalMethod, IType invokedType)
        {
            var containingType = originalMethod.GetContainingType();

            if (containingType == null)
            {
                return(null);
            }

            if (!invokedType.IsGenericIQueryable() || !containingType.IsEnumerableClass())
            {
                return(null);
            }

            var queryableName        = new ClrTypeName("System.Data.Entity.QueryableExtensions");
            var queryableType        = TypeFactory.CreateTypeByCLRName(queryableName, NullableAnnotation.Unknown, invokedType.Module);
            var queryableTypeElement = queryableType.GetTypeElement();

            return(queryableTypeElement);
        }
Beispiel #19
0
        private static ITypeElement GetClassForMethodSearch(IType invokedObjectType, [NotNull] IParametersOwner originalMethod, [NotNull] IPsiModule psiModule)
        {
            var containingType = originalMethod.GetContainingType();

            if (containingType == null)
            {
                return(null);
            }

            if (!invokedObjectType.IsGenericIQueryable() || !IsEnumerable(containingType))
            {
                return(containingType);
            }

            var queryableName        = new ClrTypeName("System.Data.Entity.QueryableExtensions");
            var queryableType        = TypeFactory.CreateTypeByCLRName(queryableName, psiModule);
            var queryableTypeElement = queryableType.GetTypeElement();

            return(queryableTypeElement);
        }
Beispiel #20
0
        private void ProcessContext(ITypeInfo type, IDeclaration declaration, bool isClear)
        {
            var name         = new ClrTypeName(type.FullyQualifiedName);
            var project      = declaration.GetProject();
            var assemblyPath = project?.GetOutputFilePath(_observer.TargetFrameworkId);

            var context = _factory.GetOrCreateContext(
                project,
                name,
                assemblyPath,
                type.GetSubject(),
                type.GetTags().ToArray(),
                type.IsIgnored());

            _contexts[name] = context;

            if (isClear)
            {
                OnUnitTestElement(context, declaration);
            }
        }
        public void Execute(ISolution solution, ITextControl textControl)
        {
            var jetPopupMenus = solution.GetPsiServices().GetComponent <JetPopupMenus>();
            var cache         = solution.GetComponent <SpecflowStepsDefinitionsCache>();

            jetPopupMenus.ShowModal(JetPopupMenu.ShowWhen.AutoExecuteIfSingleEnabledItem,
                                    (lifetime, menu) =>
            {
                menu.Caption.Value = WindowlessControlAutomation.Create("Where to create the step ?");

                var availableSteps  = cache.GetBindingTypes(_reference.GetElement().GetPsiModule());
                var filesPerClasses = new OneToSetMap <string, SpecflowStepsDefinitionsCache.AvailableBindingClass>();
                foreach (var availableBindingClass in availableSteps)
                {
                    filesPerClasses.Add(availableBindingClass.ClassClrName, availableBindingClass);
                }

                menu.ItemKeys.AddRange(filesPerClasses);
                menu.DescribeItem.Advise(lifetime, e =>
                {
                    var(classClrFullName, _) = (KeyValuePair <string, ISet <SpecflowStepsDefinitionsCache.AvailableBindingClass> >)e.Key;

                    e.Descriptor.Icon  = PsiSymbolsThemedIcons.Class.Id;
                    e.Descriptor.Style = MenuItemStyle.Enabled;

                    var clrTypeName           = new ClrTypeName(classClrFullName);
                    e.Descriptor.Text         = new RichText(clrTypeName.ShortName, DeclaredElementPresenterTextStyles.ParameterInfo.GetStyle(DeclaredElementPresentationPartKind.Type));
                    e.Descriptor.ShortcutText = clrTypeName.GetNamespaceName();
                });
                menu.ItemClicked.Advise(lifetime, key =>
                {
                    var(_, availableBindingClasses) = (KeyValuePair <string, ISet <SpecflowStepsDefinitionsCache.AvailableBindingClass> >)key;
                    OpenFileSelectionModal(jetPopupMenus, textControl, availableBindingClasses, _reference.GetStepKind(), _reference.GetStepText());
                });
                menu.PopupWindowContextSource = textControl.PopupWindowContextFactory.ForCaret();
            });
        }
        public static IClassDeclaration GetContractClassDeclaration(this IClassLikeDeclaration classLikeDeclaration)
        {
            Contract.Requires(classLikeDeclaration != null);
            Contract.Requires(classLikeDeclaration.DeclaredElement != null);

            var contractClassAttributeType = new ClrTypeName(typeof(ContractClassAttribute).FullName);
            var contractClassAttributeInstance =
                classLikeDeclaration.DeclaredElement.GetAttributeInstances(contractClassAttributeType, true)
                    .FirstOrDefault();

            if (contractClassAttributeInstance == null)
                return null;

            Contract.Assert(contractClassAttributeInstance.PositionParameterCount == 1);

            return contractClassAttributeInstance.PositionParameters()
                .FirstOrDefault()
                .With(x => x.TypeValue)
                .With(x => x.GetScalarType())
                .With(x => x.Resolve())
                .With(x => x.DeclaredElement)
                .With(x => x.GetDeclarations().FirstOrDefault())
                .Return(x => x as IClassDeclaration);
        }
Beispiel #23
0
 /// <summary>
 /// Specifies a type closure
 /// </summary>
 /// <param name="openType">The name of the type over which to close</param>
 /// <param name="arguments">The arguments supplied to the type parameters</param>
 /// <returns></returns>
 public static ClrTypeClosure close(ClrTypeName openType, params (ClrTypeName argType, string argName)[] arguments)
Beispiel #24
0
 public ClassTestElement(ITestIdentity identity, IList<Task> tasks)
     : base(identity, tasks)
 {
     _testTypeName = new ClrTypeName(identity.Relative);
 }
Beispiel #25
0
 public ClassTestElement(ITestIdentity identity, IList <Task> tasks)
     : base(identity, tasks)
 {
     _testTypeName = new ClrTypeName(identity.Relative);
 }
        private static ICollection<IClass> GetTypesNonCached([NotNull] IArgumentsOwner argumentsOwner)
        {
            argumentsOwner.AssertIsValid("argumentsOwner is invalid");
            var invocationExpression = argumentsOwner as IInvocationExpression;
            if (invocationExpression != null)
            {
                var referenceExpression = invocationExpression.InvokedExpression as IReferenceExpression;
                if (referenceExpression != null)
                {
                    var typeofExpression = referenceExpression.QualifierExpression as ITypeofExpression;
                    if (typeofExpression != null)
                    {
                        IType type = typeofExpression.ArgumentType;
                        IClass @class = type.GetClassType();
                        return new[] {@class};
                    }
                    var gettypeExpression = referenceExpression.QualifierExpression as IInvocationExpression;
                    if (gettypeExpression != null && gettypeExpression.Reference != null && gettypeExpression.Reference.GetName() == "GetType")
                    {
                        var method = gettypeExpression.Reference.Resolve().DeclaredElement as IMethod;
                        if (method != null)
                        {
                            var containingType = method.GetContainingType();
                            if (containingType != null)
                            {
                                var containingTypeName = containingType.GetClrName().FullName;
                                if (containingTypeName == "System.Object")
                                {
                                    var typeDeclaration = gettypeExpression.GetContainingNode<ITypeDeclaration>();
                                    if (typeDeclaration != null)
                                    {
                                        return new[] {typeDeclaration.DeclaredElement as IClass};
                                    }
                                }
                                if (containingTypeName == "System.Type" && method.IsStatic)
                                {
                                    if (gettypeExpression.Arguments.Count > 0)
                                    {
                                        var literalExpression = gettypeExpression.Arguments[0].Expression as ICSharpLiteralExpression;
                                        if (literalExpression != null && literalExpression.IsConstantValue() && literalExpression.ConstantValue.IsString())
                                        {
                                            var typeName = new ClrTypeName((string) literalExpression.ConstantValue.Value);

                                            var name = new DeclaredTypeFromCLRName(typeName, gettypeExpression.GetPsiModule(), gettypeExpression.GetResolveContext());
                                            return new[] { name.GetTypeElement() as IClass };
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return EmptyList<IClass>.InstanceList;
        }