rimworld-blax-castes/Tests/BlaxCastesTests/Source/CasteTests.cs

298 lines
19 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using BlaxCastes;
using RimWorld;
using Verse;
namespace BlaxCastesTests
{
/// <summary>Applying castes to pawns: the per-slot checks, the Follow rule, the sweep and the enabled switch.</summary>
public static class CasteTests
{
// ---- policies used across tests ------------------------------------------------------
/// <summary>The Slave outfit with Ideology, otherwise the Soldier outfit (both differ from Anything, Worker and Nudist).</summary>
private static ApparelPolicy SlaveOrSoldier() =>
Fixtures.Need(Fixtures.Outfit(ModsConfig.IdeologyActive ? "OutfitSlave" : "OutfitSoldier"), "Slave or Soldier outfit");
private static ApparelPolicy Worker() => Fixtures.Need(Fixtures.Outfit("OutfitWorker"), "Worker outfit");
private static ApparelPolicy Nudist() => Fixtures.Need(Fixtures.Outfit("OutfitNudist"), "Nudist outfit");
private static FoodPolicy Simple() => Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food policy");
private static FoodPolicy Paste() => Fixtures.Need(Fixtures.Food("FoodRestrictionPaste"), "Paste food policy");
private static DrugPolicy NoDrugs() => Fixtures.Need(Fixtures.Drug("NoDrugs"), "No drugs policy (sourceDef NoDrugs)");
private static DrugPolicy Social() => Fixtures.Need(Fixtures.Drug("SocialDrugs"), "Social drugs policy (sourceDef SocialDrugs)");
private static ReadingPolicy NoneReading() => Fixtures.Need(Fixtures.Reading("NoneReadingPolicy"), "None reading policy");
private static ReadingPolicy AllReading() => Fixtures.Need(Fixtures.Reading("AllReadingPolicy"), "All reading policy");
/// <summary>A caste managing all five slots with the given values.</summary>
private static Caste FullCaste(string label, ApparelPolicy apparel, FoodPolicy food, DrugPolicy drug, ReadingPolicy reading, MedicalCareCategory care)
{
Caste c = Fixtures.NewCaste(label);
CasteManager.SetSlot(c, CasteSlot.Apparel, apparel);
CasteManager.SetSlot(c, CasteSlot.Food, food);
CasteManager.SetSlot(c, CasteSlot.Drug, drug);
CasteManager.SetSlot(c, CasteSlot.Reading, reading);
CasteManager.SetSlot(c, CasteSlot.MedCare, care);
foreach (CasteSlot slot in CasteUtility.AllSlots)
T.Check(c.IsManaged(slot), "setup: " + slot + " slot of " + c.label + " is not managed after SetSlot");
return c;
}
private static string Values(Pawn p) =>
string.Join(", ", CasteUtility.AllSlots.Select(s => s + "=" + CasteUtility.ValueLabel(s, CasteUtility.CurrentValue(p, s))));
// ---- tests ---------------------------------------------------------------------------
[BlaxTest("join rows: a new game has no castes, no default caste and five No caste rows")]
public static void NewGameState()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
T.NotNull(comp, "GameComponent_BlaxCastes.Instance");
T.Equal(0, comp.Castes.Count, "castes in a new game");
T.Check(comp.DefaultCaste == null, "a new game has a default caste: " + comp.DefaultCaste);
foreach (string reason in JoinReason.Rows)
{
JoinRow row = comp.JoinRows.FirstOrDefault(r => r.reason == reason);
T.NotNull(row, "join row " + reason);
T.Equal(JoinRowMode.NoCaste, row.mode, "mode of join row " + reason);
T.Check(comp.ResolveRow(reason) == null, "join row " + reason + " resolves to a caste");
}
T.Equal(JoinReason.Rows.Length, comp.JoinRows.Count, "join rows in a new game");
}
[BlaxTest("castes: assigning sets apparel, food, drugs, reading and medical care on a colonist")]
public static void ApplyFiveSlots()
{
ApparelPolicy outfit = SlaveOrSoldier();
FoodPolicy food = Simple();
DrugPolicy drug = NoDrugs();
ReadingPolicy reading = NoneReading();
const MedicalCareCategory care = MedicalCareCategory.HerbalOrWorse;
Pawn p = Fixtures.Colonist();
// Start from values the caste doesn't use (a generated colonist's ideo can already give it No drugs).
p.outfits.CurrentApparelPolicy = Worker();
p.foodRestriction.CurrentFoodPolicy = Paste();
p.drugs.CurrentPolicy = Social();
p.reading.CurrentPolicy = AllReading();
p.playerSettings.medCare = MedicalCareCategory.Best;
T.Info("before: " + Values(p));
T.Check(p.outfits.CurrentApparelPolicy != outfit && p.foodRestriction.CurrentFoodPolicy != food && p.drugs.CurrentPolicy != drug
&& p.reading.CurrentPolicy != reading && p.playerSettings.medCare != care,
"setup: the colonist already has one of the caste's values, so the test proves nothing: " + Values(p));
Caste c = FullCaste("Slave test", outfit, food, drug, reading, care);
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a colonist");
T.Check(p.outfits.CurrentApparelPolicy == outfit, "apparel: " + Values(p));
T.Check(p.foodRestriction.CurrentFoodPolicy == food, "food: " + Values(p));
T.Check(p.drugs.CurrentPolicy == drug, "drugs: " + Values(p));
T.Check(p.reading.CurrentPolicy == reading, "reading: " + Values(p));
T.Equal(care, p.playerSettings.medCare, "medical care");
CasteMembership m = Fixtures.Comp.MembershipOf(p);
T.NotNull(m, "membership");
T.Check(m.caste == c && !m.auto && m.reason == JoinReason.Manual, $"membership: caste={m.caste}, auto={m.auto}, reason={m.reason}");
T.Equal(0, CasteManager.Overrides(p).Count, "overrides right after Assign");
}
[BlaxTest("castes: a colony slave gets all five slots (Ideology)")]
public static void SlaveGetsFiveSlots()
{
Pawn p = Fixtures.ColonySlave();
T.Check(p.outfits != null && p.drugs != null && p.reading != null, "setup: slave lacks a policy tracker");
p.outfits.CurrentApparelPolicy = Worker();
p.drugs.CurrentPolicy = Social();
p.reading.CurrentPolicy = AllReading();
p.foodRestriction.CurrentFoodPolicy = Paste();
p.playerSettings.medCare = MedicalCareCategory.Best;
Caste c = FullCaste("Slave caste test", SlaveOrSoldier(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds);
T.Check(CasteManager.Assign(p, c, true, JoinReason.NewSlave), "Assign returned false for a colony slave");
T.Check(p.outfits.CurrentApparelPolicy == SlaveOrSoldier() && p.foodRestriction.CurrentFoodPolicy == Simple()
&& p.drugs.CurrentPolicy == NoDrugs() && p.reading.CurrentPolicy == NoneReading(), "slave's policies: " + Values(p));
T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "slave's medical care");
}
[BlaxTest("castes: a player prisoner gets food and medical care only; its outfit, drug and reading trackers are untouched")]
public static void PrisonerFoodAndCareOnly()
{
Pawn p = Fixtures.Prisoner();
// An arrested colonist keeps the trackers a colonist has; a captured raider has none.
p.outfits ??= new Pawn_OutfitTracker(p);
p.drugs ??= new Pawn_DrugPolicyTracker(p);
p.reading ??= new Pawn_ReadingTracker(p);
p.outfits.CurrentApparelPolicy = Worker();
p.drugs.CurrentPolicy = Social();
p.reading.CurrentPolicy = AllReading();
T.NotNull(p.foodRestriction, "prisoner's food tracker");
T.NotNull(p.playerSettings, "prisoner's player settings");
FoodPolicy food = p.foodRestriction.CurrentFoodPolicy == Paste() ? Simple() : Paste();
MedicalCareCategory care = p.playerSettings.medCare == MedicalCareCategory.NoMeds ? MedicalCareCategory.NormalOrWorse : MedicalCareCategory.NoMeds;
Caste c = FullCaste("Prisoner test", Nudist(), food, NoDrugs(), NoneReading(), care);
T.Check(CasteManager.Assign(p, c, true, JoinReason.NewPrisoner), "Assign returned false for a player prisoner");
T.Check(p.foodRestriction.CurrentFoodPolicy == food, "food not set: " + Values(p));
T.Equal(care, p.playerSettings.medCare, "medical care");
T.Check(p.outfits.CurrentApparelPolicy == Worker(), "prisoner's outfit changed: " + Values(p));
T.Check(p.drugs.CurrentPolicy == Social(), "prisoner's drug policy changed: " + Values(p));
T.Check(p.reading.CurrentPolicy == AllReading(), "prisoner's reading policy changed: " + Values(p));
T.Equal(0, CasteManager.Overrides(p).Count, "overrides on a prisoner (slots it can't take are never overrides)");
}
[BlaxTest("castes: a quest lodger gets food and medical care, not apparel, drugs or reading")]
public static void QuestLodgerSkips()
{
Pawn p = Fixtures.QuestLodger();
p.outfits.CurrentApparelPolicy = Worker();
p.drugs.CurrentPolicy = Social();
p.reading.CurrentPolicy = AllReading();
p.foodRestriction.CurrentFoodPolicy = Paste();
p.playerSettings.medCare = MedicalCareCategory.Best;
Caste c = FullCaste("Lodger test", Nudist(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds);
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a quest lodger");
T.Check(p.outfits.CurrentApparelPolicy == Worker(), "lodger's outfit changed: " + Values(p));
T.Check(p.drugs.CurrentPolicy == Social(), "lodger's drug policy changed: " + Values(p));
T.Check(p.reading.CurrentPolicy == AllReading(), "lodger's reading policy changed: " + Values(p));
T.Check(p.foodRestriction.CurrentFoodPolicy == Simple(), "lodger's food not set: " + Values(p));
T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "lodger's medical care");
}
[BlaxTest("castes: a baby gets apparel, drugs, reading and medical care, not food")]
public static void BabySkipsFood()
{
Pawn p = Fixtures.Baby();
T.Check(p.outfits != null && p.drugs != null && p.reading != null && p.playerSettings != null && p.foodRestriction != null,
"setup: baby lacks a tracker: outfits=" + (p.outfits != null) + " drugs=" + (p.drugs != null) + " reading=" + (p.reading != null)
+ " playerSettings=" + (p.playerSettings != null) + " food=" + (p.foodRestriction != null));
p.outfits.CurrentApparelPolicy = Worker();
p.drugs.CurrentPolicy = Social();
p.reading.CurrentPolicy = AllReading();
p.foodRestriction.CurrentFoodPolicy = Paste();
p.playerSettings.medCare = MedicalCareCategory.Best;
Caste c = FullCaste("Baby test", Nudist(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds);
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a baby");
T.Check(p.foodRestriction.CurrentFoodPolicy == Paste(), "baby's food changed: " + Values(p));
T.Check(p.outfits.CurrentApparelPolicy == Nudist(), "baby's outfit not set: " + Values(p));
T.Check(p.drugs.CurrentPolicy == NoDrugs(), "baby's drug policy not set: " + Values(p));
T.Check(p.reading.CurrentPolicy == NoneReading(), "baby's reading policy not set: " + Values(p));
T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "baby's medical care");
}
[BlaxTest("castes: changing a slot moves members on the old value and keeps hand changes (Follow rule)")]
public static void FollowRule()
{
ApparelPolicy first = SlaveOrSoldier();
Caste c = Fixtures.NewCaste("Follow test");
CasteManager.SetSlot(c, CasteSlot.Apparel, first);
Pawn a = Fixtures.Colonist();
Pawn b = Fixtures.Colonist();
T.Check(CasteManager.Assign(a, c, false, JoinReason.Manual) && CasteManager.Assign(b, c, false, JoinReason.Manual), "Assign failed");
T.Check(a.outfits.CurrentApparelPolicy == first && b.outfits.CurrentApparelPolicy == first, "setup: members not on " + first.label);
b.outfits.CurrentApparelPolicy = Worker(); // by hand
List<CasteSlot> overrides = CasteManager.Overrides(b);
T.Check(overrides.Count == 1 && overrides[0] == CasteSlot.Apparel, "B's overrides: " + string.Join(", ", overrides));
int moved = CasteManager.SetSlot(c, CasteSlot.Apparel, Nudist());
T.Equal(1, moved, "members moved by the apparel change");
T.Check(a.outfits.CurrentApparelPolicy == Nudist(), "A did not follow the caste: " + a.outfits.CurrentApparelPolicy.label);
T.Check(b.outfits.CurrentApparelPolicy == Worker(), "B lost its hand-set outfit: " + b.outfits.CurrentApparelPolicy.label);
T.Check(c.ApparelPolicy == Nudist(), "caste slot not changed");
// A slot that was unmanaged has no old value: every member gets it.
moved = CasteManager.SetSlot(c, CasteSlot.Food, Simple());
T.Equal(2, moved, "members moved when the food slot became managed");
T.Check(a.foodRestriction.CurrentFoodPolicy == Simple() && b.foodRestriction.CurrentFoodPolicy == Simple(), "food not set on both members");
// Medical care follows the same rule.
CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.HerbalOrWorse);
b.playerSettings.medCare = MedicalCareCategory.Best;
moved = CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.NoMeds);
T.Equal(1, moved, "members moved by the medical care change");
T.Equal(MedicalCareCategory.NoMeds, a.playerSettings.medCare, "A's medical care");
T.Equal(MedicalCareCategory.Best, b.playerSettings.medCare, "B's hand-set medical care");
// Unmanaging a slot changes nobody.
moved = CasteManager.SetSlot(c, CasteSlot.Food, null);
T.Equal(0, moved, "members moved by unmanaging food");
T.Check(!c.IsManaged(CasteSlot.Food), "food slot still managed");
T.Check(a.foodRestriction.CurrentFoodPolicy == Simple() && b.foodRestriction.CurrentFoodPolicy == Simple(), "unmanaging food changed a member");
}
[BlaxTest("castes: with the enabled setting off, assigning and slot changes write no policy")]
public static void DisabledAppliesNothing()
{
Pawn p = Fixtures.Colonist();
Caste c = FullCaste("Disabled test", SlaveOrSoldier(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.HerbalOrWorse);
string before = Values(p);
Fixtures.Settings.enabled = false;
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false while disabled");
T.Equal(before, Values(p), "policies after Assign while disabled");
T.Equal(0, CasteUtility.Apply(p, c).Count, "slots Apply wrote while disabled");
T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Apparel, Nudist()), "members moved by SetSlot while disabled");
T.Equal(before, Values(p), "policies after SetSlot while disabled");
T.Check(Fixtures.Comp.CasteOf(p) == c, "membership not recorded while disabled");
Fixtures.Settings.enabled = true;
T.Check(CasteManager.ResetToCaste(p), "ResetToCaste returned false");
T.Check(p.outfits.CurrentApparelPolicy == Nudist() && p.foodRestriction.CurrentFoodPolicy == Simple(), "re-enabled ResetToCaste did not apply: " + Values(p));
}
[BlaxTest("castes: the 2500-tick sweep ends the membership of a pawn that left the faction")]
public static void SweepEndsMembership()
{
Caste c = Fixtures.NewCaste("Sweep test");
CasteManager.SetSlot(c, CasteSlot.Food, Simple());
Pawn leaver = Fixtures.Colonist();
Pawn stayer = Fixtures.Colonist();
foreach (Pawn p in new[] { leaver, stayer })
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed for " + p.LabelShort);
Faction other = Fixtures.FriendlyFaction();
T.NotNull(other, "a friendly faction");
leaver.SetFaction(other);
T.Check(Fixtures.Comp.MembershipOf(leaver) != null, "membership ended before the sweep ran");
Fixtures.TickThroughSweep();
T.Check(Fixtures.Comp.MembershipOf(leaver) == null, "the sweep kept the membership of a pawn of " + other.Name);
T.Check(Fixtures.Comp.CasteOf(stayer) == c, "the sweep ended a colonist's membership");
// A player prisoner stays a member. Swept directly: a fixture prisoner outside a prison cell makes
// vanilla's prisoner job givers throw when the game ticks.
Pawn prisoner = Fixtures.Prisoner();
T.Check(CasteManager.Assign(prisoner, c, false, JoinReason.Manual), "Assign failed for the prisoner");
Fixtures.Comp.Sweep();
T.Check(Fixtures.Comp.CasteOf(prisoner) == c, "the sweep ended a player prisoner's membership");
prisoner.guest.SetGuestStatus(null);
prisoner.SetFaction(other);
Fixtures.Comp.Sweep();
T.Check(Fixtures.Comp.MembershipOf(prisoner) == null, "the sweep kept the membership of a released prisoner of " + other.Name);
}
[BlaxTest("castes: a policy removed from its database is skipped and unmanaged, without errors")]
public static void MissingPolicySkipped()
{
ApparelPolicy temp = Current.Game.outfitDatabase.MakeNewOutfit();
temp.label = "BlaxCastes test outfit";
Caste c = Fixtures.NewCaste("Missing policy test");
CasteManager.SetSlot(c, CasteSlot.Apparel, temp);
CasteManager.SetSlot(c, CasteSlot.Food, Simple());
T.Check(c.ApparelPolicy == temp, "setup: apparel slot not set");
Pawn p = Fixtures.Colonist();
ApparelPolicy before = p.outfits.CurrentApparelPolicy;
Current.Game.outfitDatabase.AllOutfits.Remove(temp); // a mod deleting it without TryDelete
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed");
T.Check(p.outfits.CurrentApparelPolicy == before, "the removed outfit was written: " + p.outfits.CurrentApparelPolicy.label);
T.Check(p.foodRestriction.CurrentFoodPolicy == Simple(), "the food slot was not written next to the missing outfit");
T.Check(!c.IsManaged(CasteSlot.Apparel), "the missing outfit is still managed");
}
}
}