-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an issue with random grawlix being interpreted as GitHub math exp…
…ressions.
- Loading branch information
1 parent
2f2c4e2
commit ac3f793
Showing
6 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/ProfanityFilter.Services/Extensions/RandomExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace ProfanityFilter.Services.Extensions; | ||
|
||
internal static class RandomExtensions | ||
{ | ||
/// <summary> | ||
/// Gets a random subset of items from the source array, with the | ||
/// specified length, while respecting the limits. | ||
/// </summary> | ||
internal static TItem[] RandomItemsWithLimitToOne<TItem>( | ||
this TItem[] array, | ||
int length, | ||
TItem limit) where TItem : notnull | ||
{ | ||
var choices = array.Except([limit]).ToArray(); | ||
|
||
var items = Random.Shared.GetItems(choices, length); | ||
|
||
var index = Random.Shared.Next(0, items.Length); | ||
|
||
items[index] = limit; | ||
|
||
return items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/ProfanityFilter.Services.Tests/RandomExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace ProfanityFilter.Services.Tests; | ||
|
||
public class RandomExtensionsTests | ||
{ | ||
[Fact] | ||
public void RandomItemsWithLimitsReturnsRandomSubsetOfItems() | ||
{ | ||
// Arrange | ||
var source = new[] { "a", "b", "c", "d", "e" }; | ||
|
||
// Act | ||
var result = source.RandomItemsWithLimitToOne(3, ""); | ||
|
||
// Assert | ||
Assert.Equal(3, result.Length); | ||
} | ||
|
||
[Fact] | ||
public void RandomItemsWithLimitsRespectsLimits() | ||
{ | ||
// Arrange | ||
var source = new[] { "a", "b", "c", "d", "e" }; | ||
|
||
// Act | ||
var result = source.RandomItemsWithLimitToOne(3, "a"); | ||
|
||
// Assert | ||
Assert.Equal(3, result.Length); | ||
Assert.Single(result.Where(str => str is "a")); | ||
} | ||
|
||
[Fact] | ||
public void RandomItemsWithLimitsReturnsHugeArrayWithOnlyOneLimitedValue() | ||
{ | ||
// Arrange | ||
var source = new[] { "a", "b", "c", "d", "e" }; | ||
|
||
// Act | ||
var result = source.RandomItemsWithLimitToOne(100_000, "c"); | ||
|
||
// Assert | ||
Assert.Contains(result, str => str is "a"); | ||
Assert.Contains(result, str => str is "b"); | ||
Assert.Single(result.Where(str => str is "c")); | ||
Assert.Contains(result, str => str is "d"); | ||
Assert.Contains(result, str => str is "e"); | ||
} | ||
} |