<commit_before>﻿/*
 JustMock Lite
 Copyright © 2018 Telerik AD

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Telerik.JustMock.Core
{
	internal static partial class MockingUtil
	{
		public static MethodInfo GetLocalFunction(object target, MethodInfo method, string localMemberName)
		{
			Type type = target.GetType();
			return GetLocalFunction(type, method, localMemberName);
		}

		public static MethodInfo GetLocalFunction(Type type, MethodInfo method, string localMemberName)
		{
			MethodInfo[] allStaticNonPublicMethods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
			MethodInfo[] potentialLocalMethods = allStaticNonPublicMethods.Where(m => (m.Name.Contains(method.Name) && m.Name.Contains(localMemberName))).ToArray();

			if (potentialLocalMethods.Length == 0)
			{
				throw new MissingMemberException(BuildMissingMethodMessage(type, null, localMemberName));
			}
			else if (potentialLocalMethods.Length == 1)
			{
				return potentialLocalMethods.First();
			}
			else
			{
				MethodBody body = method.GetMethodBody();
				byte[] il = body.GetILAsByteArray();

				MethodInfo localMethod = null;
				for (int i = 0; i < il.Length; i++)
				{
					byte opCode = il[i];
					if (opCode == 0x28) ////System.Reflection.Emit.OpCodes.Call)
					{
						int token = BitConverter.ToInt32(il, i + 1);
						foreach (var m in potentialLocalMethods)
						{
							if (token == m.MetadataToken && m.Name.Contains(localMemberName))
							{
								localMethod = m;
								break;
							}
						}
					}
					if (localMethod != null)
					{
						break;
					}
				}
				if (localMethod == null)
					throw new MissingMemberException(BuildMissingMethodMessage(type, null, localMemberName));

				return localMethod;
			}
		}

		public static MethodInfo GetMethodWithLocalFunction(object target, string methodName)
		{<commit_msg>Adopt simplified logic for searching for local members and rely mostly on the calls from IL code. This way it will be impossible to mock local functions that are never called.<commit_after>﻿/*
 JustMock Lite
 Copyright © 2018 Telerik AD

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Telerik.JustMock.Core
{
	internal static partial class MockingUtil
	{
		public static MethodInfo GetLocalFunction(object target, MethodInfo method, string localMemberName)
		{
			Type type = target.GetType();
			return GetLocalFunction(type, method, localMemberName);
		}

		public static MethodInfo GetLocalFunction(Type type, MethodInfo method, string localMemberName)
		{
			MethodBody body = method.GetMethodBody();
			byte[] il = body.GetILAsByteArray();

			MethodInfo localMethod = null;
			for (int i = 0; i < il.Length; i++)
			{
				byte opCode = il[i];
				if (opCode == 0x28) ////System.Reflection.Emit.OpCodes.Call)
				{
					int token = BitConverter.ToInt32(il, i + 1);

					MethodBase methodBase = type.Module.ResolveMethod(token);
					
					if(methodBase.DeclaringType == type)
					{
						var regEx = new System.Text.RegularExpressions.Regex(@"<(?<ContainingMethod>([a-z,A-Z,0-9]+))>g__(?<LocalFunction>([[a-z,A-Z,0-9]+))[|][0-9]+_[0-9]+");
						System.Text.RegularExpressions.Match match = regEx.Match(methodBase.Name);
						if (match.Success)
						{
							string containigMethod = match.Groups["ContainingMethod"].Value;
							string localFunction = match.Groups["LocalFunction"].Value;
							if (method.Name.Equals(containigMethod) && localMemberName.Equals(localFunction))
							{

								localMethod = methodBase as MethodInfo;
								break;
							}
						}
					}
					if (localMethod != null)
					{
						break;
					}
				}
			}
			if (localMethod == null)
				throw new MissingMemberException(BuildMissingMethodMessage(type, null, localMemberName));

			return localMethod;
		}

		public static MethodInfo GetMethodWithLocalFunction(object target, string methodName)
		{
{'fraction_non_alphanumeric': 0.09463414634146342, 'fraction_numerical': 0.007804878048780488, 'mean_word_length': 3.5242718446601944, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '670597', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1435, 'n_words': 449}