Exemplo n.º 1
0
        public void GetInstance_NullTypeBuilder_CreatesInstanceWithDefaultConstructor()
        {
            // Arrange

            var    singleton = new WeakSingleton <object>();
            object obj       = null !;

            // Act & Assert

            Assert.DoesNotThrow(() => obj = singleton.Instance);
            Assert.NotNull(obj);
        }
Exemplo n.º 2
0
        public void GetInstance_ExplicitTypeBuilder_CreatesInstanceWithTypeBuilder()
        {
            // Arrange

            var    singleton = new WeakSingleton <string>(() => new string("Hello"));
            string str       = null !;

            // Act & Assert

            Assert.DoesNotThrow(() => str = singleton.Instance);
            Assert.NotNull(str);
            Assert.AreEqual(str, "Hello");
        }
Exemplo n.º 3
0
        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.NotNull(obj);
            Assert.AreEqual(obj.Value, "Default");
        }