rimworld-blax-castes/Tests/BlaxCastesTests/Source/OverlapTests.cs

71 lines
4.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using BlaxCastes;
using RimWorld;
using Verse;
namespace BlaxCastesTests
{
/// <summary>
/// Overlap warnings. None of the overlapping mods is installed on the test machine, so a package id is
/// injected into the detection list (OverlapWarnings.SimulatedActive). The message goes to a fresh
/// component, so the running save's "warned" list stays as it is.
/// </summary>
public static class OverlapTests
{
[BlaxTest("overlap: every overlapping mod has a distinct package id, a name and a Keyed description; the work managers are listed")]
public static void ListIsSane()
{
OverlapMod[] mods = OverlapWarnings.Mods;
T.Equal(5, mods.Length, "overlapping mods");
T.Equal(mods.Length, mods.Select(m => m.PackageId.ToLowerInvariant()).Distinct().Count(), "distinct package ids");
foreach (OverlapMod m in mods)
{
T.Check(!m.Name.NullOrEmpty() && m.PackageId.Contains("."), "bad entry: " + m.Name + " / " + m.PackageId);
T.Check(m.WhatKey.CanTranslate(), m.Name + ": " + m.WhatKey + " missing from Keyed");
}
T.Equal(6, Compat.WorkManagers.Distinct().Count(), "work manager package ids");
string[] keys = { "BlaxCastes_OverlapSettingsIntro", "BlaxCastes_OverlapLine", "BlaxCastes_OverlapAdvice", "BlaxCastes_OverlapMessage",
"BlaxCastes_SettingWarnOverlaps", "BlaxCastes_SettingWarnOverlapsTip" };
List<string> missing = keys.Where(k => !k.CanTranslate()).ToList();
T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing));
List<OverlapMod> real = OverlapWarnings.Active();
T.Info("overlapping mods active in this run: " + (real.Count == 0 ? "none" : string.Join(", ", real.Select(m => m.Name))));
}
[BlaxTest("overlap: an injected active id shows in the settings text and in one message per save; warnOverlaps off hides both")]
public static void InjectedIdIsNamed()
{
OverlapMod target = OverlapWarnings.Mods.First(m => m.PackageId == Compat.AutomaticSlaveOutfit);
bool hadReal = OverlapWarnings.Active().Contains(target);
OverlapWarnings.SimulatedActive.Add(target.PackageId.ToUpperInvariant()); // the id matches in any case, as RimWorld does
try
{
T.Check(OverlapWarnings.Active().Contains(target), "the injected id is not detected");
string text = OverlapWarnings.SettingsText();
T.NotNull(text, "settings text with an overlapping mod active");
T.Check(text.Contains(target.Name) && text.Contains(target.What), "settings text does not name " + target.Name + ": " + text);
Messages.Clear();
var fresh = new GameComponent_BlaxCastes(Current.Game);
List<OverlapMod> named = OverlapWarnings.ShowMessageOnce(fresh);
T.Check(named.Contains(target), "the message did not name " + target.Name);
T.Check(fresh.OverlapWarned(target.PackageId), "the save does not remember the warning");
string message = ((string)OverlapWarnings.MessageText(named)).CapitalizeFirst();
T.Check(message.Contains(target.Name), "message text: " + message);
T.Equal(1, Find.Archive.ArchivablesListForReading.OfType<Message>().Count(m => m.text == message), "overlap messages posted");
T.Equal(0, OverlapWarnings.ShowMessageOnce(fresh).Count, "mods named by a second call in the same save");
Fixtures.Settings.warnOverlaps = false;
T.Check(OverlapWarnings.SettingsText() == null, "settings text with warnOverlaps off");
T.Equal(0, OverlapWarnings.ShowMessageOnce(new GameComponent_BlaxCastes(Current.Game)).Count, "mods named with warnOverlaps off");
}
finally
{
OverlapWarnings.SimulatedActive.Clear();
}
T.Equal(hadReal, OverlapWarnings.Active().Contains(target), "detection after removing the injected id");
}
}
}