using System; using System.Collections.Generic; using System.Linq; using BlaxDefaults; using RimWorld; using Verse; namespace BlaxDefaultsTests { /// /// Work package "setup". The tests of design.md: Quick setup: new shelves start empty; Overlap warnings. /// Methods: [BlaxTest(": ", phase: N)] public static void X(). Skip DLC-only checks with /// Fixtures.NeedDlc(DlcSupport.Biotech) ("skipped: needs Biotech"). /// The end-to-end check "a shelf built afterwards allows nothing" needs the blueprint hook (package "hooks"); /// here the template goes through StorageKind.LoadDefault, the same path a new shelf takes. /// public static class SetupTests { private static readonly string[] SetupKeys = { "BlaxDefaults_QuickSetupButton", "BlaxDefaults_QuickSetupTip", "BlaxDefaults_QuickSetupTipFsf", "BlaxDefaults_QuickSetupTitle", "BlaxDefaults_QuickSetupIntro", "BlaxDefaults_QuickSetupNone", "BlaxDefaults_QuickSetupHasDefault", "BlaxDefaults_QuickSetupReasonHasDefault", "BlaxDefaults_QuickSetupReasonNotShelf", "BlaxDefaults_QuickSetupReasonHopper", "BlaxDefaults_QuickSetupReasonOneCategory", "BlaxDefaults_QuickSetupSaved", "BlaxDefaults_WarnOverlaps", "BlaxDefaults_WarnOverlapsTip", "BlaxDefaults_OverlapSettingsIntro", "BlaxDefaults_OverlapLine", "BlaxDefaults_OverlapAdvice", "BlaxDefaults_OverlapTitle", "BlaxDefaults_OverlapMessage", "BlaxDefaults_OverlapDontRemind", "BlaxDefaults_OverlapDismissed", "BlaxDefaults_OverlapWhatEmptyStorage", "BlaxDefaults_OverlapWhatRepeat", }; // ---- quick setup --------------------------------------------------------------- [BlaxTest("setup: the preselection saves empty Normal defaults for Shelf and ShelfSmall, none for Hopper or bookcases")] public static void PreselectionSavesShelves() { ClearStorageEntries(); List candidates = QuickSetup.Candidates(); T.Info("candidates: " + string.Join(", ", candidates.Select(c => c.Def.defName + (c.Ticked ? "+" : "-")))); foreach (string name in new[] { "Shelf", "ShelfSmall" }) T.Check(Candidate(candidates, name).Ticked, name + " is not ticked"); foreach (string name in new[] { "Hopper", "Bookcase", "BookcaseSmall" }) T.Check(!Candidate(candidates, name).Ticked, name + " is ticked"); T.Check(candidates.All(c => !c.HasDefault), "a kind has a default after clearing the library"); int saved = QuickSetup.Apply(candidates.Where(c => c.Ticked).Select(c => c.Def)); T.Equal(candidates.Count(c => c.Ticked), saved, "defaults saved by Apply"); foreach (string name in new[] { "Shelf", "ShelfSmall" }) { StorageKind kind = Fixtures.Kind(name); StorageTemplate t = DefaultsLibrary.Get(kind.Key); T.NotNull(t, "the saved default for " + name); T.Equal(StoragePriority.Normal, t.Priority, name + " priority"); T.Equal(0, t.allowed.Count, name + " allowed names"); T.Equal(UnlistedRule.Refuse, t.unlisted, name + " unlisted rule"); var storable = StorageKinds.StorableDefs(kind.ParentFilter).Select(d => d.defName).ToList(); T.Check(t.refused.Count == storable.Count && !storable.Except(t.refused).Any(), name + " refuses " + t.refused.Count + " names, its parent allows " + storable.Count + " storable defs"); T.Check(t.refused.Contains(ThingDefOf.Steel.defName), name + " does not refuse Steel"); } foreach (string name in new[] { "Hopper", "Bookcase", "BookcaseSmall" }) T.Check(!DefaultsLibrary.Has(Fixtures.Kind(name).Key), name + " got a default"); } [BlaxTest("setup: a kind with a default starts unticked and is marked")] public static void ExistingDefaultUnticked() { ClearStorageEntries(); StorageKind shelf = Fixtures.Kind("Shelf"); StorageTemplate own = Fixtures.Save(Fixtures.Template(shelf, StoragePriority.Important, ThingDefOf.Steel)); List candidates = QuickSetup.Candidates(); QuickSetup.Candidate c = Candidate(candidates, "Shelf"); T.Check(c.HasDefault, "Shelf is not marked as having a default"); T.Check(!c.Ticked, "Shelf with a default is ticked"); T.Check(QuickSetup.Preselect(shelf.Def), "Preselect(Shelf) is false"); QuickSetup.Candidate small = Candidate(candidates, "ShelfSmall"); T.Check(!small.HasDefault && small.Ticked, "ShelfSmall without a default is not ticked or is marked"); QuickSetup.Apply(candidates.Where(x => x.Ticked).Select(x => x.Def)); T.Check(DefaultsLibrary.Get(shelf.Key).SameSettingsAs(own), "Apply replaced the unticked Shelf default"); } [BlaxTest("setup: top-level categories count by each def's first category")] public static void TopLevelCategories() { ThingDef widget = Fixtures.Thing("BlaxTest_Widget"); T.Equal(ThingCategoryDefOf.Manufactured, QuickSetup.TopLevelCategoryOf(widget), "top-level category of BlaxTest_Widget"); var f = new ThingFilter(); f.SetAllow(widget, true); T.Equal(1, QuickSetup.TopLevelCategoryCount(f), "categories of a widget-only filter"); f.SetAllow(ThingDefOf.ComponentIndustrial, true); T.Equal(1, QuickSetup.TopLevelCategoryCount(f), "categories of widgets and components"); f.SetAllow(ThingDefOf.Steel, true); T.Equal(2, QuickSetup.TopLevelCategoryCount(f), "categories with Steel added"); T.Equal(0, QuickSetup.TopLevelCategoryCount(new ThingFilter()), "categories of an empty filter"); T.Check(QuickSetup.TopLevelCategoryCount(StorageKinds.ParentFilterFor(Fixtures.Thing("Shelf"))) >= 2, "the shelf's parent filter has fewer than two categories"); T.Equal("BlaxDefaults_QuickSetupReasonHopper", QuickSetup.UntickedReason(Fixtures.Thing("Hopper")), "why the hopper is unticked"); T.Equal("BlaxDefaults_QuickSetupReasonNotShelf", QuickSetup.UntickedReason(Fixtures.Thing("Bookcase")), "why the bookcase is unticked"); } [BlaxTest("setup: the empty template loaded onto a shelf allows nothing, at Normal")] public static void EmptyTemplateAllowsNothing() { StorageKind kind = Fixtures.Kind("Shelf"); StorageTemplate t = QuickSetup.EmptyTemplate(kind.Def); T.Equal(kind.Key, t.key, "key"); T.Check(!DefaultsLibrary.Has(kind.Key) || DefaultsLibrary.Get(kind.Key) != t, "EmptyTemplate saved the template"); var s = new StorageSettings(); kind.LoadDefault(s, t); T.Equal(StoragePriority.Normal, s.Priority, "priority"); var allowed = StorageKinds.StorableDefs(kind.ParentFilter).Where(d => s.filter.Allows(d)).Select(d => d.defName).ToList(); T.Check(allowed.Count == 0, "the shelf allows " + allowed.Count + " storable defs: " + string.Join(", ", allowed.Take(5))); T.Info("defs still allowed outside the storable set: " + s.filter.AllowedDefCount); T.Check(T.ErrorsSoFar().Count == 0, "loading the empty template logged errors"); } [BlaxTest("setup: a shelf built after the quick setup allows nothing, at Normal, and Apply writes the file once")] public static void ShelfBuiltAfterSetupAllowsNothing() { ClearStorageEntries(); int changes = 0; Action counter = () => changes++; DefaultsLibrary.Changed += counter; int saved; try { saved = QuickSetup.Apply(QuickSetup.Candidates().Where(c => c.Ticked).Select(c => c.Def)); } finally { DefaultsLibrary.Changed -= counter; } T.Check(saved >= 2, "the quick setup saved " + saved + " defaults"); T.Equal(1, changes, "library writes for one quick setup"); T.Check(DefaultsLibrary.LastWriteOk, "the defaults file write failed"); StorageKind kind = Fixtures.Kind("Shelf"); CellRect area = Fixtures.EmptyArea(3, 3); Thing shelf = HooksTests.Build(ThingDefOf.Shelf, ThingDefOf.WoodLog, area.CenterCell); StorageSettings s = HooksTests.SettingsOf(shelf); T.Equal(StoragePriority.Normal, s.Priority, "built shelf priority"); var allowed = StorageKinds.StorableDefs(kind.ParentFilter).Where(d => s.filter.Allows(d)).Select(d => d.defName).ToList(); T.Check(allowed.Count == 0, "the built shelf allows " + allowed.Count + " storable defs: " + string.Join(", ", allowed.Take(5))); } [BlaxTest("setup: an item a mod adds later is refused, and follows the def default with unlisted Game")] public static void LaterItemRefused() { StorageKind kind = Fixtures.Kind("Shelf"); ThingDef widget = Fixtures.Thing("BlaxTest_Widget"); T.Check(kind.GameDefault().filter.Allows(widget), "the shelf's def default refuses BlaxTest_Widget"); StorageTemplate t = QuickSetup.EmptyTemplate(kind.Def); T.Check(t.refused.Remove(widget.defName), "the empty template does not list BlaxTest_Widget"); T.Check(!t.allowed.Contains(widget.defName), "the empty template allows BlaxTest_Widget"); var refuse = new StorageSettings(); kind.LoadDefault(refuse, t); T.Check(!refuse.filter.Allows(widget), "unlisted Refuse: the shelf allows BlaxTest_Widget"); t.unlisted = UnlistedRule.Game; var game = new StorageSettings(); kind.LoadDefault(game, t); T.Check(game.filter.Allows(widget), "unlisted Game: the shelf refuses BlaxTest_Widget, its def default allows it"); T.Check(!game.filter.Allows(ThingDefOf.Steel), "unlisted Game: the shelf allows Steel"); T.Equal(StoragePriority.Normal, game.Priority, "unlisted Game: priority"); } [BlaxTest("setup: each candidate carries its unticked reason, worked out once, and Ticked follows it")] public static void CandidateReasons() { ClearStorageEntries(); List candidates = QuickSetup.Candidates(); T.Equal(null, Candidate(candidates, "Shelf").UntickedReason, "reason stored for Shelf"); T.Equal("BlaxDefaults_QuickSetupReasonHopper", Candidate(candidates, "Hopper").UntickedReason, "reason stored for Hopper"); T.Equal("BlaxDefaults_QuickSetupReasonNotShelf", Candidate(candidates, "Bookcase").UntickedReason, "reason stored for Bookcase"); foreach (QuickSetup.Candidate c in candidates) { T.Equal(QuickSetup.UntickedReason(c.Def), c.UntickedReason, c.Def.defName + ": stored reason"); T.Equal(c.UntickedReason == null && !c.HasDefault, c.Ticked, c.Def.defName + ": ticked"); } } // design.md expects the fridge ticked, but RimFridge sets building.isHopper on its FridgeBase (its 1.6 // Defs/ThingDefs_Buildings/Fridge_Base.xml:75), so paste dispensers draw from fridges, and the quick // setup's own hopper rule leaves it unticked: an empty default would starve them like an empty hopper. [BlaxTest("setup: -Compat: the RimFridge fridge is unticked as a hopper")] public static void CompatFridgeUnticked() { if (!Compat.IsActive(Compat.RimFridge)) T.Skip("needs RimFridge (-Compat)"); ClearStorageEntries(); List candidates = QuickSetup.Candidates(); T.Info("RimFridge kinds: " + string.Join(", ", candidates.Where(c => c.Def.defName.StartsWith("RimFridge_")).Select(c => c.Def.defName + (c.Ticked ? "+" : "-")))); QuickSetup.Candidate fridge = Candidate(candidates, "RimFridge_Refrigerator"); T.Check(fridge.Def.building.isHopper, "RimFridge_Refrigerator is no longer a hopper: revisit this test"); T.Check(!fridge.Ticked, "RimFridge_Refrigerator is ticked"); T.Equal("BlaxDefaults_QuickSetupReasonHopper", fridge.UntickedReason, "why RimFridge_Refrigerator is unticked"); } [BlaxTest("setup: -Compat: Weapon Racks racks are unticked")] public static void CompatWeaponRackUnticked() { if (!Compat.IsActive(Compat.WeaponRacks)) T.Skip("needs Weapon Racks (-Compat)"); ClearStorageEntries(); List racks = QuickSetup.Candidates().Where(c => c.Def.defName.StartsWith("WeaponRacks_")).ToList(); T.Check(racks.Count > 0, "no Weapon Racks kind in the list"); T.Info("Weapon Racks kinds: " + string.Join(", ", racks.Select(c => c.Def.defName))); T.Check(racks.All(c => !c.Ticked), "ticked racks: " + string.Join(", ", racks.Where(c => c.Ticked).Select(c => c.Def.defName))); T.Equal("BlaxDefaults_QuickSetupReasonOneCategory", QuickSetup.UntickedReason(racks[0].Def), "why a rack is unticked"); } // ---- overlap warnings ---------------------------------------------------------- [BlaxTest("overlap: the list has unique ids, translated descriptions and every key")] public static void OverlapList() { var ids = OverlapWarnings.Mods.Select(m => m.PackageId.ToLowerInvariant()).ToList(); T.Check(ids.Count > 0, "no overlap mods listed"); T.Equal(ids.Count, ids.Distinct().Count(), "distinct package ids"); T.Check(!ids.Contains("blax.defaults"), "Blax Defaults lists itself"); var missing = OverlapWarnings.Mods.Select(m => m.WhatKey).Concat(SetupKeys).Where(k => !k.CanTranslate()).Distinct().ToList(); T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing)); T.Info("really active: " + string.Join(", ", OverlapWarnings.Mods.Where(m => Compat.IsActive(m.PackageId)).Select(m => m.Name))); T.Equal(null, OverlapWarnings.MessageText(new List()), "message text for no mods"); } [BlaxTest("overlap: an injected id shows on the settings page and in the window, once per session")] public static void OverlapShownOnce() { OverlapMod mod = SimulatedMod(0); Fixtures.Settings.warnOverlaps = true; Fixtures.Settings.dismissedOverlaps = new List(); try { OverlapWarnings.ResetSessionForTests(); OverlapWarnings.SimulatedActive.Add(mod.PackageId); T.Check(OverlapWarnings.Active().Contains(mod), "Active() misses the injected mod"); T.Check(OverlapWarnings.IsActive(mod.PackageId), "IsActive misses the injected mod"); string text = OverlapWarnings.SettingsText(); T.NotNull(text, "SettingsText"); T.Check(text.Contains(mod.Name) && text.Contains(mod.What), "SettingsText does not name " + mod.Name + " and what it sets: " + text); string message = OverlapWarnings.MessageText(new List { mod }); T.Check(message != null && message.Contains(mod.Name), "MessageText does not name " + mod.Name); List first = OverlapWarnings.ShowOncePerSession(); T.Check(first.Contains(mod), "the first call of the session does not name " + mod.Name); Dialog_OverlapWarnings window = Find.WindowStack.WindowOfType(); T.Check(window != null && window.Mods.Contains(mod), "no overlap window naming " + mod.Name); List second = OverlapWarnings.ShowOncePerSession(); T.Equal(0, second.Count, "mods named by the second call of the session"); } finally { OverlapWarnings.SimulatedActive.Clear(); OverlapWarnings.ResetSessionForTests(); CloseOverlapWindows(); } } [BlaxTest("overlap: a dismissed id is not shown again, and Dismiss keeps it in the mod settings")] public static void OverlapDismissed() { OverlapMod dismissed = SimulatedMod(0); OverlapMod other = SimulatedMod(1); Fixtures.Settings.warnOverlaps = true; Fixtures.Settings.dismissedOverlaps = new List(); try { OverlapWarnings.ResetSessionForTests(); OverlapWarnings.SimulatedActive.Add(dismissed.PackageId); OverlapWarnings.SimulatedActive.Add(other.PackageId); OverlapWarnings.Dismiss(dismissed.PackageId.ToUpperInvariant()); OverlapWarnings.Dismiss(dismissed.PackageId); T.Equal(1, Fixtures.Settings.dismissedOverlaps.Count, "dismissed ids after dismissing one id twice"); T.Check(OverlapWarnings.IsDismissed(dismissed.PackageId), "IsDismissed is false after Dismiss"); List shown = OverlapWarnings.ShowOncePerSession(); T.Check(!shown.Contains(dismissed), "the dismissed " + dismissed.Name + " is shown"); T.Check(shown.Contains(other), "the not dismissed " + other.Name + " is not shown"); // The settings page names every active mod; "Don't remind me" only stops the window. string text = OverlapWarnings.SettingsText(); T.Check(text != null && text.Contains(dismissed.Name), "SettingsText drops the dismissed mod"); OverlapWarnings.ResetSessionForTests(); CloseOverlapWindows(); OverlapWarnings.Dismiss(other.PackageId); T.Equal(0, OverlapWarnings.ShowOncePerSession().Count, "mods shown with every active one dismissed"); T.Check(!Find.WindowStack.IsOpen(typeof(Dialog_OverlapWarnings)), "an overlap window opened with nothing to show"); } finally { OverlapWarnings.SimulatedActive.Clear(); OverlapWarnings.ResetSessionForTests(); CloseOverlapWindows(); // Dismiss wrote the mod settings; write them back without the test's ids. Fixtures.Settings.dismissedOverlaps = new List(); Fixtures.Settings.Write(); } } [BlaxTest("overlap: with warnOverlaps off, no settings text and nothing shown")] public static void OverlapOff() { OverlapMod mod = SimulatedMod(0); Fixtures.Settings.warnOverlaps = false; Fixtures.Settings.dismissedOverlaps = new List(); try { OverlapWarnings.ResetSessionForTests(); OverlapWarnings.SimulatedActive.Add(mod.PackageId); T.Equal(null, OverlapWarnings.SettingsText(), "SettingsText with warnOverlaps off"); T.Equal(0, OverlapWarnings.ShowOncePerSession().Count, "mods shown with warnOverlaps off"); T.Check(!Find.WindowStack.IsOpen(typeof(Dialog_OverlapWarnings)), "an overlap window opened with warnOverlaps off"); } finally { OverlapWarnings.SimulatedActive.Clear(); OverlapWarnings.ResetSessionForTests(); CloseOverlapWindows(); } } // ---- helpers ------------------------------------------------------------------- private static QuickSetup.Candidate Candidate(List candidates, string defName) { QuickSetup.Candidate c = candidates.FirstOrDefault(x => x.Def.defName == defName); T.NotNull(c, defName + " in the quick setup list"); return c; } /// Drops every storage building default from the library in memory; the runner restores it after the test. private static void ClearStorageEntries() => DefaultsLibrary.RestoreForTests(DefaultsLibrary.Snapshot().Where(e => !DefaultsKeys.IsStorage(e.key)).ToList()); /// The n-th listed overlap mod that is not really active, to inject. private static OverlapMod SimulatedMod(int n) { List inactive = OverlapWarnings.Mods.Where(m => !Compat.IsActive(m.PackageId)).ToList(); if (inactive.Count <= n) T.Skip("needs " + (n + 1) + " overlap mods that are not active"); return inactive[n]; } private static void CloseOverlapWindows() { while (Find.WindowStack != null && Find.WindowStack.TryRemove(typeof(Dialog_OverlapWarnings), doCloseSound: false)) { } } } }