-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
TestStack.Dossier.Tests/DataSources/Picking/PickingTests.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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -40,6 +40,7 @@ public IList<T> Data | |
} | ||
return _list; | ||
} | ||
internal set { _list = value; } | ||
} | ||
|
||
/// <summary> | ||
|
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
TestStack.Dossier/DataSources/Picking/RepeatingSequenceSource.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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. :-)