Skip to content

Commit

Permalink
#178: Add .OrDefault() typed extension method. Thanks @anorborg!
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Sep 26, 2018
1 parent b00bf60 commit cd5e078
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v24.0.1
## v24.1.0
* Added `nullWeight` parameter to `.OrNull()` extension method for weighted generation of null values.
* Added new `.OrDefault()` extension method. Thanks @anorborg!

## v24.0.0
* BREAKING: Deterministic values may have changed. Parity with **faker.js** @ 07f39bd3.
Expand Down
1 change: 1 addition & 0 deletions Source/Bogus.Tests/Bogus.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net471;netcoreapp2.0</TargetFrameworks>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
28 changes: 25 additions & 3 deletions Source/Bogus.Tests/GitHubIssues/Issue178.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,40 @@ public void weighted_null_check()
{
var f = new Faker();
var mostlyNull = Enumerable.Range(1, 100)
.Select(n => (int?)n.OrNull(f, 0.9))
.Select(n => (int?)n.OrNull(f, 0.9f))
.Count( n => !n.HasValue);

mostlyNull.Should().BeGreaterThan(80);


var mostlyNotNull = Enumerable.Range(1, 100)
.Select(n => (int?)n.OrNull(f, 0.1))
.Select(n => (int?)n.OrNull(f, 0.1f))
.Count(n => !n.HasValue);

mostlyNotNull.Should().BeLessThan(20);
}

[Fact]
public void weighted_default_check()
{
var f = new Faker();
var mostlyDefault = Enumerable.Range(1, 100)
.Select(n => n.OrDefault(f, 0.9f))
.Count(n => n == default);

mostlyDefault.Should().BeGreaterThan(80);

var mostlyNotDefault = Enumerable.Range(1, 100)
.Select(n => n.OrDefault(f, 0.1f))
.Count(n => n == default);

mostlyNotDefault.Should().BeLessThan(20);

var mostlyNotDefaultObject = Enumerable.Range(1, 100)
.Select( n => new object())
.Select(s => s.OrDefault(f, 0.1f))
.Count(s => s == null);

mostlyNotDefaultObject.Should().BeLessThan(20);
}
}
}
14 changes: 13 additions & 1 deletion Source/Bogus/Extensions/ExtensionsForFakerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@ public static List<T> GenerateBetween<T>(this Faker<T> faker, int min, int max,
/// </summary>
/// <param name="f">The Faker facade. This is usually the f from f => lambda.</param>
/// <param name="nullWeight">The probability of null occuring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% null is desired pass nullWeight = 0.15f.</param>
public static object OrNull(this object value, Faker f, double nullWeight = 0.5f)
public static object OrNull(this object value, Faker f, float nullWeight = 0.5f)
{
if (nullWeight > 1 || nullWeight < 0) throw new ArgumentOutOfRangeException(nameof(nullWeight), $".{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' must be between 1.0f and 0.0f. ");
return f.Random.Float() > nullWeight ? value : null;
}

/// <summary>
/// Helpful extension for creating randomly default(T) values for <seealso cref="Faker{T}"/>.RuleFor() rules.
/// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrDefault(f))
/// </summary>
/// <param name="f">The Faker facade. This is usually the f from f => lambda.</param>
/// <param name="defaultWeight">The probability of default(T) occuring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% default(T) is desired pass defaultWeight = 0.15f.</param>
public static T OrDefault<T>(this T value, Faker f, float defaultWeight = 0.5f)
{
if (defaultWeight > 1 || defaultWeight < 0) throw new ArgumentOutOfRangeException(nameof(defaultWeight), $".{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultWeight}' must be between 1.0f and 0.0f. ");
return f.Random.Float() > defaultWeight ? value : default;
}
}
}

0 comments on commit cd5e078

Please sign in to comment.