Skip to content

Commit

Permalink
Added unit tests for SyllableGenerator:
Browse files Browse the repository at this point in the history
- Added two more unit tests to confirm and show effect of the AllowEmptyStringGeneration flag on vowel-less SyllableGenerators
  • Loading branch information
kesac committed Jan 6, 2024
1 parent 9526fcc commit 42cbed4
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Syllabore.Tests
Expand Down Expand Up @@ -224,6 +223,55 @@ public void NameGenerator_OnlyVowelsWithNoConsonants_GenerationSuccessful()
}
}

[TestMethod]
public void NameGenerator_ProbabilisticConsonants_GenerationSometimesNotSuccessful()
{
// The expectation here is that the name generator will sometimes generate valid names
// because the probability of a leading and final consonant is not zero. However, if by chance
// neither a leading or final consonant appears in the output, an exception will be thrown
// because the default behaviour of the generator is to not allow empty strings.

var sut = new SyllableGenerator()
.WithLeadingConsonants("str")
.WithFinalConsonants("lmn")
.WithProbability(x => x
.OfLeadingConsonants(0.50)
.OfFinalConsonants(0.50));

var ng = new NameGenerator(sut);

Assert.ThrowsException<InvalidOperationException>(() => {

for(int i = 0; i < 1000; i++)
{
ng.Next();
}

});
}

[TestMethod]
public void NameGenerator_ProbabilisticConsonantsButEmptyStringsAllowed_GenerationAlwaysSuccessful()
{
// If neither a leading or final consonant appears in the output, no exception will be thrown
// because the generator has been instructed to allow empty strings.

var sut = new SyllableGenerator()
.WithLeadingConsonants("str")
.WithFinalConsonants("lmn")
.WithProbability(x => x
.OfLeadingConsonants(0.50)
.OfFinalConsonants(0.50))
.AllowEmptyStrings(true);

var ng = new NameGenerator(sut);

for (int i = 0; i < 1000; i++)
{
Assert.IsNotNull(ng.Next());
}
}

[TestMethod]
public void SyllableGenerator_NoVowelsDefinedWithoutGuaranteedConsonants1_NotAllowed()
{
Expand Down

0 comments on commit 42cbed4

Please sign in to comment.