From 9257d11e54378d1a170f8abfb40e60007f73fba9 Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Thu, 1 Feb 2024 20:00:58 +0100 Subject: [PATCH] Add unit tests for SizeVisitor. --- .../SizeVisitorTests.cs | 36 +++++++++++++++++++ ...StringVisitor.cs => StringVisitorTests.cs} | 2 +- Behavioral/DesignPatterns.Visitor/ISize.cs | 2 +- .../DesignPatterns.Visitor/Models/Size.cs | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs rename Behavioral/DesignPatterns.Visitor.UnitTests/{StringVisitor.cs => StringVisitorTests.cs} (99%) diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs new file mode 100644 index 0000000..fe65d52 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/SizeVisitorTests.cs @@ -0,0 +1,36 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests; + +public class SizeVisitorTests +{ + [Theory] + [MemberData(nameof(TestCases))] + public void GivenSize_WhenCallVisit_ThenResultAsExpected(ISize size, ISize expectedResult) + { + var sizeVisitor = this.GetSut(); + var actualResult = sizeVisitor.Visit(size); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + + private IVisitor GetSut() => new SizeVisitor( + [ + new KiloBytesToBytesConverter(), + new MegaBytesToBytesConverter(), + new GigaBytesToBytesConverter(), + new TeraBytesToBytesConverter(), + new PetaBytesToBytesConverter() + ]); + + public static IEnumerable TestCases() + { + yield return [new SizeInKiloBytes { Value = 100 }, new SizeInBytes { Value = 102400 }]; + yield return [new SizeInMegaBytes { Value = 200 }, new SizeInBytes { Value = 209715200 }]; + yield return [new SizeInGigaBytes { Value = 300 }, new SizeInBytes { Value = 322122547200 }]; + yield return [new SizeInTeraBytes { Value = 400 }, new SizeInBytes { Value = 439804651110400 }]; + yield return [new SizeInPetaBytes { Value = 500 }, new SizeInBytes { Value = 562949953421312000 }]; + } +} diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitor.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs similarity index 99% rename from Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitor.cs rename to Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs index 97b08fa..2b8259b 100644 --- a/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitor.cs +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/StringVisitorTests.cs @@ -24,4 +24,4 @@ public void GivenStringValue_WhenCallVisit_ThenResultAsExpected(string inputValu result.ShouldBeOfType(expectedType); result.Value.ShouldBe(expectedValue); } -} +} \ No newline at end of file diff --git a/Behavioral/DesignPatterns.Visitor/ISize.cs b/Behavioral/DesignPatterns.Visitor/ISize.cs index 1f46572..1526800 100644 --- a/Behavioral/DesignPatterns.Visitor/ISize.cs +++ b/Behavioral/DesignPatterns.Visitor/ISize.cs @@ -2,7 +2,7 @@ namespace DesignPatterns.Visitor; public interface ISize { - double Value { get; } + long Value { get; } string Unit { get; } ISize Accept(IVisitor visitor); } diff --git a/Behavioral/DesignPatterns.Visitor/Models/Size.cs b/Behavioral/DesignPatterns.Visitor/Models/Size.cs index 541f65b..13bbdfa 100644 --- a/Behavioral/DesignPatterns.Visitor/Models/Size.cs +++ b/Behavioral/DesignPatterns.Visitor/Models/Size.cs @@ -2,7 +2,7 @@ namespace DesignPatterns.Visitor.Models; public record class Size : ISize { - public double Value { get; init; } + public long Value { get; init; } public virtual string Unit { get; init; } public ISize Accept(IVisitor visitor)