﻿using System;

using Ab.Factory;

using FluentAssertions;

using SimpleInjector;
using SimpleInjector.Extensions.ExecutionContextScoping;

using Xunit;

namespace Ab.SimpleInjector.Tests
{
	public class ContainerExtensionsTest
	{
		[Fact]
		public void RegisterFactoryForDisposal_Should_Register_Type_To_Be_Disposed_After_Scope_Is_Disposed()
		{
			// Arrange
			var container = new Container();
			container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

			// Act
			container.RegisterFactoryForDisposal<Boo, BooFactory>(Lifestyle.Scoped);

			Boo boo;
			using (container.BeginExecutionContextScope())
			{
				boo = container.GetInstance<Boo>();
			}

			// Assert
			boo.Disposed.Should().BeTrue();
		}

		class Boo : IDisposable
		{
			public void Dispose()
			{
				Disposed = true;
			}

			public bool Disposed { get; set; }
		}

		class BooFactory : IFactory<Boo>
		{
			public Boo Create()
			{
				return new Boo();
			}
		}
	}
}