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

Refactor SimpleContainer tests and improve code quality #959

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/Component.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class Component : IComponent
{
public IDependency1 Dependency1 { get; set; }
public NonInterfaceDependency NonInterfaceDependency { get; set; }
}
}

11 changes: 11 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/Dependency1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Caliburn.Micro.Core.Tests.Services
{
internal class Dependency1 : IDependency1
{
public IDependency2 Dependency2 { get; set; }
public IList<IEnumerableDependency> EnumerableDependencies { get; set; }
}
}

4 changes: 4 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/Dependency2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class Dependency2 : IDependency2 { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class EnumerableDependency1 : IEnumerableDependency
{
public IDependency2 Dependency2 { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class EnumerableDependency2 : IEnumerableDependency { }

}
5 changes: 5 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/IComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal interface IComponent { }

}
4 changes: 4 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/IDependency1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal interface IDependency1 { }
}
4 changes: 4 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/IDependency2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal interface IDependency2 { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal interface IEnumerableDependency { }
}
4 changes: 4 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/ITestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal interface ITestService { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class NonInterfaceDependency { }
}
4 changes: 4 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/SecondDependency1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class SecondDependency1 : Dependency1 { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Caliburn.Micro.Core.Tests.Services
{
public class SingleEmptyConstructorType
{
public SingleEmptyConstructorType()
{

}
}
}
13 changes: 13 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/SingleIntConstructor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Caliburn.Micro.Core.Tests.Services
{
public class SingleIntConstructor
{
public int Value { get; private set; }

public SingleIntConstructor(int x)
{
this.Value = x;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Caliburn.Micro.Core.Tests.Services
{
public class SingleNonEmptyConstructorType
{
public SingleNonEmptyConstructorType(SingleEmptyConstructorType type)
{
}
}
}
5 changes: 5 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/TestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class TestService : ITestService { }
internal class AnotherTestService : ITestService { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Caliburn.Micro.Core.Tests.Services
{
internal class TestServiceWithDependency
{
public ITestService Dependency { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/Caliburn.Micro.Core.Tests/Services/TwoConstructors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Caliburn.Micro.Core.Tests.Services
{
public class TwoConstructors
{
public int Value { get; set; }

public TwoConstructors()
{
Value = 42;
}

public TwoConstructors(int value)
{
Value = value;
}
}

}
58 changes: 58 additions & 0 deletions src/Caliburn.Micro.Core.Tests/SimpleContainerCheckingForHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Caliburn.Micro.Core.Tests.Services;
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class SimpleContainerCheckingForHandler
{
[Fact]
public void HasHandlerReturnsFalseWhenHandlerDoesNotExist()
{
Assert.False(new SimpleContainer().HasHandler(typeof(object), null));
Assert.False(new SimpleContainer().HasHandler(null, "Object"));
}

[Fact]
public void HasHandlerReturnsTrueWhenHandlerExists()
{
var container = new SimpleContainer();
container.RegisterPerRequest(typeof(object), "Object", typeof(object));

Assert.True(container.HasHandler(typeof(object), null));
Assert.True(container.HasHandler(null, "Object"));
}

[Fact]
public void GetInstanceShouldReturnNullIfNoHandler()
{
var container = new SimpleContainer();

var resolvedInstance = container.GetInstance(typeof(ITestService), null);

Assert.Null(resolvedInstance);
}

[Fact]
public void HasHandlerShouldReturnTrueIfHandlerExists()
{
var container = new SimpleContainer();
var instance = new TestService();

container.RegisterInstance(typeof(ITestService), null, instance);

var hasHandler = container.HasHandler(typeof(ITestService), null);

Assert.True(hasHandler);
}

[Fact]
public void HasHandlerShouldReturnFalseIfHandlerDoesNotExist()
{
var container = new SimpleContainer();

var hasHandler = container.HasHandler(typeof(ITestService), null);

Assert.False(hasHandler);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class SimpleContainerCreatingAChildContainer
{
[Fact]
public void Singleton_instances_are_the_same_across_parent_and_child()
{
var container = new SimpleContainer();
container.Singleton<object>();
var childContainer = container.CreateChildContainer();

var parentInstance = container.GetInstance(typeof(object), null);
var childInstance = childContainer.GetInstance(typeof(object), null);

Assert.Same(parentInstance, childInstance);
}

[Fact]
public void The_child_container_returned_contains_parent_entries()
{
var container = new SimpleContainer();
container.PerRequest<object>();
var childContainer = container.CreateChildContainer();

Assert.NotNull(childContainer.GetInstance(typeof(object), null));
}

[Fact]
public void The_child_container_returned_is_not_the_same_instance_as_its_parent()
{
var container = new SimpleContainer();
var childContainer = container.CreateChildContainer();

Assert.NotSame(container, childContainer);
}



[Fact]
public void CreateChildContainer_ShouldCreateChildContainer()
{
var container = new SimpleContainer();
var childContainer = container.CreateChildContainer();

Assert.NotNull(childContainer);
Assert.NotEqual(container, childContainer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class SimpleContainerGettingASingleInstance
{
[Fact]
public void An_instance_is_returned_for_the_type_specified_if_found()
{
var container = new SimpleContainer();
container.PerRequest<object>();

Assert.NotNull(container.GetInstance(typeof(object), null));
}

[Fact]
public void Instances_can_be_found_by_name_only()
{
var container = new SimpleContainer();
container.RegisterPerRequest(typeof(object), "AnObject", typeof(object));

Assert.NotNull(container.GetInstance(null, "AnObject"));
}

[Fact]
public void Null_is_returned_when_no_instance_is_found()
{
Assert.Null(new SimpleContainer().GetInstance(typeof(object), null));
}
}
}
105 changes: 105 additions & 0 deletions src/Caliburn.Micro.Core.Tests/SimpleContainerRegisteringInstances.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Linq;
using Caliburn.Micro.Core.Tests.Services;
using Xunit;

namespace Caliburn.Micro.Core.Tests
{
public class SimpleContainerRegisteringInstances
{
[Fact]
public void RegisterInstance_ShouldRegisterInstance()
{
var container = new SimpleContainer();
var instance = new TestService();

container.RegisterInstance(typeof(ITestService), null, instance);

var resolvedInstance = container.GetInstance(typeof(ITestService), null);

Assert.NotNull(resolvedInstance);
Assert.Equal(instance, resolvedInstance);
}

[Fact]
public void RegisterPerRequest_ShouldRegisterPerRequest()
{
var container = new SimpleContainer();

container.RegisterPerRequest(typeof(ITestService), null, typeof(TestService));

var instance1 = container.GetInstance(typeof(ITestService), null);
var instance2 = container.GetInstance(typeof(ITestService), null);

Assert.NotNull(instance1);
Assert.NotNull(instance2);
Assert.NotEqual(instance1, instance2);
}

[Fact]
public void RegisterSingleton_ShouldRegisterSingleton()
{
var container = new SimpleContainer();

container.RegisterSingleton(typeof(ITestService), null, typeof(TestService));

var instance1 = container.GetInstance(typeof(ITestService), null);
var instance2 = container.GetInstance(typeof(ITestService), null);

Assert.NotNull(instance1);
Assert.NotNull(instance2);
Assert.Equal(instance1, instance2);
}

[Fact]
public void Instances_registed_Singleton_return_the_same_instance_for_each_call()
{
var container = new SimpleContainer();
container.Singleton<object>();

var instanceA = container.GetInstance(typeof(object), null);
var instanceB = container.GetInstance(typeof(object), null);

Assert.Same(instanceA, instanceB);
}

[Fact]
public void Instances_registered_PerRequest_returns_a_different_instance_for_each_call()
{
var container = new SimpleContainer();
container.PerRequest<object>();

var instanceA = container.GetInstance(typeof(object), null);
var instanceB = container.GetInstance(typeof(object), null);

Assert.NotSame(instanceA, instanceB);
}

[Fact]
public void Instances_registered_with_different_keys_get_all_instances_return_all()
{
var container = new SimpleContainer();
container.RegisterInstance(typeof(object), "test", new object());
container.RegisterInstance(typeof(object), "test", new object());

var results = container.GetAllInstances<object>("test");

Assert.Equal(2, results.Count());
}


[Fact]
public void UnregisterHandler_ShouldUnregisterHandler()
{
var container = new SimpleContainer();
var instance = new TestService();

container.RegisterInstance(typeof(ITestService), null, instance);
container.UnregisterHandler(typeof(ITestService), null);

var resolvedInstance = container.GetInstance(typeof(ITestService), null);

Assert.Null(resolvedInstance);
}

}
}
Loading
Loading