-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWeakSingletonTests.cs
More file actions
74 lines (55 loc) · 1.45 KB
/
WeakSingletonTests.cs
File metadata and controls
74 lines (55 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using NUnit.Framework;
namespace Simplify.System.Tests;
[TestFixture]
public class WeakSingletonTests
{
[Test]
public void GetInstance_ExplicitTypeBuilder_CreatesInstanceWithTypeBuilder()
{
// Arrange
var singleton = new WeakSingleton<string>(() => new string("Hello"));
string str = null!;
// Act & Assert
Assert.That(() => str = singleton.Instance, Throws.Nothing);
Assert.That(str, Is.Not.Null);
Assert.That(str, Is.EqualTo("Hello"));
}
[Test]
public void GetInstance_NullTypeBuilder_CreatesInstanceWithDefaultConstructor()
{
// Arrange
var singleton = new WeakSingleton<object>();
object obj = null!;
// Act & Assert
Assert.That(() => obj = singleton.Instance, Throws.Nothing);
Assert.That(obj, Is.Not.Null);
}
#if RELEASE
[Test]
public void GetInstanceMultipleTimes_GcCollectBetween_CreatesDifferentInstances()
{
// Arrange
var singleton = new WeakSingleton<TestClass>();
var obj = singleton.Instance;
obj.Value = "Some string";
// Set strong reference to null and allow GC to collect unreferenced object
obj = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
obj = singleton.Instance;
// Act & Assert
Assert.That(obj, Is.Not.Null);
Assert.That(obj.Value, Is.EqualTo("Default"));
}
#endif
[OneTimeSetUp]
public void OneTimeSetUp()
{
GC.Collect();
}
private class TestClass
{
public string Value = "Default";
}
}