<commit_before>﻿using Rocks.Exceptions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;

namespace Rocks.Extensions
{
	internal static class PropertyInfoExtensions
	{
		internal static void CheckPropertyAccessors(this PropertyInfo @this, PropertyAccessors accessors)
		{
			if (accessors == PropertyAccessors.Get || accessors == PropertyAccessors.GetAndSet)
			{
				if (!@this.CanRead)
				{
					throw new PropertyNotFoundException($"Property {@this.Name} on type {@this.DeclaringType.Name} cannot be read from.");
				}
			}

			if (accessors == PropertyAccessors.Set || accessors == PropertyAccessors.GetAndSet)
			{
				if (!@this.CanWrite)
				{
					throw new PropertyNotFoundException($"Property {@this.Name} on type {@this.DeclaringType.Name} cannot be written to.");
				}
			}
		}

		internal static MethodInfo GetDefaultMethod(this PropertyInfo @this) =>
			@this.CanRead ? @this.GetMethod : @this.SetMethod;

		private static ReadOnlyDictionary<string, ArgumentExpectation> CreateEmptyExpectations() =>
			new ReadOnlyDictionary<string, ArgumentExpectation>(new Dictionary<string, ArgumentExpectation>());

		private static Dictionary<string, ArgumentExpectation> GetIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var expectations = new Dictionary<string, ArgumentExpectation>();
			var propertyIndexers = @this.GetIndexParameters();

			for (var i = 0; i < indexers.Count; i++)
			{
				var propertyIndexer = propertyIndexers[i];
				expectations.Add(propertyIndexer.Name, indexers[i].Create(
					propertyIndexer.ParameterType, propertyIndexer));
			}

			return expectations;
		}

		internal static ReadOnlyDictionary<string, ArgumentExpectation> GetGetterIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers) =>
			new ReadOnlyDictionary<string, ArgumentExpectation>(@this.GetIndexerExpectations(indexers));

		internal static ReadOnlyDictionary<string, ArgumentExpectation> GetSetterIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var expectations = @this.GetIndexerExpectations(indexers);
			var setterExpectations = @this.CreateDefaultSetterExpectation();
			expectations.Add(setterExpectations.Item1, setterExpectations.Item2);
			return new ReadOnlyDictionary<string, ArgumentExpectation>(expectations);
		}

		internal static ReadOnlyDictionary<string, ArgumentExpectation> GetSetterIndexerExpectations<TProperty>(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers,
			Expression<Func<TProperty>> setterExpectation)
		{
			var expectations = @this.GetIndexerExpectations(indexers);
			var setterExpectations = @this.CreateDefaultSetterExpectation();
			expectations.Add(Values.PropertySetterArgumentName, setterExpectation.GetExpectationForSetter());
			return new ReadOnlyDictionary<string, ArgumentExpectation>(expectations);
		}

		private static Tuple<string, ArgumentExpectation> CreateDefaultSetterExpectation(this PropertyInfo @this)
		{
			var expectationType = typeof(ArgumentExpectation<>).MakeGenericType(@this.PropertyType);
			var expectation = expectationType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				Type.EmptyTypes, null).Invoke(null) as ArgumentExpectation;
			return new Tuple<string, ArgumentExpectation>(Values.PropertySetterArgumentName, expectation);
		}

		internal static ReadOnlyDictionary<string, ArgumentExpectation> CreateDefaultSetterExpectationDictionary(this PropertyInfo @this)
		{
			var expectation = @this.CreateDefaultSetterExpectation();
			return new ReadOnlyDictionary<string, ArgumentExpectation>(new Dictionary<string, ArgumentExpectation>
				{
					{ expectation.Item1, expectation.Item2 }
				});
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this) =>
			new HandlerInformation(@this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, uint expectedCallCount) =>
			new HandlerInformation(expectedCallCount, @this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, Delegate handler) =>
			new HandlerInformation(handler, @this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, Delegate handler, uint expectedCallCount) =>
			new HandlerInformation(handler, expectedCallCount, @this.CreateDefaultSetterExpectationDictionary());
	}
}
<commit_msg>Removed Tuple type from code<commit_after>﻿using Rocks.Exceptions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;

namespace Rocks.Extensions
{
	internal static class PropertyInfoExtensions
	{
		internal static void CheckPropertyAccessors(this PropertyInfo @this, PropertyAccessors accessors)
		{
			if (accessors == PropertyAccessors.Get || accessors == PropertyAccessors.GetAndSet)
			{
				if (!@this.CanRead)
				{
					throw new PropertyNotFoundException($"Property {@this.Name} on type {@this.DeclaringType.Name} cannot be read from.");
				}
			}

			if (accessors == PropertyAccessors.Set || accessors == PropertyAccessors.GetAndSet)
			{
				if (!@this.CanWrite)
				{
					throw new PropertyNotFoundException($"Property {@this.Name} on type {@this.DeclaringType.Name} cannot be written to.");
				}
			}
		}

		internal static MethodInfo GetDefaultMethod(this PropertyInfo @this) =>
			@this.CanRead ? @this.GetMethod : @this.SetMethod;

		private static ReadOnlyDictionary<string, ArgumentExpectation> CreateEmptyExpectations() =>
			new ReadOnlyDictionary<string, ArgumentExpectation>(new Dictionary<string, ArgumentExpectation>());

		private static Dictionary<string, ArgumentExpectation> GetIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var expectations = new Dictionary<string, ArgumentExpectation>();
			var propertyIndexers = @this.GetIndexParameters();

			for (var i = 0; i < indexers.Count; i++)
			{
				var propertyIndexer = propertyIndexers[i];
				expectations.Add(propertyIndexer.Name, indexers[i].Create(
					propertyIndexer.ParameterType, propertyIndexer));
			}

			return expectations;
		}

		internal static ReadOnlyDictionary<string, ArgumentExpectation> GetGetterIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers) =>
			new ReadOnlyDictionary<string, ArgumentExpectation>(@this.GetIndexerExpectations(indexers));

		internal static ReadOnlyDictionary<string, ArgumentExpectation> GetSetterIndexerExpectations(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var expectations = @this.GetIndexerExpectations(indexers);
			var (name, expectation) = @this.CreateDefaultSetterExpectation();
			expectations.Add(name, expectation);
			return new ReadOnlyDictionary<string, ArgumentExpectation>(expectations);
		}

		private static (string, ArgumentExpectation) CreateDefaultSetterExpectation(this PropertyInfo @this)
		{
			var expectationType = typeof(ArgumentExpectation<>).MakeGenericType(@this.PropertyType);
			var expectation = expectationType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				Type.EmptyTypes, null).Invoke(null) as ArgumentExpectation;
			return (Values.PropertySetterArgumentName, expectation);
		}

		internal static ReadOnlyDictionary<string, ArgumentExpectation> CreateDefaultSetterExpectationDictionary(this PropertyInfo @this)
		{
			var (name, expectation) = @this.CreateDefaultSetterExpectation();
			return new ReadOnlyDictionary<string, ArgumentExpectation>(new Dictionary<string, ArgumentExpectation>
				{
					{ name, expectation }
				});
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this) =>
			new HandlerInformation(@this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new[] { @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { expectedCallCount, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, uint expectedCallCount) =>
			new HandlerInformation(expectedCallCount, @this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, @this.GetGetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, ReadOnlyCollection<Expression> indexers, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, @this.GetSetterIndexerExpectations(indexers) }) as HandlerInformation;
		}

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, Delegate handler)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, Delegate handler) =>
			new HandlerInformation(handler, @this.CreateDefaultSetterExpectationDictionary());

		internal static HandlerInformation GetGetterHandler(this PropertyInfo @this, Delegate handler, uint expectedCallCount)
		{
			var handlerType = typeof(HandlerInformation<>).MakeGenericType(@this.PropertyType);
			return handlerType.GetConstructor(ReflectionValues.PublicNonPublicInstance, null,
				new[] { typeof(Delegate), typeof(uint), typeof(ReadOnlyDictionary<string, ArgumentExpectation>) }, null)
				.Invoke(new object[] { handler, expectedCallCount, PropertyInfoExtensions.CreateEmptyExpectations() }) as HandlerInformation;
		}

		internal static HandlerInformation GetSetterHandler(this PropertyInfo @this, Delegate handler, uint expectedCallCount) =>
			new HandlerInformation(handler, expectedCallCount, @this.CreateDefaultSetterExpectationDictionary());
	}
}

{'fraction_non_alphanumeric': 0.09520626432391138, 'fraction_numerical': 0.00028647822765469827, 'mean_word_length': 7.083751447317638, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 121, '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': '1874948', 'n_tokens_mistral': 5902, 'n_tokens_neox': 5191, 'n_words': 1121}