Skip to content

Commit

Permalink
test: refactor line shader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinWilkinson committed Nov 13, 2024
1 parent 6ded2b3 commit a777c58
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions Testing/VelaptorTests/OpenGL/Shaders/LineShaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace VelaptorTests.OpenGL.Shaders;
using Carbonate.NonDirectional;
using Carbonate.OneWay;
using FluentAssertions;
using Moq;
using NSubstitute;
using Velaptor;
using Velaptor.Factories;
using Velaptor.NativeInterop.OpenGL;
Expand All @@ -23,41 +23,43 @@ namespace VelaptorTests.OpenGL.Shaders;

public class LineShaderTests
{
private readonly Mock<IGLInvoker> mockGL;
private readonly Mock<IOpenGLService> mockGLService;
private readonly Mock<IShaderLoaderService> mockShaderLoader;
private readonly Mock<IReactableFactory> mockReactableFactory;
private readonly Mock<IPushReactable<BatchSizeData>> mockBatchSizeReactable;
private readonly Mock<IDisposable> batchSizeUnsubscriber;
private readonly IGLInvoker mockGL;
private readonly IOpenGLService mockGLService;
private readonly IShaderLoaderService mockShaderLoader;
private readonly IReactableFactory mockReactableFactory;
private readonly IPushReactable<BatchSizeData> mockBatchSizeReactable;
private readonly IDisposable batchSizeUnsubscriber;
private IReceiveSubscription<BatchSizeData>? batchSizeReactor;

/// <summary>
/// Initializes a new instance of the <see cref="LineShaderTests"/> class.
/// </summary>
public LineShaderTests()
{
this.mockGL = new Mock<IGLInvoker>();
this.mockGLService = new Mock<IOpenGLService>();
this.mockShaderLoader = new Mock<IShaderLoaderService>();
this.mockGL = Substitute.For<IGLInvoker>();
this.mockGLService = Substitute.For<IOpenGLService>();
this.mockShaderLoader = Substitute.For<IShaderLoaderService>();

this.batchSizeUnsubscriber = new Mock<IDisposable>();
this.batchSizeUnsubscriber = Substitute.For<IDisposable>();

var mockPushReactable = new Mock<IPushReactable>();
mockPushReactable.Setup(m => m.Subscribe(It.IsAny<IReceiveSubscription>()))
.Returns<IReceiveSubscription>(_ => new Mock<IDisposable>().Object);
var mockPushReactable = Substitute.For<IPushReactable>();
mockPushReactable.Subscribe(Arg.Any<IReceiveSubscription>())
.Returns(_ => Substitute.For<IDisposable>());

this.mockBatchSizeReactable = new Mock<IPushReactable<BatchSizeData>>();
this.mockBatchSizeReactable.Setup(m => m.Subscribe(It.IsAny<IReceiveSubscription<BatchSizeData>>()))
.Returns(() => this.batchSizeUnsubscriber.Object)
.Callback<IReceiveSubscription<BatchSizeData>>(reactor =>
this.mockBatchSizeReactable = Substitute.For<IPushReactable<BatchSizeData>>();
this.mockBatchSizeReactable.Subscribe(Arg.Any<IReceiveSubscription<BatchSizeData>>())
.Returns(_ => this.batchSizeUnsubscriber);
this.mockBatchSizeReactable.When(x => x.Subscribe(Arg.Any<IReceiveSubscription<BatchSizeData>>()))
.Do(callInfo =>
{
var reactor = callInfo.Arg<IReceiveSubscription<BatchSizeData>>();
reactor.Should().NotBeNull("It is required for unit testing.");
this.batchSizeReactor = reactor;
});

this.mockReactableFactory = new Mock<IReactableFactory>();
this.mockReactableFactory.Setup(m => m.CreateNoDataPushReactable()).Returns(mockPushReactable.Object);
this.mockReactableFactory.Setup(m => m.CreateBatchSizeReactable()).Returns(this.mockBatchSizeReactable.Object);
this.mockReactableFactory = Substitute.For<IReactableFactory>();
this.mockReactableFactory.CreateNoDataPushReactable().Returns(mockPushReactable);
this.mockReactableFactory.CreateBatchSizeReactable().Returns(this.mockBatchSizeReactable);
}

#region Constructor Tests
Expand All @@ -68,9 +70,9 @@ public void Ctor_WithNullReactableFactoryParam_ThrowsException()
var act = () =>
{
_ = new LineShader(
new Mock<IGLInvoker>().Object,
new Mock<IOpenGLService>().Object,
new Mock<IShaderLoaderService>().Object,
Substitute.For<IGLInvoker>(),
Substitute.For<IOpenGLService>(),
Substitute.For<IShaderLoaderService>(),
null);
};

Expand Down Expand Up @@ -119,9 +121,10 @@ public void BatchSizeReactable_WhenReceivingBatchSizeNotification_SetsBatchSize(
public void BatchSizeReactable_WhenCreatingSubscription_CreatesSubscriptionCorrectly()
{
// Arrange & Act & Assert
this.mockBatchSizeReactable.Setup(m => m.Subscribe(It.IsAny<IReceiveSubscription<BatchSizeData>>()))
.Callback<IReceiveSubscription<BatchSizeData>>(reactor =>
this.mockBatchSizeReactable.When(x => x.Subscribe(Arg.Any<IReceiveSubscription<BatchSizeData>>()))
.Do(callInfo =>
{
var reactor = callInfo.Arg<IReceiveSubscription<BatchSizeData>>();
reactor.Should().NotBeNull("It is required for unit testing.");
this.batchSizeReactor = reactor;
reactor.Name.Should().Be($"LineShader.ctor() - {PushNotifications.BatchSizeChangedId}");
Expand All @@ -140,7 +143,7 @@ public void BatchSizeReactable_WhenUnsubscribingGlInit_Unsubscribes()
this.batchSizeReactor.OnUnsubscribe();

// Assert
this.batchSizeUnsubscriber.Verify(m => m.Dispose(), Times.Once);
this.batchSizeUnsubscriber.Received(1).Dispose();
}
#endregion

Expand All @@ -149,8 +152,8 @@ public void BatchSizeReactable_WhenUnsubscribingGlInit_Unsubscribes()
/// </summary>
/// <returns>The instance to test.</returns>
private LineShader CreateSystemUnderTest()
=> new (this.mockGL.Object,
this.mockGLService.Object,
this.mockShaderLoader.Object,
this.mockReactableFactory.Object);
=> new (this.mockGL,
this.mockGLService,
this.mockShaderLoader,
this.mockReactableFactory);
}

0 comments on commit a777c58

Please sign in to comment.