-
Notifications
You must be signed in to change notification settings - Fork 772
Refactor SimpleContainer tests and improve code quality #959
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
Open
vb2ae
wants to merge
10
commits into
master
Choose a base branch
from
958-fix-code-scanning-alert-missed-opportunity-to-use-where
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a4db143
Refactor SimpleContainer tests and improve code quality
vb2ae 12518e1
Refactor test method names for consistency
vb2ae f0ff704
fix typo
vb2ae 4f4cda3
Update SimpleContainerRegisteringInstances.cs
vb2ae 23c0656
Update Caliburn.Micro.Platform.Core.csproj
vb2ae 9b49e30
Update Caliburn.Micro.Platform.Core.csproj
vb2ae 8130ee7
Update dependabot.yml
vb2ae fe17adc
Update Caliburn.Micro.Platform.Core.csproj
vb2ae 27ed2a1
update test names
vb2ae 2bca384
Update Caliburn.Micro.Platform.Core.csproj
vb2ae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal class Dependency2 : IDependency2 { } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Caliburn.Micro.Core.Tests/Services/EnumerableDependency1.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
src/Caliburn.Micro.Core.Tests/Services/EnumerableDependency2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal class EnumerableDependency2 : IEnumerableDependency { } | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal interface IComponent { } | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal interface IDependency1 { } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal interface IDependency2 { } | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Caliburn.Micro.Core.Tests/Services/IEnumerableDependency.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal interface IEnumerableDependency { } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal interface ITestService { } | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Caliburn.Micro.Core.Tests/Services/NonInterfaceDependency.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal class NonInterfaceDependency { } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Caliburn.Micro.Core.Tests.Services | ||
{ | ||
internal class SecondDependency1 : Dependency1 { } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Caliburn.Micro.Core.Tests/Services/SingleEmptyConstructorType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/Caliburn.Micro.Core.Tests/Services/SingleIntConstructor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/Caliburn.Micro.Core.Tests/Services/SingleNonEmptyConstructorType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Caliburn.Micro.Core.Tests/Services/TestServiceWithDependency.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
src/Caliburn.Micro.Core.Tests/SimpleContainerCheckingForHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 HasHandlerReturnsRalseWhenHandlerDoesNotExist() | ||
{ | ||
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); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Caliburn.Micro.Core.Tests/SimpleContainerCreatingAChildContainer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Caliburn.Micro.Core.Tests/SimpleContainerGettingASingleInstance.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
src/Caliburn.Micro.Core.Tests/SimpleContainerRegisteringInstances.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.