From 3cf2f471d7db21b3777ef9430796e32149f5c35f Mon Sep 17 00:00:00 2001 From: nth-commit Date: Fri, 27 Jan 2023 10:43:37 +1300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=8D=20#373=20Support=20ICollection<>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AboutGeneratingDeepObjects.cs | 16 ++++++++++++++++ .../CollectionReflectedGenHandler.cs | 1 + .../ErrorDiagnosticReflectedGenHandler.cs | 14 ++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/ErrorDiagnosticReflectedGenHandler.cs diff --git a/src/GalaxyCheck.Tests/GenTests/ReflectedGenTests/AboutGeneratingDeepObjects.cs b/src/GalaxyCheck.Tests/GenTests/ReflectedGenTests/AboutGeneratingDeepObjects.cs index 4ec6ba32..d4a5c212 100644 --- a/src/GalaxyCheck.Tests/GenTests/ReflectedGenTests/AboutGeneratingDeepObjects.cs +++ b/src/GalaxyCheck.Tests/GenTests/ReflectedGenTests/AboutGeneratingDeepObjects.cs @@ -3,6 +3,7 @@ using NebulaCheck; using System; using System.Linq; +using Xunit; using Property = NebulaCheck.Property; using Test = NebulaCheck.Test; @@ -58,5 +59,20 @@ select Property.ForThese(() => .Throw() .WithMessage("Error while running generator ReflectedGen: detected circular reference on type '*ClassWithRecursiveProperty' at path '$.Property'"); }); + + [Fact] + public void ItCanHandleDeepErrors() + { + // Arrange + var gen = GalaxyCheck.Gen.Create() + .OverrideMember(x => x.Property.Property, GalaxyCheck.Gen.Advanced.Error("Error", "Error")); + + Action action = () => gen.SampleOne(seed: 0, size: 0); + + action.Should() + .Throw() + .WithMessage("Error while running generator Error at path '$.Property': Error"); + } + } } diff --git a/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/CollectionReflectedGenHandler.cs b/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/CollectionReflectedGenHandler.cs index b1810dbb..15314155 100644 --- a/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/CollectionReflectedGenHandler.cs +++ b/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/CollectionReflectedGenHandler.cs @@ -49,6 +49,7 @@ public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGen { typeof(HashSet<>), nameof(CreateHashSetGen) }, { typeof(ISet<>), nameof(CreateHashSetGen) }, { typeof(ImmutableHashSet<>), nameof(CreateImmutableHashSetGen) }, + { typeof(ICollection<>), nameof(CreateListGen) } }; private static IGen> CreateIReadOnlyListGen(IGen elementGen) => elementGen.ListOf(); diff --git a/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/ErrorDiagnosticReflectedGenHandler.cs b/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/ErrorDiagnosticReflectedGenHandler.cs new file mode 100644 index 00000000..088aa70a --- /dev/null +++ b/src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenHandlers/ErrorDiagnosticReflectedGenHandler.cs @@ -0,0 +1,14 @@ +using System; + +namespace GalaxyCheck.Gens.ReflectedGenHelpers.ReflectedGenHandlers +{ + internal class ErrorDiagnosticReflectedGenHandler : IReflectedGenHandler + { + public bool CanHandleGen(Type type, ReflectedGenHandlerContext context) => true; + + public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) + { + return innerHandler.CreateGen(type, context); + } + } +}