146 lines
8.2 KiB
C#
146 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BlaxCastes;
|
|
using HarmonyLib;
|
|
using Verse;
|
|
|
|
namespace BlaxCastesTests
|
|
{
|
|
/// <summary>
|
|
/// The settings (BlaxCastesSettings, BlaxCastesMod). Settings files go through vanilla's own
|
|
/// LoadedModManager.WriteModSettings and ReadModSettings under a test-only mod id, so they land in the
|
|
/// test run's Config folder (run-tests.ps1 passes -savedatafolder) and are deleted again. The window
|
|
/// itself needs OnGUI, so it isn't drawn.
|
|
/// </summary>
|
|
public static class SettingsTests
|
|
{
|
|
private const string TestModId = "BlaxCastesSettingsTest";
|
|
private const string Handle = nameof(BlaxCastesMod);
|
|
|
|
private static readonly string[] Expected =
|
|
{
|
|
"enabled", "manageWorkPriorities", "manageWeaponLoadout", "enableJoinDefaults", "joinMessages", "revertOnLeave",
|
|
"returnToPreviousCaste", "formerCasteDays", "seedStarterCastes", "warnOverlaps", "showAssignColumn",
|
|
};
|
|
|
|
private static readonly string[] SettingsUiKeys =
|
|
{
|
|
"BlaxCastes_SettingEnabled", "BlaxCastes_SettingEnabledTip", "BlaxCastes_SettingJoinDefaults", "BlaxCastes_SettingJoinDefaultsTip",
|
|
"BlaxCastes_SettingJoinMessages", "BlaxCastes_SettingJoinMessagesTip", "BlaxCastes_SettingRevertOnLeave", "BlaxCastes_SettingRevertOnLeaveTip",
|
|
"BlaxCastes_SettingReturnToPrevious", "BlaxCastes_SettingReturnToPreviousTip", "BlaxCastes_SettingFormerCasteDays", "BlaxCastes_SettingFormerCasteDaysTip",
|
|
"BlaxCastes_SettingSeedStarter", "BlaxCastes_SettingSeedStarterTip", "BlaxCastes_SettingWarnOverlaps", "BlaxCastes_SettingWarnOverlapsTip",
|
|
"BlaxCastes_SettingShowAssignColumn", "BlaxCastes_SettingShowAssignColumnTip", "BlaxCastes_SettingNoIdeology", "BlaxCastes_SettingsReset",
|
|
"BlaxCastes_SettingWorkPriorities", "BlaxCastes_SettingWorkPrioritiesTip", "BlaxCastes_SettingWeaponLoadout", "BlaxCastes_SettingWeaponLoadoutTip",
|
|
"BlaxCastes_WorkManagerActive", "BlaxCastes_SettingSeedStarterSlaveNote",
|
|
"BlaxCastes_SettingJoinDefaultsTipNoIdeology", "BlaxCastes_SettingReturnToPreviousTipNoIdeology",
|
|
"BlaxCastes_SettingSeedStarterNoIdeology", "BlaxCastes_SettingSeedStarterTipNoIdeology",
|
|
};
|
|
|
|
private static FieldInfo[] Fields => typeof(BlaxCastesSettings).GetFields(BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
[BlaxTest("settings: mod has a settings category and Settings is the mod's own instance")]
|
|
public static void ModRegistered()
|
|
{
|
|
BlaxCastesMod mod = LoadedModManager.GetMod<BlaxCastesMod>();
|
|
T.NotNull(mod, "LoadedModManager.GetMod<BlaxCastesMod>()");
|
|
T.Check(!mod.SettingsCategory().NullOrEmpty(), "SettingsCategory() is empty, so the mod gets no settings window");
|
|
T.NotNull(BlaxCastesMod.Settings, "BlaxCastesMod.Settings");
|
|
T.Check(ReferenceEquals(mod.GetSettings<BlaxCastesSettings>(), BlaxCastesMod.Settings),
|
|
"BlaxCastesMod.Settings is not the instance the mod writes on close");
|
|
var missing = SettingsUiKeys.Where(k => !k.CanTranslate()).ToList();
|
|
T.Check(missing.Count == 0, "settings window strings missing from Keyed: " + string.Join(", ", missing));
|
|
}
|
|
|
|
[BlaxTest("settings: the 1.0 settings exist; the toggles default to on and formerCasteDays to 60")]
|
|
public static void DefaultsOn()
|
|
{
|
|
var names = Fields.Select(f => f.Name).ToList();
|
|
T.Check(Expected.All(names.Contains), "missing settings fields: " + string.Join(", ", Expected.Where(n => !names.Contains(n))));
|
|
T.Check(names.All(Expected.Contains), "settings fields this test doesn't know (extend it): " + string.Join(", ", names.Where(n => !Expected.Contains(n))));
|
|
var d = new BlaxCastesSettings();
|
|
var off = Fields.Where(f => f.FieldType == typeof(bool) && !(bool)f.GetValue(d)).Select(f => f.Name).ToList();
|
|
T.Check(off.Count == 0, "settings not defaulting to on: " + string.Join(", ", off));
|
|
T.Equal(60, d.formerCasteDays, "formerCasteDays default");
|
|
T.Equal(BlaxCastesSettings.DefaultFormerCasteDays, d.formerCasteDays, "DefaultFormerCasteDays");
|
|
var other = Fields.Where(f => f.FieldType != typeof(bool) && f.Name != "formerCasteDays").Select(f => f.Name).ToList();
|
|
T.Check(other.Count == 0, "settings of another type this test does not know: " + string.Join(", ", other));
|
|
}
|
|
|
|
[BlaxTest("settings: every field round-trips through the settings file, and Reset restores the defaults")]
|
|
public static void RoundTripAndReset()
|
|
{
|
|
var src = new BlaxCastesSettings();
|
|
foreach (FieldInfo f in Fields)
|
|
f.SetValue(src, f.FieldType == typeof(int) ? (object)7 : false);
|
|
string path = SettingsPath();
|
|
try
|
|
{
|
|
LoadedModManager.WriteModSettings(TestModId, Handle, src);
|
|
StopScribe();
|
|
T.Check(File.Exists(path), "WriteModSettings wrote no file at " + path);
|
|
BlaxCastesSettings back = LoadedModManager.ReadModSettings<BlaxCastesSettings>(TestModId, Handle);
|
|
StopScribe();
|
|
T.NotNull(back, "ReadModSettings result");
|
|
var diff = Fields.Where(f => !Equals(f.GetValue(src), f.GetValue(back))).Select(f => f.Name).ToList();
|
|
T.Check(diff.Count == 0, "settings changed over write and read (missing Scribe key or wrong default?): " + string.Join(", ", diff));
|
|
|
|
back.Reset();
|
|
var notReset = Fields.Where(f => !Equals(f.GetValue(back), f.GetValue(new BlaxCastesSettings()))).Select(f => f.Name).ToList();
|
|
T.Check(notReset.Count == 0, "Reset left fields changed: " + string.Join(", ", notReset));
|
|
}
|
|
finally
|
|
{
|
|
StopScribe();
|
|
try { if (File.Exists(path)) File.Delete(path); } catch (Exception e) { T.Info("could not delete " + path + ": " + e.Message); }
|
|
}
|
|
}
|
|
|
|
[BlaxTest("settings: closing the window (WriteSettings) raises SettingsChanged once, without errors")]
|
|
public static void WriteSettingsRaisesChanged()
|
|
{
|
|
BlaxCastesMod mod = LoadedModManager.GetMod<BlaxCastesMod>();
|
|
T.NotNull(mod, "LoadedModManager.GetMod<BlaxCastesMod>()");
|
|
int raised = 0;
|
|
Action handler = () => raised++;
|
|
BlaxCastesMod.SettingsChanged += handler;
|
|
try
|
|
{
|
|
mod.WriteSettings();
|
|
}
|
|
finally
|
|
{
|
|
BlaxCastesMod.SettingsChanged -= handler;
|
|
StopScribe();
|
|
}
|
|
T.Equal(1, raised, "SettingsChanged raised by WriteSettings");
|
|
}
|
|
|
|
[BlaxTest("settings: the window draws only fields Reset restores")]
|
|
public static void WindowFieldsAreReset()
|
|
{
|
|
MethodInfo window = AccessTools.DeclaredMethod(typeof(BlaxCastesMod), nameof(BlaxCastesMod.DoSettingsWindowContents));
|
|
T.NotNull(window, "BlaxCastesMod.DoSettingsWindowContents");
|
|
List<string> drawn = PatchProcessor.ReadMethodBody(window).Select(op => op.Value).OfType<FieldInfo>()
|
|
.Where(f => f.DeclaringType == typeof(BlaxCastesSettings) && !f.IsStatic).Select(f => f.Name).Distinct().ToList();
|
|
T.Info("window uses " + string.Join(", ", drawn));
|
|
var notDrawn = Expected.Where(n => !drawn.Contains(n)).ToList();
|
|
T.Check(notDrawn.Count == 0, "settings the window never draws: " + string.Join(", ", notDrawn));
|
|
}
|
|
|
|
private static string SettingsPath() =>
|
|
Path.Combine(GenFilePaths.ConfigFolderPath, GenText.SanitizeFilename($"Mod_{TestModId}_{Handle}.xml"));
|
|
|
|
/// <summary>A Scribe left mid-load or mid-save would break the runner's save and reload after phase 1.</summary>
|
|
private static void StopScribe()
|
|
{
|
|
if (Scribe.mode == LoadSaveMode.Inactive)
|
|
return;
|
|
T.Info("Scribe was left in mode " + Scribe.mode + "; forced stop");
|
|
Scribe.ForceStop();
|
|
}
|
|
}
|
|
}
|