Skip to content
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

NameFilterTests refactor #11

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 77 additions & 67 deletions Syllabore/Syllabore.Tests/NameFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
[TestClass]
public class NameFilterTests
{
private readonly NameGenerator _sut;
public NameFilterTests()
{
_sut = new NameGenerator()
.UsingSyllables(x => x
.WithVowels("aei")
.WithLeadingConsonants("str"));
}

/* Create a unit test that calls methods that start with "DoNot" and verifies that the name is not valid. */
[TestMethod, Timeout(10000)]
public void NameValidation_WithoutInstantiatingNameFilterExplicitly_OutputReflectsConstraints()
{
// Purposely using depcreated method UsingProvider()
var g = new NameGenerator()
var sut = new NameGenerator()

Check warning on line 26 in Syllabore/Syllabore.Tests/NameFilterTests.cs

View workflow job for this annotation

GitHub Actions / build

'NameGenerator.UsingProvider(Func<SyllableGenerator, SyllableGenerator>)' is obsolete: 'Use UsingSyllables() instead'
.UsingProvider(x => x
.WithVowels("aei")
.WithLeadingConsonants("bcdf"))
Expand All @@ -26,94 +34,102 @@

for (int i = 0; i < 1000; i++)
{
var name = g.Next().ToLower();
var name = sut.Next().ToLower();
Assert.IsFalse(name.ContainsAny("a", "e"));
Assert.IsFalse(name.StartsWith("b"));
Assert.IsFalse(name.EndsWith("c"));
}

}



[TestMethod, Timeout(10000)]
public void NameValidation_WhenPrefixConstraintSpecified_OutputReflectsConstraints()
[TestMethod, Timeout(10000)]
public void NameValidation_WhenPrefixConstraintNotSpecified_OutputReflectsConstraints()
{
var p = new SyllableGenerator()
var syllableGenerator = new SyllableGenerator()
.WithVowels("aei")
.WithConsonants("str")
.WithProbability(x => x.OfLeadingVowelsInStartingSyllable(1));

var g = new NameGenerator().UsingSyllables(p);
var sut = new NameGenerator().UsingSyllables(syllableGenerator);

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
Assert.IsTrue(name.StartsWith("A","E","I"));
var name = sut.Next();
Assert.IsTrue(name.StartsWith("A", "E", "I"));
}
}

g.UsingFilter(x => x.DoNotAllowStart("a")); // this method should be case insensitive too
[TestMethod, Timeout(10000)]
public void NameValidation_WhenPrefixConstraintSpecified_OutputReflectsConstraints()
{
var syllableGenerator = new SyllableGenerator()
.WithVowels("aei")
.WithConsonants("str")
.WithProbability(x => x.OfLeadingVowelsInStartingSyllable(1));

var sut = new NameGenerator().UsingSyllables(syllableGenerator);

sut.UsingFilter(x => x.DoNotAllowStart("a")); // this method should be case insensitive too

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
var name = sut.Next();
Assert.IsTrue(name.StartsWith("E", "I"));
}

}

[TestMethod, Timeout(10000)]
public void NameValidation_WhenSuffixConstraintSpecified_OutputReflectsConstraints()
public void NameValidation_WhenNoSuffixConstraintSpecified_OutputReflectsConstraints()
{
var g = new NameGenerator()
.UsingSyllables(x => x
.WithVowels("aei")
.WithLeadingConsonants("str"));

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
var name = _sut.Next();
Assert.IsTrue(name.EndsWith("a", "e", "i"));
}
}

g.UsingFilter(x => x.DoNotAllowEnding("I")); // this method should be case insensitive too
[TestMethod, Timeout(10000)]
public void NameValidation_WhenSuffixConstraintSpecified_OutputReflectsConstraints()
{
_sut.UsingFilter(x => x.DoNotAllowEnding("I")); // this method should be case insensitive too

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
Assert.IsTrue(name.EndsWith("a", "e"));
var name = _sut.Next();
Assert.IsTrue(!name.EndsWith("I", "i"));
}

}

[TestMethod, Timeout(10000)]
public void NameValidation_WhenNonRegexConstraintSpecified_OutputReflectsConstraints()
[DataRow(new string[] { "a", "e", "i" })]
public void NameValidation_WhenNonRegexConstraintSpecified_OutputReflectsConstraints(string[] characters)
{
var g = new NameGenerator()
.UsingSyllables(x => x
.WithVowels("aei")
.WithLeadingConsonants("str"));

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
Assert.IsTrue(name.ContainsAny("a", "e", "i"));
var name = _sut.Next();
Assert.IsTrue(name.ContainsAny(characters));
}
}

g.UsingFilter(x => x.DoNotAllow("e")); // this method should be case insensitive too

[TestMethod, Timeout(10000)]
[DataRow("e")]
[DataRow("a")]
[DataRow("q")]
public void NameValidation_WhenNonRegexConstraintSpecified_WithDisallowedCharacter_OutputReflectsConstraints(string character)
{
_sut.UsingFilter(x => x.DoNotAllow(character));

for (int i = 0; i < 1000; i++)
{
var name = g.Next();
Assert.IsFalse(name.ContainsAny("E","e"));
var name = _sut.Next();
Assert.IsTrue(!name.ContainsAny(character.ToLower(), character.ToUpper()));
}

}

[TestMethod, Timeout(10000)]
public void NameValidation_WhenRegexConstraintsSpecified_OutputReflectsConstraints()
{
var generator = new NameGenerator()
var sut = new NameGenerator()
.UsingSyllables(x => x
.WithVowels("a")
.WithVowelSequences("ee")
Expand All @@ -122,59 +138,57 @@
.WithTrailingConsonants("d")
.WithTrailingConsonantSequences("ff"))
.UsingFilter(x => x
.DoNotAllowPattern(@"[aeiouAEIOU]{2}") // This rule rejects names with vowel sequences
.DoNotAllowPattern(@"[^aeiouAEIOU]{2}")); // This rule rejects names with consonant sequences
.DoNotAllow(@"[aeiouAEIOU]{2}") // This rule rejects names with vowel sequences
.DoNotAllow(@"[^aeiouAEIOU]{2}")); // This rule rejects names with consonant sequences

for (int i = 0; i < 1000; i++)
{
Assert.IsFalse(Regex.IsMatch(generator.Next(), "(ee|cc|ff)"));
Assert.IsFalse(Regex.IsMatch(sut.Next(), "(ee|cc|ff)"));
}

}

[TestMethod, Timeout(10000)]
public void NameValidation_WhenRegexConstraintsSpecified3_OutputReflectsConstraints()
{
var g = new NameGenerator();
g.UsingSyllableCount(2, 3);

var f = new NameFilter();
f.DoNotAllowEnding("f", "g", "h", "j", "q", "v", "w", "z");
f.DoNotAllowPattern("([^aieou]{3})"); // Regex reads: non-vowels, three times in a row
var sut = new NameGenerator();
sut.UsingSyllableCount(2, 3);

f.DoNotAllowPattern("(q[^u])"); // Q must always be followed by a u
f.DoNotAllowPattern("([^tsao]w)"); // W must always be preceded with a t, s, a, or o
f.DoNotAllow("pn"); // Looks a little awkward
var nameFilter = new NameFilter();
nameFilter.DoNotAllowEnding("f", "g", "h", "j", "q", "v", "w", "z");
nameFilter.DoNotAllow("([^aieou]{3})"); // Regex reads: non-vowels, three times in a row
nameFilter.DoNotAllow("(q[^u])"); // Q must always be followed by a u
nameFilter.DoNotAllow("([^tsao]w)"); // W must always be preceded with a t, s, a, or o
nameFilter.DoNotAllow("pn"); // Looks a little awkward

Assert.IsFalse(f.IsValidName(new Name() { Syllables = new List<string>() { "qello" } }));
Assert.IsTrue(f.IsValidName(new Name() { Syllables = new List<string>() { "quello" } }));
Assert.IsFalse(nameFilter.IsValidName(new Name() { Syllables = new List<string>() { "qello" } }));
Assert.IsTrue(nameFilter.IsValidName(new Name() { Syllables = new List<string>() { "quello" } }));

Assert.IsFalse(f.IsValidName(new Name() { Syllables = new List<string>() { "lwas" } }));
Assert.IsTrue(f.IsValidName(new Name() { Syllables = new List<string>() { "twas" } }));
Assert.IsFalse(nameFilter.IsValidName(new Name() { Syllables = new List<string>() { "lwas" } }));
Assert.IsTrue(nameFilter.IsValidName(new Name() { Syllables = new List<string>() { "twas" } }));

g.UsingFilter(f);
sut.UsingFilter(nameFilter);
}

[TestMethod, Timeout(10000)]
public void NameValidation_WhenRegexConstraintsSpecified2_OutputReflectsConstraints()
{

var provider = new DefaultSyllableGenerator();
var filter = new NameFilter();
filter.DoNotAllowPattern(@"[^aeiouAEIOU]{3,}"); // Rejects 3 or more consecutive consonants
filter.DoNotAllow(@"[^aeiouAEIOU]{3,}"); // Rejects 3 or more consecutive consonants

Assert.IsTrue(filter.IsValidName(new Name() { Syllables = new List<String>() { "bc" } }));
Assert.IsFalse(filter.IsValidName(new Name() { Syllables = new List<String>() { "bcd" } }));
Assert.IsFalse(filter.IsValidName(new Name() { Syllables = new List<String>() { "bcdf" } }));

var generator = new NameGenerator();
generator.UsingSyllables(provider);
generator.UsingFilter(filter);
var sut = new NameGenerator();
sut.UsingSyllables(provider);
sut.UsingFilter(filter);

for (int i = 0; i < 1000; i++)
{
var original = generator.Next();
var name = new StringBuilder(original.ToLower());
var original = sut.Next().ToLower();
var name = new StringBuilder(original);

name.Replace("a", " ");
name.Replace("e", " ");
Expand All @@ -192,12 +206,8 @@
maxConsonantSequenceLength = sequence.Length;
}
}

Assert.IsTrue(maxConsonantSequenceLength < 3);
}


}

}
}
Loading