Remove power from UnitSpec and add hold regex tests

This commit is contained in:
Tim Van Baak 2024-08-26 04:56:09 +00:00
parent ebeb178984
commit ffe164975b
2 changed files with 102 additions and 73 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,28 +27,26 @@ 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})?$");
public static ( public static (
string power, string power,
@ -57,14 +55,14 @@ public class OrderRegex(World world)
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].Value,

View File

@ -1,5 +1,3 @@
using System.Text.RegularExpressions;
using NUnit.Framework; using NUnit.Framework;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
@ -8,6 +6,39 @@ namespace MultiversalDiplomacyTests;
public class RegexTest public class RegexTest
{ {
[TestCase(
"Army a-Munich/l@0 holds",
ExpectedResult = new string[] {"Army", "a", "Munich", "l", "0", "holds"},
Description = "Full specification")]
[TestCase(
"fleet B-lon/C@0 H",
ExpectedResult = new string[] {"fleet", "B", "lon", "C", "0", "H"},
Description = "Case insensitivity")]
[TestCase(
"ROM h",
ExpectedResult = new string[] {"", "", "ROM", "", "", "h"},
Description = "All optionals missing")]
[TestCase(
"A F-STP hold",
ExpectedResult = new string[] {"A", "F", "STP", "", "", "hold"},
Description = "No confusion of unit type and timeline")]
[TestCase(
"Fleet North Sea Hold",
ExpectedResult = new string[] {"Fleet", "", "North Sea", "", "", "Hold"},
Description = "Province with space in name")]
[TestCase(
"F Spain(nc) holds",
ExpectedResult = new string[] {"F", "", "Spain", "nc", "", "holds"},
Description = "Parenthesis location")]
public string[] HoldRegexMatches(string order)
{
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);
return [type, timeline, province, location, turn, holdVerb];
}
[Test] [Test]
public void UnitTokenizer() public void UnitTokenizer()
{ {
@ -17,53 +48,53 @@ public class RegexTest
var match = re.Hold.Match("Germany Army a-Munich/l@0 holds"); var match = re.Hold.Match("Germany Army a-Munich/l@0 holds");
Assert.That(match.Success, Is.True); Assert.That(match.Success, Is.True);
var hold = OrderRegex.ParseHold(match); var hold = OrderRegex.ParseHold(match);
Assert.That(hold.power, Is.EqualTo("Germany")); // Assert.That(hold.power, Is.EqualTo("Germany"));
Assert.That(hold.type, Is.EqualTo("Army")); Assert.That(hold.type, Is.EqualTo("Army"));
Assert.That(hold.timeline, Is.EqualTo("a")); Assert.That(hold.timeline, Is.EqualTo("a"));
Assert.That(hold.province, Is.EqualTo("Munich")); Assert.That(hold.province, Is.EqualTo("Munich"));
Assert.That(hold.location, Is.EqualTo("l")); Assert.That(hold.location, Is.EqualTo("l"));
Assert.That(hold.turn, Is.EqualTo("0")); Assert.That(hold.turn, Is.EqualTo("0"));
Assert.That(hold.verb, Is.EqualTo("holds")); Assert.That(hold.holdVerb, Is.EqualTo("holds"));
match = re.Hold.Match("F Venice hold"); match = re.Hold.Match("F Venice hold");
Assert.That(match.Success, Is.True); Assert.That(match.Success, Is.True);
hold = OrderRegex.ParseHold(match); hold = OrderRegex.ParseHold(match);
Assert.That(hold.power, Is.EqualTo("")); // Assert.That(hold.power, Is.Null);
Assert.That(hold.type, Is.EqualTo("F")); Assert.That(hold.type, Is.EqualTo("F"));
Assert.That(hold.timeline, Is.EqualTo("")); Assert.That(hold.timeline, Is.Null);
Assert.That(hold.province, Is.EqualTo("Venice")); Assert.That(hold.province, Is.EqualTo("Venice"));
Assert.That(hold.location, Is.EqualTo("")); Assert.That(hold.location, Is.Null);
Assert.That(hold.turn, Is.EqualTo("")); Assert.That(hold.turn, Is.Null);
Assert.That(hold.verb, Is.EqualTo("hold")); Assert.That(hold.holdVerb, Is.EqualTo("hold"));
match = re.Move.Match("F Gascony - Spain(nc)"); match = re.Move.Match("F Gascony - Spain(nc)");
Assert.That(match.Success, Is.True); Assert.That(match.Success, Is.True);
var move = OrderRegex.ParseMove(match); var move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.EqualTo("")); Assert.That(move.power, Is.Null);
Assert.That(move.type, Is.EqualTo("F")); Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.EqualTo("")); Assert.That(move.timeline, Is.Null);
Assert.That(move.province, Is.EqualTo("Gascony")); Assert.That(move.province, Is.EqualTo("Gascony"));
Assert.That(move.location, Is.EqualTo("")); Assert.That(move.location, Is.Null);
Assert.That(move.turn, Is.EqualTo("")); Assert.That(move.turn, Is.Null);
Assert.That(move.verb, Is.EqualTo("-")); Assert.That(move.moveVerb, Is.EqualTo("-"));
Assert.That(move.timeline2, Is.EqualTo("")); Assert.That(move.destTimeline, Is.Null);
Assert.That(move.province2, Is.EqualTo("Spain")); Assert.That(move.destProvince, Is.EqualTo("Spain"));
Assert.That(move.location2, Is.EqualTo("nc")); Assert.That(move.destLocation, Is.EqualTo("nc"));
Assert.That(move.turn2, Is.EqualTo("")); Assert.That(move.destTurn, Is.Null);
match = re.Move.Match("F North Sea - Picardy"); match = re.Move.Match("F North Sea - Picardy");
Assert.That(match.Success, Is.True); Assert.That(match.Success, Is.True);
move = OrderRegex.ParseMove(match); move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.EqualTo("")); Assert.That(move.power, Is.Null);
Assert.That(move.type, Is.EqualTo("F")); Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.EqualTo("")); Assert.That(move.timeline, Is.Null);
Assert.That(move.province, Is.EqualTo("North Sea")); Assert.That(move.province, Is.EqualTo("North Sea"));
Assert.That(move.location, Is.EqualTo("")); Assert.That(move.location, Is.Null);
Assert.That(move.turn, Is.EqualTo("")); Assert.That(move.turn, Is.Null);
Assert.That(move.verb, Is.EqualTo("-")); Assert.That(move.moveVerb, Is.EqualTo("-"));
Assert.That(move.timeline2, Is.EqualTo("")); Assert.That(move.destTimeline, Is.Null);
Assert.That(move.province2, Is.EqualTo("Picardy")); Assert.That(move.destProvince, Is.EqualTo("Picardy"));
Assert.That(move.location2, Is.EqualTo("")); Assert.That(move.destLocation, Is.Null);
Assert.That(move.turn2, Is.EqualTo("")); Assert.That(move.destTurn, Is.Null);
} }
} }