forked from AArnott/ImmutableObjectGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGenTests.cs
92 lines (80 loc) · 3.39 KB
/
CodeGenTests.cs
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
namespace ImmutableObjectGraph.CodeGeneration.Tests
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Text;
using Microsoft.ImmutableObjectGraph_SFG;
using Xunit;
public class CodeGenTests
{
protected Solution solution;
protected ProjectId projectId;
protected DocumentId inputDocumentId;
public CodeGenTests()
{
var workspace = new CustomWorkspace();
var project = workspace.CurrentSolution.AddProject("test", "test", "C#")
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddMetadataReference(MetadataReference.CreateFromAssembly(typeof(string).Assembly))
.AddMetadataReference(MetadataReference.CreateFromAssembly(typeof(GenerateImmutableAttribute).Assembly))
.AddMetadataReference(MetadataReference.CreateFromAssembly(typeof(CodeGenerationAttribute).Assembly))
.AddMetadataReference(MetadataReference.CreateFromAssembly(typeof(Optional).Assembly));
var inputDocument = project.AddDocument("input.cs", string.Empty);
this.inputDocumentId = inputDocument.Id;
project = inputDocument.Project;
this.projectId = inputDocument.Project.Id;
this.solution = project.Solution;
}
[Fact]
public async Task Empty_NoBuildBreaks()
{
Document result = await this.GenerateAsync(@"
[ImmutableObjectGraph.CodeGeneration.GenerateImmutable]
partial class Empty { }
");
}
protected async Task<Document> GenerateAsync(string inputSource)
{
var solution = this.solution.WithDocumentText(this.inputDocumentId, SourceText.From(inputSource));
var inputDocument = solution.GetDocument(this.inputDocumentId);
var outputDocument = await DocumentTransform.TransformAsync(inputDocument, new MockProgress());
// Make sure there are no compile errors.
var compilation = await outputDocument.Project.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics();
var errors = from diagnostic in diagnostics
where diagnostic.Severity >= DiagnosticSeverity.Error
select diagnostic;
Console.WriteLine(await outputDocument.GetTextAsync());
foreach (var error in errors)
{
Console.WriteLine(error);
}
Assert.Empty(errors);
return outputDocument;
}
private class MockProgress : IProgressAndErrors
{
public void Error(string message, uint line, uint column)
{
throw new NotImplementedException();
}
public void Report(uint progress, uint total)
{
throw new NotImplementedException();
}
public void Warning(string message, uint line, uint column)
{
throw new NotImplementedException();
}
}
}
}