Skip to content

Commit

Permalink
Re: #178, #124. Added nullWeight parameter to .OrNull() extension…
Browse files Browse the repository at this point in the history
… method for weighted generation of null values.
  • Loading branch information
bchavez committed Sep 26, 2018
1 parent 0c90ed6 commit b00bf60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v24.0.1
* Added `nullWeight` parameter to `.OrNull()` extension method for weighted generation of null values.

## v24.0.0
* BREAKING: Deterministic values may have changed. Parity with **faker.js** @ 07f39bd3.
* `en_ZA` - South Africa (English) locale added.
Expand Down
9 changes: 5 additions & 4 deletions Source/Bogus.Tests/GitHubIssues/Issue124.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ public void test_deterministic_or_null()

bars.Should()
.ContainInOrder(
null,
notNullObjects[0],
null,
null,
notNullObjects[1],
null,
notNullObjects[2],
notNullObjects[3],
null,
null,
notNullObjects[3],
notNullObjects[4],
null);
notNullObjects[4]
);
}

public class Foo
Expand Down
29 changes: 29 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue178.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;
using Bogus.Extensions;
using FluentAssertions;
using Xunit;

namespace Bogus.Tests.GitHubIssues
{
public class Issue178 : SeededTest
{
[Fact]
public void weighted_null_check()
{
var f = new Faker();
var mostlyNull = Enumerable.Range(1, 100)
.Select(n => (int?)n.OrNull(f, 0.9))
.Count( n => !n.HasValue);

mostlyNull.Should().BeGreaterThan(80);


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

mostlyNotNull.Should().BeLessThan(20);

}
}
}
10 changes: 7 additions & 3 deletions Source/Bogus/Extensions/ExtensionsForFakerT.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Bogus.Extensions
{
Expand Down Expand Up @@ -26,9 +27,12 @@ public static List<T> GenerateBetween<T>(this Faker<T> faker, int min, int max,
/// Helpful extension for creating randomly null values for <seealso cref="Faker{T}"/>.RuleFor() rules.
/// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrNull(f))
/// </summary>
public static object OrNull(this object value, Faker f)
/// <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)
{
return f.Random.Bool() ? value : null;
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;
}
}
}

0 comments on commit b00bf60

Please sign in to comment.