Compare commits

..

No commits in common. "7b890046b69b9b1156a81d869862e2fc9edf0aaf" and "f02e71d4f94f23af529d6b1ab11b03963e06cc93" have entirely different histories.

3 changed files with 28 additions and 255 deletions

View File

@ -17,7 +17,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
public IScriptHandler? HandleInput(string input) public IScriptHandler? HandleInput(string input)
{ {
var args = input.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (args.Length == 0 || input.StartsWith('#')) if (args.Length == 0 || input.StartsWith('#'))
{ {
return this; return this;
@ -35,8 +35,16 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
break; break;
case "assert": case "assert":
if (!EvaluateAssertion(args[1])) return Strict ? null : this; string assertion = input["assert ".Length..];
break; 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;
case "status": case "status":
throw new NotImplementedException(); throw new NotImplementedException();
@ -49,49 +57,4 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
return this; return this;
} }
private bool EvaluateAssertion(string assertion)
{
var args = assertion.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
switch (args[0])
{
case "true":
return true;
case "false":
return false;
case "order-valid":
// Assert order was valid
case "order-invalid":
// Assert order was invalid
case "has-past":
// Assert a timeline's past
case "holds":
// Assert a unit successfully held
case "dislodged":
// Assert a unit was dislodged
case "moves":
// Assert a unit successfully moved
case "no-move":
// Assert a unit did not move
case "supports":
// Assert a unit's support was given
case "cut":
// Assert a unit's support was cut
default:
Console.WriteLine($"Unknown assertion \"{args[0]}\"");
return !Strict;
}
}
} }

View File

@ -16,7 +16,7 @@ public class ReplDriver(IScriptHandler initialHandler, bool echo = false)
bool Echo { get; } = echo; bool Echo { get; } = echo;
/// <summary> /// <summary>
/// Input a multiline string into the repl. Call <see cref="AssertReady"/> or <see cref="AssertClosed"/> at the end so the /// Input a multiline string into the repl. Call <see cref="Ready"/> or <see cref="Closed"/> at the end so the
/// statement is valid. /// statement is valid.
/// </summary> /// </summary>
public ReplDriver this[string input] => ExecuteAll(input); public ReplDriver this[string input] => ExecuteAll(input);
@ -39,13 +39,13 @@ public class ReplDriver(IScriptHandler initialHandler, bool echo = false)
return this; return this;
} }
public void AssertReady() public void Ready()
{ {
if (Handler is null) Assert.Fail($"Handler terminated after \"{LastInput}\""); Assert.That(Handler, Is.Not.Null, "Handler is closed");
} }
public void AssertClosed() public void Closed()
{ {
if (Handler is not null) Assert.Fail($"Handler did not terminate after \"{LastInput}\""); Assert.That(Handler, Is.Null, "Handler is not closed");
} }
} }

View File

@ -8,19 +8,17 @@ namespace MultiversalDiplomacyTests;
public class ReplTest public class ReplTest
{ {
private static ReplDriver StandardRepl() => new(
new SetupScriptHandler(World.WithStandardMap(), strict: true));
[Test] [Test]
public void SetupHandler() public void SetupHandler()
{ {
var repl = StandardRepl(); SetupScriptHandler setup = new(World.WithStandardMap(), strict: true);
ReplDriver repl = new(setup);
repl[""" repl["""
unit Germany A Munich unit Germany A Munich
unit Austria Army Tyrolia unit Austria Army Tyrolia
unit England F Lon unit England F Lon
"""].AssertReady(); """].Ready();
Assert.That(repl.Handler, Is.TypeOf<SetupScriptHandler>()); Assert.That(repl.Handler, Is.TypeOf<SetupScriptHandler>());
SetupScriptHandler handler = (SetupScriptHandler)repl.Handler!; SetupScriptHandler handler = (SetupScriptHandler)repl.Handler!;
@ -31,7 +29,7 @@ public class ReplTest
repl[""" repl["""
--- ---
"""].AssertReady(); """].Ready();
Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>()); Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>());
} }
@ -39,18 +37,20 @@ public class ReplTest
[Test] [Test]
public void SubmitOrders() public void SubmitOrders()
{ {
var repl = StandardRepl(); SetupScriptHandler setup = new(World.WithStandardMap(), strict: true);
ReplDriver repl = new ReplDriver(setup)["""
repl["""
unit Germany A Mun unit Germany A Mun
unit Austria A Tyr unit Austria A Tyr
unit England F Lon unit England F Lon
--- begin
"""];
repl["""
Germany A Mun hold Germany A Mun hold
Austria: Army Tyrolia - Vienna Austria: Army Tyrolia - Vienna
England: England:
Lon h Lon h
"""].AssertReady(); """].Ready();
Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>()); Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>());
GameScriptHandler handler = (GameScriptHandler)repl.Handler!; GameScriptHandler handler = (GameScriptHandler)repl.Handler!;
@ -64,201 +64,11 @@ public class ReplTest
repl[""" repl["""
--- ---
"""].AssertReady(); """].Ready();
Assert.That(repl.Handler, Is.TypeOf<AdjudicationQueryScriptHandler>()); Assert.That(repl.Handler, Is.TypeOf<AdjudicationQueryScriptHandler>());
var newHandler = (AdjudicationQueryScriptHandler)repl.Handler!; var newHandler = (AdjudicationQueryScriptHandler)repl.Handler!;
Assert.That(newHandler.World, Is.Not.EqualTo(before)); Assert.That(newHandler.World, Is.Not.EqualTo(before));
Assert.That(newHandler.World.Timelines.Pasts.Count, Is.EqualTo(2)); Assert.That(newHandler.World.Timelines.Pasts.Count, Is.EqualTo(2));
} }
[Test]
public void AssertBasic()
{
var repl = StandardRepl();
repl["""
unit Germany A Munich
---
---
assert true
"""].AssertReady();
repl["assert false"].AssertClosed();
}
[Test]
public void AssertInvalidOrder()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
---
Germany A Mun - Mars
---
"""].AssertReady();
// Assertion should pass for an invalid order
repl["assert order-invalid Mun"].AssertReady();
// Assertion should fail for an invalid order
repl["assert order-valid Mun"].AssertClosed();
}
[Test]
public void AssertValidOrder()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
---
Germany A Mun - Tyr
---
"""].AssertReady();
// Assertion should pass for a valid order
repl["assert order-valid Mun"].AssertReady();
// Assertion should fail for a valid order
repl["assert order-invalid Mun"].AssertClosed();
}
[Test]
public void AssertSeasonPast()
{
var repl = StandardRepl();
repl["""
unit England F London
---
---
"""].AssertReady();
// Assertion should pass for a season's past
repl["assert has-past a1>a0"].AssertReady();
// Assertion should fail for an incorrect past
repl["assert has-past a0>a1"].AssertClosed();
}
[Test]
public void AssertHolds()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Austria A Tyr
---
Germany Mun - Tyr
---
"""].AssertReady();
// Assertion should pass for a repelled move
repl["assert holds Tyr"].AssertReady();
// Assertion should fail for a repelled move
repl["assert dislodged Tyr"].AssertClosed();
}
[Test]
public void AssertDislodged()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Germany A Boh
unit Austria A Tyr
---
Germany Mun - Tyr
Germany Boh s Mun - Tyr
---
"""].AssertReady();
// Assertion should pass for a dislodge
repl["assert dislodged Tyr"].AssertReady();
// Assertion should fail for a repelled move
repl["assert holds Tyr"].AssertClosed();
}
[Test]
public void AssertMoves()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
---
Germany Mun - Tyr
---
"""].AssertReady();
// Assertion should pass for a move
repl["assert moves Mun"].AssertReady();
// Assertion should fail for a successful move
repl["assert no-move Mun"].AssertClosed();
}
[Test]
public void AssertRepelled()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Austria A Tyr
---
Germany Mun - Tyr
---
"""].AssertReady();
// Assertion should pass for a repelled move
repl["assert no-move Mun"].AssertReady();
// Assertion should fail for no move
repl["assert moves Mun"].AssertClosed();
}
[Test]
public void AssertSupports()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Germany A Boh
unit Austria A Tyr
---
Germany:
Mun - Tyr
Boh s Mun - Tyr
---
"""].AssertReady();
// `supports` and `cut` are opposites
repl["assert supports Boh"].AssertReady();
repl["assert cut Boh"].AssertClosed();
}
[Test]
public void AssertCutSupport()
{
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Germany A Boh
unit Austria A Tyr
unit Italy A Vienna
---
Germany:
Mun - Tyr
Boh s Mun - Tyr
Italy Vienna - Boh
---
"""].AssertReady();
// `supports` and `cut` are opposites
repl["assert cut Boh"].AssertReady();
repl["assert supports Boh"].AssertClosed();
}
} }