5dplomacy/MultiversalDiplomacyTests/RegexTest.cs

101 lines
4.0 KiB
C#
Raw Normal View History

2024-08-20 14:39:49 +00:00
using NUnit.Framework;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacyTests;
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];
}
2024-08-20 14:39:49 +00:00
[Test]
public void UnitTokenizer()
{
World world = World.WithStandardMap();
OrderRegex re = new(world);
var match = re.Hold.Match("Germany Army a-Munich/l@0 holds");
Assert.That(match.Success, Is.True);
var hold = OrderRegex.ParseHold(match);
// Assert.That(hold.power, Is.EqualTo("Germany"));
2024-08-20 14:39:49 +00:00
Assert.That(hold.type, Is.EqualTo("Army"));
Assert.That(hold.timeline, Is.EqualTo("a"));
Assert.That(hold.province, Is.EqualTo("Munich"));
Assert.That(hold.location, Is.EqualTo("l"));
Assert.That(hold.turn, Is.EqualTo("0"));
Assert.That(hold.holdVerb, Is.EqualTo("holds"));
2024-08-20 14:39:49 +00:00
match = re.Hold.Match("F Venice hold");
Assert.That(match.Success, Is.True);
hold = OrderRegex.ParseHold(match);
// Assert.That(hold.power, Is.Null);
2024-08-20 14:39:49 +00:00
Assert.That(hold.type, Is.EqualTo("F"));
Assert.That(hold.timeline, Is.Null);
2024-08-20 14:39:49 +00:00
Assert.That(hold.province, Is.EqualTo("Venice"));
Assert.That(hold.location, Is.Null);
Assert.That(hold.turn, Is.Null);
Assert.That(hold.holdVerb, Is.EqualTo("hold"));
2024-08-20 14:39:49 +00:00
match = re.Move.Match("F Gascony - Spain(nc)");
Assert.That(match.Success, Is.True);
var move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.Null);
2024-08-20 14:39:49 +00:00
Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.Null);
2024-08-20 14:39:49 +00:00
Assert.That(move.province, Is.EqualTo("Gascony"));
Assert.That(move.location, Is.Null);
Assert.That(move.turn, Is.Null);
Assert.That(move.moveVerb, Is.EqualTo("-"));
Assert.That(move.destTimeline, Is.Null);
Assert.That(move.destProvince, Is.EqualTo("Spain"));
Assert.That(move.destLocation, Is.EqualTo("nc"));
Assert.That(move.destTurn, Is.Null);
match = re.Move.Match("F North Sea - Picardy");
Assert.That(match.Success, Is.True);
move = OrderRegex.ParseMove(match);
Assert.That(move.power, Is.Null);
Assert.That(move.type, Is.EqualTo("F"));
Assert.That(move.timeline, Is.Null);
Assert.That(move.province, Is.EqualTo("North Sea"));
Assert.That(move.location, Is.Null);
Assert.That(move.turn, Is.Null);
Assert.That(move.moveVerb, Is.EqualTo("-"));
Assert.That(move.destTimeline, Is.Null);
Assert.That(move.destProvince, Is.EqualTo("Picardy"));
Assert.That(move.destLocation, Is.Null);
Assert.That(move.destTurn, Is.Null);
2024-08-20 14:39:49 +00:00
}
}