-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 allow multiple usages of [RequireNonZero] and [RequireUnique]
- Loading branch information
1 parent
08422e3
commit 8113e91
Showing
3 changed files
with
98 additions
and
80 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
90 changes: 90 additions & 0 deletions
90
source/TestUtils/PeanutButter.RandomGenerators/Restrict.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,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 | ||
|
||
/// <summary> | ||
/// Restricts the possible values for a property | ||
/// to the provided list | ||
/// </summary> | ||
[AttributeUsage( | ||
validOn: AttributeTargets.Class, | ||
AllowMultiple = true | ||
)] | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
internal | ||
#else | ||
public | ||
#endif | ||
class Restrict : RandomizerAttribute | ||
{ | ||
/// <summary> | ||
/// The restricted values for this property | ||
/// </summary> | ||
public object[] Values { get; } | ||
|
||
/// <inheritdoc /> | ||
public Restrict( | ||
string propertyName, | ||
object value, | ||
params object[] moreValues | ||
) : base(propertyName) | ||
{ | ||
Values = new[] | ||
{ | ||
value | ||
}.And(moreValues); | ||
} | ||
|
||
/// <inheritdoc /> | ||
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); | ||
} | ||
} |