Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created February 4, 2014 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyburden/8813406 to your computer and use it in GitHub Desktop.
Save randyburden/8813406 to your computer and use it in GitHub Desktop.
A little test helper code generator class that will script out some C# for a given type. It is meant to help aid in generating boilerplate unit test code.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace TestHelper
{
[TestFixture]
public class CodeGeneratorTests
{
[Test]
public void CanGenerateCodeFromAClassWithProperties()
{
// Act
var code = CodeGenerator.ScriptOutObjectInitializationAndPopulationCode<SomeClassWithProperties>();
// Visual Assertion
Trace.WriteLine( code );
// Assert
Assert.That( code.Contains( "SomeClassWithProperties" ) );
/* Example Output:
var SomeClassWithProperties = new SomeClassWithProperties();
SomeClassWithProperties.Id = 274;
SomeClassWithProperties.Active = false;
SomeClassWithProperties.StartDate = DateTime.Parse("6/23/2014 4:11:00 PM");
SomeClassWithProperties.EndDate = DateTime.Parse("9/29/2014 4:11:00 PM");
SomeClassWithProperties.FirstName = "3222f659-2185-4a2f-988c-977916d98b69";
SomeClassWithProperties.MiddleInitial = 'A';
SomeClassWithProperties.LastName = "88222ff2-a6fb-4c98-8083-c8ae0a09680a";
*/
}
[Test]
public void CanGenerateCodeFromAClassWithFields()
{
// Act
var code = CodeGenerator.ScriptOutObjectInitializationAndPopulationCode<SomeClassWithFields>();
// Visual Assertion
Trace.WriteLine( code );
// Assert
Assert.That( code.Contains( "SomeClassWithFields" ) );
}
public class SomeClassWithProperties
{
public int Id { get; set; }
public bool? Active { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string FirstName { get; set; }
public char MiddleInitial { get; set; }
public string LastName { get; set; }
}
public class SomeClassWithFields
{
public int Id;
public bool? Active;
public DateTime StartDate;
public DateTime? EndDate;
public string FirstName;
public char MiddleInitial;
public string LastName;
}
}
public static class CodeGenerator
{
public static string ScriptOutObjectInitializationAndPopulationCode<T>()
{
var type = typeof ( T );
var dictionary = CreateMemberDictionary<T>();
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine( string.Format( "var {0} = new {0}();", type.Name ) );
foreach ( var item in dictionary )
{
object defaultValue = null;
var memberType = Nullable.GetUnderlyingType( item.Value ) ?? item.Value;
if ( memberType == typeof( string ) )
defaultValue = string.Format( "\"{0}\"", Guid.NewGuid() );
else if ( memberType == typeof( DateTime ) )
defaultValue = string.Format( "DateTime.Parse(\"{0}\")", DateTime.Now.AddDays( new Random( Guid.NewGuid().GetHashCode() ).Next( 1, 365 ) ) );
else if ( memberType == typeof( int ) )
defaultValue = new Random( Guid.NewGuid().GetHashCode() ).Next( 1, 365 );
else if ( memberType == typeof( Boolean ) )
defaultValue = "false";
else if ( memberType == typeof ( char ) )
defaultValue = "'A'";
if ( defaultValue == null )
defaultValue = GetDefault( memberType );
stringBuilder.AppendLine( string.Format( "{0}.{1} = {2};", type.Name, item.Key, defaultValue ) );
}
return stringBuilder.ToString();
}
public static Dictionary<string, Type> CreateMemberDictionary<T>()
{
var type = typeof ( T );
var fields = type.GetFields();
var dictionary = fields.ToDictionary( fieldInfo => fieldInfo.Name, fieldInfo => fieldInfo.FieldType );
var properties = type.GetProperties();
foreach ( var propertyInfo in properties )
{
dictionary.Add( propertyInfo.Name, propertyInfo.PropertyType );
}
return dictionary;
}
public static object GetDefault( Type t )
{
Func<object> f = GetDefault<object>;
return f.Method.GetGenericMethodDefinition().MakeGenericMethod( t ).Invoke( null, null );
}
private static T GetDefault<T>()
{
return default( T );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment