From 2e15665aa090954c8ecfdd450f90135856305163 Mon Sep 17 00:00:00 2001 From: Niklas Weimann Date: Sun, 19 May 2024 21:10:35 +0200 Subject: [PATCH] Improve HexToRGB24Converter --- .../Utils/HexToRGB24Converter.cs | 28 ++++++--- src/UnitTests/HexToRgb24Tests.cs | 62 +++++++++++++++++++ 2 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 src/UnitTests/HexToRgb24Tests.cs diff --git a/src/RxTelegram.Bot/Utils/HexToRGB24Converter.cs b/src/RxTelegram.Bot/Utils/HexToRGB24Converter.cs index b530820..541dd52 100644 --- a/src/RxTelegram.Bot/Utils/HexToRGB24Converter.cs +++ b/src/RxTelegram.Bot/Utils/HexToRGB24Converter.cs @@ -3,11 +3,29 @@ namespace RxTelegram.Bot.Utils; -public class HexToRgb24 +public static class HexToRgb24 { - public int Convert(string hexColor) => int.Parse(FormatHexString(hexColor), NumberStyles.HexNumber); + public static int Convert(string hexColor) + { + var formatHexString = FormatHexString(hexColor); + if (formatHexString.Length != 6) + { + throw new ArgumentException("Invalid hex color", nameof(hexColor)); + } + return int.Parse(formatHexString, NumberStyles.HexNumber); + } - public bool TryConvert(string hexColor, out int result) => int.TryParse(FormatHexString(hexColor), NumberStyles.HexNumber, null, out result); + public static bool TryConvert(string hexColor, out int result) + { + var formatHexString = FormatHexString(hexColor); + if (formatHexString.Length == 6) + { + return int.TryParse(FormatHexString(hexColor), NumberStyles.HexNumber, null, out result); + } + + result = -1; + return false; + } private static string FormatHexString(string hexColor) { @@ -16,10 +34,6 @@ private static string FormatHexString(string hexColor) { hexColor = string.Concat(hexColor[0], hexColor[0], hexColor[1], hexColor[1], hexColor[2], hexColor[2]); } - else if (hexColor.Length != 6) - { - throw new ArgumentException("Hex color must be 3 or 6 characters long."); - } return hexColor; } diff --git a/src/UnitTests/HexToRgb24Tests.cs b/src/UnitTests/HexToRgb24Tests.cs new file mode 100644 index 0000000..8a71100 --- /dev/null +++ b/src/UnitTests/HexToRgb24Tests.cs @@ -0,0 +1,62 @@ +using System; +using NUnit.Framework; +using RxTelegram.Bot.Utils; + +namespace RxTelegram.Bot.UnitTests; + +[TestFixture] +public class HexToRgb24Tests +{ + [TestCase("#FF0000", ExpectedResult = 16711680)] // Red + [TestCase("#00FF00", ExpectedResult = 65280)] // Green + [TestCase("#0000FF", ExpectedResult = 255)] // Blue + [TestCase("#000000", ExpectedResult = 0)] // Black + [TestCase("#FFFFFF", ExpectedResult = 16777215)] // White + [TestCase("#ABCDEF", ExpectedResult = 11259375)] // Random color + public int Convert_ValidHexColor_ReturnsRgbValue(string hexColor) => HexToRgb24.Convert(hexColor); + + [TestCase("#0000000")] + [TestCase("#00000000")] + public void Convert_InvalidHexLength_ThrowsArgumentException(string hexColor) => + Assert.Throws(() => HexToRgb24.Convert(hexColor)); + + [TestCase("#G00000")] + [TestCase("#00000G")] + public void Convert_InvalidHexColor_ThrowsFormatException(string hexColor) => + Assert.Throws(() => HexToRgb24.Convert(hexColor)); + + [TestCase("#0000000")] + [TestCase("#00000000")] + public void TryConvert_InvalidHexLength_ThrowsArgumentException(string hexColor) => + Assert.That(HexToRgb24.TryConvert(hexColor, out _), Is.False); + + [TestCase("#G00000")] + [TestCase("#00000G")] + public void TryConvert_InvalidHexColor_ThrowsFormatException(string hexColor) => + Assert.That(HexToRgb24.TryConvert(hexColor, out _), Is.False); + + [TestCase("#FF0000", ExpectedResult = true)] // Red + [TestCase("#00FF00", ExpectedResult = true)] // Green + [TestCase("#0000FF", ExpectedResult = true)] // Blue + [TestCase("#000000", ExpectedResult = true)] // Black + [TestCase("#FFFFFF", ExpectedResult = true)] // White + [TestCase("#ABCDEF", ExpectedResult = true)] // Random color + [TestCase("#G00000", ExpectedResult = false)] + [TestCase("#00000G", ExpectedResult = false)] + [TestCase("#0000000", ExpectedResult = false)] + [TestCase("#00000000", ExpectedResult = false)] + public bool TryConvert_ValidHexColor_ReturnsTrue(string hexColor) => HexToRgb24.TryConvert(hexColor, out _); + + [TestCase("#FF0000", 16711680)] // Red + [TestCase("#00FF00", 65280)] // Green + [TestCase("#0000FF", 255)] // Blue + [TestCase("#000000", 0)] // Black + [TestCase("#FFFFFF", 16777215)] // White + [TestCase("#ABCDEF", 11259375)] // Random color + public void TryConvert_ValidHexColor_ReturnsRgbValue(string hexColor, int expected) + { + HexToRgb24.TryConvert(hexColor, out var result); + Assert.That(result, Is.EqualTo(expected)); + } + +}