Skip to content

Commit

Permalink
Added fluent-style methods for controlling name/syllable generators
Browse files Browse the repository at this point in the history
  • Loading branch information
kesac committed Jun 5, 2024
1 parent b51192f commit 5e80538
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
36 changes: 20 additions & 16 deletions Syllabore/Syllabore.Tests/NameGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,27 @@ public void NameGeneration_WithSequencesOnly_Allowed()


[TestMethod]
[DataRow(0)]
[DataRow(12345)]
public void NameGenerator_StaticRandomSeed_CreatesPredictableOutput(int seed)
[DataRow("aeiou", "strlmnp", 0)]
[DataRow("aeiou", "strlmnp", 12345)]
public void NameGenerator_StaticRandomSeed_CreatesPredictableOutput(
string vowels,
string consonants,
int seed
)
{
var sutSyllables = new SyllableGenerator("aeiou", "strlmnp");
sutSyllables.Random = new Random(seed);

var sut = new NameGenerator();
sut.Random = new Random(seed);
sut.UsingSyllables(sutSyllables);

var comparisonSyllables = new SyllableGenerator("aeiou", "strlmnp");
comparisonSyllables.Random = new Random(seed);

var comparison = new NameGenerator();
comparison.Random = new Random(seed);
comparison.UsingSyllables(comparisonSyllables);
var sut = new NameGenerator()
.UsingSyllables(x => x
.WithVowels(vowels)
.WithConsonants(consonants)
.WithRandom(new Random(seed)))
.UsingRandom(new Random(seed));
var comparison = new NameGenerator()
.UsingSyllables(x => x
.WithVowels(vowels)
.WithConsonants(consonants)
.WithRandom(new Random(seed)))
.UsingRandom(new Random(seed));

for (int i = 0; i < 1000; i++)
{
Expand Down
17 changes: 9 additions & 8 deletions Syllabore/Syllabore.Tests/SyllableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,15 +738,16 @@ bool isTrailingConsonantSequenceExpected
}

[TestMethod]
[DataRow(0)]
[DataRow(12345)]
public void SyllableGenerator_StaticRandomSeed_CreatesPredictableOutput(int seed)
[DataRow("aeiou", "strlmnp", 0)]
[DataRow("aeiou", "strlmnp", 12345)]
public void SyllableGenerator_StaticRandomSeed_CreatesPredictableOutput(
string vowels,
string consonants,
int seed
)
{
var sut = new SyllableGenerator("aeiou", "strlmnp");
sut.Random = new Random(seed);

var comparison = new SyllableGenerator("aeiou", "strlmnp");
comparison.Random = new Random(seed);
var sut = new SyllableGenerator(vowels, consonants).WithRandom(new Random(seed));
var comparison = new SyllableGenerator(vowels, consonants).WithRandom(new Random(seed));

for (int i = 0; i < 1000; i++)
{
Expand Down
11 changes: 11 additions & 0 deletions Syllabore/Syllabore/NameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ public NameGenerator UsingProbability(Func<GeneratorProbabilityBuilder, Generato
return this;
}

/// <summary>
/// Sets the instance of <see cref="System.Random"/>
/// this class will use to simulate randomness during
/// the name generation process.
/// </summary>
public NameGenerator UsingRandom(Random random)
{
this.Random = random;
return this;
}

/// <summary>
/// <para>
/// Sets the maximum number of generation retries before an exception is thrown.
Expand Down
10 changes: 10 additions & 0 deletions Syllabore/Syllabore/SyllableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,16 @@ public SyllableGenerator WithProbability(Func<GeneratorProbabilityBuilder, Gener
return this;
}

/// <summary>
/// Sets the instance of <see cref="System.Random"/> to use when
/// simulating randomness during the syllable generation process.
/// </summary>
public SyllableGenerator WithRandom(Random random)
{
this.Random = random;
return this;
}

/// <summary>
/// Specifying a value of <c>true</c> will permit generation of empty strings
/// as syllables. This is a scenario if there are no vowels/consonants to choose from or if the probability
Expand Down

0 comments on commit 5e80538

Please sign in to comment.