diff --git a/HISTORY.md b/HISTORY.md index 6cf50fe3..519f8c1c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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. diff --git a/Source/Bogus.Tests/GitHubIssues/Issue124.cs b/Source/Bogus.Tests/GitHubIssues/Issue124.cs index 5887f20e..ef1d57d1 100644 --- a/Source/Bogus.Tests/GitHubIssues/Issue124.cs +++ b/Source/Bogus.Tests/GitHubIssues/Issue124.cs @@ -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 diff --git a/Source/Bogus.Tests/GitHubIssues/Issue178.cs b/Source/Bogus.Tests/GitHubIssues/Issue178.cs new file mode 100644 index 00000000..df6f2d65 --- /dev/null +++ b/Source/Bogus.Tests/GitHubIssues/Issue178.cs @@ -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); + + } + } +} \ No newline at end of file diff --git a/Source/Bogus/Extensions/ExtensionsForFakerT.cs b/Source/Bogus/Extensions/ExtensionsForFakerT.cs index b8cfdfe7..f97dd451 100644 --- a/Source/Bogus/Extensions/ExtensionsForFakerT.cs +++ b/Source/Bogus/Extensions/ExtensionsForFakerT.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Bogus.Extensions { @@ -26,9 +27,12 @@ public static List GenerateBetween(this Faker faker, int min, int max, /// Helpful extension for creating randomly null values for .RuleFor() rules. /// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrNull(f)) /// - public static object OrNull(this object value, Faker f) + /// The Faker facade. This is usually the f from f => lambda. + /// 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. + 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; } } } \ No newline at end of file