Ejemplo n.º 1
0
        public async Task VerifyGetFixes(string operationName, string attributeName, bool includeUsingCsla,
                                         string expectedDescription)
        {
            var code =
                $@"{(includeUsingCsla ? "using Csla;" : string.Empty)}

public class A : Csla.BusinessBase<A>
{{
  private void {operationName}() {{ }}
}}";
            var document = TestHelpers.Create(code);
            var tree     = await document.GetSyntaxTreeAsync();

            var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new DoesOperationHaveAttributeAnalyzer());

            var sourceSpan = diagnostics[0].Location.SourceSpan;

            var actions = new List <CodeAction>();
            var codeActionRegistration = new Action <CodeAction, ImmutableArray <Diagnostic> >(
                (a, _) => { actions.Add(a); });

            var fix            = new DoesOperationHaveAttributeAddAttributeCodeFix();
            var codeFixContext = new CodeFixContext(document, diagnostics[0],
                                                    codeActionRegistration, new CancellationToken(false));
            await fix.RegisterCodeFixesAsync(codeFixContext);

            Assert.AreEqual(1, actions.Count, nameof(actions.Count));

            var expectedTexts = includeUsingCsla ?
                                new[] { $"[{attributeName}]" } :
            new[] { "using Csla;", $"[{attributeName}]" };

            await TestHelpers.VerifyActionAsync(actions,
                                                expectedDescription, document, tree, expectedTexts);
        }
Ejemplo n.º 2
0
        public async Task VerifyGetFixes(string operationName, string attributeName, bool includeUsingCsla,
                                         string expectedDescription)
        {
            var code =
                $@"{(includeUsingCsla ? "using Csla;" : string.Empty)}

public class A : Csla.BusinessBase<A>
{{
  private void {operationName}() {{ }}
}}";
            var document = TestHelpers.Create(code);
            var tree     = await document.GetSyntaxTreeAsync();

            var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new DoesOperationHaveAttributeAnalyzer());

            var sourceSpan = diagnostics[0].Location.SourceSpan;

            var actions = new List <CodeAction>();
            var codeActionRegistration = new Action <CodeAction, ImmutableArray <Diagnostic> >(
                (a, _) => { actions.Add(a); });

            var fix            = new DoesOperationHaveAttributeAddAttributeCodeFix();
            var codeFixContext = new CodeFixContext(document, diagnostics[0],
                                                    codeActionRegistration, new CancellationToken(false));
            await fix.RegisterCodeFixesAsync(codeFixContext);

            Assert.AreEqual(1, actions.Count, nameof(actions.Count));

            await TestHelpers.VerifyChangesAsync(actions,
                                                 expectedDescription, document,
                                                 (model, newRoot) =>
            {
                Assert.IsTrue(newRoot.DescendantNodes(_ => true).OfType <AttributeSyntax>().Any(_ => _.Name.ToString() == attributeName));

                if (includeUsingCsla)
                {
                    Assert.IsTrue(newRoot.DescendantNodes(_ => true).OfType <UsingDirectiveSyntax>().Any(
                                      _ => _.Name.GetText().ToString() == "Csla"));
                }
            });
        }