Skip to content

Commit ce89027

Browse files
committed
Refactored DataSource. Introduced file-based dictionaries
1 parent 4b9024d commit ce89027

25 files changed

Lines changed: 245 additions & 34 deletions

NTestDataBuilder.Tests/DataSources/DataSourceTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void WhenCreatingADataSource_ThenListShouldContainAllTheValues()
1414
{
1515
var sut = new DummyDataSource();
1616

17-
var result = sut.List;
17+
var result = sut.Data;
1818

1919
result.Count.ShouldBe(3);
2020
result.ShouldBe(new List<string> { "Value 1", "Value 2", "Value 3" });
@@ -25,7 +25,7 @@ public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateItemFrom
2525
{
2626
var sut = new DummyDataSource();
2727
var result = sut.Next();
28-
sut.List.ShouldContain(result);
28+
sut.Data.ShouldContain(result);
2929
}
3030

3131
[Fact]
@@ -39,7 +39,7 @@ public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateRandomIt
3939
results.Add(sut.Next());
4040
}
4141

42-
foreach (var item in sut.List)
42+
foreach (var item in sut.Data)
4343
{
4444
results.ShouldContain(item);
4545
}
@@ -50,20 +50,20 @@ public void WhenGeneratingValuesFromSequentialDataSource_ThenShouldGenerateSeque
5050
{
5151
var sut = new DummyDataSource(new SequentialGenerator());
5252

53-
sut.Next().ShouldBe(sut.List[0]);
54-
sut.Next().ShouldBe(sut.List[1]);
55-
sut.Next().ShouldBe(sut.List[2]);
56-
sut.Next().ShouldBe(sut.List[0]);
53+
sut.Next().ShouldBe(sut.Data[0]);
54+
sut.Next().ShouldBe(sut.Data[1]);
55+
sut.Next().ShouldBe(sut.Data[2]);
56+
sut.Next().ShouldBe(sut.Data[0]);
5757
}
5858

5959
[Fact]
6060
public void WhenGeneratingValuesFromUniqueSequentialDataSource_ThenShouldThrowWhenCollectionExceeded()
6161
{
6262
var sut = new DummyDataSource(new SequentialGenerator(0,1,true));
6363

64-
sut.Next().ShouldBe(sut.List[0]);
65-
sut.Next().ShouldBe(sut.List[1]);
66-
sut.Next().ShouldBe(sut.List[2]);
64+
sut.Next().ShouldBe(sut.Data[0]);
65+
sut.Next().ShouldBe(sut.Data[1]);
66+
sut.Next().ShouldBe(sut.Data[2]);
6767
Should.Throw<InvalidOperationException>(() => sut.Next());
6868
}
6969
}
@@ -76,7 +76,7 @@ public DummyDataSource(IGenerator generator)
7676
public DummyDataSource()
7777
: this(new RandomGenerator()) { }
7878

79-
protected override IList<string> InitializeList()
79+
protected override IList<string> InitializeDataSource()
8080
{
8181
return new List<string>{"Value 1", "Value 2", "Value 3"};
8282
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.IO;
2+
using NTestDataBuilder.DataSources.Dictionaries;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
7+
{
8+
public class FileDictionaryRepositoryIntegrationTests
9+
{
10+
[Fact]
11+
public void GivenAnExternalFileDictionaryExists_WhenRetrievingWords_ThenExternalDictionaryIsUsed()
12+
{
13+
var sut = new FileDictionaryRepository();
14+
15+
var result = sut.GetWordsFrom("SampleDictionaryFile");
16+
17+
result.Count.ShouldBe(2);
18+
result[0].ShouldBe("File first word");
19+
}
20+
21+
[Fact]
22+
public void GivenAResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenResourceDictionaryIsUsed()
23+
{
24+
var sut = new FileDictionaryRepository();
25+
26+
var result = sut.GetWordsFrom("GeoContinent");
27+
28+
result.Count.ShouldBe(7);
29+
result[0].ShouldBe("Asia");
30+
}
31+
32+
[Fact]
33+
public void GivenNoResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenFileNotFoundException()
34+
{
35+
var sut = new FileDictionaryRepository();
36+
Should.Throw<FileNotFoundException>(() => sut.GetWordsFrom("NonExistentDictionary"));
37+
}
38+
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NSubstitute;
2+
using NTestDataBuilder.DataSources.Dictionaries;
3+
using NTestDataBuilder.DataSources.Generators;
4+
using Xunit;
5+
6+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
7+
{
8+
public class FileDictionarySourceTests
9+
{
10+
[Fact]
11+
public void WhenInitializingList_ThenPassClassNameToRepositoryAsDictionaryName()
12+
{
13+
var repository = Substitute.For<IDictionaryRepository>();
14+
var sut = new DummySource(repository);
15+
16+
var result = sut.Data;
17+
18+
repository.Received().GetWordsFrom("Dummy");
19+
}
20+
}
21+
22+
public class DummySource : FileDictionarySource
23+
{
24+
public DummySource(IDictionaryRepository repository)
25+
: base(Substitute.For<IGenerator>(), repository) { }
26+
}
27+
}

NTestDataBuilder.Tests/DataSources/PersonSourceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class PersonSourceTests
1212
[PropertyData("TestCases")]
1313
public void PersonSourceSpec(DataSource<string> sut, int expectedCount)
1414
{
15-
var collection = sut.List.ToList();
15+
var collection = sut.Data.ToList();
1616
collection.Count.ShouldBe(expectedCount);
17-
collection.Count.ShouldBe(sut.List.Distinct().ToList().Count);
17+
collection.Count.ShouldBe(sut.Data.Distinct().ToList().Count);
1818
collection.ForEach(item => item.ShouldNotBeNullOrEmpty());
1919
}
2020

NTestDataBuilder.Tests/EquivalenceClasses/PersonEquivalenceClassesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void WhenGettingAnyPersonData_ThenReturnRandomPersonDataWhichIsReasonably
2626
{
2727
testCase.ShouldBeOfType<string>();
2828
testCase.ShouldNotBeNullOrEmpty();
29-
source.List.ShouldContain(testCase);
29+
source.Data.ShouldContain(testCase);
3030
}
3131
var unique = testCases.Distinct().Count();
3232
unique.ShouldBeGreaterThan(5);

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
</ItemGroup>
5454
<ItemGroup>
5555
<Compile Include="DataSources\DataSourceTests.cs" />
56+
<Compile Include="DataSources\Dictionaries\FileDictionaryRepositoryIntegrationTests.cs" />
57+
<Compile Include="DataSources\Dictionaries\FileDictionarySourceTests.cs" />
5658
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
5759
<Compile Include="DataSources\Generators\SequentiaGeneratorTests.cs" />
5860
<Compile Include="DataSources\PersonSourceTests.cs" />
@@ -84,7 +86,14 @@
8486
<Name>NTestDataBuilder</Name>
8587
</ProjectReference>
8688
</ItemGroup>
87-
<ItemGroup />
89+
<ItemGroup>
90+
<Folder Include="DataSources\Dictionaries\Resources\" />
91+
</ItemGroup>
92+
<ItemGroup>
93+
<Content Include="SampleDictionaryFile.txt">
94+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
95+
</Content>
96+
</ItemGroup>
8897
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8998
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
9099
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
File first word
2+
File second word

NTestDataBuilder/DataSources/CitySource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public CitySource(IGenerator generator)
1919
: base(generator) { }
2020

2121
/// <inheritdoc />
22-
protected override IList<string> InitializeList()
22+
protected override IList<string> InitializeDataSource()
2323
{
2424
return FileDataRepository.People
2525
.Select(person => person.City)

NTestDataBuilder/DataSources/CompanySource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public CompanySource(IGenerator generator)
1919
: base(generator) { }
2020

2121
/// <inheritdoc />
22-
protected override IList<string> InitializeList()
22+
protected override IList<string> InitializeDataSource()
2323
{
2424
return FileDataRepository.People
2525
.Select(person => person.CompanyName)

NTestDataBuilder/DataSources/CountySource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public CountySource(IGenerator generator)
1919
: base(generator) { }
2020

2121
/// <inheritdoc />
22-
protected override IList<string> InitializeList()
22+
protected override IList<string> InitializeDataSource()
2323
{
2424
return FileDataRepository.People
2525
.Select(person => person.County)

0 commit comments

Comments
 (0)