forked from UbiquityDotNET/Ubiquity.NET.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootCommandAttributeTests.cs
More file actions
93 lines (80 loc) · 4.42 KB
/
RootCommandAttributeTests.cs
File metadata and controls
93 lines (80 loc) · 4.42 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
// 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.
namespace Ubiquity.NET.CommandLine.SrcGen.UT
{
[TestClass]
public sealed class RootCommandAttributeTests
{
public TestContext TestContext { get; set; }
[TestMethod]
[DataRow( TestRuntime.Net8_0 )]
[DataRow( TestRuntime.Net10_0 )]
public async Task Basic_golden_path_succeeds( TestRuntime testRuntime )
{
const string inputFileName = "input.cs";
const string expectedFileName = "expected.cs";
string hintPath = Path.Combine("Ubiquity.NET.CommandLine.SrcGen", "Ubiquity.NET.CommandLine.SrcGen.CommandGenerator", "TestNamespace.TestOptions.g.cs");
SourceText input = GetSourceText( nameof(Basic_golden_path_succeeds), inputFileName );
SourceText expected = GetSourceText( nameof(Basic_golden_path_succeeds), expectedFileName );
var runner = CreateTestRunner( input, testRuntime, [TrackingNames.CommandClass], hintPath, expected );
await runner.RunAsync( TestContext.CancellationToken );
}
[TestMethod]
[DataRow( TestRuntime.Net8_0 )]
[DataRow( TestRuntime.Net10_0 )]
public async Task Generator_handles_nullable_types( TestRuntime testRuntime )
{
const string inputFileName = "input.cs";
const string expectedFileName = "expected.cs";
string hintPath = Path.Combine("Ubiquity.NET.CommandLine.SrcGen", "Ubiquity.NET.CommandLine.SrcGen.CommandGenerator", "TestNamespace.TestOptions.g.cs");
SourceText input = GetSourceText( nameof(Generator_handles_nullable_types), inputFileName );
SourceText expected = GetSourceText( nameof(Generator_handles_nullable_types), expectedFileName );
var runner = CreateTestRunner( input, testRuntime, [TrackingNames.CommandClass], hintPath, expected );
await runner.RunAsync( TestContext.CancellationToken );
}
[TestMethod]
[DataRow( TestRuntime.Net8_0 )]
[DataRow( TestRuntime.Net10_0 )]
public async Task DemoSource_succeeds( TestRuntime testRuntime )
{
const string inputFileName = "TestOptions.cs";
const string expectedFileName = "expected.cs";
string hintPath = Path.Combine("Ubiquity.NET.CommandLine.SrcGen", "Ubiquity.NET.CommandLine.SrcGen.CommandGenerator", "TestNamespace.TestOptions.g.cs");
SourceText input = GetSourceText( nameof(DemoSource_succeeds), inputFileName );
SourceText expected = GetSourceText( nameof(DemoSource_succeeds), expectedFileName );
var runner = CreateTestRunner( input, testRuntime, [TrackingNames.CommandClass], hintPath, expected );
await runner.RunAsync( TestContext.CancellationToken );
}
private SourceGeneratorTest<MsTestVerifier> CreateTestRunner(
SourceText source,
TestRuntime testRuntime,
ImmutableArray<string> trackingNames,
string expectedHintPath,
SourceText expectedContent
)
{
// Use a test runner with Caching, language and reference assemblies that match the runtime for the test run
return new CachedSourceGeneratorTest<CommandGenerator, MsTestVerifier>( trackingNames, testRuntime.DefaultLangVersion )
{
TestState =
{
Sources = { source },
ReferenceAssemblies = testRuntime.ReferenceAssemblies,
AdditionalReferences =
{
TestContext.GetUbiquityNetCommandLineLib( testRuntime )
},
OutputKind = OutputKind.DynamicallyLinkedLibrary, // Don't require a Main() method
GeneratedSources = { (expectedHintPath, expectedContent) }
},
// Allow ALL diagnostics for testing, input source should contain valid C# code
// but might otherwise trigger the tested analyzer.
CompilerDiagnostics = CompilerDiagnostics.All,
};
}
private static SourceText GetSourceText( params string[] nameParts )
{
return TestHelpers.GetTestText( nameof( RootCommandAttributeTests ), nameParts );
}
}
}