Skip to content

Commit

Permalink
fix(module-loader) - Using FluentAssertions in ModuleLoaderTests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilla28 committed Apr 16, 2024
1 parent e0b182e commit 4ea883c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions
// and limitations under the License.

using FluentAssertions;
using Moq;

namespace MorganStanley.ComposeUI.ModuleLoader.Abstractions.Tests;
Expand All @@ -25,20 +26,21 @@ public void WhenAdd_AddedValuesCanBeRetrieved()
new MyContextInfo { Name = "Test2" }
};

StartupContext context = new StartupContext(new StartRequest("test"), Mock.Of<IModuleInstance>());
var context = new StartupContext(new StartRequest("test"), Mock.Of<IModuleInstance>());
context.AddProperty(expected[0]);
context.AddProperty(expected[1]);

var result = context.GetProperties();
Assert.NotNull(result);
Assert.Equal(expected, result);
result.Should().NotBeNull();
result.Should().BeEquivalentTo(expected);
}

[Fact]
public void GivenNullArgument_WhenAdd_ThrowsArgumentNullException()
{
StartupContext context = new StartupContext(new StartRequest("test"), Mock.Of<IModuleInstance>());
Assert.Throws<ArgumentNullException>(() => context.AddProperty<object>(null!));
var context = new StartupContext(new StartRequest("test"), Mock.Of<IModuleInstance>());
var action = () => context.AddProperty<object>(null!);
action.Should().Throw<ArgumentNullException>();
}

private class MyContextInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions
// and limitations under the License.

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using MorganStanley.ComposeUI.ModuleLoader.Runners;

Expand All @@ -23,8 +24,8 @@ public void WhenAddModuleLoader_TypesAreAddedToServiceCollection()
var services = new ServiceCollection()
.AddModuleLoader();

Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleLoader) && s.ImplementationType == typeof(ModuleLoader)));
Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(WebModuleRunner)));
Assert.Equal(1, services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(NativeModuleRunner)));
services.Count(s => s.ServiceType == typeof(IModuleLoader) && s.ImplementationType == typeof(ModuleLoader)).Should().Be(1);
services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(WebModuleRunner)).Should().Be(1);
services.Count(s => s.ServiceType == typeof(IModuleRunner) && s.ImplementationType == typeof(NativeModuleRunner)).Should().Be(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// and limitations under the License.


using FluentAssertions;
using Moq;

namespace MorganStanley.ComposeUI.ModuleLoader.Tests;
Expand All @@ -22,17 +23,19 @@ public class ModuleInstanceTests
[Fact]
public void GivenNullArguments_WhenCtor_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new ModuleInstance(_testGuid, null!, new StartRequest("test")));
Assert.Throws<ArgumentNullException>(() => new ModuleInstance(_testGuid, new Mock<IModuleManifest>().Object, null!));
var action1 = () => new ModuleInstance(_testGuid, null!, new StartRequest("test"));
action1.Should().Throw<ArgumentNullException>();

var action2 = () => new ModuleInstance(_testGuid, new Mock<IModuleManifest>().Object, null!);
action2.Should().Throw<ArgumentNullException>();
}

[Fact]
public void WhenNewModuleInstance_GetProperties_ReturnsEmptyCollection()
{
var moduleInstance = new ModuleInstance(_testGuid, new Mock<IModuleManifest>().Object, new StartRequest("test"));

var properties = moduleInstance.GetProperties();
Assert.Empty(properties);
properties.Should().BeEmpty();
}

[Fact]
Expand All @@ -44,6 +47,6 @@ public void WhenSetProperties_GetPropertiesReturnThePropertiesAdded()
moduleInstance.AddProperties(testProperties);
var properties = moduleInstance.GetProperties();

Assert.Equal(testProperties, properties);
testProperties.Should().BeEquivalentTo(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions
// and limitations under the License.

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;

Expand All @@ -20,23 +21,28 @@ public class ModuleLoaderTests
[Fact]
public void GivenNullArguments_WhenCtor_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new ModuleLoader(null!, Enumerable.Empty<IModuleRunner>(), Enumerable.Empty<IStartupAction>()));
Assert.Throws<ArgumentNullException>(() => new ModuleLoader(new Mock<IEnumerable<IModuleCatalog>>().Object, null!, Enumerable.Empty<IStartupAction>()));
Assert.Throws<ArgumentNullException>(() => new ModuleLoader(new Mock<IEnumerable<IModuleCatalog>>().Object, Enumerable.Empty<IModuleRunner>(), null!));
var action1 = () => new ModuleLoader(null!, Enumerable.Empty<IModuleRunner>(), Enumerable.Empty<IStartupAction>());
var action2 = () => new ModuleLoader(new Mock<IEnumerable<IModuleCatalog>>().Object, null!, Enumerable.Empty<IStartupAction>());
var action3 = () => new ModuleLoader(new Mock<IEnumerable<IModuleCatalog>>().Object, Enumerable.Empty<IModuleRunner>(), null!);

action1.Should().Throw<ArgumentNullException>();
action2.Should().Throw<ArgumentNullException>();
action3.Should().Throw<ArgumentNullException>();
}

[Fact]
public void GivenUnknownModuleId_WhenStart_ThrowsException()
public async void GivenUnknownModuleId_WhenStart_ThrowsException()
{
var moduleCatalogMock = new Mock<IModuleCatalog>();
moduleCatalogMock.Setup(c => c.GetManifest(It.IsAny<string>())).Returns(Task.FromResult<IModuleManifest?>(null));

var moduleLoader = new ModuleLoader(new[] { moduleCatalogMock.Object }, Enumerable.Empty<IModuleRunner>(), Enumerable.Empty<IStartupAction>());
Assert.ThrowsAsync<Exception>(async () => await moduleLoader.StartModule(new StartRequest("invalid")));
var action = () => moduleLoader.StartModule(new StartRequest("invalid"));
await action.Should().ThrowAsync<Exception>();
}

[Fact]
public void WhenNoModuleRunnerAvailable_WhenStart_ThrowsException()
public async Task WhenNoModuleRunnerAvailable_WhenStart_ThrowsException()
{
var moduleManifestMock = new Mock<IModuleManifest>();
moduleManifestMock.Setup(m => m.ModuleType).Returns("test");
Expand All @@ -46,7 +52,8 @@ public void WhenNoModuleRunnerAvailable_WhenStart_ThrowsException()
testModuleRunnerMock.Setup(r => r.ModuleType).Returns("other");

var moduleLoader = new ModuleLoader(new[] { moduleCatalogMock.Object }, new[] { testModuleRunnerMock.Object }, Enumerable.Empty<IStartupAction>());
Assert.ThrowsAsync<Exception>(async () => await moduleLoader.StartModule(new StartRequest("valid")));
var action = () => moduleLoader.StartModule(new StartRequest("valid"));
await action.Should().ThrowAsync<Exception>();
}

[Fact]
Expand Down Expand Up @@ -77,13 +84,13 @@ public async Task StartModule_EndToEndTest()

var serviceProvider = new ServiceCollection()
.AddModuleLoader()
.AddSingleton<IModuleCatalog>(moduleCatalogMock.Object)
.AddSingleton<IEnumerable<IModuleCatalog>>(new[] { moduleCatalogMock.Object })
.AddSingleton<IStartupAction>(startupActionMock.Object)
.BuildServiceProvider();

var moduleLoader = serviceProvider.GetRequiredService<IModuleLoader>();
bool startingEventReceived = false;
bool startedEventReceived = false;
var startingEventReceived = false;
var startedEventReceived = false;
moduleLoader.LifetimeEvents.Subscribe(x =>
{
if (x.EventType == LifetimeEventType.Starting)
Expand All @@ -95,20 +102,19 @@ public async Task StartModule_EndToEndTest()

var startRequest = new StartRequest(moduleId);
var moduleInstance = await moduleLoader.StartModule(startRequest);
Assert.NotNull(moduleInstance);
Assert.Equal(moduleManifestMock.Object, moduleInstance.Manifest);
Assert.Equal(startRequest, moduleInstance.StartRequest);
moduleInstance.Should().NotBeNull();
moduleManifestMock.Object.Should().BeEquivalentTo(moduleInstance.Manifest);
startRequest.Should().BeEquivalentTo(moduleInstance.StartRequest);

var allProperties = moduleInstance.GetProperties();

var webProperties = allProperties.OfType<WebStartupProperties>().Single();
Assert.Equal(webManifestDetails.IconUrl, webProperties.IconUrl);
Assert.Equal(webManifestDetails.Url, webProperties.Url);
webManifestDetails.IconUrl.Should().BeEquivalentTo(webProperties.IconUrl);
webManifestDetails.Url.Should().BeEquivalentTo(webProperties.Url);

var stringProperty = allProperties.OfType<string>().Single();
Assert.Equal(testProperty, stringProperty);

Assert.True(startingEventReceived);
Assert.True(startedEventReceived);
testProperty.Should().BeEquivalentTo(stringProperty);
startingEventReceived.Should().BeTrue();
startedEventReceived.Should().BeTrue();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// and limitations under the License.

using System.Diagnostics;
using FluentAssertions;
using FluentAssertions.Execution;
using Moq;
using MorganStanley.ComposeUI.ModuleLoader.Runners;
using MorganStanley.ComposeUI.ModuleLoader.Tests.TestUtils;
Expand All @@ -26,7 +28,7 @@ public class NativeModuleRunnerTests : IDisposable
[Fact]
public void ModuleType_is_Native()
{
Assert.Equal(ModuleType.Native, _runner.ModuleType);
_runner.ModuleType.Should().Be(ModuleType.Native);
}

[Fact]
Expand All @@ -53,13 +55,13 @@ public async Task ModuleIsLaunchedWithProvidedArguments()

var processInfo = startupContext.GetOrAddProperty<MainProcessInfo>(() =>
{
Assert.Fail("No MainProcessInfo found");
Execute.Assertion.FailWith("No MainProcessInfo found");
return null;
});

_mainProcess = processInfo.MainProcess;
var result = await _mainProcess.WaitForExitAsync(Timeout);
Assert.Equal($"Hello ComposeUI! I am {randomString}", result.Output.Trim());
result.Output.Trim().Should().Be($"Hello ComposeUI! I am {randomString}");
}

[Fact]
Expand All @@ -86,13 +88,13 @@ public async Task AbsolutePathModuleIsLaunchedWithEnvironmentVariablesFromManife

var processInfo = startupContext.GetOrAddProperty<MainProcessInfo>(() =>
{
Assert.Fail("No MainProcessInfo found");
Execute.Assertion.FailWith("No MainProcessInfo found");
return null;
});

_mainProcess = processInfo.MainProcess;
var result = await _mainProcess.WaitForExitAsync(Timeout);
Assert.Contains($"{variableName}={randomString}", result.Output);
result.Output.Should().Contain($"{variableName}={randomString}");
}

[Fact]
Expand All @@ -119,13 +121,13 @@ public async Task RelativePathModuleIsLaunchedWithEnvironmentVariablesFromManife

var processInfo = startupContext.GetOrAddProperty<MainProcessInfo>(() =>
{
Assert.Fail("No MainProcessInfo found");
Execute.Assertion.FailWith("No MainProcessInfo found");
return null;
});

_mainProcess = processInfo.MainProcess;
var result = await _mainProcess.WaitForExitAsync(Timeout);
Assert.Contains($"{variableName}={randomString}", result.Output);
result.Output.Should().Contain($"{variableName}={randomString}");
}

[Fact]
Expand Down Expand Up @@ -156,13 +158,13 @@ await _runner.Start(startupContext, () =>

var processInfo = startupContext.GetOrAddProperty<MainProcessInfo>(() =>
{
Assert.Fail("No MainProcessInfo found");
Execute.Assertion.FailWith("No MainProcessInfo found");
return null;
});

_mainProcess = processInfo.MainProcess;
var result = await _mainProcess.WaitForExitAsync(Timeout);
Assert.Contains($"{variableName}={randomString}", result.Output);
result.Output.Should().Contain($"{variableName}={randomString}");
}

private Task RedirectMainProcessOutput(StartupContext startupContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// and limitations under the License.


using FluentAssertions;
using Moq;
using MorganStanley.ComposeUI.ModuleLoader.Runners;

Expand All @@ -22,7 +23,7 @@ public class WebModuleRunnerTests
public void ModuleType_Is_Web()
{
var runner = new WebModuleRunner();
Assert.Equal(ModuleType.Web, runner.ModuleType);
runner.ModuleType.Should().Be(ModuleType.Web);
}

[Fact]
Expand All @@ -40,10 +41,10 @@ public async Task WhenStart_WebStartupPropertiesAreAddedToStartupContext()
await runner.Start(startupContext, MockPipeline);

var result = startupContext.GetProperties();
Assert.NotNull(result);
result.Should().NotBeNull();
var webProperties = result.OfType<WebStartupProperties>().Single();
Assert.Equal(details.IconUrl, webProperties.IconUrl);
Assert.Equal(details.Url, webProperties.Url);
details.IconUrl.Should().BeEquivalentTo(webProperties.IconUrl);
details.Url.Should().BeEquivalentTo(webProperties.Url);
}

private class MockModuleManifest : IModuleManifest<WebManifestDetails>
Expand Down

0 comments on commit 4ea883c

Please sign in to comment.