forked from jitbit/MapDataReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGenerator.cs
More file actions
109 lines (95 loc) · 3.94 KB
/
TestGenerator.cs
File metadata and controls
109 lines (95 loc) · 3.94 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
109
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Collections.Immutable;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace MapDataReader.Tests
{
[TestClass]
public class TestGenerator
{
[TestMethod]
public void TestGeneral()
{
string userSource = @"
using MapDataReader;
namespace MyCode
{
[GenerateDataReaderMapper]
public class MyClass
{
public string Name {get;set;}
public int Size {get;set;}
public bool Enabled {get;set;}
public System.DateTime Created {get;set;}
public System.DateTimeOffset Offset {get;set;}
public decimal Price {get;set;}
}
}
";
var src = GetAndCheckOutputSource(userSource);
}
[TestMethod]
public void TestAccessModifier()
{
string userSource = @"
using MapDataReader;
namespace MyCode
{
[GenerateDataReaderMapper(AccessModifier = ""internal"")]
public class MyClass
{
public string Name {get;set;}
public int Size {get;set;}
public bool Enabled {get;set;}
public System.DateTime Created {get;set;}
public System.DateTimeOffset Offset {get;set;}
public decimal Price {get;set;}
}
}
";
var src = GetAndCheckOutputSource(userSource);
}
//gets generated source and also unit-tests for exceptions and empty diagnistics etc
private string GetAndCheckOutputSource(string inputSource)
{
var generator = new MapperGenerator();
Compilation comp = CreateCompilation(inputSource);
// Create the driver that will control the generation, passing in our generator
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);
// Run the generation pass
// (Note: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls)
driver = driver.RunGeneratorsAndUpdateCompilation(comp, out var outputCompilation, out var diagnostics);
// We can now assert things about the resulting compilation:
Assert.IsTrue(diagnostics.IsEmpty); // there were no diagnostics created by the generators
Assert.IsTrue(outputCompilation.SyntaxTrees.Count() == 2); // we have two syntax trees, the original 'user' provided one, and the one added by the generator
var compDiag = outputCompilation.GetDiagnostics();
Assert.IsTrue(compDiag.IsEmpty); // verify the compilation with the added source has no diagnostics
// Or we can look at the results directly:
GeneratorDriverRunResult runResult = driver.GetRunResult();
// The runResult contains the combined results of all generators passed to the driver
Assert.IsTrue(runResult.GeneratedTrees.Length == 1);
Assert.IsTrue(runResult.Diagnostics.IsEmpty);
// Or you can access the individual results on a by-generator basis
GeneratorRunResult generatorResult = runResult.Results[0];
Assert.IsTrue(generatorResult.Generator == generator);
Assert.IsTrue(generatorResult.Diagnostics.IsEmpty);
Assert.IsTrue(generatorResult.GeneratedSources.Length == 1);
Assert.IsTrue(generatorResult.Exception is null);
//now actually return the source generated
return generatorResult.GeneratedSources[0].SourceText.ToString();
}
private static Compilation CreateCompilation(string source)
=> CSharpCompilation.Create("compilation",
new[] { CSharpSyntaxTree.ParseText(source) },
new[] { MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(MapperGenerator).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(IDataReader).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "System.Runtime.dll")),
MetadataReference.CreateFromFile(AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "netstandard").Location)
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}
}