2020-03-29 16:17:06 +02:00
|
|
|
using System.Collections.Generic;
|
2020-04-12 09:47:44 +02:00
|
|
|
using System.Linq;
|
2020-03-29 16:17:06 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Common.Builders
|
|
|
|
|
{
|
|
|
|
|
public class GenericCollectionBuilder<TBuilder, T>
|
|
|
|
|
: ChildBuilderBase<TBuilder, IEnumerable<T>>
|
|
|
|
|
{
|
2020-04-12 09:47:44 +02:00
|
|
|
private IList<T> _collection;
|
2020-03-29 16:17:06 +02:00
|
|
|
|
|
|
|
|
public GenericCollectionBuilder(TBuilder parentBuilder) : base(parentBuilder)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<T> Build()
|
|
|
|
|
{
|
2020-04-12 09:47:44 +02:00
|
|
|
var collection = _collection?.ToList() ?? Enumerable.Empty<T>();
|
|
|
|
|
Reset();
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Reset()
|
|
|
|
|
{
|
|
|
|
|
_collection = null;
|
2020-03-29 16:17:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GenericCollectionBuilder<TBuilder, T> WithValue(T value)
|
|
|
|
|
{
|
2020-04-12 09:47:44 +02:00
|
|
|
if (_collection == null)
|
|
|
|
|
{
|
|
|
|
|
_collection = new List<T>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 16:17:06 +02:00
|
|
|
_collection.Add(value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|