From bcc242c17d74fcc96de87727cc96326d244afc1e Mon Sep 17 00:00:00 2001 From: bchavez Date: Wed, 26 Sep 2018 02:15:15 -0700 Subject: [PATCH] #178, #124. Added `nullWeight` parameter to `.OrNull()` extension method for weighted generation of null values. --- HISTORY.md | 3 ++ Source/Bogus.Tests/GitHubIssues/Issue178.cs | 29 +++++++++++++++++++ .../Bogus/Extensions/ExtensionsForFakerT.cs | 10 +++++-- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 Source/Bogus.Tests/GitHubIssues/Issue178.cs 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/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