<commit_before>﻿using Microsoft.Framework.DependencyInjection;
using System;
using Xunit;

namespace Compose.Tests
{
	public class ServiceProviderTests
	{
		[Fact]
		public void CanHandleImplementationInstances()
		{
			var app = new Fake.Application();
			app.UseServices(services => services.AddInstance(typeof(string), "foo"));
			Action act = app.Execute;
			Assert.Null(Record.Exception(act));
		}

		[Fact]
		public void CanHandleSameInstanceImplementingMultipleServices()
		{
			var app = new Fake.Application();
			app.UseServices(services =>
			{
				var service = new Dependency();
				services.AddInstance<IService1>(service);
				services.AddInstance<IService2>(service);
			});
			Action act = app.Execute;
			Assert.Null(Record.Exception(act));
		}

		[Fact]
		public void CanTreatInstancesAsSingletons()
		{
			var app = new Fake.Application();
			var instance = new Dependency();
			app.UseServices(services =>
			{
				services.AddInstance<IService1>(instance);
			});
			app.OnExecute<IService1>(parent =>
			{
				Assert.Equal(instance, parent);
				Assert.Equal(instance, app.ApplicationServices.GetService<IService1>());
			});
			app.Execute();
		}

		[Fact]
		public void CanUseCustomServiceProvider()
		{
			var app = new Fake.Application();
			app.UseServices(services => new CustomServiceProvider());
			app.OnExecute<IService1>(service => { });
			Action act = app.Execute;
			Assert.IsType<NotImplementedException>(Record.Exception(act));
		}

		private interface IService1 { }
		private interface IService2 { }
		private class Dependency : IService1, IService2 { }

		private class CustomServiceProvider : IServiceProvider
		{
			public object GetService(Type serviceType)
			{
				throw new NotImplementedException();
			}
		}
	}
}<commit_msg>Fixes smudge202/compose#62<commit_after>﻿using Microsoft.Framework.DependencyInjection;
using System;
using Xunit;

namespace Compose.Tests
{
	public class ServiceProviderTests
	{
		[Fact]
		public void CanHandleImplementationInstances()
		{
			var app = new Fake.Application();
			app.UseServices(services => services.AddInstance(typeof(string), "foo"));
			Action act = app.Execute;
			Assert.Null(Record.Exception(act));
		}

		[Fact]
		public void CanHandleSameInstanceImplementingMultipleServices()
		{
			var app = new Fake.Application();
			app.UseServices(services =>
			{
				var service = new Service();
				services.AddInstance<IService1>(service);
				services.AddInstance<IService2>(service);
			});
			Action act = app.Execute;
			Assert.Null(Record.Exception(act));
		}

		[Fact]
		public void CanTreatInstancesAsSingletons()
		{
			var app = new Fake.Application();
			var instance = new Service();
			app.UseServices(services =>
			{
				services.AddInstance<IService1>(instance);
			});
			app.OnExecute<IService1>(parent =>
			{
				Assert.Equal(instance, parent);
				Assert.Equal(instance, app.ApplicationServices.GetService<IService1>());
			});
			app.Execute();
		}

		[Fact]
		public void CanUseCustomServiceProvider()
		{
			var app = new Fake.Application();
			app.UseServices(services => new CustomServiceProvider());
			app.OnExecute<IService1>(service => { });
			Action act = app.Execute;
			Assert.IsType<NotImplementedException>(Record.Exception(act));
		}

		[Fact]
		public void CanResolveSingletonsIndirectly()
		{
			var app = new Fake.Application();
			app.UseServices(services => services
				.AddTransient<IConsumer, Consumer>()
				.AddSingleton<IDependency, Dependency>()
			);
			app.OnExecute<IConsumer>(consumer =>
			{
				var newConsumer = app.ApplicationServices.GetRequiredService<IConsumer>();
				Assert.Equal(consumer.Id, newConsumer.Id);
				Assert.NotEqual(consumer, newConsumer);
			});
		}

		private interface IService1 { }
		private interface IService2 { }
		private class Service : IService1, IService2 { }

		private interface IConsumer { Guid Id { get; } }
		private class Consumer : IConsumer
		{
			private readonly IDependency _dependency;
			public Consumer(IDependency dependency)
			{
				_dependency = dependency;
			}

			public Guid Id { get { return _dependency.Id; } }
		}

		private interface IDependency { Guid Id { get; } }
		private class Dependency : IDependency
		{
			public Guid Id { get; } = Guid.NewGuid();
		}

		private class CustomServiceProvider : IServiceProvider
		{
			public object GetService(Type serviceType)
			{
				throw new NotImplementedException();
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.11775321452740807, 'fraction_numerical': 0.005639521768554027, 'mean_word_length': 4.021517553793885, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2969090', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1320, 'n_words': 269}