aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/TestIsolationExtensions.cs
blob: aa983ae47f78de990cd001e20f5374cf54d6aceb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
namespace slskd.Tests.Unit
{
    using System;
    using System.Reflection;

    public static class TestIsolationExtensions
    {
        private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

        public static T GetField<T>(this object target, string fieldName)
        {
            var type = target.GetType();
            var field = type.GetField(fieldName, Flags);

            if (field == default)
            {
                throw new ArgumentException($"No such field '{fieldName}' exists on target Type {type.Name}.", nameof(fieldName));
            }

            try
            {
                return (T)field.GetValue(target);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to get field '{fieldName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static T GetProperty<T>(this object target, string propertyName)
        {
            var type = target.GetType();
            var property = type.GetProperty(propertyName, Flags);

            if (property == default)
            {
                throw new ArgumentException($"No such property '{propertyName}' exists on target Type {type.Name}.", nameof(propertyName));
            }

            try
            {
                return (T)property.GetValue(target);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to get property '{propertyName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static void InvokeGenericMethod(this object target, string methodName, Type typeArgument, params object[] args)
        {
            InvokeGenericMethod(target, methodName, typeArgument, Flags, args);
        }

        public static void InvokeGenericMethod(this object target, string methodName, Type typeArgument, BindingFlags bindingFlags, params object[] args)
        {
            var type = target.GetType();

            try
            {
                var method = GetMethod(type, methodName, bindingFlags);
                var generic = method.MakeGenericMethod(typeArgument);

                generic.Invoke(target, args);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to invoke method '{methodName}<{typeArgument.Name}>' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static TResult InvokeGenericMethod<TArg, TResult>(this object target, string methodName, params object[] args)
        {
            return InvokeGenericMethod<TArg, TResult>(target, methodName, Flags, args);
        }

        public static TResult InvokeGenericMethod<TArg, TResult>(this object target, string methodName, BindingFlags bindingFlags, params object[] args)
        {
            var type = target.GetType();

            try
            {
                var method = GetMethod(type, methodName, bindingFlags);
                var generic = method.MakeGenericMethod(typeof(TArg));

                return (TResult)generic.Invoke(target, args);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to invoke method '{methodName}<{typeof(TArg).Name}>' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static void InvokeMethod(this object target, string methodName, BindingFlags bindingFlags, params object[] args)
        {
            var type = target.GetType();

            try
            {
                GetMethod(type, methodName, bindingFlags).Invoke(target, args);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to invoke method '{methodName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static void InvokeMethod(this object target, string methodName, params object[] args)
        {
            InvokeMethod(target, methodName, Flags, args);
        }

        public static T InvokeMethod<T>(this object target, string methodName, BindingFlags bindingFlags, params object[] args)
        {
            var type = target.GetType();

            try
            {
                return (T)GetMethod(type, methodName, bindingFlags).Invoke(target, args);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to invoke method '{methodName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static T InvokeMethod<T>(this object target, string methodName, params object[] args)
        {
            return InvokeMethod<T>(target, methodName, Flags, args);
        }

        public static void RaiseEvent(this object target, Type targetType, string eventName, object eventArgs)
        {
            var type = targetType;
            var @event = (MulticastDelegate)type.GetField(eventName, Flags)?.GetValue(target);

            if (@event == null)
            {
                throw new ArgumentException($"No such event '{eventName}' exists on target Type {type.Name}.", nameof(eventName));
            }

            try
            {
                foreach (var handler in @event?.GetInvocationList())
                {
                    handler.Method.Invoke(handler.Target, new object[] { target, eventArgs });
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to raise event '{eventName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static void RaiseEvent(this object target, string eventName, object eventArgs)
        {
            RaiseEvent(target, target.GetType(), eventName, eventArgs);
        }

        public static void SetField(this object target, string fieldName, object value)
        {
            var type = target.GetType();
            var field = type.GetField(fieldName, Flags);

            if (field == default)
            {
                throw new ArgumentException($"No such field '{fieldName}' exists on target Type {type.Name}.", nameof(fieldName));
            }

            try
            {
                field.SetValue(target, value);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to set field '{fieldName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        public static void SetProperty(this object target, string propertyName, object value)
        {
            var type = target.GetType();
            var property = type.GetProperty(propertyName, Flags);

            if (property == default)
            {
                throw new ArgumentException($"No such property '{propertyName}' exists on target Type {type.Name}.", nameof(propertyName));
            }

            try
            {
                property.SetValue(target, value);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to set property '{propertyName}' on target Type {type.Name}.  See inner Exception for details.", ex);
            }
        }

        private static MethodInfo GetMethod(Type type, string methodName, BindingFlags flags)
        {
            var method = type.GetMethod(methodName, flags);

            if (method == default)
            {
                throw new ArgumentException($"No such method '{methodName}' exists on target Type {type.Name}.", nameof(methodName));
            }

            return method;
        }
    }
}