Skip to content

Commit

Permalink
Added test for #244
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 26, 2020
1 parent fb13930 commit 5596ac6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
53 changes: 53 additions & 0 deletions HarmonyTests/Patching/Assets/PatchClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,59 @@ public static void Prefix(object __originalMethod, ref int item)
}
}

public class Class14
{
public static List<string> state = new List<string>();

[MethodImpl(MethodImplOptions.NoInlining)]
public bool Test(string s, KeyValuePair<string, int> p)
{
state.Add(s);
try { return true; }
finally { }
}

[MethodImpl(MethodImplOptions.NoInlining)]
public bool Test(string s, KeyValuePair<string, int> p1, KeyValuePair<string, int> p2)
{
state.Add(s);
try { return true; }
finally { }
}
}

[HarmonyPatch]
public static class Class14Patch
{
[HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair<string, int>) })]
[HarmonyPrefix]
static bool Prefix0()
{
Class14.state.Add("Prefix0");
return true;
}
[HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair<string, int>) })]
[HarmonyPostfix]
static void Postfix0()
{
Class14.state.Add("Postfix0");
}

[HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair<string, int>), typeof(KeyValuePair<string, int>) })]
[HarmonyPrefix]
static bool Prefix1()
{
Class14.state.Add("Prefix1");
return true;
}
[HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair<string, int>), typeof(KeyValuePair<string, int>) })]
[HarmonyPostfix]
static void Postfix1()
{
Class14.state.Add("Postfix1");
}
}

// disabled - see test case
/*
public class ClassExceptionFilter
Expand Down
27 changes: 27 additions & 0 deletions HarmonyTests/Patching/CombinedPatches.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HarmonyLib;
using HarmonyLibTests.Assets;
using NUnit.Framework;
using System.Collections.Generic;

namespace HarmonyLibTests
{
Expand Down Expand Up @@ -29,5 +30,31 @@ public void Test_ManyFinalizers()
instance.Method3(123);
Assert.AreEqual(4, CombinedPatchClass_Patch_1.counter);
}

[Test]
public static void Test_Method11()
{
var originalClass = typeof(Class14);
Assert.IsNotNull(originalClass);
var patchClass = typeof(Class14Patch);
Assert.IsNotNull(patchClass);

var harmonyInstance = new Harmony("test");
Assert.IsNotNull(harmonyInstance);

var processor = harmonyInstance.ProcessorForAnnotatedClass(patchClass);
Assert.IsNotNull(processor);
Assert.IsNotNull(processor.Patch());

_ = new Class14().Test("Test1", new KeyValuePair<string, int>("1", 1));
_ = new Class14().Test("Test2", new KeyValuePair<string, int>("1", 1), new KeyValuePair<string, int>("2", 2));

Assert.AreEqual("Prefix0", Class14.state[0]);
Assert.AreEqual("Test1", Class14.state[1]);
Assert.AreEqual("Postfix0", Class14.state[2]);
Assert.AreEqual("Prefix1", Class14.state[3]);
Assert.AreEqual("Test2", Class14.state[4]);
Assert.AreEqual("Postfix1", Class14.state[5]);
}
}
}

0 comments on commit 5596ac6

Please sign in to comment.