Skip to content

Commit

Permalink
Simplified unit tests for custom leading vowel probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
kesac committed Dec 22, 2023
1 parent 25ce14b commit 61eb321
Showing 1 changed file with 43 additions and 66 deletions.
109 changes: 43 additions & 66 deletions Syllabore/Syllabore.Tests/SyllableProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ namespace Syllabore.Tests
[TestClass]
public class SyllableProviderTests
{
// This is just a helper method to provide a vanilla
// provider with one instance of every vowel/consonant
// combination defined

// Looking to deprecate this helper method
private static SyllableGenerator GetSyllableGeneratorWithAllComponentsDefined()
{
return new SyllableGenerator()
Expand Down Expand Up @@ -504,88 +503,66 @@ public void SyllableGenerator_WithNoSequencesDefined_NameGeneratorStillGenerates

}

// TODO

[TestMethod]
public void SyllableGenerator_SettingLeadingVowelInStartingSyllable_AffectsOutput()
[DataRow(0.0, false, true)]
[DataRow(0.5, true, true)]
[DataRow(1.0, true, false)]
public void SyllableGenerator_CustomLeadingVowelProbability_AffectsOutput(
double leadingVowelProbability,
bool isLeadingVowelExpected,
bool isLeadingConsonantExpected
)
{
// 1. By default, vowels do not have a probability of starting a name
var provider = GetSyllableGeneratorWithAllComponentsDefined();
provider.WithProbability(x => x.OfLeadingConsonants(1.0));
var sut = GetSyllableGenerator("a", "ee", "b", "cc", "d", "ff")
.WithProbability(x => x
.OfLeadingConsonants(1.0)
.OfLeadingVowelsInStartingSyllable(leadingVowelProbability));

bool leadingVowelDetected = false;
for(int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("o")) { leadingVowelDetected = true; break; }
}
Assert.IsFalse(leadingVowelDetected);
var leadingVowelsDetected = false;
var leadingConsonantsDetected = false;

// 2. Leading vowels in the starting syllables can be enabled
// by increasing the probability to non-zero
provider.WithProbability(x => x.OfLeadingVowelsInStartingSyllable(0.5)); // You can turn this off explicitly without adjusting probability settings
leadingVowelDetected = false; // In which case we expect this variable to remain false
for (int i = 0; i < 1000; i++)
for(int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("o")) { leadingVowelDetected = true; break; }
var s = sut.NextStartingSyllable();
leadingVowelsDetected |= Regex.IsMatch(s, "^[aieou]");
leadingConsonantsDetected |= Regex.IsMatch(s, "^[^aieou]"); // note the negation in the regex
}
Assert.IsTrue(leadingVowelDetected);

// 2. Leading vowels in the starting syllables can be disabled
// by setting the probability to zero
provider.WithProbability(x => x.OfLeadingVowelsInStartingSyllable(0));
leadingVowelDetected = false; // In which case we expect this variable to be true again
for (int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("o")) { leadingVowelDetected = true; break; }
}
Assert.IsFalse(leadingVowelDetected);
Assert.IsTrue(leadingVowelsDetected == isLeadingVowelExpected);
Assert.IsTrue(leadingConsonantsDetected == isLeadingConsonantExpected);
}

[TestMethod]
public void SyllableGenerator_SettingLeadingVowelSequenceInStartingSyllable_AffectsOutput()
[DataRow(0.0, false, true)]
[DataRow(0.5, true, true)]
[DataRow(1.0, true, false)]
public void SyllableGenerator_CustomLeadingVowelSequenceProbability_AffectsOutput(
double leadingVowelSequenceProbability,
bool outputWithLeadingVowelSequenceExpected,
bool outputWithoutLeadingVowelSequenceExpected
)
{
// 1. By default, vowel sequences do not have a probability of starting a name
var provider = GetSyllableGeneratorWithAllComponentsDefined();
provider.WithProbability(x => x.OfLeadingConsonants(1.0));
var sut = GetSyllableGenerator("a", "ee", "b", "cc", "d", "ff")
.WithProbability(x => x
.OfLeadingConsonants(1.0)
.OfLeadingVowelsInStartingSyllable(1.0, leadingVowelSequenceProbability));

bool leadingVowelDetected = false;
for (int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("uu")) { leadingVowelDetected = true; break; }
}
Assert.IsFalse(leadingVowelDetected);
var leadingVowelSequenceDetected = false;
var otherLeadingGraphemeDetected = false;

// 2. Leading vowel sequences in the starting syllables can be enabled
// by increasing the probability to non-zero
provider.WithProbability(x => x
.OfLeadingVowelsInStartingSyllable(0.5)
.OfLeadingVowelIsSequenceInStartingSyllable(0.5)); // You can turn this off explicitly without adjusting probability settings
leadingVowelDetected = false; // In which case we expect this variable to remain false
for (int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("uu")) { leadingVowelDetected = true; break; }
var s = sut.NextStartingSyllable();
leadingVowelSequenceDetected |= Regex.IsMatch(s, "^ee");
otherLeadingGraphemeDetected |= !Regex.IsMatch(s, "^ee");
}
Assert.IsTrue(leadingVowelDetected);

// 2. Leading vowel sequences in the starting syllables can be disabled
// by setting the probability to zero
provider.WithProbability(x => x
.OfLeadingVowelsInStartingSyllable(0.5)
.OfLeadingVowelIsSequenceInStartingSyllable(0.0));
leadingVowelDetected = false; // In which case we expect this variable to be true again
for (int i = 0; i < 1000; i++)
{
var s = provider.NextStartingSyllable();
if (s.StartsWith("uu")) { leadingVowelDetected = true; break; }
}
Assert.IsFalse(leadingVowelDetected);
Assert.IsTrue(leadingVowelSequenceDetected == outputWithLeadingVowelSequenceExpected);
Assert.IsTrue(otherLeadingGraphemeDetected == outputWithoutLeadingVowelSequenceExpected);
}

// TODO

[TestMethod]
public void SyllableGenerator_WithTogglingOfLeadingConsonants_TurnsOnOrOffAsExpected()
{
Expand Down

0 comments on commit 61eb321

Please sign in to comment.