public void When_indexer_parameter_in_base_class_is_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        abstract class B
                        {
                            public abstract int this[[NotNull] string p] { get; set; }
                        }

                        abstract class D1 : B { }

                        class D2 : D1
                        {
                            // implicitly inherits decoration from base class
                            public override int this[string p]
                            {
                                get { throw new NotImplementedException(); }
                                set { throw new NotImplementedException(); }
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 2
0
        public void When_return_value_in_base_class_is_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IList <>).Namespace)
                                                       .InGlobalScope(@"
                        class B
                        {
                            [ItemNotNull]
                            public virtual IList<string> M() { throw new NotImplementedException(); }
                        }

                        class D1 : B { }

                        class D2 : D1
                        {
                            // implicitly inherits decoration from base class
                            public override IList<string> M() { throw new NotImplementedException(); }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 3
0
        public void When_base_method_inherits_item_annotation_from_interface_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IList <>).Namespace)
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            public interface I
                            {
                                [ItemNotNull]
                                IList<string> M();
                            }

                            public class B : I
                            {
                                public virtual IList<string> M() { throw new NotImplementedException(); }
                            }

                            public class C : B
                            {
                                public override IList<string> M() { throw new NotImplementedException(); }
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_property_in_explicit_interface_is_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                        interface I
                        {
                            [ItemCanBeNull]
                            IEnumerable<string> P { get; set; }
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            IEnumerable<string> I.P { get; set; }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_base_property_inherits_item_annotation_from_interface_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable).Namespace)
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            public interface I
                            {
                                [ItemNotNull]
                                IEnumerable P { get; set; }
                            }

                            public class B : I
                            {
                                public virtual IEnumerable P { get; set; }
                            }

                            public class C : B
                            {
                                public override IEnumerable P { get; set; }
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 6
0
        public void When_property_in_base_class_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            class B
                            {
                                public virtual string P { get; set; }
                            }

                            class D1 : B { }

                            class D2 : D1
                            {
                                // implicitly inherits decoration from base class
                                public override string P { get; set; }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("P:N.B.P")
                                                                                 .NotNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 7
0
        public void When_property_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            class C
                            {
                                string P { get; set; }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("P:N.C.P")
                                                                                 .NotNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        When_nested_generic_parameters_in_method_of_generic_interface_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                        namespace SystemCollections
                        {
                            public interface IDictionary<TKey, TValue>
                            {
                                void SetItems(IEnumerable<KeyValuePair<TKey, TValue>> items);
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named(
                                                                                     "M:SystemCollections.IDictionary`2.SetItems(System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{`0,`1}})")
                                                                                 .WithParameter(new ExternalAnnotationParameterBuilder()
                                                                                                .Named("items")
                                                                                                .CanBeNull())))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_property_in_generic_class_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                        namespace SampleNamespace
                        {
                            public class SampleClass<T>
                            {
                                public virtual IEnumerable<T> TheEnumerable { get; set; }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("P:SampleNamespace.SampleClass`1.TheEnumerable")
                                                                                 .CanBeNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        When_parameter_in_implicit_interface_implementation_is_effectively_annotated_through_annotation_on_interface_it_must_be_skipped
            ()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        interface I
                        {
                            void M([NotNull] string p);
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            void I.M(string p) { }

                            // requires explicit decoration
                            public void M([NotNull] string p) { }

                            // unrelated overload
                            public void M([NotNull] object p) { }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_base_parameter_inherits_annotation_from_interface_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            public interface I
                            {
                                void M([NotNull] string p);
                            }

                            public class B : I
                            {
                                public virtual void M(string p) { }
                            }

                            public class C : B
                            {
                                public override void M(string p) { }
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_parameter_in_implicit_interface_implementation_is_not_annotated_it_must_be_reported()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        interface I
                        {
                            void M([NotNull] string p);
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            void I.M(string p) { }

                            // requires explicit decoration
                            public void M(string p) { }

                            // unrelated overload
                            public void M([NotNull] object p) { }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(NullabilityRule.RuleName);
        }
        public void When_parameter_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            class C
                            {
                                void M(string p) { }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("M:N.C.M(System.String)")
                                                                                 .WithParameter(new ExternalAnnotationParameterBuilder()
                                                                                                .Named("p")
                                                                                                .CanBeNull())))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_parameter_in_base_constructor_is_annotated_it_must_be_reported()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        class B
                        {
                            protected B([NotNull] string p) { }
                        }

                        class D : B
                        {
                            public D(string p) : base(p) { }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(NullabilityRule.RuleName);
        }
        public void When_return_value_in_implicit_interface_is_not_annotated_with_explicit_interface_it_must_be_reported
            ()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        interface I
                        {
                            [NotNull]
                            string M();
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            string I.M() { throw new NotImplementedException(); }

                            // requires explicit decoration
                            public string M() { throw new NotImplementedException(); }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(NullabilityRule.RuleName);
        }
        public void When_field_in_nested_class_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace TestSystem
                        {
                            public class Outer
                            {
                                private class Inner
                                {
                                    public int? Value;
                                }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("F:TestSystem.Outer.Inner.Value")
                                                                                 .CanBeNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 17
0
        public void When_property_in_base_class_is_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        class B
                        {
                            [NotNull]
                            public virtual string P { get; set; }
                        }

                        class D1 : B { }

                        class D2 : D1
                        {
                            // implicitly inherits decoration from base class
                            public override string P { get; set; }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_generic_parameters_in_method_of_generic_class_are_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace SystemCollections
                        {
                            public class Dictionary<TKey, TValue>
                            {
                                public void Add(TKey key, TValue value) { throw new NotImplementedException(); }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("M:SystemCollections.Dictionary`2.Add(`0,`1)")
                                                                                 .WithParameter(new ExternalAnnotationParameterBuilder()
                                                                                                .Named("key")
                                                                                                .CanBeNull())
                                                                                 .WithParameter(new ExternalAnnotationParameterBuilder()
                                                                                                .Named("value")
                                                                                                .CanBeNull())))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 19
0
        public void When_containing_type_is_decorated_with_conditional_its_members_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .WithReference(typeof(ConditionalAttribute).Assembly)
                                                       .Using(typeof(ConditionalAttribute).Namespace)
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            [Conditional(""JETBRAINS_ANNOTATIONS"")]
                            class C : Attribute
                            {
                                public string P { get; set; }
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 20
0
        public void When_generic_field_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            public class C<T> where T : struct
                            {
                                public T? F;
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("F:N.C`1.F")
                                                                                 .CanBeNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 21
0
        public void When_override_breaks_inheritance_it_must_be_reported()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                    namespace N
                    {
                        public class B
                        {
                            [NotNull]
                            public virtual string P { get; set; }
                        }

                        public class C : B
                        {
                            public new string P { get; set; }
                        }
                    }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(NullabilityRule.RuleName);
        }
Ejemplo n.º 22
0
        public void When_field_is_annotated_with_nullable_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .WithNullabilityAttributes(new NullabilityAttributesBuilder()
                                                                                  .InCodeNamespace("N1"))
                                                       .InGlobalScope(@"
                        namespace N2
                        {
                            using CBN = N1.CanBeNullAttribute;

                            class C
                            {
                                [CBN()] // Using type/namespace alias
                                string f;
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_property_in_implicit_interface_is_not_annotated_with_explicit_interface_it_must_be_reported()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                        interface I
                        {
                            [ItemNotNull]
                            IEnumerable<string> P { get; set; }
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            IEnumerable<string> I.P { get; set; }

                            // requires explicit decoration
                            public IEnumerable<string> P { get; set; }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(ItemNullabilityRule.RuleName);
        }
        public void When_method_is_lambda_named_by_compiler_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        class C1
                        {
                            private void Test()
                            {
                                C2.M( () =>     // no syntax exists to decorate this lambda expression
                                {
                                    throw new NotImplementedException();
                                });
                            }
                        }
                        public class C2
                        {
                            public static void M([NotNull] Func<int?> callback)
                            {
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 25
0
        public void When_method_is_anonymous_named_by_compiler_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                        class C1
                        {
                            private void Test()
                            {
                                C2.M(delegate       // no syntax exists to decorate this anonymous method
                                {
                                    throw new NotImplementedException();
                                });
                            }
                        }
                        public class C2
                        {
                            public static void M([ItemNotNull] Func<IEnumerable<int?>> callback)
                            {
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_return_value_in_implicit_interface_is_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        interface I
                        {
                            [CanBeNull]
                            string M();
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            public string M() { throw new NotImplementedException(); }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 27
0
        public void When_return_value_in_implicit_interface_is_annotated_with_explicit_interface_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IList <>).Namespace)
                                                       .InGlobalScope(@"
                        interface I
                        {
                            [ItemNotNull]
                            IList<string> M();
                        }

                        class C : I
                        {
                            // implicitly inherits decoration from interface
                            IList<string> I.M() { throw new NotImplementedException(); }

                            // requires explicit decoration
                            [ItemNotNull]
                            public IList<string> M() { throw new NotImplementedException(); }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
        public void When_return_value_in_explicit_interface_is_externally_annotated_it_must_be_skipped()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        namespace N
                        {
                            interface I
                            {
                                string M();
                            }

                            class C : I
                            {
                                // implicitly inherits decoration from interface
                                string I.M() { throw new NotImplementedException(); }
                            }
                        }
                    "))
                                           .ExternallyAnnotated(new ExternalAnnotationsBuilder()
                                                                .IncludingMember(new ExternalAnnotationFragmentBuilder()
                                                                                 .Named("M:N.I.M")
                                                                                 .CanBeNull()))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.ProblemText.Should().Be(FxCopRuleValidationResult.NoProblemsText);
        }
Ejemplo n.º 29
0
        public void When_override_breaks_inheritance_it_must_be_reported()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <ItemNullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .Using(typeof(IEnumerable <>).Namespace)
                                                       .InGlobalScope(@"
                    namespace N
                    {
                        public class B
                        {
                            [ItemNotNull]
                            public virtual IEnumerable<int?> M() { throw new NotImplementedException(); }
                        }

                        public class C : B
                        {
                            public new IEnumerable<int?> M() { throw new NotImplementedException(); }
                        }
                    }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(1);
            result.Problems[0].Resolution.Name.Should().Be(ItemNullabilityRule.RuleName);
        }
        public void When_type_parameter_is_inherited_it_must_be_resolved_without_error()
        {
            // Arrange
            FxCopRuleValidator validator = new FxCopNullabilityRuleValidatorBuilder()
                                           .ForRule <NullabilityRule>()
                                           .OnAssembly(new ClassSourceCodeBuilder()
                                                       .InGlobalScope(@"
                        public class B<T>
                        {
                            [NotNull]
                            public virtual T M(/* missing annotation */ T p)
                            {
                                throw new NotImplementedException();
                            }
                        }

                        public class D<T> : B<T>
                        {
                            [NotNull]
                            public override T M(/* missing annotation */ T p) // Should not throw 'Unable to resolve TypeParameter to a type argument.'
                            {
                                throw new NotImplementedException();
                            }
                        }
                    "))
                                           .Build();

            // Act
            FxCopRuleValidationResult result = validator.Execute();

            // Assert
            result.Problems.Should().HaveCount(2);
        }