Skip to content

Commit

Permalink
Add unit tests for converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 1, 2024
1 parent 66d5848 commit c9c1c2d
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.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();
}
}
}

0 comments on commit c9c1c2d

Please sign in to comment.