<commit_before>﻿using System.Linq;
using Shouldly;
using Xunit;

namespace Maestro.Tests.Core
{
	public class open_generic_type
	{
		[Fact]
		public void GetService_should_support_generic_type_definitions()
		{
			var container = new Container(x => x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)));

			var instance = container.GetService<Instance<int>>();

			instance.ShouldBeOfType<Instance<int>>();
		}

		[Fact]
		public void should_execute_configured_interceptors()
		{
			var container = new Container(x =>
			{
				x.For<int>().Use.Instance(1);
				x.For<string>().Use.Instance("foobar");
				x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)).SetProperty("Value");
			});

			var instance1 = container.GetService<Instance<int>>();
			var instance2 = container.GetService<Instance<string>>();

			instance1.Value.ShouldBe(1);
			instance2.Value.ShouldBe("foobar");
		}

		[Fact]
		public void should_use_configured_lifetime()
		{
			var container = new Container(x =>
			{
				x.For<int>().Use.Instance(1);
				x.For<string>().Use.Instance("foobar");
				x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)).Lifetime.Singleton();
			});

			var instance1 = container.GetService<Instance<int>>();
			var instance2 = container.GetService<Instance<int>>();
			var instance3 = container.GetService<Instance<string>>();
			var instance4 = container.GetService<Instance<string>>();

			instance1.Value.ShouldBe(instance2.Value);
			instance3.Value.ShouldBe(instance4.Value);
		}

		[Fact]
		public void child_container_config_should_be_considered_over_parent_container()
		{
			var parent = new Container(x => x.For<Instance<string>>().Use.Factory(() => new Instance<string> { Value = "root" }));
			var child = parent.GetChildContainer(x => x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)));

			var instance = child.GetService<Instance<string>>();

			instance.Value.ShouldBe(null);
		}

		[Fact]
		public void GetServices_should_support_generic_type_definitions()
		{
			var container = new Container(x => x.For<object>().Add.Self());

			container.GetServices<object>().Count().ShouldBe(1);
		}

		[Fact]
		public void GetServices_should_handle_container_reconfiguration()
		{
			var container = new Container(x => x.For(typeof(Instance<>)).Add.Self());

			container.GetServices<Instance<object>>().Count().ShouldBe(1);

			container.Configure(x => x.For(typeof(Instance<>)).Add.Self());

			container.GetServices<Instance<object>>().Count().ShouldBe(2);
		}

		class Instance<T>
		{
			public T Value { get; set; }
		}
	}
}<commit_msg>open generic type test refactoring<commit_after>﻿using System.Linq;
using Shouldly;
using Xunit;

namespace Maestro.Tests.Core
{
	public class open_generic_type
	{
		[Fact]
		public void GetService_should_support_generic_type_definitions()
		{
			var container = new Container(x => x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)));

			var instance = container.GetService<Instance<int>>();

			instance.ShouldBeOfType<Instance<int>>();
		}

		[Fact]
		public void should_execute_configured_interceptors()
		{
			var container = new Container(x =>
			{
				x.For<int>().Use.Instance(1);
				x.For<string>().Use.Instance("foobar");
				x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)).SetProperty("Value");
			});

			var instance1 = container.GetService<Instance<int>>();
			var instance2 = container.GetService<Instance<string>>();

			instance1.Value.ShouldBe(1);
			instance2.Value.ShouldBe("foobar");
		}

		[Fact]
		public void should_use_configured_lifetime()
		{
			var container = new Container(x =>
			{
				x.For<string>().Use.Instance("success");
				x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)).Lifetime.Singleton();
			});

			var instance1 = container.GetService<Instance<string>>();
			var instance2 = container.GetService<Instance<string>>();

			instance1.ShouldBe(instance2);
		}

		[Fact]
		public void child_container_config_should_be_considered_over_parent_container()
		{
			var parent = new Container(x => x.For<Instance<string>>().Use.Factory(() => new Instance<string> { Value = "root" }));
			var child = parent.GetChildContainer(x => x.For(typeof(Instance<>)).Use.Type(typeof(Instance<>)));

			var instance = child.GetService<Instance<string>>();

			instance.Value.ShouldBe(null);
		}

		[Fact]
		public void GetServices_should_support_generic_type_definitions()
		{
			var container = new Container(x => x.For<object>().Add.Self());

			container.GetServices<object>().Count().ShouldBe(1);
		}

		[Fact]
		public void GetServices_should_handle_container_reconfiguration()
		{
			var container = new Container(x => x.For(typeof(Instance<>)).Add.Self());

			container.GetServices<Instance<object>>().Count().ShouldBe(1);

			container.Configure(x => x.For(typeof(Instance<>)).Add.Self());

			container.GetServices<Instance<object>>().Count().ShouldBe(2);
		}

		class Instance<T>
		{
			public T Value { get; set; }
		}
	}
}
{'fraction_non_alphanumeric': 0.16605913325232888, 'fraction_numerical': 0.006277845281490482, 'mean_word_length': 4.893794749403341, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 100, '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': '5708071', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1570, 'n_words': 234}