Esempio n. 1
0
        public void ConditionalAccessWithInvocation_ReportsCorrectly(string precedingDottedExpression)
        {
            var parentConditionalAccess =
                SyntaxFactory.ParseExpression($@"{precedingDottedExpression}.someObject?.AccessedMethod().Other") as ConditionalAccessExpressionSyntax;

            var conditionalAccess = parentConditionalAccess?.DescendantNodesAndSelf().OfType <ConditionalAccessExpressionSyntax>().ElementAt(1);
            var expectedLocation  = Location.Create(parentConditionalAccess?.SyntaxTree, TextSpan.FromBounds(0, precedingDottedExpression.Length + 28));
            var diagnostic        = _diagnosticFormatter.CreateDiagnostic(_rule, conditionalAccess);

            Assert.That($@"{precedingDottedExpression}.someObject?.AccessedMethod".Contains(diagnostic.GetMessage()));
            AssertLocation.IsWithin(expectedLocation, diagnostic.Location);
        }
Esempio n. 2
0
        public void MultipleNestedConditionalInvocations()
        {
            var expression = SyntaxFactory.ParseExpression(@"new CMS.DataEngine.WhereCondition().Or()?.WhereLike(""val"", ""col"")");
            var invocation = expression.DescendantNodesAndSelf().OfType <InvocationExpressionSyntax>().Last();

            var expectedLocation = Location.Create(invocation?.SyntaxTree, TextSpan.FromBounds(0, 65));
            var diagnostic       = _diagnosticFormatter.CreateDiagnostic(_rule, invocation);

            AssertLocation.IsWithin(expectedLocation, diagnostic.Location);
            Assert.That(@"new CMS.DataEngine.WhereCondition().Or()?.WhereLike(""val"", ""col"")".Contains(diagnostic.GetMessage()));
            Assert.IsFalse(diagnostic.IsMarkedAsSimpleMemberAccess());
            Assert.IsTrue(diagnostic.IsMarkedAsConditionalAccess());
        }
Esempio n. 3
0
        public void ConditionalAccessWithPrecedingConditionalAccessesAndInvocations(string precedingDottedExpression)
        {
            var parentConditionalAccess =
                SyntaxFactory.ParseExpression($@"{precedingDottedExpression}.someObject?.SomeProperty") as ConditionalAccessExpressionSyntax;

            var conditionalAccess = parentConditionalAccess?.WhenNotNull as ConditionalAccessExpressionSyntax;

            var expectedLocation = Location.Create(parentConditionalAccess?.SyntaxTree, TextSpan.FromBounds(0, precedingDottedExpression.Length + 25));
            var diagnostic       = _diagnosticFormatter.CreateDiagnostic(_rule, conditionalAccess);

            Assert.That($@"{precedingDottedExpression}.someObject?.SomeProperty".Contains(diagnostic.GetMessage()));
            AssertLocation.IsWithin(expectedLocation, diagnostic.Location);
            Assert.IsTrue(diagnostic.IsMarkedAsConditionalAccess());
        }