diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs index fe65d52..ea8a26b 100644 --- a/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs @@ -7,6 +7,14 @@ namespace DesignPatterns.Visitor.UnitTests; public class SizeVisitorTests { + [Fact] + public void GivenSize_WhenCallVisit_ThenInvalidOperationExceptionThrown() + { + var sizeVisitor = new SizeVisitor(Enumerable.Empty>()); + var action = () => sizeVisitor.Visit(default); + action.ShouldThrow(); + } + [Theory] [MemberData(nameof(TestCases))] public void GivenSize_WhenCallVisit_ThenResultAsExpected(ISize size, ISize expectedResult) @@ -16,14 +24,14 @@ public void GivenSize_WhenCallVisit_ThenResultAsExpected(ISize size, ISize expec actualResult.ShouldBeEquivalentTo(expectedResult); } - private IVisitor GetSut() => new SizeVisitor( - [ - new KiloBytesToBytesConverter(), - new MegaBytesToBytesConverter(), - new GigaBytesToBytesConverter(), - new TeraBytesToBytesConverter(), - new PetaBytesToBytesConverter() - ]); + private IVisitor GetSut() => + new SizeVisitor([ + new KiloBytesToBytesConverter(), + new MegaBytesToBytesConverter(), + new GigaBytesToBytesConverter(), + new TeraBytesToBytesConverter(), + new PetaBytesToBytesConverter() + ]); public static IEnumerable TestCases() { diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs index 2b8259b..9edcd62 100644 --- a/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs @@ -24,4 +24,34 @@ public void GivenStringValue_WhenCallVisit_ThenResultAsExpected(string inputValu result.ShouldBeOfType(expectedType); result.Value.ShouldBe(expectedValue); } -} \ No newline at end of file + + [Fact] + public void GivenNull_WhenCallVisit_ThenArgumentExceptionThrown() + { + var stringVisitor = new StringVisitor(); + var action = () => stringVisitor.Visit(null!); + action.ShouldThrow(); + } + + [Theory] + [InlineData("1ZB")] + public void GivenInvalidValue_WhenCallVisit_ThenArgumentExceptionThrown(string value) + { + var stringVisitor = new StringVisitor(); + var action = () => stringVisitor.Visit(value); + action + .ShouldThrow() + .Message.ShouldBe("Could not parse the provided value. (Parameter 'size')"); + } + + [Theory] + [InlineData("1EB")] + public void GivenUnknownSizeUnit_WhenCallVisit_ThenArgumentOutOfRangeExceptionThrown(string value) + { + var stringVisitor = new StringVisitor(); + var action = () => stringVisitor.Visit(value); + action + .ShouldThrow() + .Message.ShouldBe("Unknown size unit. (Parameter 'size')"); + } +} diff --git a/Behavioral/DesignPatterns.Visitor/StringVisitor.cs b/Behavioral/DesignPatterns.Visitor/StringVisitor.cs index e859bbc..8d39fa5 100644 --- a/Behavioral/DesignPatterns.Visitor/StringVisitor.cs +++ b/Behavioral/DesignPatterns.Visitor/StringVisitor.cs @@ -12,12 +12,12 @@ public ISize Visit(string size) throw new ArgumentException("Input size string cannot be null or empty.", nameof(size)); } - var regexPattern = @"^(?\d+(\.\d+)?)\s*(?[KMGTP]?B)$"; + var regexPattern = @"^(?\d+(\.\d+)?)\s*(?[KMGTPE]?B)$"; var match = Regex.Match(size, regexPattern, RegexOptions.IgnoreCase); if (!match.Success) { - throw new ArgumentException("Invalid size format.", nameof(size)); + throw new ArgumentException("Could not parse the provided value.", nameof(size)); } var value = long.Parse(match.Groups["value"].Value);