public async Task VerifyGetFixesWhenPrivateConstructorNoArgumentsExists()
        {
            var code = File.ReadAllText(
                $@"Targets\{nameof(CheckConstructorsAnalyzerPublicConstructorCodeFixTests)}\{(nameof(this.VerifyGetFixesWhenPrivateConstructorNoArgumentsExists))}.cs");
            var document = TestHelpers.Create(code);
            var tree     = await document.GetSyntaxTreeAsync();

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

            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 CheckConstructorsAnalyzerPublicConstructorCodeFix();
            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.VerifyActionAsync(actions,
                                                CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.UpdateNonPublicConstructorToPublicDescription, document,
                                                tree, new[] { "    public" });
        }
        public async Task VerifyGetFixesWhenPrivateConstructorNoArgumentsExistsAndLeadingTriviaExists()
        {
            var code =
                @"namespace Csla.Analyzers.Tests.Targets.CheckConstructorsAnalyzerPublicConstructorCodeFixTests
{
  public class VerifyGetFixesWhenPrivateConstructorNoArgumentsExistsAndLeadingTriviaExists
    : BusinessBase<VerifyGetFixesWhenPrivateConstructorNoArgumentsExistsAndLeadingTriviaExists>
  {
    // Hey! Don't loose me! 
    private VerifyGetFixesWhenPrivateConstructorNoArgumentsExistsAndLeadingTriviaExists() { }
  }
}";
            var document = TestHelpers.Create(code);
            var tree     = await document.GetSyntaxTreeAsync();

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

            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 CheckConstructorsAnalyzerPublicConstructorCodeFix();
            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.VerifyActionAsync(actions,
                                                CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.UpdateNonPublicConstructorToPublicDescription, document,
                                                tree, new[] { "// Hey! Don't loose me!", "public" });
        }
        public async Task VerifyGetFixesWhenConstructorNoArgumentsDoesNotExist()
        {
            var code =
                @"using Csla;

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

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

            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 CheckConstructorsAnalyzerPublicConstructorCodeFix();
            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.VerifyActionAsync(actions,
                                                CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.AddPublicConstructorDescription, document,
                                                tree, new[] { "public A()" });
        }
        public void VerifyGetFixableDiagnosticIds()
        {
            var fix = new CheckConstructorsAnalyzerPublicConstructorCodeFix();
            var ids = fix.FixableDiagnosticIds.ToList();

            Assert.AreEqual(1, ids.Count, nameof(ids.Count));
            Assert.AreEqual(Constants.AnalyzerIdentifiers.PublicNoArgumentConstructorIsMissing, ids[0],
                            nameof(Constants.AnalyzerIdentifiers.PublicNoArgumentConstructorIsMissing));
        }
    public void VerifyGetFixableDiagnosticIds()
    {
      var fix = new CheckConstructorsAnalyzerPublicConstructorCodeFix();
      var ids = fix.FixableDiagnosticIds.ToList();

      Assert.AreEqual(1, ids.Count, nameof(ids.Count));
      Assert.AreEqual(Constants.AnalyzerIdentifiers.PublicNoArgumentConstructorIsMissing, ids[0],
        nameof(Constants.AnalyzerIdentifiers.PublicNoArgumentConstructorIsMissing));
    }
コード例 #6
0
        public async Task VerifyGetFixesWhenPrivateConstructorNoArgumentsExistsWithNestedClasses()
        {
            var code =
                @"using Csla;

public class A : BusinessBase<A>
{
  public A() { }

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

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

            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 CheckConstructorsAnalyzerPublicConstructorCodeFix();
            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,
                                                 CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.UpdateNonPublicConstructorToPublicDescription, document,
                                                 (model, newRoot) =>
            {
                var classBNode    = newRoot.DescendantNodes(_ => true).OfType <ClassDeclarationSyntax>().Single(_ => _.Identifier.Text == "B");
                var classBSymbol  = model.GetDeclaredSymbol(classBNode) as INamedTypeSymbol;
                var methodBSymbol = classBSymbol.GetMembers().OfType <IMethodSymbol>()
                                    .Single(_ => _.MethodKind == MethodKind.Constructor && _.Name == ".ctor" && _.DeclaredAccessibility == Accessibility.Public);
                Assert.IsTrue(methodBSymbol.Parameters.Length == 0);
            });
        }
    public async Task VerifyGetFixesWhenPrivateConstructorNoArgumentsExists()
    {
      var code = File.ReadAllText(
        $@"Targets\{nameof(CheckConstructorsAnalyzerPublicConstructorCodeFixTests)}\{(nameof(this.VerifyGetFixesWhenPrivateConstructorNoArgumentsExists))}.cs");
      var document = TestHelpers.Create(code);
      var tree = await document.GetSyntaxTreeAsync();
      var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new CheckConstructorsAnalyzer());
      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 CheckConstructorsAnalyzerPublicConstructorCodeFix();
      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.VerifyActionAsync(actions,
        CheckConstructorsAnalyzerPublicConstructorCodeFixConstants.UpdateNonPublicConstructorToPublicDescription, document,
        tree, new[] { "    public" });
    }