-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1214 from JasonBock/1100-operation-return-types
closes #1100 operation return types
- Loading branch information
Showing
21 changed files
with
488 additions
and
29 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...alyzers/Csla.Analyzers.IntegrationTests/Csla.Analyzers.IntegrationTests/BusyProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Csla.Analyzers.IntegrationTests | ||
{ | ||
[Serializable] | ||
public sealed class BusyProperties | ||
: BusinessBase<BusyProperties> | ||
{ | ||
public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id); | ||
public int Id | ||
{ | ||
get | ||
{ | ||
var x = 42; | ||
return ReadProperty(IdProperty) + x; | ||
} | ||
private set { LoadProperty(IdProperty, value); } | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../Csla.Analyzers.IntegrationTests/Csla.Analyzers.IntegrationTests/OperationReturnValues.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Csla.Analyzers.IntegrationTests | ||
{ | ||
[Serializable] | ||
public sealed class OperationReturnValues | ||
: BusinessBase<OperationReturnValues> | ||
{ | ||
private void Foo() { } | ||
private void DataPortal_Fetch(Guid id) { } | ||
private Task DataPortal_Fetch(int id) => Task.CompletedTask; | ||
private string DataPortal_Fetch() => string.Empty; | ||
} | ||
|
||
public sealed class OperationReturnValuesNotCsla | ||
{ | ||
private string DataPortal_Fetch() => string.Empty; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ers/Csla.Analyzers.IntegrationTests/Csla.Analyzers.IntegrationTests/PublicForInterface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Csla.Core; | ||
|
||
namespace Csla.Analyzers.IntegrationTests | ||
{ | ||
public interface PublicForInterface | ||
: IBusinessObject | ||
{ | ||
void DataPortal_Fetch(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lyzers/Csla.Analyzers.IntegrationTests/Csla.Analyzers.IntegrationTests/PublicOperation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace Csla.Analyzers.IntegrationTests | ||
{ | ||
[Serializable] | ||
public sealed class PublicOperation | ||
: BusinessBase<PublicOperation> | ||
{ | ||
public void DataPortal_Fetch() { } | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
...la.Analyzers.Tests/FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Csla.Analyzers.Tests | ||
{ | ||
[TestClass] | ||
public sealed class FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixTests | ||
{ | ||
[TestMethod] | ||
public void VerifyGetFixableDiagnosticIds() | ||
{ | ||
var fix = new FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFix(); | ||
var ids = fix.FixableDiagnosticIds.ToList(); | ||
|
||
Assert.AreEqual(1, ids.Count, nameof(ids.Count)); | ||
Assert.AreEqual(ids[0], Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes, | ||
nameof(Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task VerifyGetFixesWhenChangingToVoid() | ||
{ | ||
var code = | ||
@"using Csla; | ||
public class A : BusinessBase<A> | ||
{ | ||
public string DataPortal_Fetch() { } | ||
}"; | ||
|
||
var document = TestHelpers.Create(code); | ||
var tree = await document.GetSyntaxTreeAsync(); | ||
var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new FindOperationsWithIncorrectReturnTypesAnalyzer()); | ||
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 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFix(); | ||
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, | ||
FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToVoidDescription, document, | ||
tree, new[] { "void" }); | ||
} | ||
|
||
[TestMethod] | ||
public async Task VerifyGetFixesWhenChangingToTask() | ||
{ | ||
var code = | ||
@"using Csla; | ||
using System.Threading.Tasks; | ||
public class A : BusinessBase<A> | ||
{ | ||
public async string DataPortal_Fetch() { } | ||
}"; | ||
|
||
var document = TestHelpers.Create(code); | ||
var tree = await document.GetSyntaxTreeAsync(); | ||
var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new FindOperationsWithIncorrectReturnTypesAnalyzer()); | ||
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 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFix(); | ||
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, | ||
FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription, document, | ||
tree, new[] { "Task" }); | ||
} | ||
|
||
[TestMethod] | ||
public async Task VerifyGetFixesWhenChangingToTaskAndUsingDoesNotExist() | ||
{ | ||
var code = | ||
@"using Csla; | ||
public class A : BusinessBase<A> | ||
{ | ||
public async string DataPortal_Fetch() { } | ||
}"; | ||
|
||
var document = TestHelpers.Create(code); | ||
var tree = await document.GetSyntaxTreeAsync(); | ||
var diagnostics = await TestHelpers.GetDiagnosticsAsync(code, new FindOperationsWithIncorrectReturnTypesAnalyzer()); | ||
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 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFix(); | ||
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, | ||
FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription, document, | ||
tree, new[] { "using System.Threading.Tasks;", "Task" }); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...sla.Analyzers/Csla.Analyzers.Tests/FindOperationsWithIncorrectReturnTypesAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Csla.Analyzers.Tests | ||
{ | ||
[TestClass] | ||
public sealed class FindOperationsWithIncorrectReturnTypesAnalyzerTests | ||
{ | ||
[TestMethod] | ||
public void VerifySupportedDiagnostics() | ||
{ | ||
var analyzer = new FindOperationsWithIncorrectReturnTypesAnalyzer(); | ||
var diagnostics = analyzer.SupportedDiagnostics; | ||
Assert.AreEqual(1, diagnostics.Length); | ||
|
||
var diagnostic = diagnostics[0]; | ||
Assert.AreEqual(Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes, diagnostic.Id, | ||
nameof(DiagnosticDescriptor.Id)); | ||
Assert.AreEqual(FindOperationsWithIncorrectReturnTypesAnalyzerConstants.Title, diagnostic.Title.ToString(), | ||
nameof(DiagnosticDescriptor.Title)); | ||
Assert.AreEqual(FindOperationsWithIncorrectReturnTypesAnalyzerConstants.Message, diagnostic.MessageFormat.ToString(), | ||
nameof(DiagnosticDescriptor.MessageFormat)); | ||
Assert.AreEqual(Constants.Categories.Design, diagnostic.Category, | ||
nameof(DiagnosticDescriptor.Category)); | ||
Assert.AreEqual(DiagnosticSeverity.Error, diagnostic.DefaultSeverity, | ||
nameof(DiagnosticDescriptor.DefaultSeverity)); | ||
Assert.AreEqual(HelpUrlBuilder.Build(Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes, nameof(FindOperationsWithIncorrectReturnTypesAnalyzer)), | ||
diagnostic.HelpLinkUri, | ||
nameof(DiagnosticDescriptor.HelpLinkUri)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AnalyzeWithNotMobileObject() | ||
{ | ||
var code = "public class A { }"; | ||
await TestHelpers.RunAnalysisAsync<FindOperationsWithIncorrectReturnTypesAnalyzer>( | ||
code, Array.Empty<string>()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AnalyzeWithMobileObjectAndMethodIsNotOperation() | ||
{ | ||
var code = | ||
@"using Csla; | ||
public class A : BusinessBase<A> | ||
{ | ||
public void Foo() { } | ||
}"; | ||
await TestHelpers.RunAnalysisAsync<FindOperationsWithIncorrectReturnTypesAnalyzer>( | ||
code, Array.Empty<string>()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AnalyzeWithMobileObjectAndMethodIsOperationReturningVoid() | ||
{ | ||
var code = | ||
@"using Csla; | ||
public class A : BusinessBase<A> | ||
{ | ||
private void DataPortal_Fetch() { } | ||
}"; | ||
await TestHelpers.RunAnalysisAsync<FindOperationsWithIncorrectReturnTypesAnalyzer>( | ||
code, Array.Empty<string>()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AnalyzeWithMobileObjectAndMethodIsOperationReturningTask() | ||
{ | ||
var code = | ||
@"using Csla; | ||
using System.Threading.Tasks; | ||
public class A : BusinessBase<A> | ||
{ | ||
private async Task DataPortal_Fetch() { } | ||
}"; | ||
await TestHelpers.RunAnalysisAsync<FindOperationsWithIncorrectReturnTypesAnalyzer>( | ||
code, Array.Empty<string>()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AnalyzeWithMobileObjectAndMethodIsOperationReturningIncorrectType() | ||
{ | ||
var code = | ||
@"using Csla; | ||
public class A : BusinessBase<A> | ||
{ | ||
private string DataPortal_Fetch() { } | ||
}"; | ||
await TestHelpers.RunAnalysisAsync<FindOperationsWithIncorrectReturnTypesAnalyzer>( | ||
code, new[] { Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.