Exemple #1
0
        public void LocateFields_LocatesOnlyUnitializedFields()
        {
            var classSource =
                @"public class TestClass {
  private string s;
  private object o = new object();
  
  public TestClass() {
  }
}";

            var(semanticModel, syntax) = CompiledSourceFileProvider.CompileClass(classSource);
            var fieldLocator = new FieldLocator(syntax, semanticModel);

            var fields = fieldLocator.LocateFields();

            Assert.That(fields, Has.One.Items);
            var field = fields.Single();

            Assert.That(field.Declaration.Type.ToString(), Is.EqualTo("string"));
            Assert.That(field.Declaration.Variables, Has.One.Items);
            Assert.That(field.Declaration.Variables, Has.One.Items);
            var variable = field.Declaration.Variables.Single();

            Assert.That(variable.Identifier.ToString(), Is.EqualTo("s"));
        }
        public void CreateFromAttributeData_ThrowsWhenTypeAndNameIsSpecified()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute(
                "[ExpectedException (typeof(DivideByZeroException), ExpectedExceptionName = \"DivideByZeroException\")]");

            Assert.That(
                () => ExpectedExceptionModel.CreateFromAttributeData(attributeData),
                Throws.Exception.With.InstanceOf <InvalidOperationException>());
        }
Exemple #3
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 #4
0
        public void ExpectedExceptionRewriter_RewritesToExpectedCases(string method)
        {
            var(rewriter, syntax) = CreateRewriterFor(c_testCasesSourceFileName, method);
            var expectedSyntax = CompiledSourceFileProvider.LoadMethod(c_expectedTestResultsSourceFileName, method);

            var rewrittenMethodSyntax = rewriter.VisitMethodDeclaration(syntax);

            Assert.That(rewrittenMethodSyntax.ToFullString().Trim(), Is.EqualTo(expectedSyntax.ToFullString().Trim()));
        }
        public void ConvertToConstraint_ConvertsExpectedExceptionMessage()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException(ExpectedMessage = \"Test Message\")]");
            IExpectedExceptionModel expectedException
                = new ExpectedExceptionModel(attributeData, ParseExpression("Exception"), null, ParseExpression("\"Test Message\""), null);

            var constraint = expectedException.AsConstraintExpression("");

            Assert.That(constraint, Is.EquivalentTo(ParseExpression("Throws.Exception.With.Message.EqualTo(\"Test Message\")")));
        }
        public void ConvertToConstraint_ConvertsForCustomWellKnownException()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException(typeof(InvalidOperationException))]");
            IExpectedExceptionModel expectedException
                = new ExpectedExceptionModel(attributeData, ParseExpression("typeof(InvalidOperationException)"), null, null, null);

            var constraint = expectedException.AsConstraintExpression("");

            Assert.That(constraint, Is.EquivalentTo(ParseExpression("Throws.InvalidOperationException")));
        }
        public void ConvertToConstraint_ConvertsForSimpleExceptionCase()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException]");
            IExpectedExceptionModel expectedException
                = new ExpectedExceptionModel(attributeData, IdentifierName("Exception"), null, null, null);

            var constraint = expectedException.AsConstraintExpression("");

            Assert.That(constraint, Is.EquivalentTo(ParseExpression("Throws.Exception")));
        }
        public void MethodParameterNullAnnotator_WithEmptyList_ReturnsInput()
        {
            var(_, methodSyntax) = CompiledSourceFileProvider.CompileMethod(
                "void T (object o1, object o2, string s1, string s2, int i1, double d1) {}");
            var rewriter = new MethodParameterNullAnnotator(new ParameterSyntax[0]);

            var rewritten = rewriter.Visit(methodSyntax);

            Assert.That(rewritten, Is.EqualTo(methodSyntax));
        }
        public void CreateFromAttributeData_ExceptionNameIsSetCorrectly(string attribute)
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute(attribute);

            var expectedExceptionModel = ExpectedExceptionModel.CreateFromAttributeData(attributeData);

            Assert.That(
                expectedExceptionModel.ExceptionType,
                Is.EquivalentTo(TypeOfExpression(IdentifierName("DivideByZeroException"))));
        }
        public void LocalDeclarationNullAnnotator_DoesAnnotateDeclarationsToReferenceTypes(string declarationSource)
        {
            var(model, syntax) = CompiledSourceFileProvider.CompileStatement(declarationSource);
            var rewriter = new LocalDeclarationNullAnnotator(model);

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

            Assert.That(rewritten, Is.Not.Null);
            Assert.That(rewritten !.Declaration.Type, Is.InstanceOf <NullableTypeSyntax>());
        }
Exemple #11
0
        private (ExpectedExceptionRewriter, MethodDeclarationSyntax) CreateRewriterFor(string file, string methodName)
        {
            var(semantic, root) = CompiledSourceFileProvider.LoadCompilationFromFile(file);
            var rewriter = new ExpectedExceptionRewriter(semantic, new ExpectedExceptionMethodBodyTransformer(), new ExpectedExceptionAttributeRemover());
            var method   = root.DescendantNodes()
                           .OfType <MethodDeclarationSyntax>()
                           .Single(syntax => syntax.Identifier.ToString() == methodName);

            return(rewriter, method);
        }
        public void LocalDeclarationNullAnnotator_DoesNotAnnotateDeclarationsToVar(string declarationSource)
        {
            var(model, syntax) = CompiledSourceFileProvider.CompileStatement(declarationSource);
            var rewriter = new LocalDeclarationNullAnnotator(model);

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

            Assert.That(rewritten, Is.Not.Null);
            Assert.That(rewritten, Is.EqualTo(syntax));
        }
        public void ClassFieldNotInitializedAnnotator_DoesAnnotateSingleUninitializedField()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileClass(c_classWithOneUnitializedField);
            var(_, expected)      = CompiledSourceFileProvider.CompileClass(c_classWithOneUnitializedFieldAnnotated);
            var annotator = new ClassFieldNotInitializedAnnotator(semantic);

            var annotatedClass = annotator.Visit(syntax);

            Assert.That(annotatedClass.ToString(), Is.EqualTo(expected.ToString()));
        }
        public void MethodNullReturnAnnotator_DoesNotAnnotateNonNullReturningMethod(string methodSource)
        {
            var source = string.Format(c_classTemplate, methodSource);

            var(model, syntax) = CompiledSourceFileProvider.CompileMethodInClass(source, "t");
            var rewriter = new MethodReturnNullAnnotator(model);

            var rewritten = (MethodDeclarationSyntax)rewriter.Visit(syntax);

            Assert.That(rewritten.ReturnType, Is.Not.InstanceOf <NullableTypeSyntax>());
        }
        public void CreateFromAttributeData_UsesCustomExceptionType()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException(typeof(DivideByZeroException))]");

            var expectedExceptionModel = ExpectedExceptionModel.CreateFromAttributeData(attributeData);

            Assert.That(expectedExceptionModel, Is.Not.Null);
            Assert.That(
                expectedExceptionModel.ExceptionType,
                Is.EquivalentTo(TypeOfExpression(IdentifierName("DivideByZeroException"))));
        }
        public void CreateFromAttributeData_UsesUserMessage()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException (UserMessage = \"test user message\")]");

            var expectedExceptionModel = ExpectedExceptionModel.CreateFromAttributeData(attributeData);

            Assert.That(expectedExceptionModel, Is.Not.Null);
            Assert.That(
                expectedExceptionModel.UserMessage,
                Is.EquivalentTo(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("test user message"))));
        }
        public void GetUnitializedFields_WithInitializerInCtorOverload_IsNotRecognized()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileClass(string.Format(c_constructorTemplate, @"field1 = ""hello""; ", @"field2 = """";"));
            var(f1, f2, f3)       = GetTestFields(syntax);
            var constructorInitFilter = new ConstructorInitializationFilter(syntax, new[] { f1, f2, f3 });

            var uninitialized = constructorInitFilter.GetUnitializedFields();

            Assert.That(uninitialized, Has.Exactly(2).Items);
            Assert.That(uninitialized, Is.EquivalentTo(new[] { f1, f3 }));
        }
        public void GetUnitializedFields_WithBaseInitializer()
        {
            var(_, syntax)  = CompiledSourceFileProvider.CompileClass(string.Format(c_multipleConstructorWithBaseCallTemplate, @"field1 = ""hello""; "));
            var(f1, f2, f3) = GetTestFields(syntax);
            var constructorInitFilter = new ConstructorInitializationFilter(syntax, new[] { f1, f2, f3 });

            var uninitialized = constructorInitFilter.GetUnitializedFields();

            Assert.That(uninitialized, Has.Exactly(2).Items);
            Assert.That(uninitialized, Is.EquivalentTo(new[] { f2, f3 }));
        }
        public void GetUnitializedFields_WithNoInitializations_FiltersNothing()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileClass(string.Format(c_constructorTemplate, "", ""));
            var(f1, f2, f3)       = GetTestFields(syntax);
            var constructorInitFilter = new ConstructorInitializationFilter(syntax, new[] { f1, f2, f3 });

            var uninitialized = constructorInitFilter.GetUnitializedFields();

            Assert.That(uninitialized, Has.Exactly(3).Items);
            Assert.That(uninitialized, Is.EquivalentTo(new[] { f1, f2, f3 }));
        }
        public void TestSetUpFixtureRewrite_RewriteAttributeToNewNames(string className)
        {
            var expectedClass  = className + "Transformed";
            var expectedSyntax = CompiledSourceFileProvider.LoadClass(c_testCasesSourceFileName, expectedClass);

            var(rewriter, classSyntax) = CreateRewriterFor(c_testCasesSourceFileName, className);

            var result = (ClassDeclarationSyntax)rewriter.Visit(classSyntax);

            Assert.That(
                result.Members.ToFullString(),
                Is.EqualTo(expectedSyntax.Members.ToFullString()));
        }
Exemple #21
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 #22
0
        public void PropertyAnnotator_WithReadonlyPropertyInInterface_DoesNotAnnotateNullable()
        {
            var(semantic, syntax) = CompiledSourceFileProvider.CompileInNameSpace("TestNamespace",
                                                                                  @"public interface ITest
{
  string Property { get; }
}");
            var annotator = new PropertyNullAnnotator(semantic);

            var annotated = annotator.Visit(syntax);

            Assert.That(annotated, Is.EqualTo(syntax));
        }
        private (SetUpFixtureLifeCycleRewriter, ClassDeclarationSyntax) CreateRewriterFor(string file, string className)
        {
            var(semantic, root) = CompiledSourceFileProvider.LoadCompilationFromFile(file);
            var rewriter = new SetUpFixtureLifeCycleRewriter(
                semantic,
                new SetUpFixtureSetUpAttributeRenamer(),
                new SetUpFixtureTearDownAttributeRenamer());
            var method = root.DescendantNodes()
                         .OfType <ClassDeclarationSyntax>()
                         .Single(syntax => syntax.Identifier.ToString() == className);

            return(rewriter, method);
        }
Exemple #24
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 #25
0
        public void CastExpressionNullAnnotator_DoesNotAnnotateNonNullableCast(string castExpression)
        {
            var source = string.Format(c_castTemplate, castExpression);

            var(semanticModel, method) = CompiledSourceFileProvider.CompileMethod(source);
            var stmt = (method.Body !.Statements.First() as LocalDeclarationStatementSyntax) !;
            var castExpressionSyntax = (stmt.Declaration.Variables.First().Initializer !.Value as CastExpressionSyntax) !;
            var rewriter             = new CastExpressionNullAnnotator(semanticModel);

            var rewritten = rewriter.Visit(castExpressionSyntax);

            Assert.That(rewritten, Is.InstanceOf <CastExpressionSyntax>());
            Assert.That(rewritten, Is.EqualTo(castExpressionSyntax));
        }
        public void CreateFromAttributeData_UsesExpectedMessage(string attribute, string message, string matchType)
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute(attribute);

            var expectedExceptionModel = ExpectedExceptionModel.CreateFromAttributeData(attributeData);

            Assert.That(expectedExceptionModel, Is.Not.Null);
            Assert.That(
                expectedExceptionModel.ExpectedMessage,
                Is.EquivalentTo(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(message))));
            Assert.That(
                expectedExceptionModel.MatchType,
                Is.EquivalentTo(MemberAccess(IdentifierName("MessageMatch"), matchType)));
        }
Exemple #27
0
        public void LocateFields_LocatesNoFields()
        {
            var classSource =
                @"public class TestClass {
  public TestClass() {
  }
}";

            var(semanticModel, syntax) = CompiledSourceFileProvider.CompileClass(classSource);
            var fieldLocator = new FieldLocator(syntax, semanticModel);

            var fields = fieldLocator.LocateFields();

            Assert.That(fields, Has.Exactly(0).Items);
        }
        public async Task CreateFromAttributeData_NoArgsAttribute()
        {
            var attributeData = CompiledSourceFileProvider.CompileAttribute("[ExpectedException]");

            var expectedExceptionModel = ExpectedExceptionModel.CreateFromAttributeData(attributeData);

            Assert.That(expectedExceptionModel, Is.Not.Null);
            Assert.That(
                expectedExceptionModel.ExceptionType,
                Is.EquivalentTo(TypeOfExpression(IdentifierName("Exception"))));
            Assert.That(
                await expectedExceptionModel.GetAttributeSyntax(),
                Is.SameAs(await attributeData.ApplicationSyntaxReference.GetSyntaxAsync()));
            Assert.That(expectedExceptionModel.ExpectedMessage, Is.Null);
        }
Exemple #29
0
        public void MethodArgumentFromInvocationNullAnnotator_InvokesCallbackWithSingleNullableArgument()
        {
            var source = string.Format(c_argumentTemplate, "TestMethod (1, null, 2.3, new string())");

            var(semanticModel, classSyntax) = CompiledSourceFileProvider.CompileClass(source);
            var callSiteMethod = classSyntax.Members[1] as MethodDeclarationSyntax;
            var list           = new List <ParameterSyntax>();
            var rewriter       = new MethodArgumentFromInvocationNullAnnotator(semanticModel, p => list.Add(p));

            var rewritten = rewriter.Visit(callSiteMethod);

            Assert.That(rewritten, Is.EqualTo(callSiteMethod));
            Assert.That(list, Has.One.Items);
            Assert.That(list[0].Identifier.ToString(), Is.EqualTo("referenceType1"));
        }
Exemple #30
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"));
        }