-
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.
♻️ make PeanutButter.RandomGenerators able to build internal
- Loading branch information
1 parent
15b249d
commit 71e0fb7
Showing
23 changed files
with
10,053 additions
and
7,138 deletions.
There are no files selected for viewing
77 changes: 41 additions & 36 deletions
77
source/TestUtils/PeanutButter.RandomGenerators/BuilderFinderExtensions.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 |
---|---|---|
@@ -1,50 +1,55 @@ | ||
using System; | ||
using PeanutButter.Utils; | ||
|
||
namespace PeanutButter.RandomGenerators | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
namespace Imported.PeanutButter.RandomGenerators; | ||
#else | ||
namespace PeanutButter.RandomGenerators; | ||
#endif | ||
|
||
internal static class BuilderFinderExtensions | ||
{ | ||
internal static class BuilderFinderExtensions | ||
{ | ||
private static readonly Type GenericBuilderBaseType = typeof(GenericBuilder<,>); | ||
private static readonly Type ObjectType = typeof(object); | ||
private static readonly Type GenericBuilderBaseType = typeof(GenericBuilder<,>); | ||
private static readonly Type ObjectType = typeof(object); | ||
|
||
public static bool IsBuilderFor(this Type t, Type toBuild) | ||
{ | ||
var builderType = TryFindBuilderTypeInClassHeirachyFor(t, toBuild); | ||
return builderType != null; | ||
} | ||
public static bool IsBuilderFor(this Type t, Type toBuild) | ||
{ | ||
var builderType = TryFindBuilderTypeInClassHeirachyFor(t, toBuild); | ||
return builderType != null; | ||
} | ||
|
||
public static bool IsABuilder(this Type t) | ||
{ | ||
return t.TryFindGenericBuilderInClassHeirachy() != null; | ||
} | ||
public static bool IsABuilder(this Type t) | ||
{ | ||
return t.TryFindGenericBuilderInClassHeirachy() != null; | ||
} | ||
|
||
private static Type TryFindBuilderTypeInClassHeirachyFor(Type potentialBuilder, Type buildType) | ||
{ | ||
var current = TryFindGenericBuilderInClassHeirachy(potentialBuilder); | ||
if (current == null) | ||
return null; | ||
var typeParameters = current.GetGenericArguments(); | ||
return typeParameters.Length > 1 && typeParameters[1] == buildType | ||
? current | ||
: null; | ||
} | ||
private static Type TryFindBuilderTypeInClassHeirachyFor(Type potentialBuilder, Type buildType) | ||
{ | ||
var current = TryFindGenericBuilderInClassHeirachy(potentialBuilder); | ||
if (current == null) | ||
return null; | ||
var typeParameters = current.GetGenericArguments(); | ||
return typeParameters.Length > 1 && typeParameters[1] == buildType | ||
? current | ||
: null; | ||
} | ||
|
||
internal static Type TryFindGenericBuilderInClassHeirachy(this Type current) | ||
internal static Type TryFindGenericBuilderInClassHeirachy(this Type current) | ||
{ | ||
while (current != ObjectType && current != null) | ||
{ | ||
while (current != ObjectType && current != null) | ||
if (current.IsGenericType()) | ||
{ | ||
if (current.IsGenericType()) | ||
{ | ||
var genericBase = current.GetGenericTypeDefinition(); | ||
if (genericBase == GenericBuilderBaseType) | ||
break; | ||
} | ||
current = current.BaseType(); | ||
var genericBase = current.GetGenericTypeDefinition(); | ||
if (genericBase == GenericBuilderBaseType) | ||
break; | ||
} | ||
if (current == ObjectType || current == null) | ||
return null; | ||
return current; | ||
|
||
current = current.BaseType(); | ||
} | ||
|
||
if (current == ObjectType || current == null) | ||
return null; | ||
return current; | ||
} | ||
} |
47 changes: 29 additions & 18 deletions
47
.../TestUtils/PeanutButter.RandomGenerators/CannotGetAnotherDifferentRandomValueException.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 |
---|---|---|
@@ -1,26 +1,37 @@ | ||
using System; | ||
|
||
namespace PeanutButter.RandomGenerators | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
namespace Imported.PeanutButter.RandomGenerators; | ||
#else | ||
namespace PeanutButter.RandomGenerators; | ||
#endif | ||
|
||
/// <summary> | ||
/// Exception thrown when the method GetAnother is unable to find another | ||
/// random value different from the exclusion value specified | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
internal | ||
#else | ||
public | ||
#endif | ||
class CannotGetAnotherDifferentRandomValueException<T> : Exception | ||
{ | ||
/// <summary> | ||
/// Exception thrown when the method GetAnother is unable to find another | ||
/// random value different from the exclusion value specified | ||
/// Value which was specified to be avoided | ||
/// </summary> | ||
public T Value { get; private set; } | ||
|
||
/// <summary> | ||
/// Constructs a new instance of the exception, storing the unwanted value | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class CannotGetAnotherDifferentRandomValueException<T>: Exception | ||
/// <param name="unwantedValue">Value which was to be avoided when generating a new value</param> | ||
public CannotGetAnotherDifferentRandomValueException(T unwantedValue) : | ||
base( | ||
$"Unable to get a value different from {unwantedValue} after {RandomValueGen.MAX_DIFFERENT_RANDOM_VALUE_ATTEMPTS} attempts )':" | ||
) | ||
{ | ||
/// <summary> | ||
/// Value which was specified to be avoided | ||
/// </summary> | ||
public T Value { get; private set; } | ||
/// <summary> | ||
/// Constructs a new instance of the exception, storing the unwanted value | ||
/// </summary> | ||
/// <param name="unwantedValue">Value which was to be avoided when generating a new value</param> | ||
public CannotGetAnotherDifferentRandomValueException(T unwantedValue): | ||
base($"Unable to get a value different from {unwantedValue} after {RandomValueGen.MAX_DIFFERENT_RANDOM_VALUE_ATTEMPTS} attempts )':") | ||
{ | ||
Value = unwantedValue; | ||
} | ||
Value = unwantedValue; | ||
} | ||
} |
74 changes: 39 additions & 35 deletions
74
source/TestUtils/PeanutButter.RandomGenerators/DateRange.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 |
---|---|---|
@@ -1,47 +1,51 @@ | ||
using System; | ||
// ReSharper disable SwapViaDeconstruction | ||
|
||
namespace PeanutButter.RandomGenerators | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
namespace Imported.PeanutButter.RandomGenerators; | ||
#else | ||
namespace PeanutButter.RandomGenerators; | ||
#endif | ||
|
||
/// <summary> | ||
/// Holds a date range | ||
/// </summary> | ||
public class DateRange | ||
{ | ||
/// <summary> | ||
/// Holds a date range | ||
/// Start of the date range | ||
/// </summary> | ||
public class DateRange | ||
{ | ||
/// <summary> | ||
/// Start of the date range | ||
/// </summary> | ||
public DateTime From { get; } | ||
public DateTime From { get; } | ||
|
||
/// <summary> | ||
/// End of the date range | ||
/// </summary> | ||
public DateTime To { get; } | ||
/// <summary> | ||
/// End of the date range | ||
/// </summary> | ||
public DateTime To { get; } | ||
|
||
/// <summary> | ||
/// Constructs a new DateRange object, ensuring that {from} is less than {to} | ||
/// </summary> | ||
/// <param name="from">Date to start the range</param> | ||
/// <param name="to">Date for the end of the range</param> | ||
public DateRange(DateTime from, DateTime to) | ||
/// <summary> | ||
/// Constructs a new DateRange object, ensuring that {from} is less than {to} | ||
/// </summary> | ||
/// <param name="from">Date to start the range</param> | ||
/// <param name="to">Date for the end of the range</param> | ||
public DateRange(DateTime from, DateTime to) | ||
{ | ||
From = from; | ||
To = to; | ||
if (From > To) | ||
{ | ||
From = from; | ||
To = to; | ||
if (From > To) | ||
{ | ||
var swap = From; | ||
From = To; | ||
To = swap; | ||
} | ||
var swap = From; | ||
From = To; | ||
To = swap; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tests whether a provided DateTime value is within the stored range | ||
/// </summary> | ||
/// <param name="value">Value to test</param> | ||
/// <returns>True if the value falls within the date range</returns> | ||
public bool InRange(DateTime value) | ||
{ | ||
return value >= From && value <= To; | ||
} | ||
/// <summary> | ||
/// Tests whether a provided DateTime value is within the stored range | ||
/// </summary> | ||
/// <param name="value">Value to test</param> | ||
/// <returns>True if the value falls within the date range</returns> | ||
public bool InRange(DateTime value) | ||
{ | ||
return value >= From && value <= To; | ||
} | ||
} |
52 changes: 28 additions & 24 deletions
52
source/TestUtils/PeanutButter.RandomGenerators/EnumerableExtensions.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 |
---|---|---|
@@ -1,33 +1,37 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace PeanutButter.RandomGenerators | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
namespace Imported.PeanutButter.RandomGenerators; | ||
#else | ||
namespace PeanutButter.RandomGenerators; | ||
#endif | ||
|
||
/// <summary> | ||
/// Provides extensions for generic IEnumerable collections | ||
/// </summary> | ||
public static class EnumerableExtensions | ||
{ | ||
/// <summary> | ||
/// Provides extensions for generic IEnumerable collections | ||
/// Takes an input collection and returns a new collection which is the | ||
/// input collection in random order | ||
/// </summary> | ||
public static class EnumerableExtensions | ||
/// <param name="input">Collection to randomize</param> | ||
/// <typeparam name="T">Item type of the collection</typeparam> | ||
/// <returns>A new collection with the same items as the original, but in random order</returns> | ||
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> input) | ||
{ | ||
/// <summary> | ||
/// Takes an input collection and returns a new collection which is the | ||
/// input collection in random order | ||
/// </summary> | ||
/// <param name="input">Collection to randomize</param> | ||
/// <typeparam name="T">Item type of the collection</typeparam> | ||
/// <returns>A new collection with the same items as the original, but in random order</returns> | ||
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> input) | ||
if (input == null) | ||
return null; | ||
var src = new List<T>(input); | ||
var result = new List<T>(); | ||
while (src.Count > 0) | ||
{ | ||
if (input == null) | ||
return null; | ||
var src = new List<T>(input); | ||
var result = new List<T>(); | ||
while (src.Count > 0) | ||
{ | ||
var pick = RandomValueGen.GetRandomInt(0, src.Count - 1); | ||
var pickItem = src[pick]; | ||
src.RemoveAt(pick); | ||
result.Add(pickItem); | ||
} | ||
return result; | ||
var pick = RandomValueGen.GetRandomInt(0, src.Count - 1); | ||
var pickItem = src[pick]; | ||
src.RemoveAt(pick); | ||
result.Add(pickItem); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Oops, something went wrong.