From cd5e0782064c8370ccdf8d29e787aa51d9cb4310 Mon Sep 17 00:00:00 2001 From: bchavez Date: Wed, 26 Sep 2018 10:08:30 -0700 Subject: [PATCH] #178: Add .OrDefault() typed extension method. Thanks @anorborg! --- HISTORY.md | 3 +- Source/Bogus.Tests/Bogus.Tests.csproj | 1 + Source/Bogus.Tests/GitHubIssues/Issue178.cs | 28 +++++++++++++++++-- .../Bogus/Extensions/ExtensionsForFakerT.cs | 14 +++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 519f8c1c..fbee58f0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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. diff --git a/Source/Bogus.Tests/Bogus.Tests.csproj b/Source/Bogus.Tests/Bogus.Tests.csproj index 10a390b9..eac17adb 100644 --- a/Source/Bogus.Tests/Bogus.Tests.csproj +++ b/Source/Bogus.Tests/Bogus.Tests.csproj @@ -2,6 +2,7 @@ net471;netcoreapp2.0 + 7.3 diff --git a/Source/Bogus.Tests/GitHubIssues/Issue178.cs b/Source/Bogus.Tests/GitHubIssues/Issue178.cs index df6f2d65..97a13e9a 100644 --- a/Source/Bogus.Tests/GitHubIssues/Issue178.cs +++ b/Source/Bogus.Tests/GitHubIssues/Issue178.cs @@ -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); } } } \ No newline at end of file diff --git a/Source/Bogus/Extensions/ExtensionsForFakerT.cs b/Source/Bogus/Extensions/ExtensionsForFakerT.cs index f97dd451..e4404c89 100644 --- a/Source/Bogus/Extensions/ExtensionsForFakerT.cs +++ b/Source/Bogus/Extensions/ExtensionsForFakerT.cs @@ -29,10 +29,22 @@ public static List GenerateBetween(this Faker faker, int min, int max, /// /// 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) + 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; } + + /// + /// Helpful extension for creating randomly default(T) values for .RuleFor() rules. + /// Example: .RuleFor(x=>x.Prop, f=>f.Random.Word().OrDefault(f)) + /// + /// The Faker facade. This is usually the f from f => lambda. + /// 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. + public static T OrDefault(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; + } } } \ No newline at end of file