< Summary

Class:TestBase
Assembly:bamlab.test
File(s):/github/workspace/Assets/Tests/TestBase.cs
Covered lines:0
Uncovered lines:34
Coverable lines:34
Total lines:52
Line coverage:0% (0 of 34)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:6
Method coverage:0% (0 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetPrivateField[T](...)0%2100%
GetPrivateField[T](...)0%2100%
InvokePrivateMethod[T](...)0%2100%
InvokePrivateMethod(...)0%2100%
GetFieldInfo(...)0%20400%
GetMethodInfo(...)0%6200%

File(s)

/github/workspace/Assets/Tests/TestBase.cs

#LineLine coverage
 1using System;
 2using System.Reflection;
 3using NUnit.Framework;
 4using UnityEngine;
 5
 6public abstract class TestBase {
 07  protected void SetPrivateField<T>(object obj, string fieldName, T value) {
 08    FieldInfo field = GetFieldInfo(obj, fieldName);
 09    field.SetValue(obj, value);
 010  }
 11
 012  protected T GetPrivateField<T>(object obj, string fieldName) {
 013    FieldInfo field = GetFieldInfo(obj, fieldName);
 014    return (T)field.GetValue(obj);
 015  }
 16
 017  protected T InvokePrivateMethod<T>(object obj, string methodName, params object[] parameters) {
 018    MethodInfo method = GetMethodInfo(obj, methodName);
 019    return (T)method.Invoke(obj, parameters);
 020  }
 21
 022  protected void InvokePrivateMethod(object obj, string methodName, params object[] parameters) {
 023    MethodInfo method = GetMethodInfo(obj, methodName);
 024    method.Invoke(obj, parameters);
 025  }
 26
 027  private FieldInfo GetFieldInfo(object obj, string fieldName) {
 028    var type = obj.GetType();
 029    var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
 30
 31    // If not found in the immediate type, search the inheritance hierarchy.
 032    while (field == null && type.BaseType != null) {
 033      type = type.BaseType;
 034      field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
 035    }
 36
 037    if (field == null) {
 038      throw new Exception(
 39          $"Field '{fieldName}' not found in type hierarchy of '{obj.GetType().FullName}'.");
 40    }
 041    return field;
 042  }
 43
 044  private MethodInfo GetMethodInfo(object obj, string methodName) {
 045    var type = obj.GetType();
 046    var method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
 047    if (method == null) {
 048      throw new Exception($"Method '{methodName}' not found in type '{type.FullName}'.");
 49    }
 050    return method;
 051  }
 52}