Created
February 13, 2012 10:47
-
-
Save LeeCampbell/1815969 to your computer and use it in GitHub Desktop.
LinqPad Query to query the Given/When/Then list of tests
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
void Main() | |
{ | |
//I want to have it format like: | |
//------------------------------- | |
// Given a new strategy | |
// When the name is modified | |
// Then raise property changed | |
// When trade added | |
// The DeltaCashSum equals Trade DeltaCash | |
// When invalid trade added | |
// With old date | |
// then reject with red | |
// With duplicate id | |
// then reject with yellow | |
// then email support | |
var testDllPath = @"c:\Users\Lee\Documents\GitHub\MyProject\src\MyProject.Tests\bin\Debug\MyProject.Tests.dll"; | |
var assembly = Assembly.LoadFrom(testDllPath); | |
var nUnitMethods = from module in assembly.GetModules() | |
from type in module.GetTypes() | |
//where type.IsNestedPublic | |
from method in type.GetMethods() | |
where method.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true).Count()>0 | |
where !method.ReflectedType.IsAbstract | |
//select string.Format("{0}.{1}", method.ReflectedType.GetFriendlyName(), method.Name); | |
select method.GetQualifiedMethodName(); | |
//nUnitMethods.OrderBy (n => n).Dump(); | |
//I can use the ILookup type to create a tree | |
// nUnitMethods.ToLookup( | |
// name => name.Split('.').First(), | |
// name => name.Substring(name.IndexOf('.')+1)) | |
// .Dump(); | |
var ts = new TestSuite("MyProject.Tests", nUnitMethods); | |
ts.Format(0).Dump(); | |
} | |
// Define other methods and classes here | |
public static class TypeEx | |
{ | |
public static string GetFriendlyName(this Type t) | |
{ | |
using (var provider = new CSharpCodeProvider()) | |
{ | |
var typeRef = new CodeTypeReference(t); | |
return provider.GetTypeOutput(typeRef); | |
} | |
} | |
public static string GetQualifiedMethodName(this MethodInfo m) | |
{ | |
var typeName = m.ReflectedType.GetFriendlyName().Replace(m.ReflectedType.Namespace + ".", ""); | |
return string.Format("{0}.{1}", typeName, m.Name); | |
} | |
} | |
public class TestSuite | |
{ | |
private static int count = 0; | |
private readonly string _name; | |
private readonly IList<string> _tests; | |
private readonly IList<TestSuite> _subSuites; | |
public TestSuite(string name, IEnumerable<string> tests) | |
{ | |
if(count++>100) return; | |
_name = name; | |
_tests = tests.Where (t => !t.Contains('.')) | |
.Select (t => t.Replace('_', ' ')) | |
.ToList(); | |
_subSuites = tests | |
.Where (t => t.Contains('.')) | |
.GroupBy( | |
test => test.Split('.').First(), | |
test => test.Substring(test.IndexOf('.')+1)) | |
.Select(grp=>new TestSuite(grp.Key.Replace('_', ' '), grp)) | |
.ToList(); | |
} | |
public string Name { get { return _name; } } | |
public IList<string> Tests { get { return _tests; } } | |
public IList<TestSuite> SubSuites { get{ return _subSuites; } } | |
public override string ToString() | |
{ | |
return Format(0); | |
} | |
public string Format(int indent) | |
{ | |
var sb = new StringBuilder(); | |
sb.AppendFormat("{0}{1}", new string(' ', indent*3), Name); | |
indent++; | |
foreach (var test in Tests) | |
{ | |
sb.AppendLine(); | |
sb.AppendFormat("{0}{1}", new string(' ', indent*3), test); | |
} | |
foreach (var suite in SubSuites) | |
{ | |
sb.AppendLine(); | |
sb.Append(suite.Format(indent)); | |
} | |
return sb.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment