Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced dependency container #232

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using MultiplayerMod.Core.Dependency;
using NUnit.Framework;

namespace MultiplayerMod.Test.Core.Dependency;

[TestFixture]
public class DependencyContainerBuilderTests {

[Test]
public void ContainerReferencesAvailable() {
var container = new DependencyContainerBuilder().Build();
var containerByInterface = container.Get<IDependencyContainer>();
var injector = container.Get<IDependencyInjector>();
var self = container.Get<DependencyContainer>();
Assert.AreSame(expected: container, actual: self);
Assert.AreSame(expected: container, actual: containerByInterface);
Assert.AreSame(expected: container, actual: injector);
}

}
183 changes: 109 additions & 74 deletions src/MultiplayerMod.Test/Core/Dependency/DependencyContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,143 +1,178 @@
using MultiplayerMod.Core.Dependency;
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using MultiplayerMod.Core.Dependency;
using NUnit.Framework;

// ReSharper disable ClassNeverInstantiated.Global

namespace MultiplayerMod.Test.Core.Dependency;

[TestFixture]
[Parallelizable]
public class DependencyContainerTests {

[Test]
public void SelfReferenceAvailable() {
public void ResolvesSimpleDependencies() {
var container = new DependencyContainer();
var self = container.Resolve<DependencyContainer>();
Assert.AreSame(expected: container, actual: self);
container.Register(CreateInfo<DependencyA>());
var instance = container.Get<DependencyA>();
Assert.AreSame(expected: instance, actual: container.Get<IDependencyA>());
Assert.AreSame(expected: instance, actual: container.Get<IDependencyAb>());
}

[Test]
public void SimpleInstantiation() {
public void ResolvesListOfDependencies() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Resolve<ISimpleInterface>();
Assert.NotNull(instance);
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyB>());
var dependencyAb = container.Get<List<IDependencyAb>>();
Assert.AreEqual(expected: 2, actual: dependencyAb.Count);
Assert.Contains(container.Get<DependencyA>(), dependencyAb);
Assert.Contains(container.Get<DependencyB>(), dependencyAb);
}

[Test]
public void DependencyIsAvailableAsClass() {
public void InstantiateWithDependencies() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: container.Resolve<SimpleClass>());
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyB>());
container.Register(CreateInfo<DependencyInConstructor>());
var dependencyInConstructor = container.Get<DependencyInConstructor>();
Assert.AreSame(expected: container.Get<DependencyA>(), actual: dependencyInConstructor.AInstance);
Assert.AreSame(expected: container.Get<DependencyB>(), actual: dependencyInConstructor.BInstance);
}

[Test]
public void DependencyRegisteredAsClass() {
public void InjectDependenciesIntoFields() {
var container = new DependencyContainer();
container.Register<SimpleClass>();
Assert.NotNull(container.Resolve<SimpleClass>());
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyB>());
var instance = container.Inject(new DependencyInFields());
Assert.AreSame(expected: container.Get<IDependencyA>(), actual: instance.AInstance);
Assert.AreSame(expected: container.Get<IDependencyB>(), actual: instance.BInstance);
}

[Test]
public void InstantiationWithDependenciesInConstructor() {
public void InjectDependenciesIntoProperties() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Resolve<ClassWithConstructorDependencies>();
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Interface);
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyB>());
var instance = container.Inject(new DependencyInProperties());
Assert.AreSame(expected: container.Get<IDependencyA>(), actual: instance.AInstance);
Assert.AreSame(expected: container.Get<IDependencyB>(), actual: instance.BInstance);
}

[Test]
public void InstantiationWithDependenciesInProperties() {
public void RegistersSingleton() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Resolve<ClassWithPropertyDependencies>();
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Interface);
container.RegisterSingleton("Hello");
Assert.AreSame(expected: "Hello", actual: container.Get<string>());
}

[Test]
public void InstantiationWithDependenciesInFields() {
public void MustFailOnMissingDependency() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Resolve<ClassWithFieldDependencies>();
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Interface);
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyInConstructor>());
try {
container.Get<DependencyInConstructor>();
} catch (DependencyInstantiationException exception) {
Assert.AreSame(expected: typeof(MissingDependencyException), actual: UnwindCause(exception).GetType());
}
}

[Test]
public void NestedDependencies() {
public void MustFailOnAlreadyRegisteredDependency() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
container.Register<INestedDependenciesInterface, NestedDependenciesClass>();
var instance = container.Resolve<ClassWithComplexDependencies>();
var nested = (NestedDependenciesClass) instance.Nested;
Assert.AreSame(expected: container.Resolve<INestedDependenciesInterface>(), actual: instance.Nested);
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Simple);
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: nested.Interface);
container.Register(CreateInfo<DependencyA>());
Assert.Throws<DependencyAlreadyRegisteredException>(() => container.Register(CreateInfo<DependencyA>()));
}

[Test]
public void NonSingletonInstantiation() {
public void MustFailOnCircularDependency() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Resolve<ClassWithConstructorDependencies>();
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Interface);
Assert.Throws(typeof(MissingDependencyException), () => container.Get<ClassWithConstructorDependencies>());
container.Register(CreateInfo<CircularDependencyA>());
container.Register(CreateInfo<CircularDependencyB>());
try {
container.Get<CircularDependencyB>();
} catch (DependencyInstantiationException exception) {
Assert.AreSame(
expected: typeof(DependencyIsInstantiatingException),
actual: UnwindCause(exception).GetType()
);
}
}

[Test]
public void InjectDependencies() {
public void MustFailOnAmbiguousDependency() {
var container = new DependencyContainer();
container.Register<ISimpleInterface, SimpleClass>();
var instance = container.Inject(new ClassWithFieldDependencies());
Assert.Throws(typeof(MissingDependencyException), () => container.Get<ClassWithFieldDependencies>());
Assert.AreSame(expected: container.Resolve<ISimpleInterface>(), actual: instance.Interface);
container.Register(CreateInfo<DependencyA>());
container.Register(CreateInfo<DependencyB>());
Assert.Throws<AmbiguousDependencyException>(() => container.Get<IDependencyAb>());
}

public interface ISimpleInterface { }
private static DependencyInfo CreateInfo<T>() => new(typeof(T).FullName!, typeof(T), false);

public class SimpleClass : ISimpleInterface { }
private static Exception UnwindCause(Exception exception) {
var current = exception;
while (current.InnerException != null)
current = current.InnerException;
return current;
}

[UsedImplicitly]
public class DependencyA : IDependencyA, IDependencyAb { }

public interface INestedDependenciesInterface { }
[UsedImplicitly]
public class DependencyB : IDependencyB, IDependencyAb { }

public class NestedDependenciesClass : INestedDependenciesInterface {
public ISimpleInterface Interface { get; }
[UsedImplicitly]
public class DependencyInConstructor {
public readonly IDependencyA AInstance;
public readonly IDependencyB BInstance;

public NestedDependenciesClass(ISimpleInterface @interface) {
Interface = @interface;
public DependencyInConstructor(IDependencyA dependencyA, IDependencyB dependencyB) {
AInstance = dependencyA;
BInstance = dependencyB;
}
}

public class ClassWithConstructorDependencies {
public ISimpleInterface? Interface { get; }
[UsedImplicitly]
public class CircularDependencyA {
public CircularDependencyA(CircularDependencyB _) { }
}

public ClassWithConstructorDependencies(ISimpleInterface @interface) {
Interface = @interface;
}
[UsedImplicitly]
public class CircularDependencyB {
public CircularDependencyB(CircularDependencyA _) { }
}

public class ClassWithPropertyDependencies {
[UsedImplicitly]
public class DependencyInFields {

[InjectDependency]
public readonly IDependencyA AInstance = null!;

// ReSharper disable once UnusedAutoPropertyAccessor.Global
[Dependency] public ISimpleInterface? Interface { get; set; }
[InjectDependency]
public readonly IDependencyB BInstance = null!;

}

public class ClassWithFieldDependencies {
[UsedImplicitly]
public class DependencyInProperties {

// ReSharper disable once UnassignedField.Global
[Dependency] public ISimpleInterface? Interface;
[UsedImplicitly]
[InjectDependency]
public IDependencyA AInstance { get; set; } = null!;

}
[UsedImplicitly]
[InjectDependency]
public IDependencyB BInstance { get; set; } = null!;

public class ClassWithComplexDependencies {
}

public ISimpleInterface Simple;
public INestedDependenciesInterface Nested;
public interface IDependencyA { }

public ClassWithComplexDependencies(ISimpleInterface simple, INestedDependenciesInterface nested) {
Simple = simple;
Nested = nested;
}
public interface IDependencyB { }

}
public interface IDependencyAb { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class TestMultiplayerClient : IMultiplayerClient {

public bool EnablePendingActions { get; set; }

public TestMultiplayerClient(TestMultiplayerClientId id) {
public TestMultiplayerClient(TestMultiplayerClientId id, TestRuntime runtime) {
this.runtime = runtime;
Id = id;
runtime = (TestRuntime) Runtime.Instance;
id.Client = this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class TestMultiplayerServer : IMultiplayerServer {

public bool EnablePendingActions { get; set; }

public TestMultiplayerServer(TestMultiplayerClientId identity) {
public TestMultiplayerServer(TestMultiplayerClientId identity, TestRuntime runtime) {
Endpoint = new TestMultiplayerEndpoint(this);
runtime = (TestRuntime) Runtime.Instance;
currentPlayer = identity;
this.runtime = runtime;
}

public void Start() {
Expand Down
3 changes: 3 additions & 0 deletions src/MultiplayerMod.Test/Environment/TestRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.ModRuntime;

namespace MultiplayerMod.Test.Environment;
Expand All @@ -9,6 +10,8 @@ public class TestRuntime : Runtime {
public event Action<Runtime>? Deactivated;
public event Action<Runtime>? Activated;

public TestRuntime(DependencyContainer container) : base(container) { }

public void Activate() {
var oldRuntime = Instance;
var property = typeof(Runtime).GetProperty("Instance", BindingFlags.Static | BindingFlags.Public)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public void ManualGuardedOverride() {
Assert.AreEqual(expected: ExecutionLevel.System, actual: manager.Context.Level);
}

private static DependencyContainer CreateContainer() {
var container = new DependencyContainer();
container.Register<ExecutionContextManager>();
container.Register<ExecutionLevelManager>();
return container;
}
private static DependencyContainer CreateContainer() => new DependencyContainerBuilder()
.AddType<ExecutionContextManager>()
.AddType<ExecutionLevelManager>()
.Build();

}
Loading