Skip to content

Commit

Permalink
Merge pull request #4 from quozd/refactor-tests
Browse files Browse the repository at this point in the history
extract test data factory to separate file
  • Loading branch information
quozd committed Jan 23, 2014
2 parents c3de86d + 21e8721 commit 120a34d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
56 changes: 1 addition & 55 deletions src/NPetrovich.Tests/Fixtures/CaseInflectionFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NPetrovich.Inflection;
using NPetrovich.Rules;
using NPetrovich.Rules.Loader.Yaml;
using NPetrovich.Tests.TestDataProviders;
using NUnit.Framework;
using System;
using System.Collections;
Expand Down Expand Up @@ -44,60 +45,5 @@ public void Should_inflect_middle_name_correctly(string middleName, Gender gende
var actual = new CaseInflection(provider, gender).InflectMiddleNameTo(middleName, @case);
Assert.AreEqual(expected, actual, string.Format("Gender: {0}, Case: {1}", gender, @case));
}

public class InflectionTestCaseDataFactory
{
public static IEnumerable LastNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "LastNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

public static IEnumerable FirstNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "FirstNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

public static IEnumerable MiddleNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "MiddleNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

private static IEnumerable ReadCaseData(StreamReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
continue;

var chunks = line.Split(',').Select(s => s.Trim()).ToList();

var gender = (Gender)Enum.Parse(typeof(Gender), chunks[1]);
var @case = (Case)Enum.Parse(typeof(Case), chunks[2]);

yield return new object[] { chunks[0], gender, @case, chunks[3] };
}
}

}

}
}
1 change: 1 addition & 0 deletions src/NPetrovich.Tests/NPetrovich.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Fixtures\GenderToolsFixture.cs" />
<Compile Include="Fixtures\CaseInflectionFixture.cs" />
<Compile Include="TestDataProviders\InflectionTestCaseDataFactory.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\FirstNames.csv">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.IO;
using System.Linq;

namespace NPetrovich.Tests.TestDataProviders
{
public class InflectionTestCaseDataFactory
{
public static IEnumerable LastNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "LastNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

public static IEnumerable FirstNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "FirstNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

public static IEnumerable MiddleNamesInflectionData
{
get
{
using (var reader = new StreamReader(Path.Combine("Data", "MiddleNames.csv")))
{
foreach (var p in ReadCaseData(reader)) yield return p;
}
}
}

private static IEnumerable ReadCaseData(StreamReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
continue;

var chunks = line.Split(',').Select(s => s.Trim()).ToList();

var gender = (Gender)Enum.Parse(typeof(Gender), chunks[1]);
var @case = (Case)Enum.Parse(typeof(Case), chunks[2]);

yield return new object[] { chunks[0], gender, @case, chunks[3] };
}
}

}
}

0 comments on commit 120a34d

Please sign in to comment.