153 lines
9.9 KiB
C#
153 lines
9.9 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: 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");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|