Created
April 24, 2013 05:13
-
-
Save kazuk/5449781 to your computer and use it in GitHub Desktop.
Code Digger が吐いたテストを単体テストに取りこむ君
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="false" hostspecific="true" language="C#" #> | |
<#@ assembly name="$(SolutionDir)\CodeDiggerTestPickuper\bin\Debug\CodeDiggerTestPickuper.dll"#> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Xml" #> | |
<#@ assembly name="System.Xml.linq" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Xml.Linq" #> | |
<#@ output extension=".cs" #> | |
<# | |
/* HOW TO CONFIGURE | |
1. Create "Unit Test Project" and add this tt | |
2. add assembly reference for TargetProject | |
add T4 directive like bellow | |
@ assembly name="$(SolutionDir)\TargetProjectName\bin\Debug\TargetAssemblyName.dll" | |
save this file. | |
3. clear some TODO's | |
*/ | |
#> | |
// TODO: configure output namespace | |
<# | |
var namespaceName = "CodeDiggerTestPickuper.Tests"; | |
List<string> imports = new List<string>(); | |
Dictionary<string,List<string>> methods = new Dictionary<string,List<string>>(); | |
Dictionary<string,Type> testTypes = new Dictionary<string,Type>(); | |
foreach( var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a=>!a.IsDynamic) ) | |
{ | |
var codebaseUri = new Uri(assembly.CodeBase); | |
var path = Path.Combine( Path.GetDirectoryName( codebaseUri.LocalPath ), "reports" ); | |
if(!Directory.Exists( path ) ) continue; | |
foreach( var reportFolder in Directory.GetDirectories(path) ) | |
{ | |
var perFile =Path.Combine( reportFolder, "report.per" ); | |
if( !File.Exists( perFile ) ) continue; | |
#> | |
// loading <#= perFile #> | |
<# | |
var doc = XDocument.Load( perFile ); | |
var profilerPath = doc.Element("pex").Element("environment").Element("envVars").Elements("envVar") | |
.Where( e=>e.Attribute("name").Value=="COR_PROFILER_PATH" ) | |
.Single(); | |
#> | |
// info PexFrameworkDir = <#=Path.GetDirectoryName( profilerPath.Attribute("value").Value )#> | |
// TODO: add reference Microsoft.Pex.Framework.dll to this Project. path shown previous line | |
<# | |
var fixtures =doc.Element("pex").Element("assembly").Elements("fixture"); | |
foreach( var fixture in fixtures ) { | |
var methodCodes = fixture.Elements("exploration").Elements("generatedTest").Elements("methodCode"); | |
var typeUnderTest = fixture.Element("settings").Attribute("typeUnderTest").Value; | |
var type = FindType( typeUnderTest ); | |
testTypes.Add(fixture.Attribute("name").Value , type ); | |
List<string> m = new List<string>(); | |
foreach(var methodCode in methodCodes ) | |
{ | |
imports.AddRange( methodCode.Attribute("imports").Value.Split(';') ); | |
foreach( var cdata in methodCode.Nodes().OfType<XCData>() ) | |
{ | |
m.Add( | |
cdata.Value.Replace("\r","\n").Replace("\n\n","\n").Replace("\n","\r\n") ); | |
} | |
} | |
methods.Add( fixture.Attribute("name").Value, m ); | |
} | |
} | |
} | |
#> | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
<# | |
foreach( var import in imports.OrderBy( _=>_).Distinct() ) | |
{ | |
#> | |
using <#=import#>; | |
<# | |
} | |
foreach( var ns in testTypes.Values.Select( t=>t.Namespace ).Distinct() ) | |
{ | |
#> | |
using <#=ns#>; | |
<# | |
} | |
#> | |
namespace <#=namespaceName #> | |
{ | |
<# | |
PushIndent(" "); | |
foreach( var fixtureKvp in methods ) { | |
#> | |
[TestClass] | |
public partial class <#=fixtureKvp.Key#> | |
{ | |
<# | |
PushIndent(" "); | |
foreach( var method in fixtureKvp.Value ) | |
{ | |
#> | |
[TestMethod] | |
<#=method#> | |
<# | |
} | |
PopIndent(); | |
#> | |
} | |
<# | |
} | |
#> | |
<# | |
foreach( var testTypeKvp in testTypes ) | |
{ | |
#> | |
public partial class <#=testTypeKvp.Key#> | |
{ | |
<# | |
PushIndent(" "); | |
foreach( var m in testTypeKvp.Value.GetMethods() ) | |
{ | |
#> | |
public <#=m.ReturnType#> <#=m.Name#>( <#=testTypeKvp.Value.Name#> instance <#=m.GetParameters().Any()?",":""#> <# Generate( m.GetParameters(), "," , (p)=>{#> <#=p.ParameterType#> <#=p.Name#> <#}); #> ) | |
{ | |
return instance.<#=m.Name#>( <# Generate( m.GetParameters(), "," , (p)=>{#> <#=p.Name#> <#}); #>); | |
} | |
<# | |
} | |
PopIndent(); | |
#> | |
} | |
<# | |
} | |
PopIndent(); | |
#> | |
} | |
<#+ | |
public Type FindType( string typeName ) | |
{ | |
foreach( var assembly in AppDomain.CurrentDomain.GetAssemblies() ) | |
{ | |
var type = assembly.GetType( typeName ); | |
if( type!=null ) return type; | |
} | |
throw new ApplicationException( typeName + " not found "); | |
} | |
public void Generate<T>( IEnumerable<T> items , string separator, Action<T> action ) | |
{ | |
bool first = true; | |
foreach( var item in items ) | |
{ | |
if( !first ) Write( separator ); | |
action(item); | |
first =false; | |
} | |
} | |
#> |
hi, MNF.
I blogged this. but japanese.
"how to configure" described in source line 14.
@MNF: I think this T4 is used to output unit tests based on code explorations done by Pex engine. This T4 is likely initiated by the fact that full Pex functionality (including automated unit test generation) is only supported in VS2010. In VS2012 one can only use CodeDigger that only does code explorations.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you describe when & how should developer use your template?