-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for StringExtensions.
- Loading branch information
1 parent
9257d11
commit 696cd5b
Showing
7 changed files
with
137 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build
Check warning on line 15 in Behavioral/DesignPatterns.Visitor.UnitTests/StringExtensionsTests.cs GitHub Actions / build
|
||
{ | ||
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 }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |"); | ||
} |