198 lines
12 KiB
C#
198 lines
12 KiB
C#
using System.Linq;
|
|
using BlaxCastes;
|
|
using RimWorld;
|
|
using RimWorld.Planet;
|
|
using Verse;
|
|
|
|
namespace BlaxCastesTests
|
|
{
|
|
/// <summary>Save safety: purging, a component added mid-save, and state that must survive save and reload.</summary>
|
|
public static class PersistenceTests
|
|
{
|
|
[BlaxTest("save: Purge drops destroyed and discarded pawns and keeps a dead one its corpse still holds")]
|
|
public static void PurgeRules()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
Caste c = Fixtures.NewCaste("Purge test");
|
|
|
|
Pawn destroyed = Fixtures.Colonist();
|
|
Pawn discarded = Fixtures.Generate(PawnKindDefOf.Colonist, Faction.OfPlayer);
|
|
Pawn dead = Fixtures.Colonist();
|
|
Pawn alive = Fixtures.Colonist();
|
|
foreach (Pawn p in new[] { destroyed, discarded, dead, alive })
|
|
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed for " + p.LabelShort);
|
|
|
|
destroyed.Destroy(DestroyMode.Vanish);
|
|
Find.WorldPawns.PassToWorld(discarded);
|
|
Find.WorldPawns.RemoveAndDiscardPawnViaGC(discarded);
|
|
dead.Kill(null);
|
|
T.Check(destroyed.Destroyed && !destroyed.Dead, "setup: destroyed pawn is not destroyed-but-alive");
|
|
T.Check(discarded.Discarded, "setup: discarded pawn is not discarded");
|
|
T.Check(dead.Dead && dead.Corpse != null, "setup: dead pawn has no corpse");
|
|
|
|
int removed = comp.Purge();
|
|
T.Check(comp.MembershipOf(destroyed) == null, "Purge kept a destroyed, living pawn");
|
|
T.Check(comp.MembershipOf(discarded) == null, "Purge kept a discarded pawn");
|
|
T.Check(comp.CasteOf(dead) == c, "Purge dropped a dead pawn whose corpse still exists");
|
|
T.Check(comp.CasteOf(alive) == c, "Purge dropped a living colonist");
|
|
T.Check(removed >= 2, "Purge reported " + removed + " removed, expected at least 2");
|
|
}
|
|
|
|
[BlaxTest("save: a component created for a save without one (mod added mid-save) starts empty with five No caste rows")]
|
|
public static void FreshComponent()
|
|
{
|
|
var fresh = new GameComponent_BlaxCastes(Current.Game);
|
|
T.Equal(0, fresh.Castes.Count, "castes");
|
|
T.Equal(0, fresh.Memberships.Count, "memberships");
|
|
T.Equal(0, fresh.JoinQueue.Count, "queued joins");
|
|
T.Check(fresh.DefaultCaste == null, "default caste set");
|
|
T.Equal(1, fresh.NextCasteId, "next caste id");
|
|
T.Equal(JoinReason.Rows.Length, fresh.JoinRows.Count, "join rows");
|
|
T.Check(fresh.JoinRows.All(r => r.mode == JoinRowMode.NoCaste), "a join row of a fresh component is not No caste");
|
|
T.Check(GameComponent_BlaxCastes.Instance != fresh, "a stray component became Instance");
|
|
T.Equal(1, Current.Game.components.Count(x => x is GameComponent_BlaxCastes), "GameComponent_BlaxCastes in the game");
|
|
}
|
|
|
|
// Runs last in this class (source order): the castes, rows and pawns it keeps are checked after the reload.
|
|
[BlaxTest("save: set up castes, rows and memberships for the reload")]
|
|
public static void ReloadSetup()
|
|
{
|
|
Fixtures.KeepCasteState();
|
|
ApparelPolicy worker = Fixtures.Need(Fixtures.Outfit("OutfitWorker"), "Worker outfit");
|
|
FoodPolicy simple = Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food");
|
|
FoodPolicy paste = Fixtures.Need(Fixtures.Food("FoodRestrictionPaste"), "Paste food");
|
|
DrugPolicy noDrugs = Fixtures.Need(Fixtures.Drug("NoDrugs"), "No drugs");
|
|
ReadingPolicy none = Fixtures.Need(Fixtures.Reading("NoneReadingPolicy"), "None reading");
|
|
|
|
Caste full = Fixtures.KeepCaste(Fixtures.NewCaste("Reload full"));
|
|
CasteManager.SetSlot(full, CasteSlot.Apparel, worker);
|
|
CasteManager.SetSlot(full, CasteSlot.Food, simple);
|
|
CasteManager.SetSlot(full, CasteSlot.Drug, noDrugs);
|
|
CasteManager.SetSlot(full, CasteSlot.Reading, none);
|
|
CasteManager.SetSlot(full, CasteSlot.MedCare, MedicalCareCategory.HerbalOrWorse);
|
|
Caste partial = Fixtures.KeepCaste(Fixtures.NewCaste("Reload partial"));
|
|
CasteManager.SetSlot(partial, CasteSlot.Food, paste);
|
|
|
|
T.Check(CasteManager.SetDefault(partial), "SetDefault failed");
|
|
T.Check(CasteManager.SetJoinRow(JoinReason.BoughtSlave, JoinRowMode.SpecificCaste, full), "SetJoinRow boughtSlave failed");
|
|
T.Check(CasteManager.SetJoinRow(JoinReason.NewPrisoner, JoinRowMode.DefaultCaste), "SetJoinRow newPrisoner failed");
|
|
T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.NoCaste), "SetJoinRow newColonist failed");
|
|
|
|
Pawn member = Fixtures.Keep(Fixtures.Colonist());
|
|
Pawn casteless = Fixtures.Keep(Fixtures.Colonist());
|
|
Pawn prisoner = Fixtures.Keep(Fixtures.Prisoner());
|
|
Pawn gone = Fixtures.Colonist(); // destroyed by the cleanup after this test; its record must not survive the save
|
|
T.Check(CasteManager.Assign(member, full, true, JoinReason.NewColonist), "Assign member failed");
|
|
T.Check(CasteManager.Assign(casteless, full, false, JoinReason.Manual) && CasteManager.MakeCasteless(casteless), "casteless setup failed");
|
|
T.Check(CasteManager.Assign(prisoner, partial, true, JoinReason.NewPrisoner), "Assign prisoner failed");
|
|
T.Check(CasteManager.Assign(gone, full, false, JoinReason.Manual), "Assign gone failed");
|
|
|
|
T.Carry["fullId"] = full.id.ToString();
|
|
T.Carry["partialId"] = partial.id.ToString();
|
|
T.Carry["worker"] = worker.id.ToString();
|
|
T.Carry["simple"] = simple.id.ToString();
|
|
T.Carry["paste"] = paste.id.ToString();
|
|
T.Carry["noDrugs"] = noDrugs.id.ToString();
|
|
T.Carry["none"] = none.id.ToString();
|
|
T.Carry["member"] = member.ThingID;
|
|
T.Carry["casteless"] = casteless.ThingID;
|
|
T.Carry["prisoner"] = prisoner.ThingID;
|
|
T.Carry["gone"] = gone.ThingID;
|
|
// Later phase-1 tests (UiTests) still create and delete castes, so read the id at save time.
|
|
T.CarryAtSave("nextId", () => Fixtures.Comp.NextCasteId.ToString());
|
|
}
|
|
|
|
// ---- phase 2 -----------------------------------------------------------------------------
|
|
|
|
private static Caste CasteById(string key)
|
|
{
|
|
T.Check(T.Carry.TryGetValue(key, out string id), "phase 1 value " + key + " missing (did the setup run?)");
|
|
Caste c = Fixtures.Comp.Castes.FirstOrDefault(x => x.id.ToString() == id);
|
|
T.NotNull(c, "caste " + key + " (id " + id + ") after reload");
|
|
return c;
|
|
}
|
|
|
|
private static Pawn PawnById(string key)
|
|
{
|
|
T.Check(T.Carry.TryGetValue(key, out string id), "phase 1 value " + key + " missing (did the setup run?)");
|
|
return Fixtures.FindPawnById(id);
|
|
}
|
|
|
|
private static void CheckPolicy(object slotValue, string carryKey, string what)
|
|
{
|
|
T.Check(slotValue is Policy p, what + " is not a policy after reload: " + (slotValue ?? "null"));
|
|
Policy policy = (Policy)slotValue;
|
|
T.Equal(T.Carry[carryKey], policy.id.ToString(), what + " policy id");
|
|
T.Check(CasteUtility.PolicyExists(policy), what + " policy " + policy.label + " is not the game's own instance after reload");
|
|
}
|
|
|
|
[BlaxTest("save: castes, slots, default caste and join rows survive save and reload", phase: 2)]
|
|
public static void CastesSurvive()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
T.NotNull(comp, "GameComponent_BlaxCastes.Instance after reload");
|
|
Caste full = CasteById("fullId");
|
|
Caste partial = CasteById("partialId");
|
|
T.Equal("Reload full", full.label, "full caste label");
|
|
T.Equal("Reload partial", partial.label, "partial caste label");
|
|
|
|
CheckPolicy(full.ApparelPolicy, "worker", "full caste apparel");
|
|
CheckPolicy(full.FoodPolicy, "simple", "full caste food");
|
|
CheckPolicy(full.DrugPolicy, "noDrugs", "full caste drugs");
|
|
CheckPolicy(full.ReadingPolicy, "none", "full caste reading");
|
|
T.Equal((int)MedicalCareCategory.HerbalOrWorse, full.MedCare, "full caste medical care");
|
|
|
|
CheckPolicy(partial.FoodPolicy, "paste", "partial caste food");
|
|
T.Check(!partial.IsManaged(CasteSlot.Apparel) && !partial.IsManaged(CasteSlot.Drug) && !partial.IsManaged(CasteSlot.Reading) && !partial.IsManaged(CasteSlot.MedCare),
|
|
"partial caste gained managed slots over the reload");
|
|
|
|
T.Check(comp.DefaultCaste == partial, "default caste after reload: " + comp.DefaultCaste);
|
|
JoinRow bought = comp.GetRow(JoinReason.BoughtSlave);
|
|
T.Check(bought.mode == JoinRowMode.SpecificCaste && bought.caste == full, $"bought-as-slave row: {bought.mode} {bought.caste}");
|
|
T.Equal(JoinRowMode.DefaultCaste, comp.GetRow(JoinReason.NewPrisoner).mode, "new-prisoner row mode");
|
|
T.Check(comp.ResolveRow(JoinReason.NewPrisoner) == partial, "new-prisoner row does not resolve to the default caste");
|
|
T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.NewColonist).mode, "new-colonist row mode");
|
|
T.Equal(JoinReason.Rows.Length, comp.JoinRows.Count, "join rows after reload");
|
|
T.Equal(T.Carry["nextId"], comp.NextCasteId.ToString(), "next caste id after reload");
|
|
}
|
|
|
|
[BlaxTest("save: memberships survive save and reload, including a casteless-by-choice entry", phase: 2)]
|
|
public static void MembershipsSurvive()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
Caste full = CasteById("fullId");
|
|
Caste partial = CasteById("partialId");
|
|
|
|
Pawn member = PawnById("member");
|
|
T.NotNull(member, "member pawn after reload");
|
|
CasteMembership m = comp.MembershipOf(member);
|
|
T.NotNull(m, "member's record after reload");
|
|
T.Check(m.caste == full && m.auto && m.reason == JoinReason.NewColonist, $"member's record: caste={m.caste}, auto={m.auto}, reason={m.reason}");
|
|
T.Equal(0, CasteManager.Overrides(member).Count, "member's overrides after reload");
|
|
|
|
Pawn casteless = PawnById("casteless");
|
|
T.NotNull(casteless, "casteless pawn after reload");
|
|
CasteMembership cm = comp.MembershipOf(casteless);
|
|
T.NotNull(cm, "casteless-by-choice record after reload (lost, so the pawn looks like it never had a caste)");
|
|
T.Check(cm.Casteless, "casteless-by-choice pawn is in caste " + cm.caste);
|
|
|
|
Pawn prisoner = PawnById("prisoner");
|
|
T.NotNull(prisoner, "prisoner after reload");
|
|
T.Check(comp.CasteOf(prisoner) == partial, "prisoner's caste after reload: " + comp.CasteOf(prisoner));
|
|
|
|
T.Check(comp.Memberships.Keys.All(p => !GameComponent_BlaxCastes.IsGone(p)), "a purged-worthy pawn is still a key after reload");
|
|
string goneId = T.Carry["gone"];
|
|
T.Check(!comp.Memberships.Keys.Any(p => p.ThingID == goneId), "the destroyed pawn's record survived the save");
|
|
}
|
|
|
|
[BlaxTest("save: Instance is the loaded component and the game holds exactly one", phase: 2)]
|
|
public static void InstanceAfterReload()
|
|
{
|
|
GameComponent_BlaxCastes comp = Current.Game.GetComponent<GameComponent_BlaxCastes>();
|
|
T.NotNull(comp, "component after reload");
|
|
T.Check(GameComponent_BlaxCastes.Instance == comp, "Instance is not the loaded game's component");
|
|
T.Equal(1, Current.Game.components.Count(x => x is GameComponent_BlaxCastes), "GameComponent_BlaxCastes in the game");
|
|
}
|
|
}
|
|
}
|