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