Skip to content

Commit

Permalink
🚧Refactor unit test assertions (#306)
Browse files Browse the repository at this point in the history
* Refactored file MP3SoundDecoderTests.cs to use FluentAssertions

* Refactored file OggSoundDecoderTests.cs to use FluentAssertions

* Removed unused dependencies

* Refactored file SoundDataTests.cs to use FluentAssertions

* Refactored file AudioDeviceDoesNotExistExceptionTests.cs to use FluentAssertions

* Empty commit to trigger tests execution

* Update AudioDeviceDoesNotExistExceptionTests.cs according to the suggestion

Co-authored-by: Calvin Wilkinson <[email protected]>

---------

Co-authored-by: Calvin Wilkinson <[email protected]>
  • Loading branch information
thestbar and CalvinWilkinson authored Nov 8, 2023
1 parent d3e118d commit 7341258
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 47 deletions.
24 changes: 12 additions & 12 deletions Testing/CASLTests/Data/MP3SoundDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CASLTests.Data;
using CASL.Data;
using Moq;
using Xunit;
using Assert = Helpers.AssertExtensions;
using FluentAssertions;

/// <summary>
/// Tests the <see cref="MP3SoundDecoder"/> class.
Expand All @@ -33,11 +33,11 @@ public void LoadData_WhenFileNameIsEmptyOrNull_ThrowsException(string fileName)
// Arrange
var decoder = new MP3SoundDecoder(this.mockDataStream.Object);

// Act & Assert
Assert.ThrowsWithMessage<ArgumentException>(() =>
{
decoder.LoadData(fileName);
}, "The param must not be null or empty. (Parameter 'fileName')");
// Act
var action = () => decoder.LoadData(fileName);

// Assert
action.Should().Throw<ArgumentException>().WithMessage("The param must not be null or empty. (Parameter 'fileName')");
}

[Fact]
Expand All @@ -46,11 +46,11 @@ public void LoadData_WhenUsingFileNameWithWrongFileExtension_ThrowsException()
// Arrange
var decoder = new MP3SoundDecoder(this.mockDataStream.Object);

// Act & Assert
Assert.ThrowsWithMessage<ArgumentException>(() =>
{
decoder.LoadData("sound.wav");
}, "The file name must have an mp3 file extension. (Parameter 'fileName')");
// Act
var action = () => decoder.LoadData("sound.wav");

// Assert
action.Should().Throw<ArgumentException>().WithMessage("The file name must have an mp3 file extension. (Parameter 'fileName')");
}

[Fact]
Expand Down Expand Up @@ -87,7 +87,7 @@ public unsafe void LoadData_WhenInvoked_ReturnsCorrectResult()
var actual = decoder.LoadData("sound.mp3");

// Assert
Assert.Equal(expected, actual);
actual.Should().Be(expected);
this.mockDataStream.Verify(m => m.ReadSamples(new byte[] { 10, 20 }, 0, 2), Times.Exactly(2));
}

Expand Down
34 changes: 17 additions & 17 deletions Testing/CASLTests/Data/OggSoundDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CASLTests.Data;
using CASL.Data.Exceptions;
using Moq;
using Xunit;
using Assert = Helpers.AssertExtensions;
using FluentAssertions;

/// <summary>
/// Tests the <see cref="OggSoundDecoder"/> class.
Expand All @@ -34,11 +34,11 @@ public void LoadData_WhenFileNameIsEmptyOrNull_ThrowsException(string fileName)
// Arrange
var decoder = new OggSoundDecoder(this.mockDataStream.Object);

// Act & Assert
Assert.ThrowsWithMessage<ArgumentException>(() =>
{
decoder.LoadData(fileName);
}, "The param must not be null or empty. (Parameter 'fileName')");
// Act
var action = () => decoder.LoadData(fileName);

// Assert
action.Should().Throw<ArgumentException>().WithMessage("The param must not be null or empty. (Parameter 'fileName')");
}

[Fact]
Expand All @@ -47,11 +47,11 @@ public void LoadData_WhenUsingFileNameWithWrongFileExtension_ThrowsException()
// Arrange
var decoder = new OggSoundDecoder(this.mockDataStream.Object);

// Act & Assert
Assert.ThrowsWithMessage<ArgumentException>(() =>
{
decoder.LoadData("sound.wav");
}, "The file name must have an ogg file extension. (Parameter 'fileName')");
// Act
var action = () => decoder.LoadData("sound.wav");

// Assert
action.Should().Throw<ArgumentException>().WithMessage("The file name must have an ogg file extension. (Parameter 'fileName')");
}

[Fact]
Expand All @@ -61,11 +61,11 @@ public void LoadData_WhenUsingInvalidFormat_ThrowsException()
this.mockDataStream.SetupGet(p => p.Channels).Returns(1234);
var decoder = new OggSoundDecoder(this.mockDataStream.Object);

// Act & Assert
Assert.ThrowsWithMessage<SoundDataException>(() =>
{
decoder.LoadData("sound.ogg");
}, "Only supported formats are Mono 32-bit and Stereo 32-bit.");
// Act
var action = () => decoder.LoadData("sound.ogg");

// Assert
action.Should().Throw<SoundDataException>().WithMessage("Only supported formats are Mono 32-bit and Stereo 32-bit.");
}

[Theory]
Expand Down Expand Up @@ -120,7 +120,7 @@ public unsafe void LoadData_WhenInvoked_ReturnsCorrectResult(AudioFormat format,
var actual = decoder.LoadData("sound.ogg");

// Assert
Assert.Equal(expected, actual);
actual.Should().Be(expected);
this.mockDataStream.Verify(m => m.ReadSamples(It.IsAny<float[]>(), 0, channels), Times.Exactly(2));
}

Expand Down
19 changes: 10 additions & 9 deletions Testing/CASLTests/Data/SoundDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace CASLTests.Data;
using CASL;
using CASL.Data;
using Xunit;
using FluentAssertions;

/// <summary>
/// Tests the <see cref="SoundData{T}"/> struct.
Expand All @@ -27,10 +28,10 @@ public void Ctor_WhenInvoked_SetsPropertiesToCorrectValues()
AudioFormat.Stereo16);

// Assert
Assert.Equal(new[] { 1f }, data.BufferData.ToArray());
Assert.Equal(44100, data.SampleRate);
Assert.Equal(2, data.Channels);
Assert.Equal(AudioFormat.Stereo16, data.Format);
data.BufferData.ToArray().Should().BeEquivalentTo(new[] { 1f });
data.SampleRate.Should().Be(44100);
data.Channels.Should().Be(2);
data.Format.Should().Be(AudioFormat.Stereo16);
}
#endregion

Expand Down Expand Up @@ -59,7 +60,7 @@ public void Equals_WhenInvokingOverloadedEqualsOperator_ReturnsCorrectResult()
var actual = dataA == dataB;

// Assert
Assert.True(actual);
actual.Should().BeTrue();
}

[Fact]
Expand All @@ -86,7 +87,7 @@ public void Equals_WhenInvokingOverloadedNoEqualsOperator_ReturnsCorrectResult()
var actual = dataA != dataB;

// Assert
Assert.False(actual);
actual.Should().BeFalse();
}

[Fact]
Expand All @@ -107,7 +108,7 @@ public void Equals_WhenInvokingWithObjectThatIsNotSoundDataType_ReturnsCorrectRe
var actual = dataA.Equals(dataB);

// Assert
Assert.False(actual);
actual.Should().BeFalse();
}

[Fact]
Expand All @@ -134,7 +135,7 @@ public void Equals_WhenInvokingWithObjectSoundDataType_ReturnsCorrectResult()
var actual = dataA.Equals(dataB);

// Assert
Assert.True(actual);
actual.Should().BeTrue();
}

[Theory]
Expand Down Expand Up @@ -168,7 +169,7 @@ public void Equals_WhenInvokingTypedParam_ReturnsCorrectResult(
var actual = dataA.Equals(dataB);

// Assert
Assert.Equal(expected, actual);
actual.Should().Be(expected);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace CASLTests.Devices.Exceptions;
using CASL.Devices.Exceptions;
using System;
using Xunit;
using FluentAssertions;

/// <summary>
/// Tests the <see cref="AudioDeviceDoesNotExistException"/> class.
Expand All @@ -17,45 +18,59 @@ public class AudioDeviceDoesNotExistExceptionTests
[Fact]
public void Ctor_WhenInvokedWithNoParam_CorrectlySetsMessage()
{
// Arrange
var expected = "The audio device does not exist.";

// Act
var exception = new AudioDeviceDoesNotExistException();

// Assert
Assert.Equal("The audio device does not exist.", exception.Message);
exception.Message.Should().Be(expected);
}

[Fact]
public void Ctor_WhenInvokedWithSingleMessageParam_CorrectlySetsMessage()
{
// Arrange
var expected = "test-message";

// Act
var exception = new AudioDeviceDoesNotExistException("test-message");
var exception = new AudioDeviceDoesNotExistException(expected);

// Assert
Assert.Equal("test-message", exception.Message);
exception.Message.Should().Be(expected);
}

[Fact]
public void Ctor_WhenInvokedWithMessageAndDeviceNameParams_CorrectlySetsMessage()
{
// Arrange
var expectedDeviceName = "device";
var expectedMessage = "test-message";
var expectedExceptionMessage = $"Device Name: {expectedDeviceName}\n{expectedMessage}";

// Act
var exception = new AudioDeviceDoesNotExistException("test-message", "device");
var exception = new AudioDeviceDoesNotExistException(expectedMessage, expectedDeviceName);

// Assert
Assert.Equal("Device Name: device\ntest-message", exception.Message);
exception.Message.Should().Be(expectedExceptionMessage);

}

[Fact]
public void Ctor_WhenInvokedWithMessageAndInnerException_ThrowsException()
{
// Arrange
var innerException = new Exception("inner-exception");
var expectedMessage = "test-exception";
var expectedInnerMessage = "inner-exception";
var innerException = new Exception(expectedInnerMessage);

// Act
var deviceException = new AudioDeviceDoesNotExistException("test-exception", innerException);
var deviceException = new AudioDeviceDoesNotExistException(expectedMessage, innerException);

// Assert
Assert.Equal("inner-exception", deviceException.InnerException.Message);
Assert.Equal("test-exception", deviceException.Message);
deviceException.InnerException.Message.Should().Be(expectedInnerMessage);
deviceException.Message.Should().Be(expectedMessage);
}
#endregion
}

0 comments on commit 7341258

Please sign in to comment.