using System.Linq.Expressions;
using AutoFixture.Dsl;
namespace Bit.Core.Test.AutoFixture;
public static class AutoFixtureExtensions
{
///
/// Registers that a writable Guid property should be assigned a random value that is derived from the given seed.
///
///
/// This can be used to generate random Guids that are deterministic based on the seed and thus can be re-used for
/// different entities that share the same identifiers. e.g. Collections, CollectionUsers, and CollectionGroups can
/// all have the same Guids generate for their "collection id" properties.
///
///
/// The Guid property to register
/// The random seed to use for random Guid generation
public static IPostprocessComposer WithGuidFromSeed(this IPostprocessComposer composer, Expression> propertyPicker, int seed)
{
var rnd = new Random(seed);
return composer.With(propertyPicker, () =>
{
// While not as random/unique as Guid.NewGuid(), this is works well enough for testing purposes.
var bytes = new byte[16];
rnd.NextBytes(bytes);
return new Guid(bytes);
});
}
///
/// Registers that a writable property should be assigned a value from the given list.
///
///
/// The value will be assigned in the order that the list is enumerated. Values will wrap around to the beginning
/// should the end of the list be reached.
///
///
///
///
public static IPostprocessComposer WithValueFromList(
this IPostprocessComposer composer,
Expression> propertyPicker,
ICollection values)
{
var index = 0;
return composer.With(propertyPicker, () =>
{
var value = values.ElementAt(index);
index = (index + 1) % values.Count;
return value;
});
}
}