Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Morpheus.Tests/MiscModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Morpheus.Database;
using Morpheus.Modules;
using Morpheus.Utilities.Extensions;

namespace Morpheus.Tests;

Expand Down Expand Up @@ -153,4 +154,12 @@ public void TryParseDiceInput_RejectsMalformedOrOutOfRangeInput(string input)
{
Assert.False(MiscModule.TryParseDiceInput(input, out _, out _));
}

[Theory]
[InlineData(-1, "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░")]
[InlineData(101, "██████████████████████████████")]
public void GetPercentageBar_ClampsOutOfRangeValues(int value, string expected)
{
Assert.Equal(expected, value.GetPercentageBar());
}
}
3 changes: 3 additions & 0 deletions Utilities/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public static string GetPercentageBar(this int value)
// Define the total length of the bar
int totalLength = 30;

// Keep malformed or future caller input from producing an invalid bar length.
value = Math.Clamp(value, 0, 100);

// Calculate the number of filled cells
int filledCells = (int)Math.Round((double)value / 100 * totalLength);

Expand Down