From c9c1c2d4f64f04cc154dd900370e6137f6e7c119 Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Thu, 1 Feb 2024 22:48:28 +0100 Subject: [PATCH] Add unit tests for converters. --- .../GigaBytesToBytesConverterTests.cs | 53 +++++++++++++++++++ .../KiloBytesToBytesConverterTests.cs | 53 +++++++++++++++++++ .../MegaBytesToBytesConverterTests.cs | 53 +++++++++++++++++++ .../PetaBytesToBytesConverterTests.cs | 53 +++++++++++++++++++ .../TeraBytesToBytesConverterTests.cs | 53 +++++++++++++++++++ 5 files changed, 265 insertions(+) create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/Converters/GigaBytesToBytesConverterTests.cs create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/Converters/KiloBytesToBytesConverterTests.cs create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/Converters/MegaBytesToBytesConverterTests.cs create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/Converters/PetaBytesToBytesConverterTests.cs create mode 100644 Behavioral/DesignPatterns.Visitor.UnitTests/Converters/TeraBytesToBytesConverterTests.cs diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/GigaBytesToBytesConverterTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/GigaBytesToBytesConverterTests.cs new file mode 100644 index 0000000..d3c4401 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/GigaBytesToBytesConverterTests.cs @@ -0,0 +1,53 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using FluentAssertions.Execution; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests.Converters; + +public class GigaBytesToBytesConverterTests +{ + [Fact] + public void GivenSize_WhenCallConvert_ThenConvertedSizeReturned() + { + var expectedResult = new SizeInBytes { Value = 1024L * 1024 * 1024 }; + var size = new SizeInGigaBytes { Value = 1L }; + var sut = new GigaBytesToBytesConverter(); + ISize? actualResult = null; + var action = () => actualResult = sut.Convert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + } + + [Fact] + public void GivenSize_WhenCallConvert_ThenInvalidOperationExceptionThrown() + { + var sut = new GigaBytesToBytesConverter(); + var action = () => sut.Convert(null!); + action + .ShouldThrow() + .Message.ShouldBe("Could not convert GigaBytes to Bytes."); + } + + [Fact] + public void GivenSize_WhenCallCanConvert_ThenTrueReturned() + { + var size = new SizeInGigaBytes { Value = 1 }; + var sut = new GigaBytesToBytesConverter(); + bool? actualResult = null; + var action = () => actualResult = sut.CanConvert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult?.ShouldBeTrue(); + } + } +} diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/KiloBytesToBytesConverterTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/KiloBytesToBytesConverterTests.cs new file mode 100644 index 0000000..e06e983 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/KiloBytesToBytesConverterTests.cs @@ -0,0 +1,53 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using FluentAssertions.Execution; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests.Converters; + +public class KiloBytesToBytesConverterTests +{ + [Fact] + public void GivenSize_WhenCallConvert_ThenConvertedSizeReturned() + { + var expectedResult = new SizeInBytes { Value = 1024L }; + var size = new SizeInKiloBytes { Value = 1L }; + var sut = new KiloBytesToBytesConverter(); + ISize? actualResult = null; + var action = () => actualResult = sut.Convert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + } + + [Fact] + public void GivenSize_WhenCallConvert_ThenInvalidOperationExceptionThrown() + { + var sut = new KiloBytesToBytesConverter(); + var action = () => sut.Convert(null!); + action + .ShouldThrow() + .Message.ShouldBe("Could not convert KiloBytes to Bytes."); + } + + [Fact] + public void GivenSize_WhenCallCanConvert_ThenTrueReturned() + { + var size = new SizeInKiloBytes { Value = 1 }; + var sut = new KiloBytesToBytesConverter(); + bool? actualResult = null; + var action = () => actualResult = sut.CanConvert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult?.ShouldBeTrue(); + } + } +} diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/MegaBytesToBytesConverterTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/MegaBytesToBytesConverterTests.cs new file mode 100644 index 0000000..cd45b9b --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/MegaBytesToBytesConverterTests.cs @@ -0,0 +1,53 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using FluentAssertions.Execution; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests.Converters; + +public class MegaBytesToBytesConverterTests +{ + [Fact] + public void GivenSize_WhenCallConvert_ThenConvertedSizeReturned() + { + var expectedResult = new SizeInBytes { Value = 1024L * 1024 }; + var size = new SizeInMegaBytes { Value = 1L }; + var sut = new MegaBytesToBytesConverter(); + ISize? actualResult = null; + var action = () => actualResult = sut.Convert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + } + + [Fact] + public void GivenSize_WhenCallConvert_ThenInvalidOperationExceptionThrown() + { + var sut = new MegaBytesToBytesConverter(); + var action = () => sut.Convert(null!); + action + .ShouldThrow() + .Message.ShouldBe("Could not convert MegaBytes to Bytes."); + } + + [Fact] + public void GivenSize_WhenCallCanConvert_ThenTrueReturned() + { + var size = new SizeInMegaBytes { Value = 1 }; + var sut = new MegaBytesToBytesConverter(); + bool? actualResult = null; + var action = () => actualResult = sut.CanConvert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult?.ShouldBeTrue(); + } + } +} diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/PetaBytesToBytesConverterTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/PetaBytesToBytesConverterTests.cs new file mode 100644 index 0000000..9427b60 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/PetaBytesToBytesConverterTests.cs @@ -0,0 +1,53 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using FluentAssertions.Execution; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests.Converters; + +public class PetaBytesToBytesConverterTests +{ + [Fact] + public void GivenSize_WhenCallConvert_ThenConvertedSizeReturned() + { + var expectedResult = new SizeInBytes { Value = 1024L * 1024 * 1024 * 1024 * 1024 }; + var size = new SizeInPetaBytes { Value = 1L }; + var sut = new PetaBytesToBytesConverter(); + ISize? actualResult = null; + var action = () => actualResult = sut.Convert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + } + + [Fact] + public void GivenSize_WhenCallConvert_ThenInvalidOperationExceptionThrown() + { + var sut = new PetaBytesToBytesConverter(); + var action = () => sut.Convert(null!); + action + .ShouldThrow() + .Message.ShouldBe("Could not convert PetaBytes to Bytes."); + } + + [Fact] + public void GivenSize_WhenCallCanConvert_ThenTrueReturned() + { + var size = new SizeInPetaBytes { Value = 1 }; + var sut = new PetaBytesToBytesConverter(); + bool? actualResult = null; + var action = () => actualResult = sut.CanConvert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult?.ShouldBeTrue(); + } + } +} diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/TeraBytesToBytesConverterTests.cs b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/TeraBytesToBytesConverterTests.cs new file mode 100644 index 0000000..d630378 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/Converters/TeraBytesToBytesConverterTests.cs @@ -0,0 +1,53 @@ +using DesignPatterns.Visitor.Converters; +using DesignPatterns.Visitor.Models; +using FluentAssertions.Execution; +using Shouldly; +using Xunit; + +namespace DesignPatterns.Visitor.UnitTests.Converters; + +public class TeraBytesToBytesConverterTests +{ + [Fact] + public void GivenSize_WhenCallConvert_ThenConvertedSizeReturned() + { + var expectedResult = new SizeInBytes { Value = 1024L * 1024 * 1024 * 1024 }; + var size = new SizeInTeraBytes { Value = 1L }; + var sut = new TeraBytesToBytesConverter(); + ISize? actualResult = null; + var action = () => actualResult = sut.Convert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult.ShouldBeEquivalentTo(expectedResult); + } + } + + [Fact] + public void GivenSize_WhenCallConvert_ThenInvalidOperationExceptionThrown() + { + var sut = new TeraBytesToBytesConverter(); + var action = () => sut.Convert(null!); + action + .ShouldThrow() + .Message.ShouldBe("Could not convert TeraBytes to Bytes."); + } + + [Fact] + public void GivenSize_WhenCallCanConvert_ThenTrueReturned() + { + var size = new SizeInTeraBytes { Value = 1 }; + var sut = new TeraBytesToBytesConverter(); + bool? actualResult = null; + var action = () => actualResult = sut.CanConvert(size); + + using (new AssertionScope()) + { + action.ShouldNotThrow(); + actualResult.ShouldNotBeNull(); + actualResult?.ShouldBeTrue(); + } + } +}