Skip to content

Commit 92fb2cd

Browse files
committed
made helper data classes internal, began adding XML comments, renamed some incorrectly named tests, removed Guard class
1 parent 029d200 commit 92fb2cd

16 files changed

Lines changed: 210 additions & 106 deletions

NTestDataBuilder.Tests/DataSources/Generators/RandomGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void WhenGeneratingRandomIntegers_ThenShouldBeAbleToGenerateLowerBoundary
4141
{
4242
var results = new List<int>();
4343
var sut = new RandomGenerator(0, 3);
44-
for (int i = 0; i < 10; i++)
44+
for (int i = 0; i < 20; i++)
4545
{
4646
results.Add(sut.Generate());
4747
}
@@ -53,7 +53,7 @@ public void WhenGeneratingRandomIntegers_ThenShouldBeAbleToGenerateUpperBoundary
5353
{
5454
var results = new List<int>();
5555
var sut = new RandomGenerator(0, 3);
56-
for (int i = 0; i < 10; i++)
56+
for (int i = 0; i < 20; i++)
5757
{
5858
results.Add(sut.Generate());
5959
}

NTestDataBuilder.Tests/DataSources/Generators/SequentiaGeneratorTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class SequentiaGeneratorTests
1111
[Theory,
1212
InlineData(5, 4),
1313
InlineData(5, 5)]
14-
public void WhenCreatingRandomGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize)
14+
public void WhenCreatingSequentialGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize)
1515
{
1616
Action factory = () => new SequentialGenerator(startIndex, listSize);
1717
Should.Throw<ArgumentException>(factory)
1818
.Message.ShouldBe(string.Format("startIndex must be less than listSize"));
1919
}
2020

2121
[Fact]
22-
public void WhenCreatingRandomGenerator_ThenStartIndexMustBeZeroOrMore()
22+
public void WhenCreatingSequentialGenerator_ThenStartIndexMustBeZeroOrMore()
2323
{
2424
Action factory = () => new SequentialGenerator(-1, 1);
2525
Should.Throw<ArgumentException>(factory)
@@ -29,7 +29,7 @@ public void WhenCreatingRandomGenerator_ThenStartIndexMustBeZeroOrMore()
2929
[Theory,
3030
InlineData(0, 0),
3131
InlineData(0, -1)]
32-
public void WhenCreatingRandomGenerator_ThenListSizeMustBeGreaterThanZero(int startIndex, int listSize)
32+
public void WhenCreatingSequentialGenerator_ThenListSizeMustBeGreaterThanZero(int startIndex, int listSize)
3333
{
3434
Action factory = () => new SequentialGenerator(startIndex, listSize);
3535
Should.Throw<ArgumentException>(factory)
@@ -60,7 +60,7 @@ public void GivenGeneratorIsNotUnique_WhenGeneratingIntegers_ThenShouldResetAtEn
6060
}
6161

6262
[Fact]
63-
public void GivenGeneratorIsUnique_WhenGeneratingIntegers_ThenShouldResetAtEndOfList()
63+
public void GivenGeneratorIsUnique_WhenGeneratingIntegers_ThenShouldThrowAtEndOfList()
6464
{
6565
var sut = new SequentialGenerator(0, 2, true);
6666
for (int index = sut.StartIndex; index < sut.ListSize; index++)

NTestDataBuilder.Tests/EquivalenceClasses/CompanyEquivalenceClassTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public CompanyEquivalenceClassTests()
1717
}
1818

1919
[Fact]
20-
public void WhenGettingAnyCountry_ThenReturnRandomCompanyWhichIsReasonablyUnique()
20+
public void WhenGettingAnyCompany_ThenReturnRandomCompanyWhichIsReasonablyUnique()
2121
{
2222
var companySource = new CompanySource();
2323

NTestDataBuilder/DataSources/DataSource.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33

44
namespace NTestDataBuilder.DataSources
55
{
6-
//public interface IDataSource<T>
7-
//{
8-
// T Next();
9-
//}
10-
public abstract class DataSource<T> //: IDataSource<T>
6+
/// <summary>
7+
/// The base class for data sources to inherit from.
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
public abstract class DataSource<T>
1111
{
12-
public IList<T> List { get; private set; }
13-
public IGenerator Generator { get; private set; }
14-
15-
public DataSource(IGenerator generator)
12+
/// <summary>
13+
/// Allows a custom data generation strategy to be passed to the data source
14+
/// </summary>
15+
/// <param name="generator">The generator that determines the strategy for returning each item from the data source collection</param>
16+
protected DataSource(IGenerator generator)
1617
{
1718
Generator = generator;
1819
List = InitializeList();
1920
Generator.ListSize = List.Count;
2021
}
2122

22-
public DataSource()
23+
/// <summary>
24+
/// The default constructor implements a RandomGenerator strategy
25+
/// </summary>
26+
protected DataSource()
2327
: this(new RandomGenerator()) { }
2428

29+
public IList<T> List { get; private set; }
30+
public IGenerator Generator { get; private set; }
31+
2532
protected abstract IList<T> InitializeList();
2633

2734
public virtual T Next()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace NTestDataBuilder.DataSources.FileData
5+
{
6+
internal class FileDataRepository
7+
{
8+
internal static IList<Person> People { get; private set; }
9+
10+
static FileDataRepository()
11+
{
12+
People = FileDataSourceHelpers.ConvertCsvToDataTable("NTestDataBuilder.DataSources.FileData.uk-500.csv")
13+
.AsEnumerable<Person>()
14+
.ToList();
15+
}
16+
}
17+
}

NTestDataBuilder/DataSources/FileData/PersonData.cs renamed to NTestDataBuilder/DataSources/FileData/FileDataSourceHelpers.cs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.IO;
5-
using System.Linq;
65
using System.Reflection;
76

87
namespace NTestDataBuilder.DataSources.FileData
98
{
10-
public static class DataTableExtensions
9+
internal static class FileDataSourceHelpers
1110
{
12-
public static IEnumerable<T> AsEnumerable<T>(this DataTable table) where T : new()
11+
internal static IEnumerable<T> AsEnumerable<T>(this DataTable table) where T : new()
1312
{
1413
if (table == null)
1514
throw new NullReferenceException("DataTable");
@@ -36,39 +35,12 @@ public static class DataTableExtensions
3635
}
3736
objList.Add(obj);
3837
}
39-
4038
return objList;
4139
}
42-
}
43-
44-
public class Person
45-
{
46-
public string FirstName { get; set; }
47-
public string LastName { get; set; }
48-
public string CompanyName { get; set; }
49-
public string Address { get; set; }
50-
public string City { get; set; }
51-
public string County { get; set; }
52-
public string Postal { get; set; }
53-
public string Phone1 { get; set; }
54-
public string Phone2 { get; set; }
55-
public string Email { get; set; }
56-
public string Web { get; set; }
57-
}
58-
public class PersonData
59-
{
60-
public static IList<Person> People { get; private set; }
61-
62-
static PersonData()
63-
{
64-
People = ConvertCSVtoDataTable()
65-
.AsEnumerable<Person>()
66-
.ToList();
67-
}
6840

69-
private static DataTable ConvertCSVtoDataTable()
41+
internal static DataTable ConvertCsvToDataTable(string embeddedFilename)
7042
{
71-
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NTestDataBuilder.DataSources.FileData.uk-500.csv");
43+
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedFilename);
7244
var sr = new StreamReader(stream);
7345
var headers = sr.ReadLine().Split(',');
7446
var dt = new DataTable();
@@ -89,4 +61,4 @@ private static DataTable ConvertCSVtoDataTable()
8961
return dt;
9062
}
9163
}
92-
}
64+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace NTestDataBuilder.DataSources.FileData
2+
{
3+
internal class Person
4+
{
5+
public string FirstName { get; set; }
6+
public string LastName { get; set; }
7+
public string CompanyName { get; set; }
8+
public string Address { get; set; }
9+
public string City { get; set; }
10+
public string County { get; set; }
11+
public string Postal { get; set; }
12+
public string Phone1 { get; set; }
13+
public string Phone2 { get; set; }
14+
public string Email { get; set; }
15+
public string Web { get; set; }
16+
}
17+
}
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using NTestDataBuilder.DataSources.FileData;
4+
using NTestDataBuilder.DataSources.Generators;
45

56
namespace NTestDataBuilder.DataSources
67
{
8+
/// <summary>
9+
/// Free sample data for testing obtained from http://www.briandunning.com/sample-data/
10+
/// </summary>
711
public class FirstNameSource : DataSource<string>
812
{
13+
/// <inheritdoc />
14+
public FirstNameSource()
15+
: base() { }
16+
17+
/// <inheritdoc />
18+
public FirstNameSource(IGenerator generator)
19+
: base(generator) { }
20+
921
protected override IList<string> InitializeList()
1022
{
11-
return PersonData.People
23+
return FileDataRepository.People
1224
.Select(person => person.FirstName)
1325
.ToList();
1426
}
1527
}
16-
public class LastNameSource : DataSource<string>
17-
{
18-
protected override IList<string> InitializeList()
19-
{
20-
return PersonData.People
21-
.Select(person => person.LastName)
22-
.ToList();
23-
}
24-
}
25-
public class FullNameSource : DataSource<string>
26-
{
27-
protected override IList<string> InitializeList()
28-
{
29-
return PersonData.People
30-
.Select(person => person.FirstName + " " + person.LastName)
31-
.ToList();
32-
}
33-
}
34-
3528
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NTestDataBuilder.DataSources.FileData;
4+
using NTestDataBuilder.DataSources.Generators;
5+
6+
namespace NTestDataBuilder.DataSources
7+
{
8+
/// <summary>
9+
/// Free sample data for testing obtained from http://www.briandunning.com/sample-data/
10+
/// </summary>
11+
public class FullNameSource : DataSource<string>
12+
{
13+
/// <inheritdoc />
14+
public FullNameSource()
15+
: base() { }
16+
17+
/// <inheritdoc />
18+
public FullNameSource(IGenerator generator)
19+
: base(generator) { }
20+
21+
protected override IList<string> InitializeList()
22+
{
23+
return FileDataRepository.People
24+
.Select(person => person.FirstName + " " + person.LastName)
25+
.ToList();
26+
}
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
namespace NTestDataBuilder.DataSources.Generators
22
{
3+
/// <summary>
4+
/// A strategy to determine the index of the next item to be selected from a list
5+
/// </summary>
36
public interface IGenerator
47
{
8+
/// <summary>
9+
/// Derives the next item to be selected in the list
10+
/// </summary>
11+
/// <returns>The index of the next item to be selected in the list</returns>
512
int Generate();
13+
14+
/// <summary>
15+
/// The first index that can be selected in a list. Ranges from 0 to one less than the number of items in the list
16+
/// </summary>
617
int StartIndex { get; set; }
18+
19+
/// <summary>
20+
/// The number of items in the list
21+
/// </summary>
722
int ListSize { get; set; }
823
}
924
}

0 commit comments

Comments
 (0)