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.
IsBusinessObjectSerializableMakeSerializableCodeFixTests.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
2using Microsoft.CodeAnalysis.CodeActions;
3using Microsoft.CodeAnalysis.CodeFixes;
4using Microsoft.CodeAnalysis.CSharp.Syntax;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using System;
7using System.Collections.Generic;
8using System.Collections.Immutable;
9using System.Linq;
10using System.Threading;
11using System.Threading.Tasks;
12
14{
15 [TestClass]
17 {
18 [TestMethod]
20 {
22 var ids = fix.FixableDiagnosticIds.ToList();
23
24 Assert.AreEqual(1, ids.Count, nameof(ids.Count));
25 Assert.AreEqual(ids[0], Constants.AnalyzerIdentifiers.IsBusinessObjectSerializable,
26 nameof(Constants.AnalyzerIdentifiers.IsBusinessObjectSerializable));
27 }
28
29 [TestMethod]
31 {
32 var code =
33@"using Csla;
34using System;
35
36public class A : BusinessBase<A>
37{
38 [Fetch]
39 public void Fetch() { }
40}";
41 var document = TestHelpers.Create(code);
42 var tree = await document.GetSyntaxTreeAsync();
43 var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new IsBusinessObjectSerializableAnalyzer());
44 var sourceSpan = diagnostics[0].Location.SourceSpan;
45
46 var actions = new List<CodeAction>();
47 var codeActionRegistration = new Action<CodeAction, ImmutableArray<Diagnostic>>(
48 (a, _) => { actions.Add(a); });
49
51 var codeFixContext = new CodeFixContext(document, diagnostics[0],
52 codeActionRegistration, new CancellationToken(false));
53 await fix.RegisterCodeFixesAsync(codeFixContext);
54
55 Assert.AreEqual(1, actions.Count, nameof(actions.Count));
56
57 await TestHelpers.VerifyChangesAsync(actions,
58 IsBusinessObjectSerializableMakeSerializableCodeFixConstants.AddSerializableDescription, document,
59 (model, newRoot) =>
60 {
61 Assert.IsTrue(newRoot.DescendantNodes(_ => true).OfType<AttributeSyntax>().Any(_ => _.Name.ToString() == "Serializable"));
62 });
63 }
64
65 [TestMethod]
67 {
68 var code =
69@"using Csla;
70
71public class A : BusinessBase<A>
72{
73 [Fetch]
74 public void Fetch() { }
75}";
76 var document = TestHelpers.Create(code);
77 var tree = await document.GetSyntaxTreeAsync();
78 var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new IsBusinessObjectSerializableAnalyzer());
79 var sourceSpan = diagnostics[0].Location.SourceSpan;
80
81 var actions = new List<CodeAction>();
82 var codeActionRegistration = new Action<CodeAction, ImmutableArray<Diagnostic>>(
83 (a, _) => { actions.Add(a); });
84
86 var codeFixContext = new CodeFixContext(document, diagnostics[0],
87 codeActionRegistration, new CancellationToken(false));
88 await fix.RegisterCodeFixesAsync(codeFixContext);
89
90 Assert.AreEqual(1, actions.Count, nameof(actions.Count));
91
92 await TestHelpers.VerifyChangesAsync(actions,
93 IsBusinessObjectSerializableMakeSerializableCodeFixConstants.AddSerializableAndUsingDescription, document,
94 (model, newRoot) =>
95 {
96 Assert.IsTrue(newRoot.DescendantNodes(_ => true).OfType<UsingDirectiveSyntax>().Any(
97 _ => _.Name.GetText().ToString() == "System"));
98 Assert.IsTrue(newRoot.DescendantNodes(_ => true).OfType<AttributeSyntax>().Any(_ => _.Name.ToString() == "Serializable"));
99 });
100 }
101 }
102}