-
Notifications
You must be signed in to change notification settings - Fork 19
Data sources, random/sequential generators, Equivalence classes #5
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
Merged
robdmoore
merged 7 commits into
TestStack:anonymous-values
from
mwhelan:anonymous-values
Sep 18, 2014
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e351fb
Created random generator
mwhelan 610fc18
Added SequentialGenerator and Guard class
mwhelan 7299367
SequentialGenerator modifications
mwhelan 3b9d157
Added data sources
mwhelan 46ee37f
Added example CompanySource and CompanyEquivalenceClass
mwhelan c91d793
example of file-based data for Person data
mwhelan 8a709c4
unsaved project
mwhelan 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,4 +9,6 @@ _ReSharper.* | |
| *~ | ||
| *.log | ||
| packages | ||
| *.DotSettings | ||
| *.DotSettings | ||
| *.ncrunchproject | ||
| *.ncrunchsolution | ||
This file contains hidden or 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,92 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using NTestDataBuilder.DataSources; | ||
| using NTestDataBuilder.DataSources.Generators; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace NTestDataBuilder.Tests.DataSources | ||
| { | ||
| public class DataSourceTests | ||
| { | ||
| [Fact] | ||
| public void WhenCreatingADataSource_ThenListShouldContainAllTheValues() | ||
| { | ||
| var sut = new DummyDataSource(); | ||
|
|
||
| var result = sut.List; | ||
|
|
||
| result.Count.ShouldBe(3); | ||
| result.ShouldBe(new List<string> { "Value 1", "Value 2", "Value 3" }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateItemFromList() | ||
| { | ||
| var sut = new DummyDataSource(); | ||
| var result = sut.Next(); | ||
| sut.List.ShouldContain(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateRandomItemsFromList() | ||
| { | ||
| var sut = new DummyDataSource(); | ||
|
|
||
| var results = new List<string>(); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| results.Add(sut.Next()); | ||
| } | ||
|
|
||
| foreach (var item in sut.List) | ||
| { | ||
| results.ShouldContain(item); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingValuesFromSequentialDataSource_ThenShouldGenerateSequentialItems() | ||
| { | ||
| var sut = new DummyDataSource(new SequentialGenerator()); | ||
|
|
||
| sut.Next().ShouldBe(sut.List[0]); | ||
| sut.Next().ShouldBe(sut.List[1]); | ||
| sut.Next().ShouldBe(sut.List[2]); | ||
| sut.Next().ShouldBe(sut.List[0]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingValuesFromUniqueSequentialDataSource_ThenShouldThrowWhenCollectionExceeded() | ||
| { | ||
| var sut = new DummyDataSource(new SequentialGenerator(0,1,true)); | ||
|
|
||
| sut.Next().ShouldBe(sut.List[0]); | ||
| sut.Next().ShouldBe(sut.List[1]); | ||
| sut.Next().ShouldBe(sut.List[2]); | ||
| Should.Throw<InvalidOperationException>(() => sut.Next()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingValuesFromPersonDataSource_ThenShouldHave500Records() | ||
| { | ||
| new FirstNameSource().List.Count.ShouldBe(500); | ||
| new LastNameSource().List.Count.ShouldBe(500); | ||
| new FullNameSource().List.Count.ShouldBe(500); | ||
| } | ||
| } | ||
|
|
||
| public class DummyDataSource : DataSource<string> | ||
| { | ||
| public DummyDataSource(IGenerator generator) | ||
| : base(generator) { } | ||
|
|
||
| public DummyDataSource() | ||
| : this(new RandomGenerator()) { } | ||
|
|
||
| protected override IList<string> InitializeList() | ||
| { | ||
| return new List<string>{"Value 1", "Value 2", "Value 3"}; | ||
| } | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
NTestDataBuilder.Tests/DataSources/Generators/RandomGeneratorTests.cs
This file contains hidden or 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,66 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using NTestDataBuilder.DataSources.Generators; | ||
| using Shouldly; | ||
| using Xunit; | ||
| using Xunit.Extensions; | ||
|
|
||
| namespace NTestDataBuilder.Tests.DataSources.Generators | ||
| { | ||
| public class RandomGeneratorTests | ||
| { | ||
| [Theory, | ||
| InlineData(5,4), | ||
| InlineData(5,5)] | ||
| public void WhenCreatingRandomGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize) | ||
| { | ||
| Action factory = () => new RandomGenerator(startIndex, listSize); | ||
| Should.Throw<ArgumentException>(factory) | ||
| .Message.ShouldBe(string.Format("startIndex must be less than listSize")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingRandomIntegers_ThenShouldAlwaysGenerateIntegerBetweenStartIndexAndListSize() | ||
| { | ||
| var random = new Random(); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| int minimumValue = random.Next(0,10); | ||
| int maximumValue = random.Next(20,30); | ||
| var sut = new RandomGenerator(minimumValue, maximumValue); | ||
|
|
||
| var result = sut.Generate(); | ||
|
|
||
| result.ShouldBeGreaterThanOrEqualTo(minimumValue); | ||
| result.ShouldBeLessThanOrEqualTo(maximumValue); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingRandomIntegers_ThenShouldBeAbleToGenerateLowerBoundaryValue() | ||
| { | ||
| var results = new List<int>(); | ||
| var sut = new RandomGenerator(0, 3); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| results.Add(sut.Generate()); | ||
| } | ||
| results.ShouldContain(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGeneratingRandomIntegers_ThenShouldBeAbleToGenerateUpperBoundaryValue() | ||
| { | ||
| var results = new List<int>(); | ||
| var sut = new RandomGenerator(0, 3); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| results.Add(sut.Generate()); | ||
| } | ||
| results.ShouldContain(2); | ||
| results.ShouldNotContain(3); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
NTestDataBuilder.Tests/DataSources/Generators/SequentiaGeneratorTests.cs
This file contains hidden or 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,77 @@ | ||
| using System; | ||
| using NTestDataBuilder.DataSources.Generators; | ||
| using Shouldly; | ||
| using Xunit; | ||
| using Xunit.Extensions; | ||
|
|
||
| namespace NTestDataBuilder.Tests.DataSources.Generators | ||
| { | ||
| public class SequentiaGeneratorTests | ||
| { | ||
| [Theory, | ||
| InlineData(5, 4), | ||
| InlineData(5, 5)] | ||
| public void WhenCreatingRandomGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize) | ||
| { | ||
| Action factory = () => new SequentialGenerator(startIndex, listSize); | ||
| Should.Throw<ArgumentException>(factory) | ||
| .Message.ShouldBe(string.Format("startIndex must be less than listSize")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenCreatingRandomGenerator_ThenStartIndexMustBeZeroOrMore() | ||
| { | ||
| Action factory = () => new SequentialGenerator(-1, 1); | ||
| Should.Throw<ArgumentException>(factory) | ||
| .Message.ShouldBe(string.Format("startIndex must be zero or more")); | ||
| } | ||
|
|
||
| [Theory, | ||
| InlineData(0, 0), | ||
| InlineData(0, -1)] | ||
| public void WhenCreatingRandomGenerator_ThenListSizeMustBeGreaterThanZero(int startIndex, int listSize) | ||
| { | ||
| Action factory = () => new SequentialGenerator(startIndex, listSize); | ||
| Should.Throw<ArgumentException>(factory) | ||
| .Message.ShouldBe(string.Format("listSize must be greater than zero")); | ||
| } | ||
|
|
||
|
|
||
| [Fact] | ||
| public void WhenGeneratingIntegers_ThenShouldBeSequential() | ||
| { | ||
| var sut = new SequentialGenerator(0, 11); | ||
| for (int index = sut.StartIndex; index < sut.ListSize; index++) | ||
| { | ||
| sut.Generate().ShouldBe(index); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenGeneratorIsNotUnique_WhenGeneratingIntegers_ThenShouldResetAtEndOfList() | ||
| { | ||
| var sut = new SequentialGenerator(0, 2); | ||
| for (int index = sut.StartIndex; index < sut.ListSize; index++) | ||
| { | ||
| sut.Generate().ShouldBe(index); | ||
| } | ||
|
|
||
| sut.Generate().ShouldBe(sut.StartIndex); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenGeneratorIsUnique_WhenGeneratingIntegers_ThenShouldResetAtEndOfList() | ||
| { | ||
| var sut = new SequentialGenerator(0, 2, true); | ||
| for (int index = sut.StartIndex; index < sut.ListSize; index++) | ||
| { | ||
| sut.Generate().ShouldBe(index); | ||
| } | ||
|
|
||
| Should.Throw<InvalidOperationException>(()=>sut.Generate()) | ||
| .Message.ShouldBe("There are not enough elements in the data source to continue adding items"); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
NTestDataBuilder.Tests/EquivalenceClasses/CompanyEquivalenceClassTests.cs
This file contains hidden or 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,40 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using NTestDataBuilder.DataSources; | ||
| using NTestDataBuilder.EquivalenceClasses; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace NTestDataBuilder.Tests.EquivalenceClasses | ||
| { | ||
| public class CompanyEquivalenceClassTests | ||
| { | ||
| public AnonymousValueFixture Any { get; private set; } | ||
|
|
||
| public CompanyEquivalenceClassTests() | ||
| { | ||
| Any = new AnonymousValueFixture(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WhenGettingAnyCountry_ThenReturnRandomCompanyWhichIsReasonablyUnique() | ||
| { | ||
| var companySource = new CompanySource(); | ||
|
|
||
| var results = new List<string>(); | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| results.Add(Any.Company()); | ||
| } | ||
|
|
||
| foreach (var result in results) | ||
| { | ||
| result.ShouldBeOfType<string>(); | ||
| result.ShouldNotBeNullOrEmpty(); | ||
| companySource.List.ShouldContain(result); | ||
| } | ||
| var unique = results.Distinct().Count(); | ||
| unique.ShouldBeGreaterThan(5); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Nice test