Exemple #1
0
        public void PropertyAnnotator_WithInitializedAutoProperty_DoesNotAnnotateNullable(string classContent)
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileInClass("TestClass", classContent);
            var annotator = new PropertyNullAnnotator(semantic);

            var annotated = annotator.Visit(syntax);

            Assert.That(annotated, Is.EqualTo(syntax));
        }
Exemple #2
0
        public void PropertyAnnotator_WithBlockGetterReturnsNull_AnnotatesNullable(string classContent)
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileInClass("TestClass", classContent);
            var annotator = new PropertyNullAnnotator(semantic);

            var annotated = annotator.Visit(syntax);

            Assert.That(annotated.DescendantNodesAndSelf().OfType <PropertyDeclarationSyntax>(), Has.One.Items);
            var property = annotated.DescendantNodesAndSelf().OfType <PropertyDeclarationSyntax>().Single();

            Assert.That(property.Type, Is.InstanceOf <NullableTypeSyntax>());
            Assert.That(((NullableTypeSyntax)property.Type).ElementType.ToString(), Is.EqualTo("string"));
        }
Exemple #3
0
        public void PropertyAnnotator_WithInitializedInMultipleCtorsAutoProperty_DoesNotAnnotateNullable()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileInClass(
                "TestClass",
                @"public TestClass () { A = """";}
          public TestClass (string s) { A = s; }
          public TestClass (string s, int i): this(s) { }
          public string A {get;}");
            var annotator = new PropertyNullAnnotator(semantic);

            var annotated = annotator.Visit(syntax);

            Assert.That(annotated, Is.EqualTo(syntax));
        }
Exemple #4
0
        public void PropertyAnnotator_WithUninitializedAutoProperty_AnnotatesNullable()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileInClass(
                "TestClass",
                @"public TestClass () {}
          public TestClass (string s) { A = s; }
          public string A {get;}");
            var annotator = new PropertyNullAnnotator(semantic);

            var annotated = annotator.Visit(syntax);

            Assert.That(annotated.DescendantNodesAndSelf().OfType <PropertyDeclarationSyntax>(), Has.One.Items);
            var property = annotated.DescendantNodesAndSelf().OfType <PropertyDeclarationSyntax>().Single();

            Assert.That(property.Type, Is.InstanceOf <NullableTypeSyntax>());
            Assert.That(((NullableTypeSyntax)property.Type).ElementType.ToString(), Is.EqualTo("string"));
        }
        public void LocalDeclarationNullAnnotator_DoesAnnotateDeclarationsCallingNullableMethods(string declarationSource)
        {
            var classContentTemplate =
                @"public string? returnNull() {
            return null;
          }
          public void TestMethod() {" +
                $"\r\n  {declarationSource}\r\n" +
                "}\r\n";

            var(semanticModel, syntaxNode) = CompiledSourceFileProvider.CompileInClass("TestClass", classContentTemplate);
            var testMethod  = syntaxNode.DescendantNodes().OfType <MethodDeclarationSyntax>().Single(m => m.Identifier.ToString() == "TestMethod");
            var declaration = testMethod.Body !.Statements.First();
            var rewriter    = new LocalDeclarationNullAnnotator(semanticModel);

            var rewritten = rewriter.Visit(declaration) as LocalDeclarationStatementSyntax;

            Assert.That(rewritten, Is.Not.Null);
            Assert.That(rewritten !.Declaration.Type, Is.InstanceOf <NullableTypeSyntax>());
        }