Compare commits

...

3 Commits

Author SHA1 Message Date
Tim Van Baak 7b890046b6 Add assertion stubs and unit tests 2024-08-28 19:14:19 +00:00
Tim Van Baak 720ccc4329 Add standard repl helper 2024-08-28 15:09:57 +00:00
Tim Van Baak d2a46aa02d Implement dummy assertions 2024-08-28 15:01:27 +00:00
3 changed files with 255 additions and 28 deletions

View File

@ -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,8 @@ 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;
if (!EvaluateAssertion(args[1])) return Strict ? null : this;
break;
case "status":
throw new NotImplementedException();
@ -57,4 +49,49 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) :
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;
/// <summary>
/// Input a multiline string into the repl. Call <see cref="Ready"/> or <see cref="Closed"/> at the end so the
/// Input a multiline string into the repl. Call <see cref="AssertReady"/> or <see cref="AssertClosed"/> at the end so the
/// statement is valid.
/// </summary>
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}\"");
}
}

View File

@ -8,17 +8,19 @@ namespace MultiversalDiplomacyTests;
public class ReplTest
{
private static ReplDriver StandardRepl() => new(
new SetupScriptHandler(World.WithStandardMap(), strict: true));
[Test]
public void SetupHandler()
{
SetupScriptHandler setup = new(World.WithStandardMap(), strict: true);
ReplDriver repl = new(setup);
var repl = StandardRepl();
repl["""
unit Germany A Munich
unit Austria Army Tyrolia
unit England F Lon
"""].Ready();
"""].AssertReady();
Assert.That(repl.Handler, Is.TypeOf<SetupScriptHandler>());
SetupScriptHandler handler = (SetupScriptHandler)repl.Handler!;
@ -29,7 +31,7 @@ public class ReplTest
repl["""
---
"""].Ready();
"""].AssertReady();
Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>());
}
@ -37,20 +39,18 @@ public class ReplTest
[Test]
public void SubmitOrders()
{
SetupScriptHandler setup = new(World.WithStandardMap(), strict: true);
ReplDriver repl = new ReplDriver(setup)["""
var repl = StandardRepl();
repl["""
unit Germany A Mun
unit Austria A Tyr
unit England F Lon
begin
"""];
repl["""
---
Germany A Mun hold
Austria: Army Tyrolia - Vienna
England:
Lon h
"""].Ready();
"""].AssertReady();
Assert.That(repl.Handler, Is.TypeOf<GameScriptHandler>());
GameScriptHandler handler = (GameScriptHandler)repl.Handler!;
@ -64,11 +64,201 @@ public class ReplTest
repl["""
---
"""].Ready();
"""].AssertReady();
Assert.That(repl.Handler, Is.TypeOf<AdjudicationQueryScriptHandler>());
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 = 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();
}
}