diff --git a/source/TestUtils/PeanutButter.RandomGenerators/RequireNonZero.cs b/source/TestUtils/PeanutButter.RandomGenerators/RequireNonZero.cs index 2e54f5b34..1a3f3d2cc 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/RequireNonZero.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/RequireNonZero.cs @@ -4,7 +4,6 @@ namespace Imported.PeanutButter.RandomGenerators; #else using System; -using PeanutButter.DuckTyping.AutoConversion; using PeanutButter.Utils; namespace PeanutButter.RandomGenerators; @@ -13,6 +12,10 @@ namespace PeanutButter.RandomGenerators; /// /// Requires that the named property is randomized to a non-zero value /// +[AttributeUsage( + AttributeTargets.Class, + AllowMultiple = true +)] #if BUILD_PEANUTBUTTER_INTERNAL internal #else @@ -34,83 +37,4 @@ ref object target { propInfo.SetValue(target, RandomValueGen.GetRandomInt(1)); } -} - -/// -/// Restricts the possible values for a property -/// to the provided list -/// -[AttributeUsage( - validOn: AttributeTargets.Class, - AllowMultiple = true -)] -#if BUILD_PEANUTBUTTER_INTERNAL -internal -#else -public -#endif - class Restrict : RandomizerAttribute -{ - /// - /// The restricted values for this property - /// - public object[] Values { get; } - - /// - public Restrict( - string propertyName, - object value, - params object[] moreValues - ) : base(propertyName) - { - Values = new[] - { - value - }.And(moreValues); - } - - /// - public override void SetRandomValue( - PropertyOrField propInfo, - ref object target - ) - { - var toSet = RandomValueGen.GetRandomFrom(Values); - if (toSet is null) - { - propInfo.SetValue( - target, - propInfo.Type.IsNullableType() - ? null - : propInfo.Type.DefaultValue() - ); - - return; - } - - var valueType = toSet.GetType(); - if (valueType == propInfo.Type) - { - propInfo.SetValue(target, toSet); - return; - } - - var converter = ConverterLocator.TryFindConverter( - valueType, - propInfo.Type - ); - if (converter is null) - { - throw new NotImplementedException( - $""" - There is no known converter to convert between {valueType} and {propInfo.Type}. - You may implement IConverter<{valueType}, {propInfo.Type}> to resolve this: - ConverterLocator should pick it up. - """ - ); - } - - var converted = converter.Convert(toSet); - propInfo.SetValue(target, converted); - } } \ No newline at end of file diff --git a/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs b/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs index ac7b70771..0eb921bd6 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs @@ -14,6 +14,10 @@ namespace PeanutButter.RandomGenerators; /// /// Abstract class to require uniqueness on a property or field by name /// +[AttributeUsage( + AttributeTargets.Class, + AllowMultiple = true +)] #if BUILD_PEANUTBUTTER_INTERNAL internal #else diff --git a/source/TestUtils/PeanutButter.RandomGenerators/Restrict.cs b/source/TestUtils/PeanutButter.RandomGenerators/Restrict.cs new file mode 100644 index 000000000..0dc9db5d3 --- /dev/null +++ b/source/TestUtils/PeanutButter.RandomGenerators/Restrict.cs @@ -0,0 +1,90 @@ +#if BUILD_PEANUTBUTTER_INTERNAL +using Imported.PeanutButter.Utils; + +namespace Imported.PeanutButter.RandomGenerators; +#else +using System; +using PeanutButter.DuckTyping.AutoConversion; +using PeanutButter.Utils; + +namespace PeanutButter.RandomGenerators; +#endif + +/// +/// Restricts the possible values for a property +/// to the provided list +/// +[AttributeUsage( + validOn: AttributeTargets.Class, + AllowMultiple = true +)] +#if BUILD_PEANUTBUTTER_INTERNAL +internal +#else +public +#endif + class Restrict : RandomizerAttribute +{ + /// + /// The restricted values for this property + /// + public object[] Values { get; } + + /// + public Restrict( + string propertyName, + object value, + params object[] moreValues + ) : base(propertyName) + { + Values = new[] + { + value + }.And(moreValues); + } + + /// + public override void SetRandomValue( + PropertyOrField propInfo, + ref object target + ) + { + var toSet = RandomValueGen.GetRandomFrom(Values); + if (toSet is null) + { + propInfo.SetValue( + target, + propInfo.Type.IsNullableType() + ? null + : propInfo.Type.DefaultValue() + ); + + return; + } + + var valueType = toSet.GetType(); + if (valueType == propInfo.Type) + { + propInfo.SetValue(target, toSet); + return; + } + + var converter = ConverterLocator.TryFindConverter( + valueType, + propInfo.Type + ); + if (converter is null) + { + throw new NotImplementedException( + $""" + There is no known converter to convert between {valueType} and {propInfo.Type}. + You may implement IConverter<{valueType}, {propInfo.Type}> to resolve this: + ConverterLocator should pick it up. + """ + ); + } + + var converted = converter.Convert(toSet); + propInfo.SetValue(target, converted); + } +} \ No newline at end of file