From 8e439ce3a42bd78c28e64067cf593b869b2ed7d2 Mon Sep 17 00:00:00 2001 From: vycdev2 Date: Sun, 9 Aug 2026 18:42:04 +0000 Subject: [PATCH] fix: clamp percentage bar input --- Morpheus.Tests/MiscModuleTests.cs | 9 +++++++++ Utilities/Extensions/Extensions.cs | 3 +++ 2 files changed, 12 insertions(+) diff --git a/Morpheus.Tests/MiscModuleTests.cs b/Morpheus.Tests/MiscModuleTests.cs index 8459f83..9f71692 100644 --- a/Morpheus.Tests/MiscModuleTests.cs +++ b/Morpheus.Tests/MiscModuleTests.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Morpheus.Database; using Morpheus.Modules; +using Morpheus.Utilities.Extensions; namespace Morpheus.Tests; @@ -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()); + } } diff --git a/Utilities/Extensions/Extensions.cs b/Utilities/Extensions/Extensions.cs index 331b66b..8d5ae60 100644 --- a/Utilities/Extensions/Extensions.cs +++ b/Utilities/Extensions/Extensions.cs @@ -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);