Skip to content

Commit

Permalink
Possible fix for #13
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Jan 21, 2016
1 parent 2e5f1d3 commit 1a746be
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
9 changes: 9 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v3.0.6
* Roll-up Release for .NET Framework since v3.0.5.
* CoreCLR users please continue using latest beta release until CoreCLR is RTM.

## v3.0.6-beta-1
* Issue #13: Fixed StrictMode to exclude private fields.
* New Feature: Ignore property or field in StrictMode: Faker[Order].Ignore(o => o.OrderId).
* CoreCLR users please continue using latest beta release until CoreCLR is RTM.

## v3.0.5
* Roll-up Release for .NET Framework since v3.0.4.
* CoreCLR users please continue using latest beta release until CoreCLR is RTM.
Expand Down
18 changes: 18 additions & 0 deletions Source/Bogus.Tests/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Xml.Linq;
using System.Xml.XPath;
using Bogus.DataSets;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
Expand Down Expand Up @@ -194,6 +195,23 @@ public void Handlebar()
randomName.Dump();
}


[Test]
public void TestIgnore()
{
var faker = new Faker<Order>()
.StrictMode(true)
.Ignore(o => o.Item)
.RuleFor(o => o.OrderId, f => 3343)
.RuleFor(o => o.Quantity, f => f.Random.Number(3));

var fake = faker.Generate();

fake.Dump();

fake.Item.Should().BeNull();
}

public class Order
{
public int OrderId { get; set; }
Expand Down
89 changes: 89 additions & 0 deletions Source/Bogus.Tests/GitHubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,94 @@ public void issue_13_readonly_property()
faker.Validate().Should().BeTrue();
faker.TypeProperties.Count.Should().Be(1);
}

[Test]
public void issue_13_with_model()
{
var counter = 0;

var faker = new Faker<TestObject>()
.StrictMode(true)
.RuleFor(c => c.SomeOtherId, f => counter++)
.RuleFor(c => c.SomeId, f => Guid.NewGuid())
.RuleFor(c => c.SomeFutureDate, f => f.Date.Future())
.RuleFor(c => c.SomePastDate, (f, b) => b.SomeFutureDate.AddHours(f.Random.Number(1, 24)))
.RuleFor(c => c.SomeStatusInt, (f, b) => (int)b.SomeExplicitInt)
.RuleFor(c => c.SomeExplicitInt, f => 2)
.RuleFor(c => c.SomeBool3, f => f.Random.Bool())
.RuleFor(c => c.SomeBool2, f => f.Random.Bool())
.RuleFor(c => c.SomeBool1, f => f.Random.Bool())
.RuleFor(c => c.SomeOtherInt, f => f.Random.Number(1, 5))

.RuleFor(c => c.SomeInt, f => 0)
.RuleFor(c => c.SomeOtherString, f => null)
.RuleFor(c => c.SomeOtherGuid, f => Guid.NewGuid())
.RuleFor(c => c.SomeString, f => null)

.RuleFor(c => c.SomeComment, f => f.Lorem.Sentence())
.RuleFor(c => c.SomeGuid, f => null)
.RuleFor(c => c.SomeTimestamp, f => null);

faker.TypeProperties.Count.Should().Be(17);

var fake = faker.Generate();
fake.Dump();
}

public class TestObject
{
private DateTime? _lastTimeToUnbook;

public int SomeOtherId { get; set; }

public Guid SomeId { get; set; }

public DateTime SomeFutureDate { get; set; }

public DateTime SomePastDate { get; set; }

public int SomeStatusInt { get; set; }

public int SomeExplicitInt
{
get { return this.SomeStatusInt; }
set { this.SomeStatusInt = value; }
}

public bool SomeBool3 { get; set; }

public bool SomeBool2 { get; set; }

public bool SomeBool1 { get; set; }

public int SomeOtherInt { get; set; }

public DateTime? ReadOnlyDateTime
{
get
{
return _lastTimeToUnbook;
}
}

public int SomeInt { get; set; }

public string SomeOtherString { get; set; }

public Guid SomeOtherGuid { get; set; }

public string SomeString { get; set; }

public bool Someboolean
{
get { return !this.SomeTimestamp.HasValue; }
}

public DateTime? SomeTimestamp { get; set; }

public Guid? SomeGuid { get; set; }

public string SomeComment { get; set; }
}
}
}
9 changes: 8 additions & 1 deletion Source/Bogus/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ public Dictionary<string, MemberInfo> GetMembers(Type t)
{
return pi.CanWrite;
}
return m is PropertyInfo || m is FieldInfo;
var fi = m as FieldInfo;
if( fi != null )
{
//No private fields.
//Github Issue #13
return !fi.IsPrivate;
}
return false;
})
.ToDictionary(pi => pi.Name);
}
Expand Down
20 changes: 19 additions & 1 deletion Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ public Faker<T> RuleFor<TProperty>(Expression<Func<T, TProperty>> property, Func
return this;
}

/// <summary>
/// Ignore a property or field when using StrictMode.
/// </summary>
/// <typeparam name="TPropertyOrField"></typeparam>
/// <param name="propertyOrField"></param>
/// <returns></returns>
public Faker<T> Ignore<TPropertyOrField>(Expression<Func<T, TPropertyOrField>> propertyOrField)
{
var propNameOrField = PropertyName.For(propertyOrField);

if( !this.TypeProperties.Remove(propNameOrField) )
{
throw new ArgumentException($"The property or field {propNameOrField} was not found on {typeof(T)} during the binding discovery of T. Can't ignore something that doesn't exist.");
}

return this;
}

/// <summary>
/// Ensures all properties of T have rules.
/// </summary>
Expand Down Expand Up @@ -146,7 +164,7 @@ public virtual void Populate(T instance)
}
if( useStrictMode && !IsValid.GetValueOrDefault())
{
throw new InvalidOperationException($"Cannot generate {typeof(T)} because strict mode is enabled on this type and some properties/fields have no rules.");
throw new InvalidOperationException($"StrictMode validation failure on {typeof(T)}. The Binder found {TypeProperties.Count} properties/fields but have {Actions.Count} actions rules.");
}

var typeProps = TypeProperties;
Expand Down

0 comments on commit 1a746be

Please sign in to comment.