Skip to content

Commit

Permalink
Add more unit tests for StringVisitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 1, 2024
1 parent 696cd5b commit 66d5848
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
24 changes: 16 additions & 8 deletions Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace DesignPatterns.Visitor.UnitTests;

public class SizeVisitorTests
{
[Fact]
public void GivenSize_WhenCallVisit_ThenInvalidOperationExceptionThrown()
{
var sizeVisitor = new SizeVisitor(Enumerable.Empty<ISizeConverter<ISize>>());
var action = () => sizeVisitor.Visit(default);

Check warning on line 14 in Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 14 in Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
action.ShouldThrow<InvalidOperationException>();
}

[Theory]
[MemberData(nameof(TestCases))]
public void GivenSize_WhenCallVisit_ThenResultAsExpected(ISize size, ISize expectedResult)
Expand All @@ -16,14 +24,14 @@ public void GivenSize_WhenCallVisit_ThenResultAsExpected(ISize size, ISize expec
actualResult.ShouldBeEquivalentTo(expectedResult);
}

private IVisitor<ISize, ISize> GetSut() => new SizeVisitor(
[
new KiloBytesToBytesConverter(),
new MegaBytesToBytesConverter(),
new GigaBytesToBytesConverter(),
new TeraBytesToBytesConverter(),
new PetaBytesToBytesConverter()
]);
private IVisitor<ISize, ISize> GetSut() =>
new SizeVisitor([
new KiloBytesToBytesConverter(),
new MegaBytesToBytesConverter(),
new GigaBytesToBytesConverter(),
new TeraBytesToBytesConverter(),
new PetaBytesToBytesConverter()
]);

public static IEnumerable<object[]> TestCases()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,34 @@ public void GivenStringValue_WhenCallVisit_ThenResultAsExpected(string inputValu
result.ShouldBeOfType(expectedType);
result.Value.ShouldBe(expectedValue);
}
}

[Fact]
public void GivenNull_WhenCallVisit_ThenArgumentExceptionThrown()
{
var stringVisitor = new StringVisitor();
var action = () => stringVisitor.Visit(null!);
action.ShouldThrow<ArgumentException>();
}

[Theory]
[InlineData("1ZB")]
public void GivenInvalidValue_WhenCallVisit_ThenArgumentExceptionThrown(string value)
{
var stringVisitor = new StringVisitor();
var action = () => stringVisitor.Visit(value);
action
.ShouldThrow<ArgumentException>()
.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<ArgumentOutOfRangeException>()
.Message.ShouldBe("Unknown size unit. (Parameter 'size')");
}
}
4 changes: 2 additions & 2 deletions Behavioral/DesignPatterns.Visitor/StringVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public ISize Visit(string size)
throw new ArgumentException("Input size string cannot be null or empty.", nameof(size));
}

var regexPattern = @"^(?<value>\d+(\.\d+)?)\s*(?<unit>[KMGTP]?B)$";
var regexPattern = @"^(?<value>\d+(\.\d+)?)\s*(?<unit>[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);
Expand Down

0 comments on commit 66d5848

Please sign in to comment.