Reflection unit testing

Jenkins integration (we can only hope)
This commit is contained in:
Westin Miller
2017-08-20 21:48:42 -07:00
parent 0554dbc608
commit 48b212faaf
11 changed files with 547 additions and 95 deletions

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Torch.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Torch.Tests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c3c8b671-6ad1-44aa-a8da-e0c0dc0fedf5")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,306 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Torch.API;
using Torch.Client;
using Torch.Managers;
using Xunit;
using Xunit.Abstractions;
namespace Torch.Tests
{
public class ReflectionTests
{
private static string GetGameBinaries()
{
string dir = Environment.CurrentDirectory;
while (!string.IsNullOrWhiteSpace(dir))
{
string gameBin = Path.Combine(dir, "GameBinaries");
if (Directory.Exists(gameBin))
return gameBin;
dir = Path.GetDirectoryName(dir);
}
throw new Exception("GetGameBinaries failed to find a folder named GameBinaries in the directory tree");
}
private static readonly TorchAssemblyResolver _torchResolver =
new TorchAssemblyResolver(GetGameBinaries());
#region Binding
[Theory]
[MemberData(nameof(Getters))]
public void TestBindingGetter(FieldRef field)
{
Assert.True(ReflectionManager.Process(field.Field));
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
[Theory]
[MemberData(nameof(Setters))]
public void TestBindingSetter(FieldRef field)
{
Assert.True(ReflectionManager.Process(field.Field));
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
[Theory]
[MemberData(nameof(Invokers))]
public void TestBindingInvoker(FieldRef field)
{
Assert.True(ReflectionManager.Process(field.Field));
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
#endregion
#region Results
#region Dummy
private class ReflectionTestTarget
{
public int TestField;
public int TestProperty { get; set; }
/// <summary>
/// Return true when greater or equal than 0
/// </summary>
public bool TestCall(int k)
{
return k >= 0;
}
public static int TestFieldStatic;
public static int TestPropertyStatic { get; set; }
/// <summary>
/// Return true when greater or equal than 0
/// </summary>
public static bool TestCallStatic(int k)
{
return k >= 0;
}
}
private class ReflectionTestBinding
{
[ReflectedGetter(Name = "TestField")]
public static Func<ReflectionTestTarget, int> TestFieldGetter;
[ReflectedSetter(Name = "TestField")]
public static Action<ReflectionTestTarget, int> TestFieldSetter;
[ReflectedGetter(Name = "TestProperty")]
public static Func<ReflectionTestTarget, int> TestPropertyGetter;
[ReflectedSetter(Name = "TestProperty")]
public static Action<ReflectionTestTarget, int> TestPropertySetter;
[ReflectedMethod]
public static Func<ReflectionTestTarget, int, bool> TestCall;
[ReflectedGetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
public static Func<int> TestStaticFieldGetter;
[ReflectedSetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
public static Action<int> TestStaticFieldSetter;
[ReflectedGetter(Name = "TestPropertyStatic", Type = typeof(ReflectionTestTarget))]
public static Func<int> TestStaticPropertyGetter;
[ReflectedSetter(Name = "TestPropertyStatic", Type = typeof(ReflectionTestTarget))]
public static Action<int> TestStaticPropertySetter;
[ReflectedStaticMethod(Type = typeof(ReflectionTestTarget))]
public static Func<int, bool> TestCallStatic;
}
#endregion
private readonly Random _rand = new Random();
private int AcquireRandomNum()
{
return _rand.Next();
}
#region Instance
[Fact]
public void TestInstanceFieldGet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
var target = new ReflectionTestTarget
{
TestField = testNumber
};
Assert.Equal(testNumber, ReflectionTestBinding.TestFieldGetter.Invoke(target));
}
[Fact]
public void TestInstanceFieldSet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
var target = new ReflectionTestTarget();
ReflectionTestBinding.TestFieldSetter.Invoke(target, testNumber);
Assert.Equal(testNumber, target.TestField);
}
[Fact]
public void TestInstancePropertyGet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
var target = new ReflectionTestTarget
{
TestProperty = testNumber
};
Assert.Equal(testNumber, ReflectionTestBinding.TestPropertyGetter.Invoke(target));
}
[Fact]
public void TestInstancePropertySet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
var target = new ReflectionTestTarget();
ReflectionTestBinding.TestPropertySetter.Invoke(target, testNumber);
Assert.Equal(testNumber, target.TestProperty);
}
[Fact]
public void TestInstanceInvoke()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
var target = new ReflectionTestTarget();
Assert.True(ReflectionTestBinding.TestCall.Invoke(target, 1));
Assert.False(ReflectionTestBinding.TestCall.Invoke(target, -1));
}
#endregion
#region Static
[Fact]
public void TestStaticFieldGet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
ReflectionTestTarget.TestFieldStatic = testNumber;
Assert.Equal(testNumber, ReflectionTestBinding.TestStaticFieldGetter.Invoke());
}
[Fact]
public void TestStaticFieldSet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
ReflectionTestBinding.TestStaticFieldSetter.Invoke(testNumber);
Assert.Equal(testNumber, ReflectionTestTarget.TestFieldStatic);
}
[Fact]
public void TestStaticPropertyGet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
ReflectionTestTarget.TestPropertyStatic = testNumber;
Assert.Equal(testNumber, ReflectionTestBinding.TestStaticPropertyGetter.Invoke());
}
[Fact]
public void TestStaticPropertySet()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
int testNumber = AcquireRandomNum();
ReflectionTestBinding.TestStaticPropertySetter.Invoke(testNumber);
Assert.Equal(testNumber, ReflectionTestTarget.TestPropertyStatic);
}
[Fact]
public void TestStaticInvoke()
{
ReflectionManager.Process(typeof(ReflectionTestBinding));
Assert.True(ReflectionTestBinding.TestCallStatic.Invoke(1));
Assert.False(ReflectionTestBinding.TestCallStatic.Invoke(-1));
}
#endregion
#endregion
#region FieldProvider
public struct FieldRef
{
public FieldInfo Field;
public FieldRef(FieldInfo f)
{
Field = f;
}
public override string ToString()
{
return Field.DeclaringType?.FullName + "." + Field.Name;
}
}
private static bool _init = false;
private static HashSet<object[]> _getters, _setters, _invokers;
private static void Init()
{
if (_init)
return;
_getters = new HashSet<object[]>();
_setters = new HashSet<object[]>();
_invokers = new HashSet<object[]>();
foreach (Type type in typeof(TorchBase).Assembly.GetTypes())
InternalInit(type);
InternalInit(typeof(ReflectionTestBinding));
_init = true;
}
private static void InternalInit(Type type)
{
foreach (FieldInfo field in type.GetFields(BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic))
{
if (field.GetCustomAttribute<ReflectedMethodAttribute>() != null)
_invokers.Add(new object[] { new FieldRef(field) });
if (field.GetCustomAttribute<ReflectedGetterAttribute>() != null)
_getters.Add(new object[] { new FieldRef(field) });
if (field.GetCustomAttribute<ReflectedSetterAttribute>() != null)
_setters.Add(new object[] { new FieldRef(field) });
}
}
public static IEnumerable<object[]> Getters
{
get
{
Init();
return _getters;
}
}
public static IEnumerable<object[]> Setters
{
get
{
Init();
return _setters;
}
}
public static IEnumerable<object[]> Invokers
{
get
{
Init();
return _invokers;
}
}
#endregion
}
}

View File

@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<ProjectGuid>{C3C8B671-6AD1-44AA-A8DA-E0C0DC0FEDF5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Torch.Tests</RootNamespace>
<AssemblyName>Torch.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<NoWarn>1591,0649</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>$(SolutionDir)\bin-test\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>$(SolutionDir)\bin-test\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>$(SolutionDir)\bin-test\x64\Release\Torch.Tests.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
<Reference Include="xunit.assert, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll</HintPath>
</Reference>
<Reference Include="xunit.core, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll</HintPath>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ReflectionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
<Name>Torch.API</Name>
</ProjectReference>
<ProjectReference Include="..\Torch\Torch.csproj">
<Project>{7e01635c-3b67-472e-bcd6-c5539564f214}</Project>
<Name>Torch</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.2.0" targetFramework="net461" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
<package id="xunit.assert" version="2.2.0" targetFramework="net461" />
<package id="xunit.core" version="2.2.0" targetFramework="net461" />
<package id="xunit.extensibility.core" version="2.2.0" targetFramework="net461" />
<package id="xunit.extensibility.execution" version="2.2.0" targetFramework="net461" />
<package id="xunit.runner.console" version="2.2.0" targetFramework="net461" developmentDependency="true" />
</packages>