From 26f7cee07097a23606e68b80ba9efaea2b78070b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 5 Sep 2024 05:08:30 +0000 Subject: [PATCH] Add unit tests for move location disambiguation Some of the coastal tests fail because the coast accessibility check isn't implemented yet --- MultiversalDiplomacyTests/OrderParserTest.cs | 86 +++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/MultiversalDiplomacyTests/OrderParserTest.cs b/MultiversalDiplomacyTests/OrderParserTest.cs index cb97e33..10882ee 100644 --- a/MultiversalDiplomacyTests/OrderParserTest.cs +++ b/MultiversalDiplomacyTests/OrderParserTest.cs @@ -5,7 +5,7 @@ using MultiversalDiplomacy.Orders; namespace MultiversalDiplomacyTests; -public class RegexTest +public class OrderParserTest { private static TestCaseData Test(string order, params string[] expected) => new TestCaseData(order, expected).SetName($"{{m}}(\"{order}\")"); @@ -216,4 +216,88 @@ public class RegexTest Assert.That(move.Location, Is.EqualTo("Tyrolia/l")); Assert.That(move.Season.Key, Is.EqualTo("a0")); } + + [Test] + public void OrderDisambiguation() + { + World world = World.WithStandardMap().AddUnits("Germany A Mun"); + OrderParser.TryParseOrder(world, "Germany", "Mun h", out Order? parsed); + Assert.That(parsed?.ToString(), Is.EqualTo("G A a-Munich/l@0 holds")); + } + + [Test] + public void UnitTypeDisambiguatesCoastalLocation() + { + World world = World.WithStandardMap().AddUnits("England F Nth", "Germany A Ruhr"); + + Assert.That( + OrderParser.TryParseOrder(world, "England", "North Sea - Holland", out Order? fleetOrder), + "Failed to parse fleet order"); + Assert.That( + OrderParser.TryParseOrder(world, "Germany", "Ruhr - Holland", out Order? armyOrder), + "Failed to parse army order"); + + Assert.That(fleetOrder, Is.TypeOf(), "Unexpected fleet order"); + Assert.That(armyOrder, Is.TypeOf(), "Unexpected army order"); + Location fleetDest = world.Map.GetLocation(((MoveOrder)fleetOrder!).Location); + Location armyDest = world.Map.GetLocation(((MoveOrder)armyOrder!).Location); + + Assert.That(fleetDest.ProvinceName, Is.EqualTo(armyDest.ProvinceName)); + Assert.That(fleetDest.Type, Is.EqualTo(LocationType.Water), "Unexpected fleet movement location"); + Assert.That(armyDest.Type, Is.EqualTo(LocationType.Land), "Unexpected army movement location"); + } + + [Test] + public void UnitTypeOverrulesNonsenseLocation() + { + World world = World.WithStandardMap().AddUnits("England F Nth", "Germany A Ruhr"); + + Assert.That( + OrderParser.TryParseOrder(world, "England", "F North Sea - Holland/l", out Order? fleetOrder), + "Failed to parse fleet order"); + Assert.That( + OrderParser.TryParseOrder(world, "Germany", "A Ruhr - Holland/w", out Order? armyOrder), + "Failed to parse army order"); + + Assert.That(fleetOrder, Is.TypeOf(), "Unexpected fleet order"); + Assert.That(armyOrder, Is.TypeOf(), "Unexpected army order"); + Location fleetDest = world.Map.GetLocation(((MoveOrder)fleetOrder!).Location); + Location armyDest = world.Map.GetLocation(((MoveOrder)armyOrder!).Location); + + Assert.That(fleetDest.ProvinceName, Is.EqualTo(armyDest.ProvinceName)); + Assert.That(fleetDest.Type, Is.EqualTo(LocationType.Water), "Unexpected fleet movement location"); + Assert.That(armyDest.Type, Is.EqualTo(LocationType.Land), "Unexpected army movement location"); + } + + [Test] + public void DisambiguateSingleAccessibleCoast() + { + World world = World.WithStandardMap().AddUnits("France F Gascony", "France F Marseilles"); + + Assert.That( + OrderParser.TryParseOrder(world, "France", "Gascony - Spain", out Order? northOrder), + "Failed to parse north coast order"); + Assert.That( + OrderParser.TryParseOrder(world, "France", "Marseilles - Spain", out Order? southOrder), + "Failed to parse south coast order"); + + Assert.That(northOrder, Is.TypeOf(), "Unexpected north coast order"); + Assert.That(southOrder, Is.TypeOf(), "Unexpected south coast order"); + Location north = world.Map.GetLocation(((MoveOrder)northOrder!).Location); + Location south = world.Map.GetLocation(((MoveOrder)southOrder!).Location); + + Assert.That(north.Name, Is.EqualTo("north coast"), "Unexpected disambiguation"); + Assert.That(south.Name, Is.EqualTo("south coast"), "Unexpected disambiguation"); + } + + [Test] + public void DisambiguateMultipleAccessibleCoasts() + { + World world = World.WithStandardMap().AddUnits("France F Portugal"); + + Assert.That( + OrderParser.TryParseOrder(world, "France", "Portugal - Spain", out Order? northOrder), + Is.False, + "Should not parse ambiguous coastal move"); + } }