Skip to content

Commit

Permalink
Fix for Issue #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
PoopBalloon committed May 27, 2014
1 parent 440d5d0 commit 5d27ba5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Fibber/FibberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public FibberConfiguration For<T>(RandGen randomGenerator)
}

/// <summary>
/// Uses the built in Generators methods for all properties of type bool, byte, byte[], decimal, float, Int16, Int32, Int64, and string (RandGen.String).
/// Uses the built in Generators methods for all properties of type bool, byte, byte[], datetime, datetimeoffset, decimal, float, Int16, Int32, Int64, and string (RandGen.String).
/// </summary>
public void UseDefaults()
{
Expand All @@ -121,6 +121,14 @@ public void UseDefaults()
byteArrayExpando.Generator = RandGen.ByteArray;
TypeGenerators.Add(typeof(byte[]), byteArrayExpando);

dynamic dateTimeExpando = new ExpandoObject();
dateTimeExpando.Generator = RandGen.DateTime;
TypeGenerators.Add(typeof(DateTime), dateTimeExpando);

dynamic dateTimeOffsetExpando = new ExpandoObject();
dateTimeOffsetExpando.Generator = RandGen.DateTimeOffset;
TypeGenerators.Add(typeof(DateTimeOffset), dateTimeOffsetExpando);

dynamic decimalExpando = new ExpandoObject();
decimalExpando.Generator = RandGen.Decimal;
TypeGenerators.Add(typeof(decimal), decimalExpando);
Expand Down
42 changes: 41 additions & 1 deletion src/Fibber/FibberEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public T Fib<T>(T item)
{
var enumValues = propertyType.GetEnumValues();
var randEnum = enumValues.GetValue(_randomGen.Value.Next(0, enumValues.Length));

property.SetValue(item, randEnum, null);
}
else if (propertyType.IsPublic && !propertyType.IsPrimitive && propertyType.IsClass && !propertyType.IsValueType && !propertyType.IsAbstract)
Expand Down Expand Up @@ -254,6 +254,14 @@ private T Fib<T>(T item, Type type, int currentDepth, out int actualDepth)
byteArrayExpando.Generator = RandGen.ByteArray;
newTypeGenerators.Add(typeof(byte[]), byteArrayExpando);

dynamic dateTimeExpando = new ExpandoObject();
dateTimeExpando.Generator = RandGen.DateTime;
newTypeGenerators.Add(typeof(DateTime), dateTimeExpando);

dynamic dateTimeOffsetExpando = new ExpandoObject();
dateTimeOffsetExpando.Generator = RandGen.DateTimeOffset;
newTypeGenerators.Add(typeof(DateTimeOffset), dateTimeOffsetExpando);

dynamic decimalExpando = new ExpandoObject();
decimalExpando.Generator = RandGen.Decimal;
newTypeGenerators.Add(typeof(decimal), decimalExpando);
Expand Down Expand Up @@ -368,6 +376,20 @@ private void GenerateRandomValue<T>(T item, PropertyInfo property, RandGen rando
property.SetValue(item, value, null);
}

break;
case RandGen.DateTime:
{
var value = this.GenerateDateTime();
property.SetValue(item, value, null);
}

break;
case RandGen.DateTimeOffset:
{
var value = this.GenerateDateTimeOffset();
property.SetValue(item, value, null);
}

break;
case RandGen.Decimal:
{
Expand Down Expand Up @@ -473,6 +495,24 @@ private byte[] GenerateByteArray()
return returnValue;
}

private DateTime GenerateDateTime()
{
var returnValue = DateTime.Now;

var seconds = _randomGen.Value.Next(-17776000, 17776000);

return returnValue.AddSeconds(seconds);
}

private DateTimeOffset GenerateDateTimeOffset()
{
var returnValue = DateTimeOffset.Now;

var seconds = _randomGen.Value.Next(-17776000, 17776000);

return returnValue.AddSeconds(seconds);
}

private decimal GenerateDecimal()
{
byte scale = (byte)_randomGen.Value.Next(29);
Expand Down
4 changes: 2 additions & 2 deletions src/Fibber/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.3.5.0")]
[assembly: AssemblyFileVersion("0.3.5.0")]
[assembly: AssemblyVersion("0.4.0.0")]
[assembly: AssemblyFileVersion("0.4.0.0")]
10 changes: 10 additions & 0 deletions src/Fibber/RandGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public enum RandGen
/// </summary>
ByteArray,

/// <summary>
/// Generate a random DateTime 90 days before or 90 after the current datetime.
/// </summary>
DateTime,

/// <summary>
/// Generate a random DateTimeOffset 90 days before or 90 after the current UTC datetime.
/// </summary>
DateTimeOffset,

/// <summary>
/// Generate a random decimal.
/// </summary>
Expand Down

0 comments on commit 5d27ba5

Please sign in to comment.