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

200 lines
12 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")]
public static void MissingSlaveOutfit()
{
if (!ModsConfig.IdeologyActive)
T.Skip("the Slave caste needs Ideology");
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");
if (!ModsConfig.IdeologyActive)
T.Skip("the Slave outfit needs Ideology; the other checks passed");
T.Check(StarterCastes.IsSlaveOutfit(StarterCastes.SlaveOutfit()), "the Slave outfit does not count as the Slave outfit");
T.Check("BlaxCastes_SlaveOutfitCold".CanTranslate(), "the cold warning is missing from Keyed");
}
[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");
}
}
}