281 lines
18 KiB
C#
281 lines
18 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BlaxCastes;
|
|
using HarmonyLib;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxCastesTests
|
|
{
|
|
/// <summary>
|
|
/// Starter castes on a loaded save. These seed a fresh component that is not part of the game, so the
|
|
/// running game's castes stay as they are. The new-game seeding is checked by CasteTests.NewGameState.
|
|
/// </summary>
|
|
public static class StarterTests
|
|
{
|
|
private static FoodPolicy Simple() => Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food policy");
|
|
|
|
private static GameComponent_BlaxCastes Fresh() => new GameComponent_BlaxCastes(Current.Game);
|
|
|
|
private static Caste ByLabel(GameComponent_BlaxCastes comp, string label) => comp.Castes.FirstOrDefault(c => c.label == label);
|
|
|
|
private static string SeededMessageText(GameComponent_BlaxCastes comp)
|
|
{
|
|
string names = comp.Castes.Select(c => c.label).ToCommaList(useAnd: true);
|
|
return ((string)"BlaxCastes_StarterSeededMessage".Translate(names.Named("CASTES"))).CapitalizeFirst();
|
|
}
|
|
|
|
private static int MessagesWithText(string text) =>
|
|
Find.Archive.ArchivablesListForReading.OfType<Message>().Count(m => m.text == text);
|
|
|
|
[BlaxTest("starter: loading a save without castes adds them once, wires the rows, posts one message and moves nobody")]
|
|
public static void SeedOnLoad()
|
|
{
|
|
Messages.Clear();
|
|
GameComponent_BlaxCastes fresh = Fresh();
|
|
T.Equal(0, fresh.Castes.Count, "setup: castes of a fresh component");
|
|
fresh.LoadedGame();
|
|
T.Check(fresh.StarterCastesSeeded, "the save is not marked as seeded");
|
|
T.Equal(ModsConfig.IdeologyActive ? 3 : 2, fresh.Castes.Count, "castes after the first load (" + string.Join(", ", fresh.Castes.Select(c => c.label)) + ")");
|
|
Caste colonist = ByLabel(fresh, StarterCastes.ColonistLabel);
|
|
Caste prisoner = ByLabel(fresh, StarterCastes.PrisonerLabel);
|
|
T.NotNull(colonist, "Colonist caste");
|
|
T.NotNull(prisoner, "Prisoner caste");
|
|
T.Check(fresh.DefaultCaste == colonist, "default caste: " + fresh.DefaultCaste);
|
|
T.Check(fresh.ResolveRow(JoinReason.NewColonist) == colonist && fresh.ResolveRow(JoinReason.BoughtColonist) == colonist, "colonist rows do not resolve to Colonist");
|
|
T.Check(fresh.ResolveRow(JoinReason.NewPrisoner) == prisoner, "new-prisoner row does not resolve to Prisoner");
|
|
if (ModsConfig.IdeologyActive)
|
|
{
|
|
Caste slave = ByLabel(fresh, StarterCastes.SlaveLabel);
|
|
T.NotNull(slave, "Slave caste");
|
|
T.Check(fresh.ResolveRow(JoinReason.NewSlave) == slave && fresh.ResolveRow(JoinReason.BoughtSlave) == slave, "slave rows do not resolve to Slave");
|
|
}
|
|
T.Equal(0, fresh.Memberships.Count, "memberships after seeding on load (nobody moves)");
|
|
T.Check(fresh.FirstLoadMessageShown, "the seeding message does not count as the first-load message");
|
|
string text = SeededMessageText(fresh);
|
|
T.Equal(1, MessagesWithText(text), "seeding messages after the first load");
|
|
|
|
int count = fresh.Castes.Count;
|
|
fresh.LoadedGame();
|
|
T.Equal(count, fresh.Castes.Count, "castes after a second load");
|
|
T.Equal(1, MessagesWithText(text), "seeding messages after a second load");
|
|
}
|
|
|
|
[BlaxTest("starter: without the Caste column (as the game started), the seeding message points to the mod settings instead of the missing button")]
|
|
public static void SeededMessageWithoutColumn()
|
|
{
|
|
if (!CasteColumns.Shown)
|
|
T.Skip("the Caste column is not in the Assign tab in this run");
|
|
Messages.Clear();
|
|
GameComponent_BlaxCastes fresh = Fresh();
|
|
// The setting is on again, but the column waits for a restart: it is the column that counts.
|
|
CasteColumns.Apply(false);
|
|
try
|
|
{
|
|
// The message history keeps earlier tests' messages, so look only at what this load adds.
|
|
var before = new HashSet<Message>(Find.Archive.ArchivablesListForReading.OfType<Message>());
|
|
fresh.LoadedGame();
|
|
List<string> added = Find.Archive.ArchivablesListForReading.OfType<Message>().Where(m => !before.Contains(m)).Select(m => m.text).ToList();
|
|
T.Check(fresh.StarterCastesSeeded, "the save is not marked as seeded");
|
|
string names = fresh.Castes.Select(c => c.label).ToCommaList(useAnd: true);
|
|
string expected = ((string)"BlaxCastes_StarterSeededMessageNoColumn".Translate(names.Named("CASTES"))).CapitalizeFirst();
|
|
T.Equal(expected, ((string)StarterCastes.SeededMessageText(fresh)).CapitalizeFirst(), "seeding message text without the column");
|
|
T.Equal(1, added.Count(t => t == expected), "seeding messages without the column (added: " + string.Join(" | ", added) + ")");
|
|
T.Equal(0, added.Count(t => t == SeededMessageText(fresh)), "messages pointing to Assign > Manage castes after a load without the column");
|
|
}
|
|
finally
|
|
{
|
|
CasteColumns.Apply(true);
|
|
}
|
|
|
|
// The setting turned off in this session waits for a restart: the column is there, so the message points to it.
|
|
Fixtures.Settings.showAssignColumn = false;
|
|
T.Equal(SeededMessageText(fresh), ((string)StarterCastes.SeededMessageText(fresh)).CapitalizeFirst(), "seeding message text with the setting off and the column still shown");
|
|
}
|
|
|
|
[BlaxTest("starter: a save that already has castes, or seedStarterCastes off, gets no starter castes")]
|
|
public static void NoSeedWhenCastesOrOff()
|
|
{
|
|
GameComponent_BlaxCastes withCastes = Fresh();
|
|
MethodInfo add = AccessTools.Method(typeof(GameComponent_BlaxCastes), "AddCaste");
|
|
T.NotNull(add, "GameComponent_BlaxCastes.AddCaste");
|
|
add.Invoke(withCastes, new object[] { new Caste(1, "Own caste") });
|
|
T.Check(!StarterCastes.SeedOnLoad(withCastes), "seeded a save that already has a caste");
|
|
T.Equal(1, withCastes.Castes.Count, "castes of a save that had one");
|
|
T.Check(withCastes.StarterCastesSeeded, "a save with castes is not marked, so deleting them later would bring the starters back");
|
|
|
|
Fixtures.Settings.seedStarterCastes = false;
|
|
GameComponent_BlaxCastes off = Fresh();
|
|
T.Check(!StarterCastes.SeedOnLoad(off) && !StarterCastes.SeedNewGame(off), "seeded with seedStarterCastes off");
|
|
T.Equal(0, off.Castes.Count, "castes with seedStarterCastes off");
|
|
T.Check(!off.StarterCastesSeeded, "marked as seeded with the setting off, so turning it on later would do nothing");
|
|
}
|
|
|
|
[BlaxTest("starter: with the Slave outfit gone before seeding, the Slave caste leaves apparel unmanaged, and Create Slave outfit makes one; without Ideology there is no button and no Slave outfit")]
|
|
public static void MissingSlaveOutfit()
|
|
{
|
|
if (!ModsConfig.IdeologyActive)
|
|
{
|
|
// The Ideology-off contract: no Create Slave outfit button, nothing created, no outfit counts as the Slave outfit.
|
|
T.Check(!StarterCastes.CanCreateSlaveOutfit, "Create Slave outfit is offered without Ideology");
|
|
int before = Current.Game.outfitDatabase.AllOutfits.Count;
|
|
T.Check(StarterCastes.CreateSlaveOutfit() == null, "Create Slave outfit made an outfit without Ideology");
|
|
T.Equal(before, Current.Game.outfitDatabase.AllOutfits.Count, "outfits after Create Slave outfit without Ideology");
|
|
T.Check(StarterCastes.SlaveOutfit() == null, "found a Slave outfit without Ideology");
|
|
List<string> slaveLike = Current.Game.outfitDatabase.AllOutfits.Where(o => StarterCastes.IsSlaveOutfit(o)).Select(o => o.label).ToList();
|
|
T.Check(slaveLike.Count == 0, "outfits that count as the Slave outfit without Ideology: " + string.Join(", ", slaveLike));
|
|
return;
|
|
}
|
|
ApparelPolicy slaveOutfit = Fixtures.Need(StarterCastes.SlaveOutfit(), "Slave outfit");
|
|
T.Check(!StarterCastes.CanCreateSlaveOutfit, "Create Slave outfit is offered while the game has one");
|
|
var outfits = Current.Game.outfitDatabase.AllOutfits;
|
|
int at = outfits.IndexOf(slaveOutfit);
|
|
GameComponent_BlaxCastes fresh = Fresh();
|
|
ApparelPolicy created = null;
|
|
try
|
|
{
|
|
outfits.RemoveAt(at);
|
|
T.Check(StarterCastes.SeedNewGame(fresh), "did not seed");
|
|
T.Check(StarterCastes.CanCreateSlaveOutfit, "Create Slave outfit is not offered without a Slave outfit");
|
|
created = StarterCastes.CreateSlaveOutfit();
|
|
T.NotNull(created, "the created Slave outfit");
|
|
T.Check(outfits.Contains(created) && StarterCastes.IsSlaveOutfit(created), "the created outfit is not in the database under the Slave outfit's name: " + created.label);
|
|
T.Check(created != slaveOutfit, "Create Slave outfit returned the removed outfit"); // the id may repeat: the removed outfit can be the newest
|
|
ThingDef slaveApparel = DefDatabase<ThingDef>.AllDefs.FirstOrDefault(d => d.apparel?.defaultOutfitTags != null && d.apparel.defaultOutfitTags.Contains("Slave"));
|
|
if (slaveApparel != null)
|
|
T.Check(created.filter.Allows(slaveApparel), "the created outfit does not allow " + slaveApparel.defName);
|
|
T.Check(!created.filter.Allows(ThingDefOf.Apparel_Parka), "the created outfit allows a parka");
|
|
T.Check(StarterCastes.CreateSlaveOutfit() == created, "a second click made another outfit");
|
|
}
|
|
finally
|
|
{
|
|
if (created != null)
|
|
outfits.Remove(created);
|
|
outfits.Insert(at, slaveOutfit);
|
|
}
|
|
Caste slave = ByLabel(fresh, StarterCastes.SlaveLabel);
|
|
T.NotNull(slave, "Slave caste");
|
|
T.Check(!slave.IsManaged(CasteSlot.Apparel), "the Slave caste manages apparel without a Slave outfit: " + slave.ApparelPolicy?.label);
|
|
T.Check(slave.FoodPolicy == Simple(), "the Slave caste lost its food next to the missing outfit");
|
|
T.Check(slave.DrugPolicy?.sourceDef?.defName == StarterCastes.NoDrugsSourceDef, "the Slave caste lost No drugs next to the missing outfit");
|
|
T.Check("BlaxCastes_CreateSlaveOutfit".CanTranslate() && "BlaxCastes_CreateSlaveOutfitTip".CanTranslate(), "Create Slave outfit strings missing from Keyed");
|
|
}
|
|
|
|
[BlaxTest("starter: the Colonist caste copies the default drug policy, so a drug-prohibiting ideo's No drugs stays")]
|
|
public static void ColonistKeepsIdeoDrugDefault()
|
|
{
|
|
DrugPolicyDatabase db = Current.Game.drugPolicyDatabase;
|
|
DrugPolicy noDrugs = Fixtures.Need(StarterCastes.NoDrugs(), "No drugs policy (source def NoDrugs)");
|
|
DrugPolicy before = db.DefaultDrugPolicy();
|
|
GameComponent_BlaxCastes fresh = Fresh();
|
|
try
|
|
{
|
|
db.SetDefault(noDrugs);
|
|
T.Check(StarterCastes.SeedNewGame(fresh), "did not seed");
|
|
}
|
|
finally
|
|
{
|
|
db.SetDefault(before);
|
|
}
|
|
Caste colonist = ByLabel(fresh, StarterCastes.ColonistLabel);
|
|
T.NotNull(colonist, "Colonist caste");
|
|
T.Check(colonist.DrugPolicy == noDrugs, "Colonist drugs: " + colonist.DrugPolicy?.label + ", expected " + noDrugs.label);
|
|
T.Check(db.DefaultDrugPolicy() == before, "the test did not restore the default drug policy");
|
|
}
|
|
|
|
[BlaxTest("starter: the Slave outfit warning names the vanilla Slave outfit only")]
|
|
public static void SlaveOutfitWarning()
|
|
{
|
|
ApparelPolicy worker = Fixtures.Need(Fixtures.Outfit("OutfitWorker"), "Worker outfit");
|
|
T.Check(!StarterCastes.IsSlaveOutfit(worker), "the Worker outfit counts as the Slave outfit");
|
|
T.Check(!StarterCastes.IsSlaveOutfit(null), "null counts as the Slave outfit");
|
|
T.Check("BlaxCastes_SlaveOutfitCold".CanTranslate(), "the cold warning is missing from Keyed");
|
|
if (!ModsConfig.IdeologyActive)
|
|
{
|
|
T.Check(Current.Game.outfitDatabase.AllOutfits.All(o => !StarterCastes.IsSlaveOutfit(o)), "an outfit counts as the Slave outfit without Ideology, so the cold warning would show");
|
|
return;
|
|
}
|
|
T.Check(StarterCastes.IsSlaveOutfit(StarterCastes.SlaveOutfit()), "the Slave outfit does not count as the Slave outfit");
|
|
}
|
|
|
|
[BlaxTest("starter: a save seeded without Ideology owes the Slave caste and gets it once on its first load with Ideology, with a Slave outfit when the save has none, picked in the slave rows; nobody moves")]
|
|
public static void SlaveCasteLater()
|
|
{
|
|
Messages.Clear();
|
|
GameComponent_BlaxCastes fresh = Fresh();
|
|
T.Check(StarterCastes.SeedNewGame(fresh), "did not seed");
|
|
// Its loads must not post the first-load message into the game's history (the UI tests count it).
|
|
AccessTools.Method(typeof(GameComponent_BlaxCastes), "MarkFirstLoadMessageShown").Invoke(fresh, null);
|
|
if (!ModsConfig.IdeologyActive)
|
|
{
|
|
T.Check(fresh.SlaveCasteOwed, "a save seeded without Ideology does not owe the Slave caste");
|
|
fresh.LoadedGame();
|
|
T.Equal(2, fresh.Castes.Count, "castes after a load without Ideology (" + string.Join(", ", fresh.Castes.Select(c => c.label)) + ")");
|
|
T.Check(fresh.SlaveCasteOwed, "a load without Ideology cleared the owed Slave caste");
|
|
T.Check(StarterCastes.AddSlaveCasteIfOwed(fresh) == null, "AddSlaveCasteIfOwed added a caste without Ideology");
|
|
return;
|
|
}
|
|
T.Check(!fresh.SlaveCasteOwed, "a save seeded with Ideology owes the Slave caste");
|
|
// Make it look seeded without Ideology: no Slave caste, slave rows at No caste, the caste owed.
|
|
Caste seededSlave = ByLabel(fresh, StarterCastes.SlaveLabel);
|
|
T.NotNull(seededSlave, "setup: Slave caste");
|
|
AccessTools.Method(typeof(GameComponent_BlaxCastes), "RemoveCaste").Invoke(fresh, new object[] { seededSlave });
|
|
MethodInfo setRow = AccessTools.Method(typeof(GameComponent_BlaxCastes), "SetRowRaw");
|
|
foreach (string row in new[] { JoinReason.BoughtSlave, JoinReason.NewSlave })
|
|
setRow.Invoke(fresh, new object[] { row, JoinRowMode.NoCaste, null });
|
|
AccessTools.Method(typeof(GameComponent_BlaxCastes), "SetSlaveCasteOwed").Invoke(fresh, new object[] { true });
|
|
T.Equal(2, fresh.Castes.Count, "setup: castes without the Slave caste");
|
|
|
|
// A save started without Ideology has no Slave outfit: the game makes it only in a new game with
|
|
// Ideology. The game's own outfit is renamed for the test and named back afterwards.
|
|
OutfitDatabase outfits = Current.Game.outfitDatabase;
|
|
ApparelPolicy gameOutfit = StarterCastes.SlaveOutfit();
|
|
T.NotNull(gameOutfit, "setup: the game's Slave outfit");
|
|
string outfitLabel = gameOutfit.label;
|
|
int outfitCount = outfits.AllOutfits.Count;
|
|
ApparelPolicy created = null;
|
|
try
|
|
{
|
|
gameOutfit.label = outfitLabel + " (Blax Castes test)";
|
|
T.Check(StarterCastes.SlaveOutfit() == null, "setup: the game still has a Slave outfit");
|
|
|
|
fresh.LoadedGame();
|
|
Caste slave = ByLabel(fresh, StarterCastes.SlaveLabel);
|
|
T.NotNull(slave, "the Slave caste after the first load with Ideology");
|
|
T.Equal(3, fresh.Castes.Count, "castes after the first load with Ideology");
|
|
T.Check(!fresh.SlaveCasteOwed, "the Slave caste is still owed after it was added");
|
|
T.Check(fresh.ResolveRow(JoinReason.BoughtSlave) == slave && fresh.ResolveRow(JoinReason.NewSlave) == slave, "the slave rows do not pick the added Slave caste");
|
|
T.Check(slave.FoodPolicy == Simple(), "the added Slave caste's food: " + slave.FoodPolicy?.label);
|
|
created = StarterCastes.SlaveOutfit();
|
|
T.Check(created != null && created != gameOutfit && outfits.AllOutfits.Count == outfitCount + 1, "the Slave outfit was not created for the added Slave caste");
|
|
T.Check(slave.ApparelPolicy == created, "the added Slave caste's apparel: " + (slave.ApparelPolicy?.label ?? "unmanaged"));
|
|
T.Equal(0, fresh.Memberships.Count, "memberships after adding the Slave caste (nobody moves)");
|
|
string text = ((string)"BlaxCastes_StarterSlaveAddedMessage".Translate(slave.label.Named("CASTE"))).CapitalizeFirst();
|
|
T.Equal(1, MessagesWithText(text), "Slave caste messages after the first load with Ideology");
|
|
|
|
fresh.LoadedGame();
|
|
T.Equal(3, fresh.Castes.Count, "castes after a second load");
|
|
T.Equal(outfitCount + 1, outfits.AllOutfits.Count, "outfits after a second load");
|
|
T.Equal(1, MessagesWithText(text), "Slave caste messages after a second load");
|
|
}
|
|
finally
|
|
{
|
|
foreach (ApparelPolicy extra in outfits.AllOutfits.Where(o => o != gameOutfit && o.label == outfitLabel).ToList())
|
|
outfits.AllOutfits.Remove(extra);
|
|
gameOutfit.label = outfitLabel;
|
|
}
|
|
}
|
|
|
|
[BlaxTest("starter: the save is still seeded after the reload, with one of each starter caste", phase: 2)]
|
|
public static void NoSecondSeeding()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
T.Check(comp.StarterCastesSeeded, "the seeded flag did not survive the reload");
|
|
T.Equal(1, comp.Castes.Count(c => c.label == StarterCastes.ColonistLabel), "Colonist castes after the reload");
|
|
T.Equal(1, comp.Castes.Count(c => c.label == StarterCastes.PrisonerLabel), "Prisoner castes after the reload");
|
|
T.Equal(ModsConfig.IdeologyActive ? 1 : 0, comp.Castes.Count(c => c.label == StarterCastes.SlaveLabel), "Slave castes after the reload");
|
|
}
|
|
}
|
|
}
|