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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolsetTest.Dnc.HostGenerator
{
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using WixToolset.Dnc.HostGenerator;
using WixToolset.Mba.Core;
using Xunit;
using VerifyCS = CSharpSourceGeneratorVerifier<WixToolset.Dnc.HostGenerator.DncHostGenerator>;
public class DncHostGeneratorTests
{
static readonly MetadataReference MbaCoreAssembly = MetadataReference.CreateFromFile(typeof(BootstrapperApplicationFactoryAttribute).Assembly.Location);
// https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#unit-testing-of-generators
[Fact]
public async Task FailsBuildWhenMissingAttribute()
{
var code = @"
//[assembly: WixToolset.Mba.Core.BootstrapperApplicationFactory(typeof(Test.BAFactory))]
namespace Test
{
using WixToolset.Mba.Core;
public class BAFactory : BaseBootstrapperApplicationFactory
{
protected override IBootstrapperApplication Create(IEngine engine, IBootstrapperCommand bootstrapperCommand)
{
return null;
}
}
}
";
await new VerifyCS.Test
{
TestState =
{
Sources = { code },
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
AdditionalReferences = { MbaCoreAssembly },
ExpectedDiagnostics =
{
new DiagnosticResult(DncHostGenerator.MissingFactoryAttributeDescriptor),
},
},
}.RunAsync();
}
[Fact]
public async Task GeneratesEntryPoint()
{
var code = @"
[assembly: WixToolset.Mba.Core.BootstrapperApplicationFactory(typeof(Test.BAFactory))]
namespace Test
{
using WixToolset.Mba.Core;
public class BAFactory : BaseBootstrapperApplicationFactory
{
protected override IBootstrapperApplication Create(IEngine engine, IBootstrapperCommand bootstrapperCommand)
{
return null;
}
}
}
";
var generated = String.Format(DncHostGenerator.Template, DncHostGenerator.Version, "Test.BAFactory");
await new VerifyCS.Test
{
TestState =
{
Sources = { code },
GeneratedSources =
{
(typeof(DncHostGenerator), "WixToolset.Dnc.Host.g.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)),
},
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
AdditionalReferences = { MbaCoreAssembly },
},
}.RunAsync();
}
}
}
|