.GetNamedSilentFail(thingDefName), "ThingDef " + thingDefName));
+
+ public static StorageKind StockpileKind => StorageKinds.ForStockpile(StorageKinds.VanillaStockpile);
+
+ public static StorageKind DumpingKind => StorageKinds.ForStockpile(StorageKinds.VanillaDumping);
+
+ // ---- defs ------------------------------------------------------------------------
+
+ /// A def that must exist; fails the test otherwise.
+ public static TD NeedDef| (TD def, string what) where TD : Def
+ {
+ T.NotNull(def, what);
+ return def;
+ }
+
+ public static ThingDef Thing(string defName) => NeedDef(DefDatabase.GetNamedSilentFail(defName), "ThingDef " + defName);
+
+ /// Skips the test unless the DLC is active: "needs Biotech".
+ public static void NeedDlc(string dlcPackageId)
+ {
+ if (!DlcSupport.IsActive(dlcPackageId))
+ T.Skip("needs " + DlcSupport.DlcName(dlcPackageId));
+ }
+
+ // ---- map, things, zones ----------------------------------------------------------
+
+ ///
+ /// A free area of this size near the map centre: in bounds, away from the edge, unfogged, standable,
+ /// heavy-supporting terrain, no zone, nothing but grass or filth, which is removed. Tracked: buildings,
+ /// blueprints, frames, items and zones in it are removed after the test (kept ones stay).
+ ///
+ public static CellRect EmptyArea(int width, int height)
+ {
+ Map map = Map;
+ foreach (IntVec3 origin in GenRadial.RadialCellsAround(map.Center, 40f, useCenter: true))
+ {
+ var rect = new CellRect(origin.x, origin.z, width, height);
+ if (rect.Cells.All(c => Free(c, map)))
+ {
+ foreach (IntVec3 c in rect.Cells)
+ foreach (Thing t in map.thingGrid.ThingsListAt(c).ToList())
+ t.Destroy(DestroyMode.Vanish);
+ areas.Add(rect);
+ return rect;
+ }
+ }
+ T.Fail("no empty " + width + "x" + height + " area near the map centre");
+ return default;
+ }
+
+ private static bool Free(IntVec3 c, Map map) =>
+ c.InBounds(map) && !c.CloseToEdge(map, 5) && !c.Fogged(map) && c.Standable(map)
+ && c.SupportsStructureType(map, TerrainAffordanceDefOf.Heavy) && c.GetZone(map) == null
+ && map.thingGrid.ThingsListAt(c).All(t => (t is Plant plant && !plant.def.plant.IsTree) || t is Filth)
+ && !areas.Any(r => r.Contains(c));
+
+ /// Removes the thing after the test unless kept.
+ public static TT Track(TT thing) where TT : Thing
+ {
+ if (thing != null) made.Add(thing);
+ return thing;
+ }
+
+ public static TZ TrackZone(TZ zone) where TZ : Zone
+ {
+ if (zone != null) madeZones.Add(zone);
+ return zone;
+ }
+
+ /// Exclude a thing from the per-test cleanup (phase-1 setup for phase-2 checks).
+ public static TT Keep(TT thing) where TT : Thing
+ {
+ kept.Add(thing);
+ return thing;
+ }
+
+ public static TZ KeepZone(TZ zone) where TZ : Zone
+ {
+ keptZones.Add(zone);
+ return zone;
+ }
+
+ /// A spawned free colonist of the quick-test colony, to complete frames with.
+ public static Pawn Worker()
+ {
+ Pawn p = Map.mapPawns.FreeColonistsSpawned.FirstOrDefault();
+ T.NotNull(p, "a spawned free colonist");
+ return p;
+ }
+
+ public static Thing FindThingById(string thingId)
+ {
+ if (thingId == null) return null;
+ foreach (Map m in Find.Maps)
+ foreach (Thing t in m.listerThings.AllThings)
+ if (t.ThingID == thingId) return t;
+ return null;
+ }
+
+ public static Zone FindZoneById(int id)
+ {
+ foreach (Map m in Find.Maps)
+ foreach (Zone z in m.zoneManager.AllZones)
+ if (z.ID == id) return z;
+ return null;
+ }
+
+ // ---- time ------------------------------------------------------------------------
+
+ public static void Tick(int ticks)
+ {
+ for (int i = 0; i < ticks; i++) Find.TickManager.DoSingleTick();
+ }
+
+ public static void QuietStoryteller()
+ {
+ Find.Storyteller.incidentQueue.Clear();
+ Find.Storyteller.storytellerComps.Clear();
+ }
+
+ // ---- settings and cleanup (used by the runner) -----------------------------------
+
+ public static Dictionary SnapshotSettings()
+ {
+ keepLibrary = false;
+ librarySnapshot = DefaultsLibrary.Snapshot();
+ var d = new Dictionary();
+ if (Settings == null) return d;
+ foreach (FieldInfo f in typeof(BlaxDefaultsSettings).GetFields(BindingFlags.Public | BindingFlags.Instance))
+ {
+ object v = f.GetValue(Settings);
+ d[f] = v is List list ? new List(list) : v;
+ }
+ return d;
+ }
+
+ public static void RestoreSettings(Dictionary snapshot)
+ {
+ if (Settings != null)
+ {
+ foreach (var kv in snapshot)
+ kv.Key.SetValue(Settings, kv.Value is List list ? new List(list) : kv.Value);
+ }
+ if (!keepLibrary && librarySnapshot != null)
+ DefaultsLibrary.RestoreForTests(librarySnapshot);
+ keepLibrary = false;
+ }
+
+ public static void Cleanup()
+ {
+ foreach (Thing t in made)
+ {
+ if (t != null && !kept.Contains(t) && !t.Destroyed && t.Spawned)
+ t.Destroy(DestroyMode.Vanish);
+ }
+ made.Clear();
+
+ foreach (Zone z in madeZones)
+ {
+ if (z != null && !keptZones.Contains(z) && z.zoneManager != null && z.zoneManager.AllZones.Contains(z))
+ z.Delete(playSound: false);
+ }
+ madeZones.Clear();
+
+ Map map = Map;
+ if (map != null)
+ {
+ foreach (CellRect rect in areas)
+ {
+ foreach (IntVec3 c in rect.Cells)
+ {
+ if (!c.InBounds(map)) continue;
+ Zone z = c.GetZone(map);
+ if (z != null && !keptZones.Contains(z))
+ z.Delete(playSound: false);
+ foreach (Thing t in map.thingGrid.ThingsListAt(c).ToList())
+ {
+ if (t is Pawn || kept.Contains(t) || t.Destroyed) continue;
+ if (t is Building || t is Blueprint || t is Frame || t.def.EverHaulable)
+ t.Destroy(DestroyMode.Vanish);
+ }
+ }
+ }
+ }
+ areas.Clear();
+ }
+ }
+}
diff --git a/Tests/BlaxDefaultsTests/Source/Harness.cs b/Tests/BlaxDefaultsTests/Source/Harness.cs
new file mode 100644
index 0000000..723fcf2
--- /dev/null
+++ b/Tests/BlaxDefaultsTests/Source/Harness.cs
@@ -0,0 +1,316 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using RimWorld;
+using UnityEngine;
+using Verse;
+
+namespace BlaxDefaultsTests
+{
+ ///
+ /// Marks a public static void method without parameters as a test. Phase 1 runs in a fresh
+ /// quick-test colony. Phase 2 runs after the runner saved that game and loaded it again.
+ ///
+ [AttributeUsage(AttributeTargets.Method)]
+ public sealed class BlaxTestAttribute : Attribute
+ {
+ public readonly string Name;
+ public readonly int Phase;
+
+ public BlaxTestAttribute(string name, int phase = 1)
+ {
+ Name = name;
+ Phase = phase;
+ }
+ }
+
+ public class TestFailure : Exception
+ {
+ public TestFailure(string message) : base(message) { }
+ }
+
+ public class TestSkipped : Exception
+ {
+ public TestSkipped(string message) : base(message) { }
+ }
+
+ /// Assertions and notes for test bodies.
+ public static class T
+ {
+ /// Values carried from phase 1 into phase 2. Store ThingIDs or def names, never objects.
+ public static readonly Dictionary Carry = new Dictionary();
+
+ /// Carry values read right before the save, after every phase-1 test ran.
+ internal static readonly Dictionary> CarryAtSaveTime = new Dictionary |