CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
EvaluateManagedBackingFieldsCodeFixTests.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
2using Microsoft.CodeAnalysis.CodeActions;
3using Microsoft.CodeAnalysis.CodeFixes;
4using Microsoft.CodeAnalysis.CSharp;
5using Microsoft.CodeAnalysis.CSharp.Syntax;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7using System;
8using System.Collections.Generic;
9using System.Collections.Immutable;
10using System.Linq;
11using System.Threading;
12using System.Threading.Tasks;
13
15{
16 [TestClass]
18 {
19 [TestMethod]
21 {
23 var ids = fix.FixableDiagnosticIds.ToList();
24
25 Assert.AreEqual(1, ids.Count, nameof(ids.Count));
26 Assert.AreEqual(ids[0], Constants.AnalyzerIdentifiers.EvaluateManagedBackingFields,
27 nameof(Constants.AnalyzerIdentifiers.EvaluateManagedBackingFields));
28 }
29
30 [TestMethod]
31 public async Task VerifyGetFixes()
32 {
33 var code =
34@"using Csla;
35
36public class A : BusinessBase<A>
37{
38 PropertyInfo<string> DataProperty =
39 RegisterProperty<string>(_ => _.Data);
40 public string Data
41 {
42 get { return GetProperty(DataProperty); }
43 set { SetProperty(DataProperty, value); }
44 }
45}";
46 var document = TestHelpers.Create(code);
47 var tree = await document.GetSyntaxTreeAsync();
48 var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new EvaluateManagedBackingFieldsAnalayzer());
49 var sourceSpan = diagnostics[0].Location.SourceSpan;
50
51 var actions = new List<CodeAction>();
52 var codeActionRegistration = new Action<CodeAction, ImmutableArray<Diagnostic>>(
53 (a, _) => { actions.Add(a); });
54
56 var codeFixContext = new CodeFixContext(document, diagnostics[0],
57 codeActionRegistration, new CancellationToken(false));
58 await fix.RegisterCodeFixesAsync(codeFixContext);
59
60 Assert.AreEqual(1, actions.Count, nameof(actions.Count));
61
62 await TestHelpers.VerifyChangesAsync(actions,
63 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription, document,
64 (model, newRoot) =>
65 {
66 var fieldNode = newRoot.DescendantNodes(_ => true).OfType<FieldDeclarationSyntax>().Single().Declaration.Variables[0];
67 var fieldSymbol = model.GetDeclaredSymbol(fieldNode) as IFieldSymbol;
68
69 Assert.IsTrue(fieldSymbol.DeclaredAccessibility == Accessibility.Public);
70 Assert.IsTrue(fieldSymbol.IsStatic);
71 Assert.IsTrue(fieldSymbol.IsReadOnly);
72 });
73 }
74
75 [TestMethod]
76 public async Task VerifyGetFixesWithTrivia()
77 {
78 var code =
79@"using Csla;
80
81public class A : BusinessBase<A>
82{
83 #region Properties
84 private static readonly PropertyInfo<string> DataProperty = RegisterProperty<string>(_ => _.Data);
85 #endregion
86
87 public string Data => GetProperty(DataProperty);
88}";
89 var document = TestHelpers.Create(code);
90 var tree = await document.GetSyntaxTreeAsync();
91 var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new EvaluateManagedBackingFieldsAnalayzer());
92 var sourceSpan = diagnostics[0].Location.SourceSpan;
93
94 var actions = new List<CodeAction>();
95 var codeActionRegistration = new Action<CodeAction, ImmutableArray<Diagnostic>>(
96 (a, _) => { actions.Add(a); });
97
99 var codeFixContext = new CodeFixContext(document, diagnostics[0],
100 codeActionRegistration, new CancellationToken(false));
101 await fix.RegisterCodeFixesAsync(codeFixContext);
102
103 Assert.AreEqual(1, actions.Count, nameof(actions.Count));
104 await TestHelpers.VerifyChangesAsync(actions,
105 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription, document,
106 (model, newRoot) =>
107 {
108 var fieldNode = newRoot.DescendantNodes(_ => true).OfType<FieldDeclarationSyntax>().Single().Declaration.Variables[0];
109 var fieldSymbol = model.GetDeclaredSymbol(fieldNode) as IFieldSymbol;
110
111 Assert.IsTrue(fieldSymbol.DeclaredAccessibility == Accessibility.Public);
112 Assert.IsTrue(fieldSymbol.IsStatic);
113 Assert.IsTrue(fieldSymbol.IsReadOnly);
114
115 Assert.IsTrue(newRoot.DescendantTrivia(_ => true, true).Any(_ => _.IsKind(SyntaxKind.RegionDirectiveTrivia)));
116 });
117 }
118 }
119}