Skip to content

MockExtensions

ClassMockMagiccom.wetzold.mock-magic
public static class MockExtensions

Convenience helpers that let generated values become null/default with a tunable probability, plus small IList/IReadOnlyList helpers (PickRandom, Shuffle).

public static T OrNull<T>(this T value, float nullProbability = 0.3) where T : class

Return value or null with nullProbability.

Parameters

NameType
value{T}
nullProbabilitySystem.Single

Returns

{T}

public static T? OrNullable<T>(this T value, float nullProbability = 0.3) where T : struct

Return value wrapped as a Nullable<T>, possibly as null. Use this for value types since OrNull is restricted to reference types — C# does not allow overload resolution based on class/struct constraints alone.

Parameters

NameType
value{T}
nullProbabilitySystem.Single

Returns

System.Nullable{{T}}

public static T OrDefault<T>(this T value, float defaultProbability = 0.3)

Return value or default(T) with defaultProbability. Works for both reference and value types.

Parameters

NameType
value{T}
defaultProbabilitySystem.Single

Returns

{T}

public static T PickRandom<T>(this IReadOnlyList<T> items)

Pick a random element from items using MockMagic’s seeded RNG.

Parameters

NameType
itemsSystem.Collections.Generic.IReadOnlyList{{T}}

Returns

{T}

public static void Shuffle<T>(this IList<T> items)

Shuffle items in place using MockMagic’s seeded RNG.

Parameters

NameType
itemsSystem.Collections.Generic.IList{{T}}