using System.Linq; using BlaxSlaves; using HarmonyLib; using RimWorld; using Verse; namespace BlaxSlavesTests { public static class SmokeTests { [BlaxTest("startup: patches applied and logged")] public static void PatchesApplied() { var ours = Harmony.GetAllPatchedMethods() .Select(Harmony.GetPatchInfo) .Where(info => info != null) .SelectMany(info => info.Prefixes.Concat(info.Postfixes).Concat(info.Finalizers).Concat(info.Transpilers)) .Count(p => p.owner == "blax.slaves"); T.Info("Harmony patches owned by blax.slaves: " + ours + ", DLCs: " + Fixtures.ActiveDlcs()); T.Check(ours >= 17, "expected at least 17 patches from blax.slaves, found " + ours); bool logged = Log.Messages.Any(m => m.text != null && m.text.Contains("[Blax Slaves]") && m.text.Contains("patches applied")); T.Check(logged, "startup line '[Blax Slaves] N patches applied' not in the log"); var errors = Log.Messages.Where(m => m.type == LogMessageType.Error && m.text != null && m.text.Contains("Blax")).ToList(); T.Check(errors.Count == 0, "errors mentioning Blax at startup: " + string.Join(" | ", errors.Take(3).Select(e => e.text))); // Any DLC combination loads quietly: no warning from the mod or about its defs either. var warnings = Log.Messages.Where(m => m.type == LogMessageType.Warning && m.text != null && (m.text.Contains("Blax Slaves") || m.text.Contains("BlaxSlaves"))).ToList(); T.Check(warnings.Count == 0, "warnings mentioning Blax Slaves at startup: " + string.Join(" | ", warnings.Take(3).Select(e => e.text))); } [BlaxTest("startup: every Blax Slaves finalizer returns void, so Harmony rethrows exceptions with their stack trace")] public static void FinalizersReturnVoid() { // One finalizer returning Exception on a method makes Harmony "throw ex" instead of "rethrow" // (0Harmony EmitFinalizers), which cuts the inner frames off every exception passing through it. var finalizers = Harmony.GetAllPatchedMethods() .Select(Harmony.GetPatchInfo) .Where(info => info != null) .SelectMany(info => info.Finalizers) .Where(p => p.owner == "blax.slaves") .ToList(); T.Check(finalizers.Count >= 9, "Blax Slaves finalizers found: " + finalizers.Count + ", expected at least 9"); var bad = finalizers.Where(p => p.PatchMethod.ReturnType != typeof(void)).Select(p => p.PatchMethod.DeclaringType?.Name + "." + p.PatchMethod.Name).ToList(); T.Check(bad.Count == 0, "finalizers that return a value: " + string.Join(", ", bad)); } [BlaxTest("startup: settings and defs loaded")] public static void DefsLoaded() { T.NotNull(BlaxSlavesMod.Settings, "BlaxSlavesMod.Settings"); T.NotNull(BlaxDefOf.BlaxSlaves_SlaveBrand, "hediff BlaxSlaves_SlaveBrand"); T.NotNull(DefDatabase.GetNamedSilentFail("BlaxSlaves_BrandSlave"), "recipe BlaxSlaves_BrandSlave"); T.NotNull(DefDatabase.GetNamedSilentFail("BlaxSlaves_RebrandSlave"), "recipe BlaxSlaves_RebrandSlave"); T.NotNull(DefDatabase.GetNamedSilentFail("BlaxSlaves_Branded"), "thought BlaxSlaves_Branded"); T.Check(ThingDefOf.Human.AllRecipes.Any(r => r.defName == "BlaxSlaves_BrandSlave"), "humans don't list the branding surgery"); } [BlaxTest("harness: colony slave fixture")] public static void SlaveFixture() { Pawn slave = Fixtures.ColonySlave(); T.Check(slave.Spawned, "slave not spawned"); T.Check(slave.IsSlaveOfColony, "fixture pawn is not a slave of the colony"); } /// /// A pawn left inside rock has no district, and vanilla AI throws when it ticks (a prisoner's /// escape check). Random quick-test maps sometimes have rock at the center, so a wall stands in for it. /// [BlaxTest("harness: test pawns spawn on an open cell, also when the map center is blocked")] public static void SpawnOnOpenCell() { Map map = Fixtures.Map; Pawn first = Fixtures.Colonist(); T.Check(Fixtures.IsOpen(first.Position, map), "test pawn " + first.LabelShort + " spawned on " + first.Position + ", which is not open (rock, fog or no district)"); IntVec3 center = map.Center; if (!Fixtures.IsOpen(center, map)) { T.Info("map center " + center + " is blocked on this map, so the spawn above already took the fallback"); return; } if (center.GetFirstPawn(map) != null || center.GetFirstItem(map) != null || center.GetEdifice(map) != null || center.Roofed(map)) T.Skip("setup: something stands on the map center " + center + " or it is roofed, so no wall can block it"); ThingDef wallDef = ThingDefOf.Wall; Thing wall = GenSpawn.Spawn(ThingMaker.MakeThing(wallDef, GenStuff.DefaultStuffFor(wallDef)), center, map); try { T.Check(!Fixtures.IsOpen(center, map), "setup: the map center " + center + " is still open with a wall on it"); Pawn p = Fixtures.Colonist(); T.Check(p.Position != center && Fixtures.IsOpen(p.Position, map), "with the map center walled off, test pawn " + p.LabelShort + " spawned on " + p.Position + ", which is not open"); } finally { if (!wall.Destroyed) wall.Destroy(DestroyMode.Vanish); } } [BlaxTest("harness: save and reload kept a marker pawn", phase: 2)] public static void ReloadWorked() { T.Check(T.Carry.TryGetValue("marker", out string id), "phase 1 marker missing"); T.Check(T.Carry.TryGetValue("marker.kind", out string kind), "phase 1 marker kind missing"); Pawn p = Fixtures.FindPawnById(id); T.NotNull(p, "marker pawn " + id + " after reload"); if (kind == "slave") T.Check(p.IsSlaveOfColony, "marker pawn lost slave status over save and load"); else T.Check(p.IsPrisonerOfColony, "marker pawn lost prisoner status over save and load"); } /// With Ideology a colony slave, without it a prisoner, so the save and reload check runs in every DLC set. [BlaxTest("harness: set up marker pawn for reload")] public static void MarkerSetup() { Pawn marker = Fixtures.Keep(ModsConfig.IdeologyActive ? Fixtures.ColonySlave() : Fixtures.ColonyPrisoner()); T.Carry["marker"] = marker.ThingID; T.Carry["marker.kind"] = ModsConfig.IdeologyActive ? "slave" : "prisoner"; } } }