Skip to content

Commit

Permalink
Use in parameter modifier to resolve compiler ambiguity when using …
Browse files Browse the repository at this point in the history
…struct Guid?, primitive type int?, and reference types like object. #124
  • Loading branch information
bchavez committed Feb 10, 2020
1 parent d491925 commit 45e287a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
## v28.5.1
Release Date: 2020-TBA
## v29.0.1
Release Date: 2020-02-10

* Data parity with faker.js. Deterministic sequences using `Internet.Avatar()` may have changed.
* Add support for .snupkg NuGet Symbol Server Packages via SourceLink.
* Added Randomizer.Enums() that makes selecting a subset of enum values easier.
* Modified `.OrNull(f)` extension method signatures for type-safe with nullable primitive types, structs, and reference types via `in` parameter to avoid ambiguous calls.

## v28.4.4
Release Date: 2019-12-10
Expand Down
32 changes: 32 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue124.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ public void test_deterministic_or_null()
);
}

[Fact]
public void nullable_int_and_nullable_reference_type()
{
var faker = new Faker<Qux>()
.RuleFor(x => x.Id, f => f.Random.Int().OrNull(f))
.RuleFor(x => x.Obj, f => new object().OrNull(f))
.RuleFor(x => x.Str, f => f.Random.Word().OrNull(f));

var q = faker.Generate(3);

console.Dump(q);

q[0].Id.Should().NotBeNull();
q[0].Obj.Should().NotBeNull();
q[0].Str.Should().BeNull();

q[1].Id.Should().BeNull();
q[1].Obj.Should().NotBeNull();
q[1].Str.Should().BeNull();

q[2].Id.Should().BeNull();
q[2].Obj.Should().NotBeNull();
q[2].Str.Should().NotBeNull();
}

public class Foo
{
public Guid Id { get; set; }
Expand All @@ -130,6 +155,13 @@ public class Bar
public Guid Id { get; set; }
public Person Person { get; set; }
}

public class Qux
{
public int? Id { get; set; }
public object Obj { get; set; }
public string Str { get; set; }
}
}

public class Person
Expand Down
4 changes: 3 additions & 1 deletion Source/Bogus/Extensions/ExtensionsForFakerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ 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>
/// <typeparam name="T">Any reference type.</typeparam>
/// <param name="f">The Faker facade. This is usually the f from RuleFor(.., f => lambda).</param>
/// <param name="nullWeight">The probability of null occurring. 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, float nullWeight = 0.5f)
public static T OrNull<T>(this T value, in Faker f, float nullWeight = 0.5f) where T : class
{
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;
Expand All @@ -39,6 +40,7 @@ public static object OrNull(this object value, Faker f, float nullWeight = 0.5f)
/// Helpful extension for creating randomly null values for <seealso cref="Faker{T}"/>.RuleFor() rules.
/// Example: .RuleFor(x=>x.Prop, f=>f.Random.Int().OrNull(f))
/// </summary>
/// <typeparam name="T">Any nullable type. ie: int?, Guid?, etc.</typeparam>
/// <param name="f">The Faker facade. This is usually the f from RuleFor(.., f => lambda).</param>
/// <param name="nullWeight">The probability of null occurring. Range [1.0f - 0.0f] (100% and 0%) respectively. For example, if 15% null is desired pass nullWeight = 0.15f.</param>
public static T? OrNull<T>(this T value, Faker f, float nullWeight = 0.5f) where T : struct
Expand Down

0 comments on commit 45e287a

Please sign in to comment.