From 696cd5b4ef6b6d639d17e63f6f30d5f717dffa5d Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Thu, 1 Feb 2024 21:10:42 +0100 Subject: [PATCH] Add unit tests for StringExtensions. --- .../DesignPatterns.Visitor.UnitTests.csproj | 1 + .../StringExtensionsTests.cs | 35 ++++++++ Behavioral/DesignPatterns.Visitor/Context.cs | 84 ++++++++++++------- .../DesignPatterns.Visitor.csproj | 4 + Behavioral/DesignPatterns.Visitor/Handler.cs | 45 ++++++---- .../DesignPatterns.Visitor/Models/Size.cs | 2 + Behavioral/DesignPatterns.Visitor/Program.cs | 48 +++-------- 7 files changed, 137 insertions(+), 82 deletions(-) create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj b/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj index 8144415..ffe9889 100644 --- a/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj @@ -11,6 +11,7 @@ + diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs new file mode 100644 index 0000000..997e1cd --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs @@ -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) + { + var visitorMock = new Mock>(); + visitorMock.Setup(x => x.Visit(It.IsNotNull())).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 TestCases() + { + yield return [ "1KB", new SizeInKiloBytes { Value = 1 }, new SizeInBytes { Value = 102400 }]; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Context.cs b/Behavioral/DesignPatterns.Visitor/Context.cs index c55dea1..702ca9a 100644 --- a/Behavioral/DesignPatterns.Visitor/Context.cs +++ b/Behavioral/DesignPatterns.Visitor/Context.cs @@ -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 itemsInBytes, ICollection itemsInGB) GetData() -// { -// var inBytes = GetSizesInBytes().ToList(); -// var inGB = GetSizesInGB().ToList(); -// return (inBytes, inGB); -// } -// -// private static IEnumerable GetSizesInBytes() -// { -// yield return new AmountInBytes { Value = 1073741824 }; -// yield return new AmountInBytes { Value = 2147483648 }; -// yield return new AmountInBytes { Value = 3221225472 }; -// } -// -// private static IEnumerable 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 GetStringVisitor() => new StringVisitor(); + + public IVisitor GetSizeVisitor() => new SizeVisitor( + new List> + { + new KiloBytesToBytesConverter(), + new MegaBytesToBytesConverter(), + new GigaBytesToBytesConverter(), + new TeraBytesToBytesConverter(), + new PetaBytesToBytesConverter(), + }); + + public (ICollection strings, ICollection sizes) GetData() + { + var strings = GetStringData().ToList(); + var sizes = GetSizes().ToList(); + return (strings, sizes); + } + + private static IEnumerable 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 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 }; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj b/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj index 6e6c6e3..ed430ca 100644 --- a/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj +++ b/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj @@ -9,4 +9,8 @@ DesignPatterns.Visitor + + + + \ No newline at end of file diff --git a/Behavioral/DesignPatterns.Visitor/Handler.cs b/Behavioral/DesignPatterns.Visitor/Handler.cs index 576e203..68c0bfa 100644 --- a/Behavioral/DesignPatterns.Visitor/Handler.cs +++ b/Behavioral/DesignPatterns.Visitor/Handler.cs @@ -1,15 +1,30 @@ -// namespace DesignPatterns.Visitor; -// -// public class Handler(IEnumerable visitors) -// { -// public IEnumerable Process(ICollection sizes) -// { -// var result = new List(); -// foreach (var visitor in visitors) -// { -// result.AddRange(sizes.Select(size => size.Accept(visitor))); -// } -// -// return result; -// } -// } +namespace DesignPatterns.Visitor; + +public class Handler +{ + public IEnumerable ProcessStrings( + ICollection sizes, + IEnumerable> visitors) + { + var result = new List(); + foreach (var visitor in visitors) + { + result.AddRange(sizes.Select(x => x.Accept(visitor))); + } + + return result; + } + + public IEnumerable ProcessSizes( + ICollection sizes, + IEnumerable> visitors) + { + var result = new List(); + foreach (var visitor in visitors) + { + result.AddRange(sizes.Select(x => x.Accept(visitor))); + } + + return result; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/Size.cs b/Behavioral/DesignPatterns.Visitor/Models/Size.cs index 13bbdfa..edd232b 100644 --- a/Behavioral/DesignPatterns.Visitor/Models/Size.cs +++ b/Behavioral/DesignPatterns.Visitor/Models/Size.cs @@ -9,4 +9,6 @@ public ISize Accept(IVisitor visitor) { return visitor.Visit(this); } + + public sealed override string ToString() => $"{this.Value.ToString()} {this.Unit}"; } diff --git a/Behavioral/DesignPatterns.Visitor/Program.cs b/Behavioral/DesignPatterns.Visitor/Program.cs index 656251f..02c9623 100644 --- a/Behavioral/DesignPatterns.Visitor/Program.cs +++ b/Behavioral/DesignPatterns.Visitor/Program.cs @@ -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 -{ - "100KB", - "200 KB", - "10 MB", - "200MB", - "1GB", - "20 GB" -}; - -var visitor = new StringVisitor(); - -var result = new List(); -result.AddRange(strings.Select(x => x.Accept(visitor))); +Console.WriteLine("| String Data | Sizes | Final Sizes |"); +Console.WriteLine("|-------------|-------------|--------------------------|"); -var amountVisitor = new SizeVisitor( - new List> - { - new KiloBytesToBytesConverter(), - new MegaBytesToBytesConverter(), - new GigaBytesToBytesConverter(), - new TeraBytesToBytesConverter(), - new PetaBytesToBytesConverter(), - }); - -var newResult = new List(); -newResult.AddRange(result.Select(x => x.Accept(amountVisitor))); - -Console.WriteLine(); \ No newline at end of file +for (var i = 0; i < data.strings.Count; i++) +{ + Console.WriteLine($"| {data.strings.ElementAt(i),-11} | {sizes.ElementAt(i),-11} | {finalSizes.ElementAt(i),-24} |"); +}