forked from pcsikos/UnitTestGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgumentCheckReuse.tt
86 lines (78 loc) · 2.97 KB
/
ArgumentCheckReuse.tt
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
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDte" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="EnvDTE" #>
<#@ Assembly Name="$(SolutionDir)FooBarLibrary\bin\Debug\FooBarLibrary.dll" #>
<#@ Assembly Name="$(SolutionDir)FooBarLibrary.Tests\bin\Debug\UnitTestGenerator.dll" #>
<#@ Assembly Name="$(SolutionDir)FooBarLibrary.Tests\bin\Debug\UnitTestGenerator.Extensions.Composition.dll" #>
<#@ import namespace="UnitTestGenerator" #>
<#@ import namespace="UnitTestGenerator.Extensions.Composition" #>
<#@ import namespace="FooBarLibrary" #>
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestGenerator;
#pragma warning disable RECS0026 // Possible unassigned object created by 'new'
#pragma warning disable RECS0001 // Class is declared partial but has only one part
<#
var assembly = typeof(FooBarLibrary.Foo).Assembly;
var generator = assembly.ComposeTestClassBuilder(GetProjectName(), configure: configure => configure
.IncludeBuiltinGenerators()
.ParameterTypeMapping(new Dictionary<Type, string> {
{ typeof(Foo), "fooInstance" }
}));
foreach(var testClass in generator.BuildTestClasses())
{
#>
namespace <#= TranslateNamespace(testClass.TestedType.Namespace) #>
{
[TestClass]
public partial class <#= testClass.TestedType.Name #>Tests_Reuse
{
<#
foreach(var method in testClass.Methods)
{
var lines = method.SourceCode.Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
#>
[TestMethod]
[TestCategory("UnitTestGenerator.ArgumentCheck")]
<#
if (method.ShouldThrowException != null)
{ #>
[ExpectedException(typeof(<#= method.ShouldThrowException.FullName #>))]
<# }
#>
public void <#=method.Name#>()
{
<# foreach (var line in lines)
{ #>
<#= line #>
<# }#>
}
<#
}
#>
}
}
<#
}
#>
#pragma warning restore RECS0026 // Possible unassigned object created by 'new'
#pragma warning restore RECS0001 // Class is declared partial but has only one part
<#+
string TranslateNamespace(string nSpace)
{
var assembly = typeof(FooBarLibrary.Foo).Assembly;
var name = assembly.GetName().Name;
return nSpace.Replace(name, name + ".Tests");
}
string GetProjectName()
{
var dte = (this.Host as IServiceProvider).GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
var currentProject = dte.Solution.FindProjectItem(this.Host.TemplateFile).ContainingProject as EnvDTE.Project;
return currentProject.Name;
}
#>