From 5596ac60d748c64a40a41c85fc62ca099317dd52 Mon Sep 17 00:00:00 2001 From: Andreas Pardeike Date: Sun, 26 Jan 2020 11:47:26 +0100 Subject: [PATCH] Added test for #244 --- HarmonyTests/Patching/Assets/PatchClasses.cs | 53 ++++++++++++++++++++ HarmonyTests/Patching/CombinedPatches.cs | 27 ++++++++++ 2 files changed, 80 insertions(+) diff --git a/HarmonyTests/Patching/Assets/PatchClasses.cs b/HarmonyTests/Patching/Assets/PatchClasses.cs index df267a31..159f1404 100644 --- a/HarmonyTests/Patching/Assets/PatchClasses.cs +++ b/HarmonyTests/Patching/Assets/PatchClasses.cs @@ -626,6 +626,59 @@ public static void Prefix(object __originalMethod, ref int item) } } + public class Class14 + { + public static List state = new List(); + + [MethodImpl(MethodImplOptions.NoInlining)] + public bool Test(string s, KeyValuePair p) + { + state.Add(s); + try { return true; } + finally { } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public bool Test(string s, KeyValuePair p1, KeyValuePair p2) + { + state.Add(s); + try { return true; } + finally { } + } + } + + [HarmonyPatch] + public static class Class14Patch + { + [HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair) })] + [HarmonyPrefix] + static bool Prefix0() + { + Class14.state.Add("Prefix0"); + return true; + } + [HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair) })] + [HarmonyPostfix] + static void Postfix0() + { + Class14.state.Add("Postfix0"); + } + + [HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair), typeof(KeyValuePair) })] + [HarmonyPrefix] + static bool Prefix1() + { + Class14.state.Add("Prefix1"); + return true; + } + [HarmonyPatch(typeof(Class14), "Test", new Type[] { typeof(string), typeof(KeyValuePair), typeof(KeyValuePair) })] + [HarmonyPostfix] + static void Postfix1() + { + Class14.state.Add("Postfix1"); + } + } + // disabled - see test case /* public class ClassExceptionFilter diff --git a/HarmonyTests/Patching/CombinedPatches.cs b/HarmonyTests/Patching/CombinedPatches.cs index f6cdd358..3eb2ac1a 100644 --- a/HarmonyTests/Patching/CombinedPatches.cs +++ b/HarmonyTests/Patching/CombinedPatches.cs @@ -1,6 +1,7 @@ using HarmonyLib; using HarmonyLibTests.Assets; using NUnit.Framework; +using System.Collections.Generic; namespace HarmonyLibTests { @@ -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("1", 1)); + _ = new Class14().Test("Test2", new KeyValuePair("1", 1), new KeyValuePair("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]); + } } } \ No newline at end of file