2024-08-27 04:18:36 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
using MultiversalDiplomacy.Script;
|
|
|
|
|
|
|
|
namespace MultiversalDiplomacyTests;
|
|
|
|
|
|
|
|
public class ReplDriver(IScriptHandler initialHandler, bool echo = false)
|
|
|
|
{
|
|
|
|
public IScriptHandler? Handler { get; private set; } = initialHandler;
|
|
|
|
|
|
|
|
private string? LastInput { get; set; } = "";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether to print the inputs as they are executed. This is primarily a debugging aid.
|
|
|
|
/// </summary>
|
|
|
|
bool Echo { get; } = echo;
|
|
|
|
|
|
|
|
public ReplDriver ExecuteAll(string multiline)
|
|
|
|
{
|
2024-09-05 05:27:48 +00:00
|
|
|
var lines = multiline.Split('\n', StringSplitOptions.TrimEntries);
|
2024-08-27 04:18:36 +00:00
|
|
|
return lines.Aggregate(this, (repl, line) => repl.Execute(line));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReplDriver Execute(string inputLine)
|
|
|
|
{
|
|
|
|
if (Handler is null) throw new AssertionException(
|
|
|
|
$"Cannot execute \"{inputLine}\", handler quit. Last input was \"{LastInput}\"");
|
|
|
|
if (Echo) Console.WriteLine($"{Handler.Prompt}{inputLine}");
|
|
|
|
|
2024-09-03 03:20:59 +00:00
|
|
|
var result = Handler.HandleInput(inputLine);
|
|
|
|
if (!result.Success) Assert.Fail($"Script failed at \"{inputLine}\": {result.Message}");
|
|
|
|
|
|
|
|
Handler = result.NextHandler;
|
2024-08-27 04:18:36 +00:00
|
|
|
LastInput = inputLine;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-09-03 03:20:59 +00:00
|
|
|
public void AssertFails(string inputLine)
|
2024-08-27 04:18:36 +00:00
|
|
|
{
|
2024-09-03 03:20:59 +00:00
|
|
|
if (Handler is null) throw new AssertionException(
|
|
|
|
$"Cannot execute \"{inputLine}\", handler quit. Last input was \"{LastInput}\"");
|
|
|
|
if (Echo) Console.WriteLine($"{Handler.Prompt}{inputLine}");
|
2024-08-27 04:18:36 +00:00
|
|
|
|
2024-09-03 03:20:59 +00:00
|
|
|
var result = Handler.HandleInput(inputLine);
|
|
|
|
if (result.Success) Assert.Fail($"Expected \"{inputLine}\" to fail, but it succeeded.");
|
2024-08-27 04:18:36 +00:00
|
|
|
}
|
|
|
|
}
|