Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Pick functionality to builders #49

Merged
merged 4 commits into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions TestStack.Dossier.Tests/DataSources/Picking/PickingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Linq;
using Shouldly;
using TestStack.Dossier.DataSources.Picking;
using TestStack.Dossier.Lists;
using TestStack.Dossier.Tests.TestHelpers.Objects.Entities;
using Xunit;

namespace TestStack.Dossier.Tests.DataSources.Picking
{
public class PickingTests
{
[Fact]
public void RandomItemFrom_should_add_items_from_list_randomly()
{
var addresses = Builder<Address>.CreateListOfSize(15).BuildList();
var customers = Builder<Customer>
.CreateListOfSize(15)
.All()
.Set(x => x.PostalAddress, Pick.RandomItemFrom(addresses).Next)
.BuildList();

var uniqueAddresses = customers.Select(x => x.PostalAddress).Distinct().Count();
uniqueAddresses.ShouldBeGreaterThan(3);
uniqueAddresses.ShouldBeLessThan(15);
}

[Fact]
public void RepeatingSequenceFrom_should_add_items_from_list_sequentially_and_repeat_when_list_completes()
{
var addresses = Builder<Address>.CreateListOfSize(3).BuildList();
var customers = Builder<Customer>
.CreateListOfSize(9)
.All()
.Set(x => x.PostalAddress, Pick.RepeatingSequenceFrom(addresses).Next)
.BuildList();

for (int i = 0; i < 2; i++)
{
var address = customers[i].PostalAddress;
address.ShouldBeSameAs(customers[i + 3].PostalAddress);
address.ShouldBeSameAs(customers[i + 6].PostalAddress);

address.ShouldNotBeSameAs(customers[i + 1].PostalAddress);
address.ShouldNotBeSameAs(customers[i + 2].PostalAddress);
}
}
}
}
2 changes: 2 additions & 0 deletions TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Builder_CreateListTests.cs" />
<Compile Include="Builder_CreateNewTests.cs" />
<Compile Include="Builder_SetUsingBuilderTests.cs" />
<Compile Include="DataSources\Picking\PickingTests.cs" />
<Compile Include="TestHelpers\Builders\AddressViewModelBuilder.cs" />
<Compile Include="TestHelpers\Builders\AutoConstructorCustomerBuilder.cs" />
<Compile Include="ChildBuilderTests.cs" />
Expand Down Expand Up @@ -119,6 +120,7 @@
<Name>TestStack.Dossier</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 1 addition & 0 deletions TestStack.Dossier/DataSources/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public IList<T> Data
}
return _list;
}
internal set { _list = value; }
}

/// <summary>
Expand Down
32 changes: 32 additions & 0 deletions TestStack.Dossier/DataSources/Picking/Pick.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;

namespace TestStack.Dossier.DataSources.Picking
{
/// <summary>
/// Pick a sequence of items from a collection of items according to different selection strategies.
/// </summary>
public class Pick
{
/// <summary>
/// Selects a random item from the list each time it is called.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <returns>The RandomItemSource class.</returns>
public static RandomItemSource<T> RandomItemFrom<T>(IList<T> list)
{
return new RandomItemSource<T>(list);
}

/// <summary>
/// Selects each item sequentially from the list and starts again from the beginning when the list is exhausted.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <returns></returns>
public static RepeatingSequenceSource<T> RepeatingSequenceFrom<T>(IList<T> list)
{
return new RepeatingSequenceSource<T>(list);
}
}
}
26 changes: 26 additions & 0 deletions TestStack.Dossier/DataSources/Picking/RandomItemSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;

namespace TestStack.Dossier.DataSources.Picking
{
/// <summary>
/// Implements the random item strategy
/// </summary>
public class RandomItemSource<T> : DataSource<T>
{
/// <inheritdoc />
public RandomItemSource(IList<T> list)
{
Data = list;
Generator.StartIndex = 0;
Generator.ListSize = Data.Count;
}

/// <inheritdoc />
protected override IList<T> InitializeDataSource()
{
// This method will never be called as the list is set in the constructor.
throw new NotImplementedException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively the constructor could store the list in a private field and this method could return the lost? Then you might not need the internal set on the list?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is there is already a _list field. I don't really like the idea of a second field to store the same thing.

One option is to make the field protected, in which case it becomes available to this constructor and I can just set it directly. The reason I didn't go for that though is it then becomes accessible to every class that inherits from it, something I didn't really want to do. Making the Setter internal achieves what I want, because I can set it from this class but consumers don't gain access to the private field.

I could return new List() from the InitializeDataSource method, but I thought that was misleading as it implied it might get called. This method is only called in the lazy instantiation of the list, meaning it will never be called in this implementation as we are setting the list in the constructor. If someone were to inherit from this class and call this method I'm happy it would give them an exception. They can just override it in their derived class with the behaviour they expect.

That was my thinking anyway. Let me know if you disagree at all. :-)

}
}
}
28 changes: 28 additions & 0 deletions TestStack.Dossier/DataSources/Picking/RepeatingSequenceSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using TestStack.Dossier.DataSources.Generators;

namespace TestStack.Dossier.DataSources.Picking
{
/// <summary>
/// Implements the repeatable sequence strategy
/// </summary>
public class RepeatingSequenceSource<T> : DataSource<T>
{
/// <inheritdoc />
public RepeatingSequenceSource(IList<T> list)
: base(new SequentialGenerator())
{
Data = list;
Generator.StartIndex = 0;
Generator.ListSize = Data.Count;
}

/// <inheritdoc />
protected override IList<T> InitializeDataSource()
{
// This method will never be called as the list is set in the constructor.
throw new NotImplementedException();
}
}
}
6 changes: 6 additions & 0 deletions TestStack.Dossier/TestStack.Dossier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<ItemGroup>
<Compile Include="AnonymousValueFixture.cs" />
<Compile Include="Builder.cs" />
<Compile Include="DataSources\Picking\Pick.cs" />
<Compile Include="DataSources\Picking\RandomItemSource.cs" />
<Compile Include="DataSources\Picking\RepeatingSequenceSource.cs" />
<Compile Include="ITestDataBuilder.cs" />
<Compile Include="DataSources\Dictionaries\Cache.cs" />
<Compile Include="DataSources\Dictionaries\IDictionaryRepository.cs" />
Expand Down Expand Up @@ -179,6 +182,9 @@
<EmbeddedResource Include="DataSources\Dictionaries\Resources\PersonUsername.txt" />
<EmbeddedResource Include="DataSources\Dictionaries\Resources\ShirtSize.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="Picking\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down