-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PatchBySequence tests; fixed sequence not found
- Loading branch information
1 parent
f5da4e7
commit 3657d52
Showing
7 changed files
with
238 additions
and
23 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
214 changes: 214 additions & 0 deletions
214
PulsarPluginLoader.Tests/Patches/PatchBySequenceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
using Harmony; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection.Emit; | ||
using static PulsarPluginLoader.Patches.HarmonyHelpers; | ||
|
||
namespace PulsarPluginLoader.Tests.Patches | ||
{ | ||
[TestFixture] | ||
class PatchBySequenceTests | ||
{ | ||
private static bool AreEqualSequences(IEnumerable<CodeInstruction> first, IEnumerable<CodeInstruction> second) | ||
{ | ||
if (first.Count() != second.Count()) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < first.Count(); i++) | ||
{ | ||
CodeInstruction a = first.ElementAt(i); | ||
CodeInstruction b = second.ElementAt(i); | ||
|
||
if (!a.opcode.Equals(b.opcode) && a.operand.Equals(b.operand)) | ||
{ | ||
return false; | ||
} | ||
|
||
} | ||
|
||
return true; | ||
} | ||
|
||
[Test] | ||
public void CanInsert_Before() | ||
{ | ||
List<CodeInstruction> original = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> targetSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> patchSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
}; | ||
|
||
List<CodeInstruction> actual = PatchBySequence(original, targetSequence, patchSequence, PatchMode.BEFORE).ToList(); | ||
|
||
List<CodeInstruction> expected = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
Assert.IsTrue(AreEqualSequences(expected, actual)); | ||
} | ||
|
||
[Test] | ||
public void CanInsert_After() | ||
{ | ||
List<CodeInstruction> original = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> targetSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> patchSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
}; | ||
|
||
List<CodeInstruction> actual = PatchBySequence(original, targetSequence, patchSequence, PatchMode.AFTER).ToList(); | ||
|
||
List<CodeInstruction> expected = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
}; | ||
|
||
Assert.IsTrue(AreEqualSequences(expected, actual)); | ||
} | ||
|
||
[Test] | ||
public void CanReplace_SameLength() | ||
{ | ||
List<CodeInstruction> original = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> targetSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> patchSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
}; | ||
|
||
List<CodeInstruction> actual = PatchBySequence(original, targetSequence, patchSequence, PatchMode.REPLACE).ToList(); | ||
|
||
List<CodeInstruction> expected = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Nop), | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
}; | ||
|
||
Assert.IsTrue(AreEqualSequences(expected, actual)); | ||
} | ||
|
||
[Test] | ||
public void CanReplace_SmallerInsert() | ||
{ | ||
List<CodeInstruction> original = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> targetSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> patchSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12) | ||
}; | ||
|
||
List<CodeInstruction> actual = PatchBySequence(original, targetSequence, patchSequence, PatchMode.REPLACE).ToList(); | ||
|
||
List<CodeInstruction> expected = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Nop), | ||
new CodeInstruction(OpCodes.Ldc_R4, 12) | ||
}; | ||
|
||
Assert.IsTrue(AreEqualSequences(expected, actual)); | ||
} | ||
|
||
[Test] | ||
public void CanReplace_LargerInsert() | ||
{ | ||
List<CodeInstruction> original = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> targetSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 0), | ||
new CodeInstruction(OpCodes.Ldc_R4, 6), | ||
}; | ||
|
||
List<CodeInstruction> patchSequence = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
new CodeInstruction(OpCodes.Ldc_R4, 24), | ||
}; | ||
|
||
List<CodeInstruction> actual = PatchBySequence(original, targetSequence, patchSequence, PatchMode.REPLACE).ToList(); | ||
|
||
List<CodeInstruction> expected = new List<CodeInstruction>() | ||
{ | ||
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")), | ||
new CodeInstruction(OpCodes.Nop), | ||
new CodeInstruction(OpCodes.Ldc_R4, 12), | ||
new CodeInstruction(OpCodes.Ldc_R4, 18), | ||
new CodeInstruction(OpCodes.Ldc_R4, 24), | ||
}; | ||
|
||
Assert.IsTrue(AreEqualSequences(expected, actual)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace PulsarPluginLoader.Tests.Patches | ||
{ | ||
class SampleClass | ||
{ | ||
public static int StaticIntField = 11; | ||
|
||
public int IntField = 36; | ||
|
||
public void VoidMethod() { } | ||
|
||
public static void StaticVoidMethod() { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters