forked from UbiquityDotNET/Llvm.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionsKeywordAnalyzerTests.cs
More file actions
108 lines (92 loc) · 3.37 KB
/
Copy pathExtensionsKeywordAnalyzerTests.cs
File metadata and controls
108 lines (92 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
// This isn't a normal built-in define; but follows the pattern for built-in defines and expresses the intent.
// There is no Known way to "light up" at runtime for functionality available in newer versions
// See: Directory.Build.targets for how this is set. (Roslyn compiler v5.x is included in .NET 10)
#if NET_SDK_10_OR_GREATER
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Microsoft.CodeAnalysis.Testing.ReferenceAssemblies;
namespace RepositoryVerifier.UT
{
[TestClass]
public class ExtensionsKeywordAnalyzerTests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task EmptySourceAnalyzesClean( )
{
var analyzerTest = CreateTestRunner(string.Empty);
await analyzerTest.RunAsync( TestContext.CancellationToken );
}
[TestMethod]
public async Task UsingKeywordReportsDiagnostic( )
{
var analyzerTest = CreateTestRunner(ExtensionKeywordUsed);
analyzerTest.ExpectedDiagnostics.AddRange(
[
new DiagnosticResult("UNL002", DiagnosticSeverity.Error).WithLocation( 5, 5 ),
]
);
await analyzerTest.RunAsync( TestContext.CancellationToken );
}
[TestMethod]
public async Task StructEquatableIsNotReferenceEquality( )
{
var analyzerTest = CreateTestRunner(NoExtensionKeywordUsed);
// no diagnostics expected
await analyzerTest.RunAsync( TestContext.CancellationToken );
}
private static AnalyzerTest<DefaultVerifier> CreateTestRunner( string source )
{
return new ExtensionKeywordAnalyzerTest()
{
TestState =
{
Sources = { source },
ReferenceAssemblies = Net.Net80
}
};
}
private class ExtensionKeywordAnalyzerTest
: CSharpAnalyzerTest<ExtensionKeywordAnalyzer, DefaultVerifier>
{
protected override ParseOptions CreateParseOptions( )
{
// Until C# 14 and .NET SDK 10 is formally released, the language version is considered "Preview"
return new CSharpParseOptions( LanguageVersion.Preview, DocumentationMode.Diagnose );
}
}
private const string ExtensionKeywordUsed = """
using System;
public static class TestExtension
{
extension(string self)
{
public string MyExtension()
{
return self;
}
}
}
file class Foo
{
}
""";
private const string NoExtensionKeywordUsed = """
using System;
public static class TestExtension
{
public static string MyExtension(this string self)
{
return self;
}
}
""";
}
}
#endif