From d2a46aa02dd66282d9bdb93b49f7676f4012bf07 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 28 Aug 2024 15:01:27 +0000 Subject: [PATCH] Implement dummy assertions --- .../Script/AdjudicationQueryScriptHandler.cs | 29 ++++++++++++------- MultiversalDiplomacyTests/ReplDriver.cs | 10 +++---- MultiversalDiplomacyTests/ReplTest.cs | 20 ++++++++++--- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs index dd1f253..683089b 100644 --- a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs +++ b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs @@ -17,7 +17,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : public IScriptHandler? HandleInput(string input) { - var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + var args = input.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); if (args.Length == 0 || input.StartsWith('#')) { return this; @@ -35,16 +35,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : break; case "assert": - string assertion = input["assert ".Length..]; - Regex prov = new($"{World.Map.ProvinceRegex} (.*)"); - Match match = prov.Match(assertion); - if (!match.Success) { - Console.WriteLine($"Could not parse province from \"{assertion}\""); - return Strict ? null : this; - } - // TODO look up order once orders are validated and adjudicated - Console.WriteLine("Order lookup not implemented yet"); - return null; + return HandleAssertion(args[1]); case "status": throw new NotImplementedException(); @@ -57,4 +48,20 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : return this; } + + private AdjudicationQueryScriptHandler? HandleAssertion(string assertion) + { + // Simple assertions + switch (assertion) + { + case "true": + return this; + + case "false": + return null; + } + + Console.WriteLine("Order lookup not implemented yet"); + return this; + } } diff --git a/MultiversalDiplomacyTests/ReplDriver.cs b/MultiversalDiplomacyTests/ReplDriver.cs index ff3b99f..b6f1f15 100644 --- a/MultiversalDiplomacyTests/ReplDriver.cs +++ b/MultiversalDiplomacyTests/ReplDriver.cs @@ -16,7 +16,7 @@ public class ReplDriver(IScriptHandler initialHandler, bool echo = false) bool Echo { get; } = echo; /// - /// Input a multiline string into the repl. Call or at the end so the + /// Input a multiline string into the repl. Call or at the end so the /// statement is valid. /// public ReplDriver this[string input] => ExecuteAll(input); @@ -39,13 +39,13 @@ public class ReplDriver(IScriptHandler initialHandler, bool echo = false) return this; } - public void Ready() + public void AssertReady() { - Assert.That(Handler, Is.Not.Null, "Handler is closed"); + if (Handler is null) Assert.Fail($"Handler terminated after \"{LastInput}\""); } - public void Closed() + public void AssertClosed() { - Assert.That(Handler, Is.Null, "Handler is not closed"); + if (Handler is not null) Assert.Fail($"Handler did not terminate after \"{LastInput}\""); } } diff --git a/MultiversalDiplomacyTests/ReplTest.cs b/MultiversalDiplomacyTests/ReplTest.cs index 77aa1a8..d6c6f46 100644 --- a/MultiversalDiplomacyTests/ReplTest.cs +++ b/MultiversalDiplomacyTests/ReplTest.cs @@ -18,7 +18,7 @@ public class ReplTest unit Germany A Munich unit Austria Army Tyrolia unit England F Lon - """].Ready(); + """].AssertReady(); Assert.That(repl.Handler, Is.TypeOf()); SetupScriptHandler handler = (SetupScriptHandler)repl.Handler!; @@ -29,7 +29,7 @@ public class ReplTest repl[""" --- - """].Ready(); + """].AssertReady(); Assert.That(repl.Handler, Is.TypeOf()); } @@ -50,7 +50,7 @@ public class ReplTest Austria: Army Tyrolia - Vienna England: Lon h - """].Ready(); + """].AssertReady(); Assert.That(repl.Handler, Is.TypeOf()); GameScriptHandler handler = (GameScriptHandler)repl.Handler!; @@ -64,11 +64,23 @@ public class ReplTest repl[""" --- - """].Ready(); + """].AssertReady(); Assert.That(repl.Handler, Is.TypeOf()); var newHandler = (AdjudicationQueryScriptHandler)repl.Handler!; Assert.That(newHandler.World, Is.Not.EqualTo(before)); Assert.That(newHandler.World.Timelines.Pasts.Count, Is.EqualTo(2)); } + + [Test] + public void AssertBasic() + { + var repl = new ReplDriver(new SetupScriptHandler(World.WithStandardMap(), strict: true))[""" + unit Germany A Munich + --- + --- + """]; + repl["assert true"].AssertReady(); + repl["assert false"].AssertClosed(); + } }