-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feature: Method to Set Instance (#45)
* ✨ feature: Add Method to Set the Instance * 🚨 tests: Create Mock Predefined Types * 🚨 tests: Add Test for Set Instance * 🧹 chore: Comment Exceptions * 🧹 chore: Add Comments for Methods * 🧹 chore: Add Comments to Singleton Base * 🧹 chore: Add Comments to Singleton Factory * 🧹 chore: Comments * 🚨 tests: Add Additional Test
- Loading branch information
1 parent
7f489cd
commit e216e9a
Showing
8 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
TJC.Singleton.Tests/Mocks/Valid/MockSingletonPredefinedTypes.cs
This file contains 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,43 @@ | ||
namespace TJC.Singleton.Tests.Mocks.Valid; | ||
|
||
/// <summary> | ||
/// This singleton is an example of the proper intended use of the <see cref="SingletonBase{TMyClass}"/>. | ||
/// <para>This singleton has predefined types, similar to what may be seen within a settings singleton.</para> | ||
/// </summary> | ||
internal class MockSingletonPreDefinedTypes : SingletonBase<MockSingletonPreDefinedTypes> | ||
{ | ||
#region Constructor | ||
|
||
private MockSingletonPreDefinedTypes() { } | ||
|
||
#endregion | ||
|
||
#region Predefined Types | ||
|
||
public static MockSingletonPreDefinedTypes Default { get; } = new() { Setting1 = "Setting1", Setting2 = "Setting2" }; | ||
|
||
public static MockSingletonPreDefinedTypes Empty { get; } = new() { Setting1 = string.Empty, Setting2 = string.Empty }; | ||
|
||
public static MockSingletonPreDefinedTypes Alphabet { get; } = new() { Setting1 = "ABC", Setting2 = "DEF" }; | ||
|
||
public static MockSingletonPreDefinedTypes Numbers { get; } = new() { Setting1 = "123", Setting2 = "456" }; | ||
|
||
public static MockSingletonPreDefinedTypes Symbols { get; } = new() { Setting1 = "!@#", Setting2 = "$%^" }; | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public string Setting1 { get; private set; } = "Setting1"; | ||
|
||
public string Setting2 { get; private set; } = "Setting2"; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
public static void SetInstance(MockSingletonPreDefinedTypes value) => | ||
SetBaseInstance(value); | ||
|
||
#endregion | ||
} |
This file contains 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,34 @@ | ||
namespace TJC.Singleton.Tests.Tests.SetInstance; | ||
|
||
[TestClass] | ||
public class SetInstanceTests | ||
{ | ||
[TestMethod] | ||
public void SetInstance_PredefinedTypes() | ||
{ | ||
MockSingletonPreDefinedTypes.SetInstance(MockSingletonPreDefinedTypes.Default); | ||
|
||
Assert.AreEqual("Setting1", MockSingletonPreDefinedTypes.Instance.Setting1); | ||
Assert.AreEqual("Setting2", MockSingletonPreDefinedTypes.Instance.Setting2); | ||
|
||
MockSingletonPreDefinedTypes.SetInstance(MockSingletonPreDefinedTypes.Empty); | ||
|
||
Assert.AreEqual(string.Empty, MockSingletonPreDefinedTypes.Instance.Setting1); | ||
Assert.AreEqual(string.Empty, MockSingletonPreDefinedTypes.Instance.Setting2); | ||
|
||
MockSingletonPreDefinedTypes.SetInstance(MockSingletonPreDefinedTypes.Alphabet); | ||
|
||
Assert.AreEqual("ABC", MockSingletonPreDefinedTypes.Instance.Setting1); | ||
Assert.AreEqual("DEF", MockSingletonPreDefinedTypes.Instance.Setting2); | ||
|
||
MockSingletonPreDefinedTypes.SetInstance(MockSingletonPreDefinedTypes.Numbers); | ||
|
||
Assert.AreEqual("123", MockSingletonPreDefinedTypes.Instance.Setting1); | ||
Assert.AreEqual("456", MockSingletonPreDefinedTypes.Instance.Setting2); | ||
|
||
MockSingletonPreDefinedTypes.SetInstance(MockSingletonPreDefinedTypes.Symbols); | ||
|
||
Assert.AreEqual("!@#", MockSingletonPreDefinedTypes.Instance.Setting1); | ||
Assert.AreEqual("$%^", MockSingletonPreDefinedTypes.Instance.Setting2); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
TJC.Singleton/Exceptions/InvalidSingletonConstructorException.cs
This file contains 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
namespace TJC.Singleton.Exceptions; | ||
|
||
/// <summary> | ||
/// This exception is thrown when a singleton class has an invalid constructor. | ||
/// </summary> | ||
/// <param name="message"></param> | ||
public class InvalidSingletonConstructorException(string? message) : Exception(message); |
This file contains 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
namespace TJC.Singleton.Exceptions; | ||
|
||
/// <summary> | ||
/// This exception is thrown when a singleton fails to initialize. | ||
/// </summary> | ||
/// <param name="message"></param> | ||
public class SingletonInitializationException(string? message) : Exception(message); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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