Skip to content

Commit 8216bc3

Browse files
committed
Add some unit tests
1 parent 49dd152 commit 8216bc3

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/Menees.Analyzers/Men019SupportAsyncCancellationToken.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
6-
using System.Text;
76
using Microsoft.CodeAnalysis;
87

98
[DiagnosticAnalyzer(LanguageNames.CSharp)]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace Menees.Analyzers.Test;
2+
3+
using Microsoft.CodeAnalysis.CodeFixes;
4+
5+
[TestClass]
6+
public class Men019UnitTests : CodeFixVerifier
7+
{
8+
#region Protected Properties
9+
10+
protected override DiagnosticAnalyzer CSharpDiagnosticAnalyzer => new Men019SupportAsyncCancellationToken();
11+
12+
#endregion
13+
14+
#region ValidCodeTest
15+
16+
[TestMethod]
17+
public void ValidCodeTest()
18+
{
19+
this.VerifyCSharpDiagnostic(string.Empty);
20+
21+
const string test = @"
22+
using System.Threading;
23+
using System.Threading.Tasks;
24+
25+
#pragma warning disable MEN019 // We have to make some 'invalid' interface methods inheritable.
26+
public interface IThink1 { Task Think1(); }
27+
public interface IThink2 { Task Think2(); }
28+
#pragma warning restore MEN019
29+
30+
public sealed class Cancellable { public CancellationToken Cancel {get;}}
31+
public sealed class TestBase : IThink1
32+
{
33+
public virtual Task Think1() => return Task.CompletedTask;
34+
}
35+
36+
public class Test : IFormattable, IThink2
37+
{
38+
private Task<bool> CheckPrivate(CancellationToken c) { return Task.FromResult(true); }
39+
40+
public async Task UseAsyncKeyword(CancellationToken c) { await Task.Yield(); }
41+
42+
// Interface implementations
43+
Task IThink2.Think2() => return Task.CompletedTask;
44+
public string ToString(string? format, IFormatProvider? formatProvider) => ""test"";
45+
public override Task Think1() => return Task.CompletedTask;
46+
47+
[TestMethod]
48+
public Task UnitTest() => return Task.CompletedTask;
49+
50+
public Task GetsCancelProperty(Cancellable cancellable) => return Task.CompletedTask;
51+
}";
52+
53+
this.VerifyCSharpDiagnostic(test);
54+
}
55+
56+
#endregion
57+
58+
#region InvalidCodeTest
59+
60+
[TestMethod]
61+
public void InvalidCodeTest()
62+
{
63+
const string test = @"
64+
public class Test
65+
{
66+
// todo: Add invalid methods
67+
// (method.DeclaredAccessibility > Accessibility.Private || settings.CheckPrivateMethodsForCancellation)
68+
// !method.IsImplicitlyDeclared
69+
// IsAsyncMethodKindSupported(method.MethodKind)
70+
// !method.ReturnsVoid
71+
// method.ReturnType is not null // Roslyn's ISymbolExtensions.IsAwaitableNonDynamic checks this
72+
// (method.IsAsync || IsAwaitable(method.ReturnType))
73+
// !method.IsOverride
74+
// method.ExplicitInterfaceImplementations.IsDefaultOrEmpty
75+
// !IsImplicitInterfaceImplementation(method)
76+
// !settings.IsUnitTestMethod(method))
77+
// !HasCancellationTokenParameter(parameters)
78+
// !parameters.Any(ParameterHasCancellationTokenProperty)
79+
}";
80+
81+
// var analyzer = this.CSharpDiagnosticAnalyzer;
82+
DiagnosticResult[] expected =
83+
[
84+
// new DiagnosticResult(analyzer)
85+
// {
86+
// Message = "The numeric literal 1000000 should use digit separators.",
87+
// Locations = [new DiagnosticResultLocation("Test0.cs", 4, 30)]
88+
// },
89+
];
90+
91+
this.VerifyCSharpDiagnostic(test, expected);
92+
}
93+
94+
#endregion
95+
}

0 commit comments

Comments
 (0)