rimworld-blax-laws/Tests/BlaxLawsTests/Source/StoreTests.cs

162 lines
10 KiB
C#

using System.Linq;
using BlaxLaws;
using RimWorld;
using Verse;
namespace BlaxLawsTests
{
/// <summary>
/// The lawbook and case store: rows keyed on Action, Option and Stage, rows whose entry is missing, the
/// starter lawbook, and rows and cases through the save as names and numbers.
/// </summary>
public static class StoreTests
{
private const string MissingCoreKey = "Ate/BlaxLawsTest_NoSuchFood";
private const string MissingIdeologyKey = "RecordedAct/BlaxLawsTest_NoSuchEvent";
[BlaxTest("lawbook: a row whose entry is missing stays, reads Missing: Core or Needs Ideology, watches nothing and files nothing")]
public static void MissingEntryRows()
{
Law core = Fixtures.LawRow(MissingCoreKey, null, LawStage.DidIt, LawSeverity.Moderate, "Warn", 3, "Core", "ludeon.rimworld");
Law ideo = Fixtures.LawRow(MissingIdeologyKey, null, LawStage.DidIt, LawSeverity.Moderate, "Warn", 3, "Ideology", "ludeon.rimworld.ideology");
T.Check(Lawbook.Laws.Contains(core) && Lawbook.Laws.Contains(ideo), "a row with a missing entry left the book");
T.Check(!core.Resolves && !ideo.Resolves, "a made-up act key resolves");
T.Equal("Missing: Core", core.MissingText, "missing text of a Core row");
T.Equal("Needs Ideology", ideo.MissingText, "missing text of an Ideology row");
T.Equal("Needs Ideology", DlcSupport.MissingText("Ideology", null), "missing text from the DLC name alone");
T.Equal("Missing: Some Mod", DlcSupport.MissingText("Some Mod", "someone.somemod"), "missing text of a mod row");
T.Check(!Watch.Any, "a row whose entry is missing turned a watch flag on");
Pawn p = Fixtures.Colonist();
T.Check(CaseFiler.Report(new ActEvent(ActCategories.Ate, ThingDefOf.MealSimple, p)) == null, "a row whose entry is missing filed a case");
}
[BlaxTest("lawbook: a second row with the same action, option and stage is refused and names the first")]
public static void DuplicateRefused()
{
Law a = Fixtures.LawRow(MissingCoreKey, null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1);
var dup = new Law { actKey = MissingCoreKey, stage = LawStage.DidIt, punishment = "Jail", days = 5 };
T.Check(!Lawbook.TryAdd(dup, out Law existing), "the duplicate row was added");
T.Check(existing == a, "the refusal names " + existing + ", not the first row");
var otherStage = new Law { actKey = MissingCoreKey, stage = LawStage.Planning, punishment = "Warn", days = 1 };
T.Check(Lawbook.TryAdd(otherStage, out _), "a row with another stage was refused");
var binge = new Law { actKey = "Break/@anyDrugBinge", option = DrugOptions.Any, stage = LawStage.DidIt, punishment = "Warn", days = 3 };
T.Check(Lawbook.TryAdd(binge, out _), "a binge row was refused");
var bingeNull = new Law { actKey = "Break/@anyDrugBinge", option = null, stage = LawStage.DidIt, punishment = "Warn", days = 3 };
T.Check(!Lawbook.TryAdd(bingeNull, out Law e2) && e2 == binge, "a binge row with no option counts as Any and must be refused");
var bingeWake = new Law { actKey = "Break/@anyDrugBinge", option = "WakeUpChemical", stage = LawStage.DidIt, punishment = "Warn", days = 3 };
T.Check(Lawbook.TryAdd(bingeWake, out _), "a binge row with another option was refused");
T.Check(a.id > 0 && otherStage.id > a.id, "row ids are not handed out in order: " + a.id + ", " + otherStage.id);
}
[BlaxTest("lawbook: Moderate to Serious moves untouched days from 3 to 5 and leaves typed days alone; days clamp to 1..50")]
public static void SeverityMovesDefaultDays()
{
Law a = Fixtures.LawRow(MissingCoreKey, null, LawStage.DidIt, LawSeverity.Moderate, "Jail", 3);
Lawbook.SetSeverity(a, LawSeverity.Serious);
T.Equal(5, a.days, "untouched days after Moderate to Serious");
Lawbook.SetDays(a, 7);
Lawbook.SetSeverity(a, LawSeverity.Grave);
T.Equal(7, a.days, "typed days after Serious to Grave");
Lawbook.SetDays(a, 0);
T.Equal(1, a.days, "days 0 clamped");
Lawbook.SetDays(a, 80);
T.Equal(50, a.days, "days 80 clamped");
T.Equal(1, Lawbook.DefaultDays(LawSeverity.Minor), "Minor days");
T.Equal(10, Lawbook.DefaultDays(LawSeverity.Grave), "Grave days");
}
[BlaxTest("lawbook: Starter lawbook adds 7 rows, pressing it again adds none")]
public static void StarterLawbook()
{
int before = Lawbook.Laws.Count;
T.Equal(7, Lawbook.AddStarter(), "rows the starter lawbook added");
T.Equal(before + 7, Lawbook.Laws.Count, "rows in the book after the starter lawbook");
T.Equal(0, Lawbook.AddStarter(), "rows a second press added");
Law jail = Lawbook.Find("Break/@anyDrugBinge", DrugOptions.Any, LawStage.DidIt);
T.NotNull(jail, "starter row 'any drug binge, did it'");
T.Equal("Jail", jail.punishment, "punishment of 'any drug binge, did it'");
T.Equal(5, jail.days, "days of 'any drug binge, did it'");
T.Equal(LawSeverity.Serious, jail.severity, "severity of 'any drug binge, did it'");
Law fire = Lawbook.Find("Break/FireStartingSpree", null, LawStage.DidIt);
T.Check(fire != null && fire.punishment == "Jail" && fire.days == 10 && fire.severity == LawSeverity.Grave, "starter fire-starting spree did-it row: " + fire);
T.NotNull(Lawbook.Find("Interaction/Insult", null, LawStage.DidIt), "starter row 'Insult, did it'");
T.NotNull(Lawbook.Find("Special/StartedSocialFight", null, LawStage.DidIt), "starter row 'started a social fight'");
T.Check(Lawbook.Laws.All(l => l.savedMod == "Core" || l.Resolves), "a starter row saved another mod than Core");
}
[BlaxTest("cases: a filed case closes with its outcome, and deleting its row closes the open ones as repealed")]
public static void CaseLifecycle()
{
GameComponent_BlaxLaws comp = Fixtures.Comp;
Pawn p = Fixtures.Colonist();
Law a = Fixtures.LawRow(MissingCoreKey, null, LawStage.DidIt, LawSeverity.Serious, "Warn", 5);
LawCase c = CaseFiler.Open(a, new ActEvent(ActCategories.Ate, ThingDefOf.MealSimple, p), LawStage.DidIt);
T.NotNull(c, "CaseFiler.Open");
T.Check(comp.Cases.Contains(c) && comp.CasesOf(p).Contains(c), "the case is not in the store or its offender index");
T.Equal(a.id, c.didItLawId, "the case's did-it row");
T.Equal(LawSeverity.Serious, c.severity, "the case's severity");
T.Equal(1, c.count, "the count of a new did-it case");
T.Equal(p.LabelShort, c.offenderName, "the offender's saved name");
T.Equal(1, Lawbook.OpenCaseCount(a), "open cases of the row");
T.Equal(1, Lawbook.Delete(a), "cases the delete closed");
T.Check(c.IsClosed && c.outcome == CaseOutcomes.Repealed, "the case after the delete: " + c);
T.Equal("Law repealed", CaseOutcomes.Label(c.outcome), "the repealed label");
}
[BlaxTest("save: a row and a case go into the save as names and numbers (setup)")]
public static void SaveSetup()
{
Pawn p = Fixtures.Keep(Fixtures.Colonist());
Law a = Fixtures.KeepLaw(Fixtures.LawRow(MissingIdeologyKey, null, LawStage.DidIt, LawSeverity.Grave, "Jail", 12, "Ideology", "ludeon.rimworld.ideology"));
Lawbook.SetEnabled(a, false);
LawCase c = CaseFiler.Open(a, new ActEvent(ActCategories.RecordedAct, null, p) { notes = CaseNotes.OnTable | CaseNotes.DugUp }, LawStage.DidIt);
c.count = 4;
c.breakState = "Binging_DrugExtreme";
c.breakStartTick = 1234;
c.MarkPunished(LawStage.DidIt);
c.status = CaseStatus.Jailed;
c.captureTick = 2000;
c.releaseTick = 2000 + 12 * GenDate.TicksPerDay;
T.Carry["save.law"] = a.id.ToString();
T.Carry["save.case"] = c.id.ToString();
T.Carry["save.pawn"] = p.ThingID;
}
[BlaxTest("save: the row and the case keep every field through the reload", phase: 2)]
public static void SaveRoundTrip()
{
T.Check(T.Carry.ContainsKey("save.law"), "phase-1 setup did not run");
GameComponent_BlaxLaws comp = Fixtures.Comp;
Law a = comp.LawById(int.Parse(T.Carry["save.law"]));
T.NotNull(a, "the kept row after the reload");
T.Equal(MissingIdeologyKey, a.actKey, "act key");
T.Equal(LawStage.DidIt, a.stage, "stage");
T.Equal(LawSeverity.Grave, a.severity, "severity");
T.Equal("Jail", a.punishment, "punishment");
T.Equal(12, a.days, "days");
T.Check(!a.enabled, "the row's On flag came back on");
T.Equal("Ideology", a.savedMod, "saved mod");
T.Equal("Needs Ideology", a.MissingText, "missing text after the reload");
T.Check(comp.NextLawId > a.id, "next row id " + comp.NextLawId + " would repeat " + a.id);
LawCase c = comp.CaseById(int.Parse(T.Carry["save.case"]));
T.NotNull(c, "the kept case after the reload");
Pawn p = Fixtures.FindPawnById(T.Carry["save.pawn"]);
T.NotNull(p, "the kept colonist after the reload");
T.Check(c.offender == p, "the case's offender reference didn't resolve to the colonist");
T.Check(comp.CasesOf(p).Contains(c), "the offender index lacks the case after the reload");
T.Equal(a.id, c.didItLawId, "did-it row");
T.Equal(4, c.count, "count");
T.Equal(CaseNotes.OnTable | CaseNotes.DugUp, c.notes, "notes");
T.Equal("Binging_DrugExtreme", c.breakState, "break state");
T.Equal(1234, c.breakStartTick, "break start tick");
T.Check(c.HasPunished(LawStage.DidIt) && !c.HasPunished(LawStage.Planning), "punished stages: " + c.punishedStages);
T.Equal(CaseStatus.Jailed, c.status, "status");
T.Equal(2000, c.captureTick, "capture tick");
T.Equal(2000 + 12 * GenDate.TicksPerDay, c.releaseTick, "release tick");
T.Equal(LawSeverity.Grave, c.severity, "severity");
T.Check(comp.NextCaseId > c.id, "next case id " + comp.NextCaseId + " would repeat " + c.id);
}
}
}