Skip to content

Commit

Permalink
Fixed regression from #170 which threw an exception when CreateSetter…
Browse files Browse the repository at this point in the history
… couldn't find a public setter. This change allows setting of non-public members. Reverts back to previous Bogus behavior which originally allowed setting of non-public members. Related #179.
  • Loading branch information
bchavez committed Sep 27, 2018
1 parent cd5e078 commit a3d2dc9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v24.2.0
* Issue 179: Fixed regression introduced in 23.0.3 that forbid setting of internal/non-public members of `T` in when `Faker<T>` is used.

## v24.1.0
* Added `nullWeight` parameter to `.OrNull()` extension method for weighted generation of null values.
* Added new `.OrDefault()` extension method. Thanks @anorborg!
Expand Down
74 changes: 74 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue179.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;


namespace Bogus.Tests.GitHubIssues
{
public class Issue179 : SeededTest
{
private readonly ITestOutputHelper console;

public Issue179(ITestOutputHelper console)
{
this.console = console;
}

[Fact]
public void should_be_able_to_set_non_public_members_of_T()
{
var fooFaker = new Faker<Buz>()
.RuleFor(x => x.Baz, f => f.Random.String2(10))
.RuleFor(x => x.Bar, f => f.Random.String2(10))
.RuleFor( x => x.Normal, f => f.Random.String2(10))
;

var foos = fooFaker.Generate(10);

foreach (var foo in foos)
{
console.WriteLine($"{foo.Baz} / {foo.Bar} / {foo.Normal}");
foo.Baz.Should().NotBeNullOrWhiteSpace();
foo.Bar.Should().NotBeNullOrWhiteSpace();
foo.Normal.Should().NotBeNullOrWhiteSpace();
}
}

[Fact]
public void can_set_members_on_internal_class()
{
var boxFaker = new Faker<Box>()
.RuleFor(x => x.Bub, f => f.Random.String2(10))
.RuleFor(x => x.Normal, f => f.Random.String2(10))
;

var boxes = boxFaker.Generate(10);

foreach( var box in boxes )
{
console.WriteLine($"{box.Bub} / {box.Normal}");
box.Bub.Should().NotBeNullOrWhiteSpace();
box.Normal.Should().NotBeNullOrWhiteSpace();
}
}
}


public class Buz
{
public string Baz { get; internal set; }

internal string Bar { get; set; }

public string Normal { get; set; }
}

internal class Box
{
internal string Bub { get; private set; }
public string Normal { get; set; }
}
}
2 changes: 1 addition & 1 deletion Source/Bogus/Extensions/ExtensionsForPropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static Action<T, object> CreateSetter<T>(this PropertyInfo property)
{
if (property == null) throw new ArgumentNullException(nameof(property));

var setter = property.GetSetMethod();
var setter = property.GetSetMethod(true);
if (setter == null) throw new ArgumentException("The specified property does not have a public setter.");

var genericHelper = GenericSetterCreationMethod.MakeGenericMethod(property.DeclaringType, property.PropertyType);
Expand Down

0 comments on commit a3d2dc9

Please sign in to comment.