| | 1 | | using System; |
| | 2 | | using System.Reflection; |
| | 3 | | using NUnit.Framework; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public abstract class TestBase { |
| 6 | 7 | | protected void SetPrivateField<T>(object obj, string fieldName, T value) { |
| 6 | 8 | | FieldInfo field = GetFieldInfo(obj, fieldName); |
| 6 | 9 | | field.SetValue(obj, value); |
| 6 | 10 | | } |
| | 11 | |
|
| 1 | 12 | | protected T GetPrivateField<T>(object obj, string fieldName) { |
| 1 | 13 | | FieldInfo field = GetFieldInfo(obj, fieldName); |
| 1 | 14 | | return (T)field.GetValue(obj); |
| 1 | 15 | | } |
| | 16 | |
|
| 27 | 17 | | protected T InvokePrivateMethod<T>(object obj, string methodName, params object[] parameters) { |
| 27 | 18 | | MethodInfo method = GetMethodInfo(obj, methodName); |
| 27 | 19 | | return (T)method.Invoke(obj, parameters); |
| 27 | 20 | | } |
| | 21 | |
|
| 34 | 22 | | protected void InvokePrivateMethod(object obj, string methodName, params object[] parameters) { |
| 34 | 23 | | MethodInfo method = GetMethodInfo(obj, methodName); |
| 34 | 24 | | method.Invoke(obj, parameters); |
| 34 | 25 | | } |
| | 26 | |
|
| 7 | 27 | | private FieldInfo GetFieldInfo(object obj, string fieldName) { |
| 7 | 28 | | var type = obj.GetType(); |
| 7 | 29 | | var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); |
| 7 | 30 | | if (field == null) { |
| 0 | 31 | | throw new Exception($"Field '{fieldName}' not found in type '{type.FullName}'."); |
| | 32 | | } |
| 7 | 33 | | return field; |
| 7 | 34 | | } |
| | 35 | |
|
| 61 | 36 | | private MethodInfo GetMethodInfo(object obj, string methodName) { |
| 61 | 37 | | var type = obj.GetType(); |
| 61 | 38 | | var method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); |
| 61 | 39 | | if (method == null) { |
| 0 | 40 | | throw new Exception($"Method '{methodName}' not found in type '{type.FullName}'."); |
| | 41 | | } |
| 61 | 42 | | return method; |
| 61 | 43 | | } |
| | 44 | | } |