138 lines
7.7 KiB
C#
138 lines
7.7 KiB
C#
|
|
using System.Linq;
|
||
|
|
using System.Reflection;
|
||
|
|
using BlaxZones;
|
||
|
|
using HarmonyLib;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxZonesTests
|
||
|
|
{
|
||
|
|
/// <summary>The skeleton and the harness work: patches, settings, the component, the API shape, map fixtures, all three phases.</summary>
|
||
|
|
public static class SmokeTests
|
||
|
|
{
|
||
|
|
private static readonly string[] SharedKeys =
|
||
|
|
{
|
||
|
|
"BlaxZones_DeliveryZone", "BlaxZones_NeedsDlc",
|
||
|
|
"BlaxZones_SettingDeliveryZones", "BlaxZones_SettingRouteOrbital", "BlaxZones_SettingOverflowNearZone",
|
||
|
|
"BlaxZones_SettingOverflowRadius", "BlaxZones_SettingCampAtZones", "BlaxZones_SettingsReset",
|
||
|
|
};
|
||
|
|
|
||
|
|
[BlaxTest("startup: every patch class applied and logged, no errors mentioning Blax Zones")]
|
||
|
|
public static void StartupLogged()
|
||
|
|
{
|
||
|
|
int ours = Harmony.GetAllPatchedMethods()
|
||
|
|
.Select(Harmony.GetPatchInfo)
|
||
|
|
.Where(info => info != null)
|
||
|
|
.SelectMany(info => info.Prefixes.Concat(info.Postfixes).Concat(info.Finalizers).Concat(info.Transpilers))
|
||
|
|
.Count(p => p.owner == BlaxZonesStartup.HarmonyId);
|
||
|
|
T.Info("Harmony patches owned by " + BlaxZonesStartup.HarmonyId + ": " + ours);
|
||
|
|
T.Check(BlaxZonesStartup.FailedPatches.Count == 0, "patch classes failed: " + string.Join(", ", BlaxZonesStartup.FailedPatches));
|
||
|
|
// Only what was logged before the first test: later tests log expected errors on purpose.
|
||
|
|
var startup = TestRunner.StartupLog;
|
||
|
|
bool logged = startup.Any(m => m.text != null && m.text.Contains("[Blax Zones]") && m.text.Contains("patches applied"));
|
||
|
|
T.Check(logged, "startup line '[Blax Zones] N patches applied' not in the log");
|
||
|
|
var errors = startup.Where(m => m.type == LogMessageType.Error && m.text != null && (m.text.Contains("Blax Zones") || m.text.Contains("BlaxZones"))).ToList();
|
||
|
|
T.Check(errors.Count == 0, "errors mentioning Blax Zones at startup: " + string.Join(" | ", errors.Take(3).Select(e => e.text)));
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("startup: settings, shared strings and one component per map")]
|
||
|
|
public static void Loaded()
|
||
|
|
{
|
||
|
|
T.NotNull(BlaxZonesMod.Settings, "BlaxZonesMod.Settings");
|
||
|
|
var missing = SharedKeys.Where(k => !k.CanTranslate()).ToList();
|
||
|
|
T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing));
|
||
|
|
foreach (Map m in Find.Maps)
|
||
|
|
T.Equal(1, m.components.Count(c => c is MapComponent_BlaxZones), "MapComponent_BlaxZones on map " + m.Index);
|
||
|
|
T.Check(Fixtures.Comp != null, "MapComponent_BlaxZones.Of(current map) is null");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("settings: the overflow radius clamps to 5..30")]
|
||
|
|
public static void OverflowRadiusClamp()
|
||
|
|
{
|
||
|
|
BlaxZonesSettings s = Fixtures.Settings;
|
||
|
|
s.overflowRadius = 2;
|
||
|
|
T.Equal(BlaxZonesSettings.MinOverflowRadius, s.OverflowRadiusClamped, "radius 2 clamped");
|
||
|
|
s.overflowRadius = 99;
|
||
|
|
T.Equal(BlaxZonesSettings.MaxOverflowRadius, s.OverflowRadiusClamped, "radius 99 clamped");
|
||
|
|
T.Equal(BlaxZonesSettings.DefaultOverflowRadius, new BlaxZonesSettings().overflowRadius, "default radius");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("api: ZonesApi version 1 binds the way Blax Slaves binds it and refuses a pawn")]
|
||
|
|
public static void ApiShape()
|
||
|
|
{
|
||
|
|
System.Type api = AccessTools.TypeByName("BlaxZones.ZonesApi");
|
||
|
|
T.NotNull(api, "BlaxZones.ZonesApi by name");
|
||
|
|
PropertyInfo version = AccessTools.Property(api, "Version");
|
||
|
|
T.NotNull(version, "ZonesApi.Version property");
|
||
|
|
T.Equal(1, (int)version.GetValue(null), "ZonesApi.Version");
|
||
|
|
MethodInfo m = AccessTools.Method(api, "TryFindDropSpot", new[] { typeof(Map), typeof(Thing), typeof(IntVec3).MakeByRefType() });
|
||
|
|
T.Check(m != null && m.IsStatic && m.ReturnType == typeof(bool), "ZonesApi.TryFindDropSpot(Map, Thing, out IntVec3) static bool");
|
||
|
|
Pawn p = Fixtures.Colonist();
|
||
|
|
T.Check(!ZonesApi.TryFindDropSpot(Fixtures.Map, p, out IntVec3 cell), "TryFindDropSpot accepted a pawn");
|
||
|
|
T.Check(!cell.IsValid, "TryFindDropSpot set a cell for a pawn");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("harness: TestArea, Roof, Wall, Water and DeliveryZone fixtures")]
|
||
|
|
public static void MapFixtures()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
CellRect a = Fixtures.TestArea(4, 3);
|
||
|
|
T.Equal(12, a.Area, "area size");
|
||
|
|
foreach (IntVec3 c in a.ExpandedBy(Fixtures.AreaMargin))
|
||
|
|
{
|
||
|
|
T.Check(c.GetRoof(map) == null, "roof left at " + c);
|
||
|
|
T.Check(!c.Fogged(map), "fog left at " + c);
|
||
|
|
T.Check(c.Standable(map), "cell not standable at " + c);
|
||
|
|
}
|
||
|
|
CellRect b = Fixtures.TestArea(4, 3);
|
||
|
|
T.Check(!a.ExpandedBy(Fixtures.AreaMargin).Overlaps(b.ExpandedBy(Fixtures.AreaMargin)), "two test areas overlap");
|
||
|
|
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a, priority: StoragePriority.Important);
|
||
|
|
T.Equal(12, zone.CellCount, "zone cells");
|
||
|
|
T.Check(map.zoneManager.ZoneAt(a.CenterCell) == zone, "ZoneAt(center) is not the delivery zone");
|
||
|
|
T.Check(map.zoneManager.AllZones.Contains(zone), "zone not registered");
|
||
|
|
T.Check(zone.Accepts(Fixtures.Stack(ThingDefOf.Steel, 10)), "default filter refuses steel");
|
||
|
|
|
||
|
|
Fixtures.Roof(new CellRect(a.minX, a.minZ, 1, 1));
|
||
|
|
T.Check(new IntVec3(a.minX, 0, a.minZ).Roofed(map), "Roof fixture left the cell unroofed");
|
||
|
|
IntVec3 wallCell = new IntVec3(a.maxX, 0, a.maxZ);
|
||
|
|
Fixtures.Wall(wallCell);
|
||
|
|
T.Equal(11, zone.CellCount, "zone cells after a wall on one");
|
||
|
|
Fixtures.Water(new IntVec3(a.minX + 1, 0, a.minZ));
|
||
|
|
T.Check(new IntVec3(a.minX + 1, 0, a.minZ).GetTerrain(map).IsWater, "Water fixture is not water");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("harness: colonist, prisoner and colony slave fixtures")]
|
||
|
|
public static void PawnFixtures()
|
||
|
|
{
|
||
|
|
Pawn c = Fixtures.Colonist();
|
||
|
|
T.Check(c.Spawned && c.IsFreeColonist, "colonist fixture is not a spawned free colonist");
|
||
|
|
Pawn pr = Fixtures.Prisoner();
|
||
|
|
T.Check(pr.IsPrisonerOfColony, "prisoner fixture is not a prisoner of the colony");
|
||
|
|
Pawn s = Fixtures.ColonySlave();
|
||
|
|
T.Check(s.IsSlaveOfColony, "slave fixture is not a slave of the colony");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("harness: the component is on every map after the reload", phase: 2)]
|
||
|
|
public static void ComponentAfterReload()
|
||
|
|
{
|
||
|
|
foreach (Map m in Find.Maps)
|
||
|
|
T.Equal(1, m.components.Count(c => c is MapComponent_BlaxZones), "MapComponent_BlaxZones on map " + m.Index);
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("harness: phase 3 loads with exactly the two removal errors per map and a fresh component", phase: 3)]
|
||
|
|
public static void Phase3Removal()
|
||
|
|
{
|
||
|
|
int maps = TestRunner.Phase3ReplacedCount;
|
||
|
|
T.Check(maps >= 1, "phase 3 replaced no component node");
|
||
|
|
T.Equal(Find.Maps.Count, maps, "replaced component nodes vs maps");
|
||
|
|
T.ExpectErrors("Could not find class " + TestRunner.RemovedComponentClass, maps);
|
||
|
|
T.ExpectErrors("Can't load abstract class Verse.MapComponent", maps);
|
||
|
|
T.Info("load errors: " + T.Phase3LoadErrors.Count + ", resolve warnings: " + T.Phase3ResolveWarnings);
|
||
|
|
T.Equal(2 * maps, T.Phase3LoadErrors.Count, "errors while loading the phase-3 save");
|
||
|
|
foreach (Map m in Find.Maps)
|
||
|
|
T.Equal(1, m.components.Count(c => c is MapComponent_BlaxZones), "fresh MapComponent_BlaxZones on map " + m.Index);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|