218 lines
14 KiB
C#
218 lines
14 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BlaxCastes;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxCastesTests
|
|
{
|
|
/// <summary>CasteManager: create, duplicate, delete, default caste, join rows and memberships.</summary>
|
|
public static class ManagerTests
|
|
{
|
|
private static Caste Full(string label)
|
|
{
|
|
Caste c = Fixtures.NewCaste(label);
|
|
CasteManager.SetSlot(c, CasteSlot.Apparel, Fixtures.Need(Fixtures.Outfit("OutfitNudist"), "Nudist outfit"));
|
|
CasteManager.SetSlot(c, CasteSlot.Food, Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food"));
|
|
CasteManager.SetSlot(c, CasteSlot.Drug, Fixtures.Need(Fixtures.Drug("NoDrugs"), "No drugs"));
|
|
CasteManager.SetSlot(c, CasteSlot.Reading, Fixtures.Need(Fixtures.Reading("NoneReadingPolicy"), "None reading"));
|
|
CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.HerbalOrWorse);
|
|
return c;
|
|
}
|
|
|
|
private static string Policies(Pawn p) =>
|
|
string.Join(", ", CasteUtility.AllSlots.Select(s => s + "=" + CasteUtility.ValueLabel(s, CasteUtility.CurrentValue(p, s))));
|
|
|
|
[BlaxTest("manager: CreateCaste gives unique, rising ids and manages nothing")]
|
|
public static void CreateUniqueIds()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
int maxBefore = comp.Castes.Count == 0 ? 0 : comp.Castes.Max(c => c.id);
|
|
Caste a = Fixtures.NewCaste();
|
|
Caste b = Fixtures.NewCaste();
|
|
Caste c3 = Fixtures.NewCaste();
|
|
var ids = new[] { a.id, b.id, c3.id };
|
|
T.Check(ids.Distinct().Count() == 3, "ids not unique: " + string.Join(", ", ids));
|
|
T.Check(ids.All(id => id > maxBefore), "ids not above the existing ones: " + string.Join(", ", ids) + ", max before " + maxBefore);
|
|
T.Check(a.id < b.id && b.id < c3.id, "ids not rising: " + string.Join(", ", ids));
|
|
T.Check(comp.Castes.Contains(a) && comp.Castes.Contains(b) && comp.Castes.Contains(c3), "new castes not in the component");
|
|
T.Equal((string)"BlaxCastes_NewCasteLabel".Translate(a.id), a.label, "default label");
|
|
T.Check(!a.AnyManaged(), "a new caste manages a slot");
|
|
T.Check(a.GetUniqueLoadID() != b.GetUniqueLoadID(), "two castes share a load ID");
|
|
T.Check(a.GetUniqueLoadID().StartsWith("BlaxCaste_"), "load ID does not use LoadKey BlaxCaste: " + a.GetUniqueLoadID());
|
|
|
|
// Deleting the newest caste does not hand its id out again.
|
|
int deletedId = c3.id;
|
|
T.Check(CasteManager.DeleteCaste(c3), "DeleteCaste failed");
|
|
Caste d = Fixtures.NewCaste();
|
|
T.Check(d.id > deletedId, $"id {d.id} reused after deleting caste {deletedId}");
|
|
}
|
|
|
|
[BlaxTest("manager: DuplicateCaste copies every slot into a new caste with its own id")]
|
|
public static void DuplicateCopiesSlots()
|
|
{
|
|
Caste src = Full("Duplicate source");
|
|
Pawn member = Fixtures.Colonist();
|
|
CasteManager.Assign(member, src, false, JoinReason.Manual);
|
|
|
|
Caste dup = CasteManager.DuplicateCaste(src);
|
|
T.NotNull(dup, "DuplicateCaste result");
|
|
T.Check(Fixtures.Comp.HasCaste(dup), "duplicate not in the component");
|
|
T.Check(dup.id != src.id, "duplicate has the source's id");
|
|
foreach (CasteSlot slot in CasteUtility.AllSlots)
|
|
T.Check(CasteUtility.SameValue(src.GetSlot(slot), dup.GetSlot(slot)), $"{slot}: source {CasteUtility.ValueLabel(slot, src.GetSlot(slot))}, duplicate {CasteUtility.ValueLabel(slot, dup.GetSlot(slot))}");
|
|
T.Equal(0, Fixtures.Comp.MembersOf(dup).Count, "members of the duplicate");
|
|
T.Check(Fixtures.Comp.CasteOf(member) == src, "the source's member moved to the duplicate");
|
|
CasteManager.DeleteCaste(dup);
|
|
}
|
|
|
|
[BlaxTest("manager: DeleteCaste removes the caste, makes members casteless, resets its rows and leaves policies alone")]
|
|
public static void DeleteCaste()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
Caste doomed = Full("Doomed");
|
|
Caste other = Fixtures.NewCaste("Other");
|
|
Pawn a = Fixtures.Colonist();
|
|
Pawn b = Fixtures.Colonist();
|
|
CasteManager.Assign(a, doomed, false, JoinReason.Manual);
|
|
CasteManager.Assign(b, doomed, true, JoinReason.NewColonist);
|
|
CasteManager.SetJoinRow(JoinReason.BoughtSlave, JoinRowMode.SpecificCaste, doomed);
|
|
CasteManager.SetJoinRow(JoinReason.NewPrisoner, JoinRowMode.SpecificCaste, doomed);
|
|
CasteManager.SetJoinRow(JoinReason.NewSlave, JoinRowMode.SpecificCaste, other);
|
|
CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.DefaultCaste);
|
|
CasteManager.SetDefault(doomed);
|
|
T.Check(comp.ResolveRow(JoinReason.NewColonist) == doomed, "setup: Default caste row does not resolve to the default");
|
|
string aBefore = Policies(a), bBefore = Policies(b);
|
|
|
|
T.Check(CasteManager.DeleteCaste(doomed), "DeleteCaste returned false");
|
|
T.Check(!comp.HasCaste(doomed), "caste still in the component");
|
|
T.Equal(0, comp.MembersOf(doomed).Count, "members left in the deleted caste");
|
|
foreach (Pawn p in new[] { a, b })
|
|
{
|
|
CasteMembership m = comp.MembershipOf(p);
|
|
T.NotNull(m, p.LabelShort + "'s membership record");
|
|
T.Check(m.caste == null && m.reason == JoinReason.CasteDeleted, $"{p.LabelShort}: caste={m.caste}, reason={m.reason}");
|
|
}
|
|
T.Equal(aBefore, Policies(a), "A's policies after delete");
|
|
T.Equal(bBefore, Policies(b), "B's policies after delete");
|
|
T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.BoughtSlave).mode, "bought-as-slave row");
|
|
T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.NewPrisoner).mode, "new-prisoner row");
|
|
T.Check(comp.GetRow(JoinReason.NewSlave).mode == JoinRowMode.SpecificCaste && comp.GetRow(JoinReason.NewSlave).caste == other, "a row pointing at another caste changed");
|
|
T.Check(comp.DefaultCaste == null, "the deleted caste is still the default");
|
|
T.Check(comp.ResolveRow(JoinReason.NewColonist) == null, "a Default caste row resolves to a caste with no default set");
|
|
T.Check(!CasteManager.DeleteCaste(doomed), "deleting twice returned true");
|
|
}
|
|
|
|
[BlaxTest("manager: SetDefault changes what Default caste rows resolve to")]
|
|
public static void SetDefaultResolves()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
Caste one = Fixtures.NewCaste("One");
|
|
Caste two = Fixtures.NewCaste("Two");
|
|
CasteManager.SetDefault(null);
|
|
T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.DefaultCaste), "SetJoinRow failed");
|
|
T.Check(comp.ResolveRow(JoinReason.NewColonist) == null, "Default caste row resolves without a default");
|
|
T.Check(CasteManager.SetDefault(one), "SetDefault(one) failed");
|
|
T.Check(comp.ResolveRow(JoinReason.NewColonist) == one, "row does not resolve to the first default");
|
|
T.Check(CasteManager.SetDefault(two), "SetDefault(two) failed");
|
|
T.Check(comp.ResolveRow(JoinReason.NewColonist) == two, "row does not resolve to the new default");
|
|
T.Check(CasteManager.SetDefault(null), "SetDefault(null) failed");
|
|
T.Check(comp.DefaultCaste == null && comp.ResolveRow(JoinReason.NewColonist) == null, "clearing the default left a default");
|
|
|
|
var stranger = new Caste(9999, "Not in this game");
|
|
T.Check(!CasteManager.SetDefault(stranger), "SetDefault accepted a caste that is not in the game");
|
|
T.Check(!CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.SpecificCaste, stranger), "SetJoinRow accepted a caste that is not in the game");
|
|
T.Check(!CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.SpecificCaste, null), "SetJoinRow accepted SpecificCaste without a caste");
|
|
T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.SpecificCaste, one) && comp.ResolveRow(JoinReason.NewColonist) == one, "SpecificCaste row does not resolve to its caste");
|
|
}
|
|
|
|
[BlaxTest("manager: Apply to all members clears an override on every member; the confirm names the member count")]
|
|
public static void ApplyToAllClearsOverrides()
|
|
{
|
|
Caste c = Full("Apply to all");
|
|
Pawn a = Fixtures.Colonist();
|
|
Pawn b = Fixtures.Colonist();
|
|
foreach (Pawn p in new[] { a, b })
|
|
{
|
|
Fixtures.Unqueue(p);
|
|
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "setup: Assign failed");
|
|
}
|
|
FoodPolicy lavish = Fixtures.Need(Fixtures.Food("FoodRestrictionLavish"), "Lavish food");
|
|
b.foodRestriction.CurrentFoodPolicy = lavish; // by hand
|
|
T.Equal(1, CasteManager.Overrides(b).Count, "setup: B's overrides");
|
|
T.Equal(2, CasteManager.LiveMembers(c).Count, "live members");
|
|
string confirm = Dialog_ManageCastes.ApplyToAllConfirmText(c, CasteManager.LiveMembers(c).Count);
|
|
T.Check(confirm.Contains("2") && confirm.Contains(c.label), "confirm text does not name the count and the caste: " + confirm);
|
|
|
|
T.Equal(2, CasteManager.ApplyToAll(c), "members written by Apply to all");
|
|
T.Equal(0, CasteManager.Overrides(b).Count, "B's overrides after Apply to all (" + Policies(b) + ")");
|
|
T.Check(b.foodRestriction.CurrentFoodPolicy == c.FoodPolicy, "B's food after Apply to all");
|
|
|
|
Fixtures.Settings.enabled = false;
|
|
b.foodRestriction.CurrentFoodPolicy = lavish;
|
|
T.Equal(0, CasteManager.ApplyToAll(c), "members written by Apply to all with Castes off");
|
|
T.Check(b.foodRestriction.CurrentFoodPolicy == lavish, "Apply to all wrote a policy with Castes off");
|
|
}
|
|
|
|
[BlaxTest("manager: Assign by status puts a pawn without a caste into its status row's caste and leaves a casteless-by-choice pawn alone")]
|
|
public static void AssignByStatus()
|
|
{
|
|
bool slaves = ModsConfig.IdeologyActive;
|
|
string row = slaves ? JoinReason.NewSlave : JoinReason.NewColonist;
|
|
Caste c = Fixtures.NewCaste("Assign by status");
|
|
CasteManager.SetSlot(c, CasteSlot.Food, Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food"));
|
|
T.Check(CasteManager.SetJoinRow(row, JoinRowMode.SpecificCaste, c), "setup: SetJoinRow failed");
|
|
|
|
Pawn unassigned = slaves ? Fixtures.ColonySlave() : Fixtures.Colonist();
|
|
Pawn casteless = slaves ? Fixtures.ColonySlave() : Fixtures.Colonist();
|
|
Pawn queued = slaves ? Fixtures.ColonySlave() : Fixtures.Colonist();
|
|
Fixtures.Unqueue(unassigned);
|
|
Fixtures.Unqueue(casteless);
|
|
Fixtures.Unqueue(queued);
|
|
JoinDefaults.Enqueue(queued, JoinDefaults.StatusFallback);
|
|
T.Check(CasteManager.MakeCasteless(casteless), "setup: MakeCasteless failed");
|
|
|
|
var waiting = CasteManager.PawnsToAssignByStatus();
|
|
T.Check(waiting.Contains(unassigned), "a pawn without a record is not offered");
|
|
T.Check(!waiting.Contains(casteless), "a casteless-by-choice pawn is offered");
|
|
T.Check(!waiting.Contains(queued), "a pawn waiting for its join is offered");
|
|
string confirm = Dialog_ManageCastes.AssignByStatusConfirmText(waiting.Count);
|
|
T.Check(confirm.Contains(waiting.Count.ToString()), "the confirm does not name the count: " + confirm);
|
|
|
|
int assigned = CasteManager.AssignByStatus();
|
|
T.Check(assigned >= 1, "Assign by status assigned " + assigned);
|
|
CasteMembership m = Fixtures.Comp.MembershipOf(unassigned);
|
|
T.Check(m != null && m.caste == c && m.auto && m.reason == row, $"unassigned pawn after Assign by status: caste={m?.caste}, auto={m?.auto}, reason={m?.reason}");
|
|
T.Check(unassigned.foodRestriction.CurrentFoodPolicy == c.FoodPolicy, "Assign by status did not write the caste's food");
|
|
T.Check(Fixtures.Comp.MembershipOf(casteless).Casteless, "the casteless-by-choice pawn got a caste");
|
|
T.Check(!CasteManager.PawnsToAssignByStatus().Contains(unassigned), "the assigned pawn is still offered");
|
|
Fixtures.Unqueue(queued);
|
|
if (!slaves)
|
|
T.Info("Ideology off: checked with colonists and the new colonist row");
|
|
}
|
|
|
|
[BlaxTest("manager: only player-side humanlikes can be members; MakeCasteless keeps policies")]
|
|
public static void MembershipRules()
|
|
{
|
|
GameComponent_BlaxCastes comp = Fixtures.Comp;
|
|
Caste c = Full("Rules");
|
|
Faction other = Fixtures.FriendlyFaction();
|
|
T.NotNull(other, "a friendly faction");
|
|
Pawn visitor = Fixtures.Spawn(Fixtures.Generate(other.def.basicMemberKind ?? PawnKindDefOf.Colonist, other));
|
|
T.Check(!CasteManager.Assign(visitor, c, false, JoinReason.Manual), "a pawn of another faction joined a caste");
|
|
T.Check(comp.MembershipOf(visitor) == null, "a refused Assign left a record");
|
|
T.Check(!CasteManager.Assign(null, c, false, JoinReason.Manual), "Assign(null) returned true");
|
|
|
|
Pawn p = Fixtures.Colonist();
|
|
T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed");
|
|
string withCaste = Policies(p);
|
|
T.Check(CasteManager.MakeCasteless(p), "MakeCasteless failed");
|
|
CasteMembership m = comp.MembershipOf(p);
|
|
T.Check(m != null && m.Casteless && !m.auto, "MakeCasteless did not leave a casteless-by-choice record");
|
|
T.Equal(withCaste, Policies(p), "policies after MakeCasteless");
|
|
T.Check(CasteManager.EndMembership(p) && comp.MembershipOf(p) == null, "EndMembership left a record");
|
|
T.Check(CasteManager.Assign(p, null, false, JoinReason.Manual) && comp.MembershipOf(p).Casteless, "Assign(pawn, null) did not make the pawn casteless");
|
|
}
|
|
}
|
|
}
|