public void UsingValueFunction_CreateByFunction_Test1()
        {
            var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            services.AddTransient <Dummy>();
            using (var serviceProvider = services.BuildServiceProvider()) {
                var u = UsingValue.CreateByServiceProvider <Dummy>(serviceProvider);
                Assert.NotNull(u);

                // first creation
                var d = u.GetValue();
                Assert.NotNull(d);
                Assert.Same(d, u.GetValue());
                Assert.False(d.IsDisposed);

                // GetValue does not change
                Assert.Equal(0, u.GetValue().Value);
                d.Value = 2;
                Assert.False(u.GetValue().IsDisposed);
                Assert.Equal(2, u.GetValue().Value);

                u.Dispose();
                Assert.True(d.IsDisposed);
            }
        }
示例#2
0
        public void UsingValueFunction_CreateByFunction_Test1()
        {
            var u = UsingValue.CreateByFunction(() => new Dummy()
            {
                Value = 1
            });

            Assert.NotNull(u);

            // first creation
            var d = u.GetValue();

            Assert.NotNull(d);
            Assert.Same(d, u.GetValue());
            Assert.False(d.IsDisposed);

            // GetValue does not change
            Assert.Equal(1, u.GetValue().Value);
            d.Value = 2;
            Assert.False(u.GetValue().IsDisposed);
            Assert.Equal(2, u.GetValue().Value);

            u.Dispose();
            Assert.True(d.IsDisposed);
        }
        public void LocalDisposables_Test2()
        {
            var sut = new LocalDisposables();
            var d   = sut.AddUsingValue(UsingValue.CreateByFunction(() => new Dummy()));

            Assert.False(d.IsDisposed);
            sut.Dispose();
            Assert.True(d.IsDisposed);
        }
        public void UsingValueFunction_CreateByFunction_Test2_Dispose()
        {
            var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            services.AddTransient <Dummy>();
            using (var serviceProvider = services.BuildServiceProvider()) {
                var u = UsingValue.CreateByServiceProvider <Dummy>(serviceProvider);
                Assert.NotNull(u);

                // first creation
                var d = u.GetValue();
                Assert.NotNull(d);
                Assert.Same(d, u.GetValue());
                Assert.False(d.IsDisposed);

                u.Dispose();
                Assert.Throws <ObjectDisposedException>(() => { u.GetValue(); });
            }
        }
示例#5
0
        public void UsingValueFunction_CreateByFunction_Test2_Dispose()
        {
            var u = UsingValue.CreateByFunction(() => new Dummy()
            {
                Value = 1
            });

            Assert.NotNull(u);

            // first creation
            var d = u.GetValue();

            Assert.NotNull(d);
            Assert.Same(d, u.GetValue());
            Assert.False(d.IsDisposed);

            u.Dispose();
            Assert.Throws <ObjectDisposedException>(() => { u.GetValue(); });
        }
示例#6
0
        public void UsingValueFunction_CreateByFunction_Test3_Dispose()
        {
            var u = UsingValue.CreateByFunction(() => new Dummy()
            {
                Value = 1
            });

            Assert.NotNull(u);

            // first creation
            var d = u.GetValue();

            Assert.NotNull(d);
            Assert.Same(d, u.GetValue());
            Assert.False(d.IsDisposed);

            u.Dispose();
            Assert.True(d.IsDisposed);

            // dispose twice
            u.Dispose();
            Assert.True(d.IsDisposed);
        }