Skip to content

Instantly share code, notes, and snippets.

@kazuk
Created April 24, 2013 05:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuk/5449781 to your computer and use it in GitHub Desktop.
Save kazuk/5449781 to your computer and use it in GitHub Desktop.
Code Digger が吐いたテストを単体テストに取りこむ君
<#@ 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;
}
}
#>
@MNF
Copy link

MNF commented Jul 2, 2013

Could you describe when & how should developer use your template?

@kazuk
Copy link
Author

kazuk commented Jul 11, 2013

@litera
Copy link

litera commented Oct 14, 2013

@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