Compare commits

..

2 Commits

3 changed files with 131 additions and 99 deletions

View File

@ -19,7 +19,7 @@ public class OrderRegex(World world)
public string FullLocation => $"(?:{Timeline}-)?{world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?"; public string FullLocation => $"(?:{Timeline}-)?{world.Map.ProvinceRegex}(?:{SlashLocation}|{ParenLocation})?(?:@{Turn})?";
public string UnitSpec => $"(?:(?:{world.Map.PowerRegex} )?{Type} )?{FullLocation}"; public string UnitSpec => $"(?:{Type} )?{FullLocation}";
public const string HoldVerb = "(h|hold|holds)"; public const string HoldVerb = "(h|hold|holds)";
@ -27,59 +27,59 @@ public class OrderRegex(World world)
public const string ViaConvoy = "(convoy|via convoy|by convoy)"; public const string ViaConvoy = "(convoy|via convoy|by convoy)";
public Regex Hold => new($"^{Unit} {HoldVerb}$"); public Regex Hold => new(
$"^{UnitSpec} {HoldVerb}$",
RegexOptions.IgnoreCase);
public static ( public static (
string power,
string type, string type,
string timeline, string timeline,
string province, string province,
string location, string location,
string turn, string turn,
string verb) string holdVerb)
ParseHold(Match match) ParseHold(Match match) => (
=> (match.Groups[1].Value, match.Groups[1].Value,
match.Groups[2].Value, match.Groups[2].Value,
match.Groups[3].Value, match.Groups[3].Value,
match.Groups[4].Value, match.Groups[4].Length > 0
match.Groups[5].Length > 0 ? match.Groups[4].Value
? match.Groups[5].Value : match.Groups[5].Value,
: match.Groups[6].Value, match.Groups[6].Value,
match.Groups[7].Value, match.Groups[7].Value);
match.Groups[8].Value);
public Regex Move => new($"^{UnitSpec} {MoveVerb} {FullLocation}$(?: {ViaConvoy})?"); public Regex Move => new(
$"^{UnitSpec} {MoveVerb} {FullLocation}(?: {ViaConvoy})?$",
RegexOptions.IgnoreCase);
public static ( public static (
string power,
string type, string type,
string timeline, string timeline,
string province, string province,
string location, string location,
string turn, string turn,
string verb, string moveVerb,
string timeline2, string destTimeline,
string province2, string destProvince,
string location2, string destLocation,
string turn2, string destTurn,
string viaConvoy) string viaConvoy)
ParseMove(Match match) ParseMove(Match match) => (
=> (match.Groups[1].Value, match.Groups[1].Value,
match.Groups[2].Value, match.Groups[2].Value,
match.Groups[3].Value, match.Groups[3].Value,
match.Groups[4].Value, match.Groups[4].Length > 0
match.Groups[5].Length > 0 ? match.Groups[4].Value
? match.Groups[5].Value : match.Groups[5].Value,
: match.Groups[6].Value, match.Groups[6].Value,
match.Groups[7].Value, match.Groups[7].Value,
match.Groups[8].Value, match.Groups[8].Value,
match.Groups[9].Value, match.Groups[9].Value,
match.Groups[10].Value, match.Groups[10].Length > 0
match.Groups[11].Length > 1 ? match.Groups[10].Value
? match.Groups[11].Value : match.Groups[11].Value,
: match.Groups[12].Value, match.Groups[12].Value,
match.Groups[13].Value, match.Groups[13].Value);
match.Groups[14].Value);
public static bool TryParseUnit(World world, string unitSpec, [NotNullWhen(true)] out Unit? newUnit) public static bool TryParseUnit(World world, string unitSpec, [NotNullWhen(true)] out Unit? newUnit)
{ {

View File

@ -71,8 +71,8 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle
if (OrderRegex.TryParseOrder(World, orderPower, orderText, out Order? order)) { if (OrderRegex.TryParseOrder(World, orderPower, orderText, out Order? order)) {
Console.WriteLine($"Parsed {orderPower} order \"{orderText}\" but doing anything with it isn't implemented yet"); Console.WriteLine($"Parsed {orderPower} order \"{orderText}\" but doing anything with it isn't implemented yet");
return this; return this;
} }
Console.WriteLine($"Failed to parse \"{orderText}\""); Console.WriteLine($"Failed to parse \"{orderText}\"");
return Strict ? null : this; return Strict ? null : this;

View File

@ -1,5 +1,3 @@
using System.Text.RegularExpressions;
using NUnit.Framework; using NUnit.Framework;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
@ -8,62 +6,96 @@ namespace MultiversalDiplomacyTests;
public class RegexTest public class RegexTest
{ {
[Test] private static TestCaseData Test(string order, params string[] expected)
public void UnitTokenizer() => new TestCaseData(order, expected).SetName($"{{m}}(\"{order}\")");
static IEnumerable<TestCaseData> HoldRegexMatchesTestCases()
{ {
World world = World.WithStandardMap(); // Full specification
OrderRegex re = new(world); yield return Test(
"Army a-Munich/l@0 holds",
var match = re.Hold.Match("Germany Army a-Munich/l@0 holds"); "Army", "a", "Munich", "l", "0", "holds");
Assert.That(match.Success, Is.True); // Case insensitivity
var hold = OrderRegex.ParseHold(match); yield return Test(
Assert.That(hold.power, Is.EqualTo("Germany")); "fleet B-lon/C@0 H",
Assert.That(hold.type, Is.EqualTo("Army")); "fleet", "B", "lon", "C", "0", "H");
Assert.That(hold.timeline, Is.EqualTo("a")); // All optionals missing
Assert.That(hold.province, Is.EqualTo("Munich")); yield return Test(
Assert.That(hold.location, Is.EqualTo("l")); "ROM h",
Assert.That(hold.turn, Is.EqualTo("0")); "", "", "ROM", "", "", "h");
Assert.That(hold.verb, Is.EqualTo("holds")); // No confusion of unit type and timeline
yield return Test(
match = re.Hold.Match("F Venice hold"); "A F-STP hold",
Assert.That(match.Success, Is.True); "A", "F", "STP", "", "", "hold");
hold = OrderRegex.ParseHold(match); // Province with space in name
Assert.That(hold.power, Is.EqualTo("")); yield return Test(
Assert.That(hold.type, Is.EqualTo("F")); "Fleet North Sea Hold",
Assert.That(hold.timeline, Is.EqualTo("")); "Fleet", "", "North Sea", "", "", "Hold");
Assert.That(hold.province, Is.EqualTo("Venice")); // Parenthesis location
Assert.That(hold.location, Is.EqualTo("")); yield return Test(
Assert.That(hold.turn, Is.EqualTo("")); "F Spain(nc) holds",
Assert.That(hold.verb, Is.EqualTo("hold")); "F", "", "Spain", "nc", "", "holds");
match = re.Move.Match("F Gascony - Spain(nc)");
Assert.That(match.Success, Is.True);
var move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.EqualTo(""));
Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.EqualTo(""));
Assert.That(move.province, Is.EqualTo("Gascony"));
Assert.That(move.location, Is.EqualTo(""));
Assert.That(move.turn, Is.EqualTo(""));
Assert.That(move.verb, Is.EqualTo("-"));
Assert.That(move.timeline2, Is.EqualTo(""));
Assert.That(move.province2, Is.EqualTo("Spain"));
Assert.That(move.location2, Is.EqualTo("nc"));
Assert.That(move.turn2, Is.EqualTo(""));
match = re.Move.Match("F North Sea - Picardy");
Assert.That(match.Success, Is.True);
move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.EqualTo(""));
Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.EqualTo(""));
Assert.That(move.province, Is.EqualTo("North Sea"));
Assert.That(move.location, Is.EqualTo(""));
Assert.That(move.turn, Is.EqualTo(""));
Assert.That(move.verb, Is.EqualTo("-"));
Assert.That(move.timeline2, Is.EqualTo(""));
Assert.That(move.province2, Is.EqualTo("Picardy"));
Assert.That(move.location2, Is.EqualTo(""));
Assert.That(move.turn2, Is.EqualTo(""));
} }
}
[TestCaseSource(nameof(HoldRegexMatchesTestCases))]
public void HoldRegexMatches(string order, string[] expected)
{
OrderRegex re = new(World.WithStandardMap());
var match = re.Hold.Match(order);
Assert.True(match.Success, "Match failed");
var (type, timeline, province, location, turn, holdVerb) = OrderRegex.ParseHold(match);
string[] actual = [type, timeline, province, location, turn, holdVerb];
// Use EquivalentTo for more detailed error message
Assert.That(actual, Is.EquivalentTo(expected), "Unexpected parse results");
Assert.That(actual, Is.EqualTo(expected), "Unexpected parse results");
}
static IEnumerable<TestCaseData> MoveRegexMatchesTestCases()
{
static TestCaseData Test(string order, params string[] expected)
=> new TestCaseData(order, expected).SetName($"{{m}}(\"{order}\")");
// Full specification
yield return Test(
"Army a-Munich/l@0 - a-Tyrolia/l@0",
"Army", "a", "Munich", "l", "0", "-", "a", "Tyrolia", "l", "0", "");
// Case insensitivity
yield return Test(
"fleet B-lon/C@0 - B-enc/W@0",
"fleet", "B", "lon", "C", "0", "-", "B", "enc", "W", "0", "");
// All optionals missing
yield return Test(
"ROM - VIE",
"", "", "ROM", "", "", "-", "", "VIE", "", "", "");
// No confusion of unit type and timeline
yield return Test(
"A F-STP - MOS",
"A", "F", "STP", "", "", "-", "", "MOS", "", "", "");
// Elements with spaces
yield return Test(
"Fleet Western Mediterranean Sea moves to Gulf of Lyons via convoy",
"Fleet", "", "Western Mediterranean Sea", "", "", "moves to", "", "Gulf of Lyons", "", "", "via convoy");
// Parenthesis location
yield return Test(
"F Spain(nc) - Spain(sc)",
"F", "", "Spain", "nc", "", "-", "", "Spain", "sc", "", "");
// Timeline designation spells out a province
yield return Test(
"A tyr-MUN(vie) - mun-TYR/vie",
"A", "tyr", "MUN", "vie", "", "-", "mun", "TYR", "vie", "", "");
}
[TestCaseSource(nameof(MoveRegexMatchesTestCases))]
public void MoveRegexMatches(string order, string[] expected)
{
OrderRegex re = new(World.WithStandardMap());
var match = re.Move.Match(order);
Assert.True(match.Success, "Match failed");
var (type, timeline, province, location, turn, moveVerb,
destTimeline, destProvince, destLocation, destTurn, viaConvoy) = OrderRegex.ParseMove(match);
string[] actual = [type, timeline, province, location, turn, moveVerb,
destTimeline, destProvince, destLocation, destTurn, viaConvoy];
// Use EquivalentTo for more detailed error message
Assert.That(actual, Is.EquivalentTo(expected), "Unexpected parse results");
Assert.That(actual, Is.EqualTo(expected), "Unexpected parse results");
}
}