Skip to content

Commit

Permalink
WeakReference generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Dec 9, 2024
1 parent 82ee2a2 commit edab4e9
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Generators/AutoFakerGeneratorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ internal static IAutoFakerGenerator CreateGenerator(AutoFakerContext context)
return new ExpandoObjectGenerator();
}

if (context.CachedType.IsWeakReference)
{
cachedType = context.CachedType.GetCachedGenericArguments()![0];
return CreateGenericGenerator(CachedTypeService.WeakReferenceGenerator.Value, cachedType);
}

(CachedType? collectionType, GenericCollectionType? genericCollectionType) = GenericTypeUtil.GetGenericCollectionType(context.CachedType);

if (collectionType != null)
Expand Down
24 changes: 24 additions & 0 deletions src/Generators/Types/WeakReferenceGenerator.cs
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!;
}
}
}
24 changes: 24 additions & 0 deletions src/Generators/Types/WeakReferenceTGenerator.cs
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!;
}
}
}
1 change: 1 addition & 0 deletions src/Services/CachedTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static class CachedTypeService
internal static readonly Lazy<CachedType> ReadOnlyCollectionGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ReadOnlyCollectionGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);
internal static readonly Lazy<CachedType> ImmutableListGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ImmutableListGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);
internal static readonly Lazy<CachedType> ImmutableArrayGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ImmutableArrayGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);
internal static readonly Lazy<CachedType> WeakReferenceGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(WeakReferenceGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);

internal static readonly Lazy<CachedType> IDictionary = new(() => StaticCacheService.Cache.GetCachedType(typeof(IDictionary<,>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);
internal static readonly Lazy<CachedType> IEnumerable = new(() => StaticCacheService.Cache.GetCachedType(typeof(IEnumerable<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly);
Expand Down
1 change: 1 addition & 0 deletions src/Services/GeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal sealed class GeneratorService
{typeof(IPAddress), new Lazy<IAutoFakerGenerator>(() => new IpAddressGenerator())},
{typeof(MemoryStream), new Lazy<IAutoFakerGenerator>(() => new MemoryStreamGenerator())},
{typeof(Exception), new Lazy<IAutoFakerGenerator>(() => new ExceptionGenerator())},
{typeof(WeakReference), new Lazy<IAutoFakerGenerator>(() => new WeakReferenceGenerator())},
};

private readonly Lazy<Dictionary<int, Lazy<IAutoFakerGenerator>>> _cachedFundamentalGeneratorsByInt;
Expand Down
2 changes: 1 addition & 1 deletion src/Soenneker.Utils.AutoBogus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Soenneker.Extensions.FieldInfo" Version="3.0.26" />
<PackageReference Include="Soenneker.Extensions.MemberInfo" Version="3.0.23" />
<PackageReference Include="Soenneker.Reflection.Cache" Version="3.0.281" />
<PackageReference Include="Soenneker.Reflection.Cache" Version="3.0.282" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
</ItemGroup>
</Project>
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();
}
}
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; }
}
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; }
}
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; }
}
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;
}

0 comments on commit edab4e9

Please sign in to comment.