From 42cbed4ae3f361405a18d47df102d0aff193f145 Mon Sep 17 00:00:00 2001 From: kesac Date: Fri, 5 Jan 2024 23:04:48 -0700 Subject: [PATCH] Added unit tests for SyllableGenerator: - Added two more unit tests to confirm and show effect of the AllowEmptyStringGeneration flag on vowel-less SyllableGenerators --- .../Syllabore.Tests/SyllableGeneratorTests.cs | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs b/Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs index eb45166..986a71b 100644 --- a/Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs +++ b/Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Text.RegularExpressions; namespace Syllabore.Tests @@ -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(() => { + + 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() {