diff --git a/src/Generators/AutoFakerGeneratorFactory.cs b/src/Generators/AutoFakerGeneratorFactory.cs index 9e0fd45..097cfd1 100644 --- a/src/Generators/AutoFakerGeneratorFactory.cs +++ b/src/Generators/AutoFakerGeneratorFactory.cs @@ -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) diff --git a/src/Generators/Types/WeakReferenceGenerator.cs b/src/Generators/Types/WeakReferenceGenerator.cs new file mode 100644 index 0000000..3886e98 --- /dev/null +++ b/src/Generators/Types/WeakReferenceGenerator.cs @@ -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(); + + try + { + var obj = new WeakReference(generated); + return obj; + } + catch (Exception) + { + return null!; + } + } +} \ No newline at end of file diff --git a/src/Generators/Types/WeakReferenceTGenerator.cs b/src/Generators/Types/WeakReferenceTGenerator.cs new file mode 100644 index 0000000..4da9f91 --- /dev/null +++ b/src/Generators/Types/WeakReferenceTGenerator.cs @@ -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 where TType : class +{ + object IAutoFakerGenerator.Generate(AutoFakerContext context) + { + var generated = context.Generate(); + + try + { + var obj = new WeakReference(generated); + return obj; + } + catch (Exception) + { + return null!; + } + } +} \ No newline at end of file diff --git a/src/Services/CachedTypeService.cs b/src/Services/CachedTypeService.cs index 4fcbc8d..815be60 100644 --- a/src/Services/CachedTypeService.cs +++ b/src/Services/CachedTypeService.cs @@ -21,6 +21,7 @@ internal static class CachedTypeService internal static readonly Lazy ReadOnlyCollectionGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ReadOnlyCollectionGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); internal static readonly Lazy ImmutableListGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ImmutableListGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); internal static readonly Lazy ImmutableArrayGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(ImmutableArrayGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); + internal static readonly Lazy WeakReferenceGenerator = new(() => StaticCacheService.Cache.GetCachedType(typeof(WeakReferenceGenerator<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); internal static readonly Lazy IDictionary = new(() => StaticCacheService.Cache.GetCachedType(typeof(IDictionary<,>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); internal static readonly Lazy IEnumerable = new(() => StaticCacheService.Cache.GetCachedType(typeof(IEnumerable<>)), System.Threading.LazyThreadSafetyMode.PublicationOnly); diff --git a/src/Services/GeneratorService.cs b/src/Services/GeneratorService.cs index bc70108..eff594f 100644 --- a/src/Services/GeneratorService.cs +++ b/src/Services/GeneratorService.cs @@ -44,6 +44,7 @@ internal sealed class GeneratorService {typeof(IPAddress), new Lazy(() => new IpAddressGenerator())}, {typeof(MemoryStream), new Lazy(() => new MemoryStreamGenerator())}, {typeof(Exception), new Lazy(() => new ExceptionGenerator())}, + {typeof(WeakReference), new Lazy(() => new WeakReferenceGenerator())}, }; private readonly Lazy>> _cachedFundamentalGeneratorsByInt; diff --git a/src/Soenneker.Utils.AutoBogus.csproj b/src/Soenneker.Utils.AutoBogus.csproj index 7bd40f2..7144232 100644 --- a/src/Soenneker.Utils.AutoBogus.csproj +++ b/src/Soenneker.Utils.AutoBogus.csproj @@ -43,7 +43,7 @@ - + diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerWeakReferenceTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerWeakReferenceTests.cs new file mode 100644 index 0000000..4cbee15 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerWeakReferenceTests.cs @@ -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(); + result.Should().NotBeNull(); + result.WeakReference.Should().NotBeNull(); + } + + [Fact] + public void Generate_TestClassWithWeakReferenceT_should_generate() + { + var faker = new AutoFaker(); + + var result = faker.Generate(); + 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(); + 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(); + + var result = faker.Generate(); + result.Should().NotBeNull(); + result.WeakReference.Should().NotBeNull(); + } + + [Fact] + public void AutoFakerT_Generate_TestClassWithWeakReferenceT_should_generate() + { + var faker = new AutoFaker(); + + 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(); + + var result = faker.Generate(); + result.Should().NotBeNull(); + + Type type = typeof(TestClassWithWeakReferenceTPrivate); + + FieldInfo? field = type.GetField("WeakReference", BindingFlags.NonPublic | BindingFlags.Instance); + + field.Should().NotBeNull(); + } +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs index 2be0c2c..d128f72 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs @@ -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; } } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReference.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReference.cs new file mode 100644 index 0000000..d121149 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReference.cs @@ -0,0 +1,6 @@ +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; + +public class TestClassWithWeakReference +{ + public System.WeakReference WeakReference { get; set; } +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceT.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceT.cs new file mode 100644 index 0000000..99c4948 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceT.cs @@ -0,0 +1,8 @@ +using System; + +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; + +public class TestClassWithWeakReferenceT +{ + internal WeakReference WeakReference { get; set; } +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceTPrivate.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceTPrivate.cs new file mode 100644 index 0000000..54b296c --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/WeakReference/TestClassWithWeakReferenceTPrivate.cs @@ -0,0 +1,6 @@ +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.WeakReference; + +public class TestClassWithWeakReferenceTPrivate +{ + private System.WeakReference WeakReference; +} \ No newline at end of file