protected virtual void PresentTestFixture(CSUnitTestFixtureElement value, IPresentableItem item,
                                                  TreeModelNode modelNode, PresentationState state)
        {
            item.Clear();
            if (IsNodeParentNatural(modelNode, value))
            {
                item.RichText = new CLRTypeName(value.GetTypeClrName()).ShortName;
            }
            else
            {
                var name = new CLRTypeName(value.GetTypeClrName());
                if (string.IsNullOrEmpty(name.NamespaceName))
                {
                    item.RichText = string.Format("{0}", name.ShortName);
                }
                else
                {
                    item.RichText = string.Format("{0}.{1}", name.NamespaceName, name.ShortName);
                }
            }

            Image typeImage  = UnitTestManager.GetStandardImage(UnitTestElementImage.TestContainer);
            Image stateImage = UnitTestManager.GetStateImage(state);

            if (stateImage != null)
            {
                item.Images.Add(stateImage);
            }
            else if (typeImage != null)
            {
                item.Images.Add(typeImage);
            }
            AppendOccurencesCount(item, modelNode, "test");
        }
        /// <summary>Initializes a new instance of the <see cref="ValueAnalysisRefactoring"/> class.</summary>
        /// <param name="typeMemberDeclaration">The type member declaration.</param>
        public ValueAnalysisRefactoring([NotNull] ITypeMemberDeclaration typeMemberDeclaration)
        {
            this.typeMemberDeclaration = typeMemberDeclaration;

              var codeAnnotationsCache = CodeAnnotationsCache.GetInstance(this.Solution);

              this.notNullTypeElement = codeAnnotationsCache.GetAttributeTypeForElement(this.TypeMemberDeclaration, CodeAnnotationsCache.NotNullAttributeShortName);
              this.canBeNullTypeElement = codeAnnotationsCache.GetAttributeTypeForElement(this.TypeMemberDeclaration, CodeAnnotationsCache.CanBeNullAttributeShortName);

              if (this.notNullTypeElement == null || this.canBeNullTypeElement == null)
              {
            return;
              }

              this.notNullableAttributeClrName = new CLRTypeName(this.notNullTypeElement.CLRName);
              this.canBeNullAttributeClrName = new CLRTypeName(this.canBeNullTypeElement.CLRName);

              this.InsertAssertStatements = true;
              this.AnnotateWithValueAnalysisAttributes = true;
        }
Beispiel #3
0
 public bool HasAttributeInstance(CLRTypeName clrName, bool inherit)
 {
     throw new NotImplementedException();
 }
Beispiel #4
0
 public IList <IAttributeInstance> GetAttributeInstances(CLRTypeName clrName, bool inherit)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Called to check if ContextAction is available.
        /// ReadLock is taken
        /// Will not be called if PsiManager, ProjectFile of Solution == null
        /// </summary>
        /// <param name="element">
        /// The element.
        /// </param>
        /// <returns>
        /// The is available.
        /// </returns>
        protected override bool IsAvailable(IElement element)
        {
            if (string.IsNullOrEmpty(ValueAnalysisSettings.Instance.AllowNullAttribute))
              {
            return false;
              }

              var parameterDeclaration = this.Provider.GetSelectedElement<IParameterDeclaration>(true, true);
              if (parameterDeclaration == null)
              {
            return false;
              }

              var parameter = parameterDeclaration.DeclaredElement;
              if (parameter == null)
              {
            return false;
              }

              if (parameter.Kind == ParameterKind.OUTPUT)
              {
            return false;
              }

              var functionDeclaration = this.Provider.GetSelectedElement<IFunctionDeclaration>(true, true);
              if (functionDeclaration == null)
              {
            return false;
              }

              var function = functionDeclaration.DeclaredElement;
              if (function == null)
              {
            return false;
              }

              var interfaceType = parameter.GetContainingType() as IInterface;
              if (interfaceType != null)
              {
            return false;
              }

              var clrTypeName = new CLRTypeName(ValueAnalysisSettings.Instance.AllowNullAttribute);

              var attributeInstances = function.GetAttributeInstances(clrTypeName, true);

              foreach (var instance in attributeInstances)
              {
            var allowNull = instance.PositionParameter(0).ConstantValue;

            if (allowNull.Value == null)
            {
              continue;
            }

            var allowNullName = allowNull.Value as string;

            if (allowNullName == parameter.ShortName)
            {
              return false;
            }

            if (allowNullName == "*")
            {
              return false;
            }
              }

              return true;
        }
        /// <summary>Gets the assertion parameters.</summary>
        /// <param name="functionDeclaration">The function declaration.</param>
        /// <param name="result">The result.</param>
        private void GetAssertionParameters([NotNull] ICSharpFunctionDeclaration functionDeclaration, [NotNull] List<ParameterStatement> result)
        {
            IParametersOwner parametersOwner = functionDeclaration.DeclaredElement;
              if (parametersOwner == null || parametersOwner.Parameters.Count <= 0)
              {
            return;
              }

              IAttributesOwner attributesOwner;
              if (functionDeclaration is IAccessorDeclaration)
              {
            attributesOwner = functionDeclaration.GetContainingTypeMemberDeclaration().DeclaredElement;
              }
              else
              {
            attributesOwner = parametersOwner as IAttributesOwner;
              }

              if (attributesOwner == null)
              {
            return;
              }

              var allowNullParameters = new List<string>();

              var allowNullAttribute = ValueAnalysisSettings.Instance.AllowNullAttribute;

              if (!string.IsNullOrEmpty(allowNullAttribute))
              {
            var typeName = new CLRTypeName(allowNullAttribute);

            var instances = attributesOwner.GetAttributeInstances(typeName, true);

            if (instances != null && instances.Count > 0)
            {
              foreach (var instance in instances)
              {
            var positionParameter = instance.PositionParameter(0).ConstantValue;
            if (!positionParameter.IsString())
            {
              continue;
            }

            var name = positionParameter.Value as string;

            if (name == "*")
            {
              return;
            }

            if (!string.IsNullOrEmpty(name))
            {
              allowNullParameters.Add(name);
            }
              }
            }
              }

              foreach (var parameter in parametersOwner.Parameters)
              {
            if (parameter == null)
            {
              continue;
            }

            if (allowNullParameters.Contains(parameter.ShortName))
            {
              continue;
            }

            if (!parameter.Type.IsReferenceType())
            {
              continue;
            }

            var parameterStatement = new ParameterStatement
            {
              Parameter = parameter
            };

            this.FindAttributes(parameterStatement);

            if (parameter.Kind == ParameterKind.OUTPUT || !this.InsertAssertStatements)
            {
              parameterStatement.NeedsStatement = false;
            }
            else
            {
              var accessRights = GetAccessRights(functionDeclaration);

              var code = this.GetCode(parameterStatement, accessRights);

              if (string.IsNullOrEmpty(code))
              {
            parameterStatement.NeedsStatement = false;
              }
              else
              {
            code = string.Format(code, parameterStatement.Parameter.ShortName);

            parameterStatement.Code = code.Trim();
              }
            }

            result.Add(parameterStatement);
              }
        }
        private static IAttributeInstance FindAttribute([NotNull] string attributeName, [NotNull] IAttributesOwner attributesOwner)
        {
            var typeName = new CLRTypeName(attributeName);
              var instances = attributesOwner.GetAttributeInstances(typeName, true);

              if (instances != null && instances.Count > 0)
              {
            return instances[0];
              }

              return null;
        }
        /// <summary>
        /// Adds the attribute.
        /// </summary>
        /// <param name="solution">
        /// The solution.
        /// </param>
        /// <param name="classDeclaration">
        /// The class declaration.
        /// </param>
        /// <param name="factory">
        /// The factory.
        /// </param>
        private static void AddAttribute(ISolution solution, IClassDeclaration classDeclaration, CSharpElementFactory factory)
        {
            var typeName = new CLRTypeName("System.SerializableAttribute");

              var scope = DeclarationsScopeFactory.SolutionScope(solution, true);
              var cache = PsiManager.GetInstance(solution).GetDeclarationsCache(scope, true);

              var typeElement = cache.GetTypeElementByCLRName(typeName);
              if (typeElement == null)
              {
            return;
              }

              var attribute = factory.CreateAttribute(typeElement);

              classDeclaration.AddAttributeAfter(attribute, null);
        }
        /// <summary>
        /// Called to check if ContextAction is available.
        /// ReadLock is taken
        /// Will not be called if PsiManager, ProjectFile of Solution == null
        /// </summary>
        /// <param name="element">
        /// The element.
        /// </param>
        /// <returns>
        /// The is available.
        /// </returns>
        protected override bool IsAvailable(IElement element)
        {
            if (string.IsNullOrEmpty(ValueAnalysisSettings.Instance.AllowNullAttribute))
              {
            return false;
              }

              var typeMemberDeclaration = this.GetTypeMemberDeclaration();
              if (typeMemberDeclaration == null)
              {
            return false;
              }

              var typeElement = GetAttribute(typeMemberDeclaration, ValueAnalysisSettings.Instance.AllowNullAttribute);
              if (typeElement == null)
              {
            return false;
              }

              var parametersOwner = typeMemberDeclaration.DeclaredElement as IParametersOwner;
              if (parametersOwner == null || parametersOwner.Parameters.Count == 0)
              {
            return false;
              }

              IAttributesOwner attributesOwner = typeMemberDeclaration.DeclaredElement;
              if (attributesOwner == null)
              {
            return false;
              }

              var clrTypeName = new CLRTypeName(ValueAnalysisSettings.Instance.AllowNullAttribute);

              var attributeInstances = attributesOwner.GetAttributeInstances(clrTypeName, true);

              foreach (var instance in attributeInstances)
              {
            var allowNull = instance.PositionParameter(0).ConstantValue;

            if (allowNull.Value == null)
            {
              continue;
            }

            var allowNullName = allowNull.Value as string;

            if (allowNullName == "*")
            {
              return false;
            }
              }

              return true;
        }