< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetPrivateField[T](...)0%110100%
GetPrivateField[T](...)0%110100%
InvokePrivateMethod[T](...)0%110100%
InvokePrivateMethod(...)0%110100%
GetFieldInfo(...)0%5.574053.85%
GetMethodInfo(...)0%2.062075%

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 {
 97  protected void SetPrivateField<T>(object obj, string fieldName, T value) {
 98    FieldInfo field = GetFieldInfo(obj, fieldName);
 99    field.SetValue(obj, value);
 910  }
 11
 212  protected T GetPrivateField<T>(object obj, string fieldName) {
 213    FieldInfo field = GetFieldInfo(obj, fieldName);
 214    return (T)field.GetValue(obj);
 215  }
 16
 617  protected T InvokePrivateMethod<T>(object obj, string methodName, params object[] parameters) {
 618    MethodInfo method = GetMethodInfo(obj, methodName);
 619    return (T)method.Invoke(obj, parameters);
 620  }
 21
 3622  protected void InvokePrivateMethod(object obj, string methodName, params object[] parameters) {
 3623    MethodInfo method = GetMethodInfo(obj, methodName);
 3624    method.Invoke(obj, parameters);
 3625  }
 26
 1127  private FieldInfo GetFieldInfo(object obj, string fieldName) {
 1128    var type = obj.GetType();
 1129    var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
 30
 31    // If not found in the immediate type, search the inheritance hierarchy.
 1132    while (field == null && type.BaseType != null) {
 033      type = type.BaseType;
 034      field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
 035    }
 36
 1137    if (field == null) {
 038      throw new Exception(
 39          $"Field '{fieldName}' not found in type hierarchy of '{obj.GetType().FullName}'.");
 40    }
 1141    return field;
 1142  }
 43
 4244  private MethodInfo GetMethodInfo(object obj, string methodName) {
 4245    var type = obj.GetType();
 4246    var method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
 4247    if (method == null) {
 048      throw new Exception($"Method '{methodName}' not found in type '{type.FullName}'.");
 49    }
 4250    return method;
 4251  }
 52}