-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Soenneker.Utils.AutoBogus.Context; | ||
using Soenneker.Utils.AutoBogus.Extensions; | ||
using Soenneker.Utils.AutoBogus.Generators.Abstract; | ||
|
||
namespace Soenneker.Utils.AutoBogus.Generators.Types; | ||
|
||
internal sealed class WeakReferenceGenerator: IAutoFakerGenerator | ||
{ | ||
object IAutoFakerGenerator.Generate(AutoFakerContext context) | ||
{ | ||
var generated = context.Generate<string>(); | ||
|
||
try | ||
{ | ||
var obj = new WeakReference(generated); | ||
return obj; | ||
} | ||
catch (Exception) | ||
{ | ||
return null!; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Soenneker.Utils.AutoBogus.Context; | ||
using Soenneker.Utils.AutoBogus.Extensions; | ||
using Soenneker.Utils.AutoBogus.Generators.Abstract; | ||
|
||
namespace Soenneker.Utils.AutoBogus.Generators.Types; | ||
|
||
internal sealed class WeakReferenceGenerator<TType> : IAutoFakerGenerator where TType : class | ||
{ | ||
object IAutoFakerGenerator.Generate(AutoFakerContext context) | ||
{ | ||
var generated = context.Generate<TType>(); | ||
|
||
try | ||
{ | ||
var obj = new WeakReference<TType>(generated); | ||
return obj; | ||
} | ||
catch (Exception) | ||
{ | ||
return null!; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
test/Soenneker.Utils.AutoBogus.Tests/AutoFakerWeakReferenceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using FluentAssertions; | ||
using Soenneker.Utils.AutoBogus.Tests.Dtos.Simple; | ||
using Xunit; | ||
using Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Soenneker.Utils.AutoBogus.Tests; | ||
|
||
public class AutoFakerWeakReferenceTests | ||
{ | ||
[Fact] | ||
public void Generate_TestClassWithWeakReference_should_generate() | ||
{ | ||
var faker = new AutoFaker(); | ||
|
||
var result = faker.Generate<TestClassWithWeakReference>(); | ||
result.Should().NotBeNull(); | ||
result.WeakReference.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Generate_TestClassWithWeakReferenceT_should_generate() | ||
{ | ||
var faker = new AutoFaker(); | ||
|
||
var result = faker.Generate<TestClassWithWeakReferenceT>(); | ||
result.Should().NotBeNull(); | ||
result.WeakReference.TryGetTarget(out TestClassWithSimpleProperties? target).Should().BeTrue(); | ||
target.Name.Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void Generate_TestClassWithWeakReferenceTPrivate_should_generate() | ||
{ | ||
var faker = new AutoFaker(); | ||
|
||
var result = faker.Generate<TestClassWithWeakReferenceTPrivate>(); | ||
result.Should().NotBeNull(); | ||
|
||
Type type = typeof(TestClassWithWeakReferenceTPrivate); | ||
|
||
FieldInfo? field = type.GetField("WeakReference", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
||
field.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void AutoFakerT_Generate_TestClassWithWeakReference_should_generate() | ||
{ | ||
var faker = new AutoFaker<TestClassWithWeakReference>(); | ||
|
||
var result = faker.Generate(); | ||
result.Should().NotBeNull(); | ||
result.WeakReference.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void AutoFakerT_Generate_TestClassWithWeakReferenceT_should_generate() | ||
{ | ||
var faker = new AutoFaker<TestClassWithWeakReferenceT>(); | ||
|
||
var result = faker.Generate(); | ||
result.Should().NotBeNull(); | ||
result.WeakReference.TryGetTarget(out TestClassWithSimpleProperties? target).Should().BeTrue(); | ||
target.Name.Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void AutoFakerT_Generate_TestClassWithWeakReferenceTPrivate_should_generate() | ||
{ | ||
var faker = new AutoFaker<TestClassWithWeakReferenceTPrivate>(); | ||
|
||
var result = faker.Generate(); | ||
result.Should().NotBeNull(); | ||
|
||
Type type = typeof(TestClassWithWeakReferenceTPrivate); | ||
|
||
FieldInfo? field = type.GetField("WeakReference", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
||
field.Should().NotBeNull(); | ||
} | ||
} |
13 changes: 6 additions & 7 deletions
13
test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
using System; | ||
|
||
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple | ||
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple; | ||
|
||
internal class TestClassWithSimpleProperties | ||
{ | ||
internal class TestClassWithSimpleProperties | ||
{ | ||
public int Id { get; set; } | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
public string Name { get; set; } | ||
|
||
public DateTime CreatedAt { get; set; } | ||
} | ||
public DateTime CreatedAt { get; set; } | ||
} |
6 changes: 6 additions & 0 deletions
6
test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReference.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; | ||
|
||
public class TestClassWithWeakReference | ||
{ | ||
public System.WeakReference WeakReference { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
.../Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceT.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; | ||
|
||
public class TestClassWithWeakReferenceT | ||
{ | ||
internal WeakReference<TestClassWithSimpleProperties> WeakReference { get; set; } | ||
} |
6 changes: 6 additions & 0 deletions
6
...ker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceTPrivate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; | ||
|
||
public class TestClassWithWeakReferenceTPrivate | ||
{ | ||
private System.WeakReference<TestClassWithSimpleProperties> WeakReference; | ||
} |