Skip to content

Commit

Permalink
Add unit tests for StringExtensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 1, 2024
1 parent 9257d11 commit 696cd5b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Shouldly" Version="4.2.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using DesignPatterns.Visitor.Models;
using FluentAssertions;
using FluentAssertions.Execution;
using Moq;
using Shouldly;
using Xunit;

namespace DesignPatterns.Visitor.UnitTests;

public class StringExtensionsTests
{
[Theory]
[MemberData(nameof(TestCases))]
public void GivenSize_WhenCallVisit_ThenResultAsExpected(
string value, ISize expectedSizeInKiloBytes, ISize expectedSizeInBytes)

Check warning on line 15 in Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'GivenSize_WhenCallVisit_ThenResultAsExpected' on test class 'StringExtensionsTests' does not use parameter 'expectedSizeInBytes'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)

Check warning on line 15 in Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'GivenSize_WhenCallVisit_ThenResultAsExpected' on test class 'StringExtensionsTests' does not use parameter 'expectedSizeInBytes'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)
{
var visitorMock = new Mock<IVisitor<string, ISize>>();
visitorMock.Setup(x => x.Visit(It.IsNotNull<string>())).Returns(expectedSizeInKiloBytes).Verifiable();

ISize? actualResult = null;
var action = () => actualResult = value.Accept(visitorMock.Object);

using (new AssertionScope())
{
action.ShouldNotThrow();
actualResult.ShouldBeEquivalentTo(expectedSizeInKiloBytes);
visitorMock.Verify(x => x.Visit(value), Times.Once);
}
}

public static IEnumerable<object[]> TestCases()
{
yield return [ "1KB", new SizeInKiloBytes { Value = 1 }, new SizeInBytes { Value = 102400 }];
}
}
84 changes: 54 additions & 30 deletions Behavioral/DesignPatterns.Visitor/Context.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
// using AutoFixture;
//
// namespace DesignPatterns.Visitor;
//
// public class Context
// {
// private readonly IFixture fixture = new Fixture();
// private readonly Random random = new Random();
//
// public (ICollection<IAmount> itemsInBytes, ICollection<IAmount> itemsInGB) GetData()
// {
// var inBytes = GetSizesInBytes().ToList();
// var inGB = GetSizesInGB().ToList();
// return (inBytes, inGB);
// }
//
// private static IEnumerable<IAmount> GetSizesInBytes()
// {
// yield return new AmountInBytes { Value = 1073741824 };
// yield return new AmountInBytes { Value = 2147483648 };
// yield return new AmountInBytes { Value = 3221225472 };
// }
//
// private static IEnumerable<IAmount> GetSizesInGB()
// {
// yield return new AmountInGB { Value = 10 };
// yield return new AmountInGB { Value = 20 };
// yield return new AmountInGB { Value = 30 };
// }
// }
using DesignPatterns.Visitor.Converters;
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor;

public class Context
{
public IVisitor<string, ISize> GetStringVisitor() => new StringVisitor();

public IVisitor<ISize, ISize> GetSizeVisitor() => new SizeVisitor(
new List<ISizeConverter<ISize>>
{
new KiloBytesToBytesConverter(),
new MegaBytesToBytesConverter(),
new GigaBytesToBytesConverter(),
new TeraBytesToBytesConverter(),
new PetaBytesToBytesConverter(),
});

public (ICollection<string> strings, ICollection<ISize> sizes) GetData()
{
var strings = GetStringData().ToList();
var sizes = GetSizes().ToList();
return (strings, sizes);
}

private static IEnumerable<string> GetStringData()
{
yield return "1KB";
yield return "2 KB";
yield return "4MB";
yield return "8 MB";
yield return "16GB";
yield return "32 GB";
yield return "64TB";
yield return "128 TB";
yield return "256PB";
yield return "512 PB";
}

private static IEnumerable<ISize> GetSizes()
{
yield return new SizeInKiloBytes { Value = 1 };
yield return new SizeInKiloBytes { Value = 2 };
yield return new SizeInMegaBytes { Value = 4 };
yield return new SizeInMegaBytes { Value = 8 };
yield return new SizeInGigaBytes { Value = 16 };
yield return new SizeInGigaBytes { Value = 32 };
yield return new SizeInTeraBytes { Value = 64 };
yield return new SizeInTeraBytes { Value = 128 };
yield return new SizeInPetaBytes { Value = 256 };
yield return new SizeInPetaBytes { Value = 512 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<RootNamespace>DesignPatterns.Visitor</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

</Project>
45 changes: 30 additions & 15 deletions Behavioral/DesignPatterns.Visitor/Handler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
// namespace DesignPatterns.Visitor;
//
// public class Handler(IEnumerable<IVisitor> visitors)
// {
// public IEnumerable<IAmount> Process(ICollection<IAmount> sizes)
// {
// var result = new List<IAmount>();
// foreach (var visitor in visitors)
// {
// result.AddRange(sizes.Select(size => size.Accept(visitor)));
// }
//
// return result;
// }
// }
namespace DesignPatterns.Visitor;

public class Handler
{
public IEnumerable<ISize> ProcessStrings(
ICollection<string> sizes,
IEnumerable<IVisitor<string, ISize>> visitors)
{
var result = new List<ISize>();
foreach (var visitor in visitors)
{
result.AddRange(sizes.Select(x => x.Accept(visitor)));
}

return result;
}

public IEnumerable<ISize> ProcessSizes(
ICollection<ISize> sizes,
IEnumerable<IVisitor<ISize, ISize>> visitors)
{
var result = new List<ISize>();
foreach (var visitor in visitors)
{
result.AddRange(sizes.Select(x => x.Accept(visitor)));
}

return result;
}
}
2 changes: 2 additions & 0 deletions Behavioral/DesignPatterns.Visitor/Models/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public ISize Accept(IVisitor<ISize, ISize> visitor)
{
return visitor.Visit(this);
}

public sealed override string ToString() => $"{this.Value.ToString()} {this.Unit}";
}
48 changes: 11 additions & 37 deletions Behavioral/DesignPatterns.Visitor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
using DesignPatterns.Visitor;
using DesignPatterns.Visitor.Converters;

// var context = new Context();
// var discountVisitor = new SizeVisitor();
// var handler = new Handler(new[] { discountVisitor });
// var data = context.GetData();
// var convertedInGB = handler.Process(data.itemsInBytes);
// var convertedInBytes = handler.Process(data.itemsInGB);
//
// Console.WriteLine();
var context = new Context();
var handler = new Handler();
var data = context.GetData();
var sizes = handler.ProcessStrings(data.strings, [context.GetStringVisitor()]).ToList();
var finalSizes = handler.ProcessSizes(sizes, [context.GetSizeVisitor()]).ToList();

var strings = new List<string>
{
"100KB",
"200 KB",
"10 MB",
"200MB",
"1GB",
"20 GB"
};

var visitor = new StringVisitor();

var result = new List<ISize>();
result.AddRange(strings.Select(x => x.Accept(visitor)));
Console.WriteLine("| String Data | Sizes | Final Sizes |");
Console.WriteLine("|-------------|-------------|--------------------------|");

var amountVisitor = new SizeVisitor(
new List<ISizeConverter<ISize>>
{
new KiloBytesToBytesConverter(),
new MegaBytesToBytesConverter(),
new GigaBytesToBytesConverter(),
new TeraBytesToBytesConverter(),
new PetaBytesToBytesConverter(),
});

var newResult = new List<ISize>();
newResult.AddRange(result.Select(x => x.Accept(amountVisitor)));

Console.WriteLine();
for (var i = 0; i < data.strings.Count; i++)
{
Console.WriteLine($"| {data.strings.ElementAt(i),-11} | {sizes.ElementAt(i),-11} | {finalSizes.ElementAt(i),-24} |");
}

0 comments on commit 696cd5b

Please sign in to comment.