5dplomacy/MultiversalDiplomacyTests/ScriptTests.cs

56 lines
1.8 KiB
C#
Raw Permalink Normal View History

2024-08-18 04:24:59 +00:00
using NUnit.Framework;
using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Script;
namespace MultiversalDiplomacyTests;
public class ScriptTests
{
static IEnumerable<TestCaseData> DatcTestCases()
{
foreach (var path in Directory.EnumerateFiles("Scripts/DATC"))
{
yield return new TestCaseData(path)
.SetName($"{{m}}({Path.GetFileNameWithoutExtension(path)})");
}
}
[TestCaseSource(nameof(DatcTestCases))]
public void Test_DATC(string testScriptPath)
{
string filename = Path.GetFileName(testScriptPath);
int line = 0;
2024-09-06 16:04:25 +00:00
bool expectFailure = false;
IScriptHandler handler = new SetupScriptHandler(
(msg) => {/* discard */},
2024-08-28 21:10:41 +00:00
World.WithStandardMap(),
Adjudicator.MovementPhase);
2024-09-06 16:04:25 +00:00
2024-08-18 04:24:59 +00:00
foreach (string input in File.ReadAllLines(testScriptPath)) {
line++;
2024-09-06 16:04:25 +00:00
// Handle test directives
if (input == "#test:skip") {
Assert.Ignore($"Script {filename} skipped at line {line}");
}
if (input == "#test:fails") {
expectFailure = true;
continue;
}
var result = handler.HandleInput(input);
2024-09-06 16:04:25 +00:00
if (expectFailure && result.Success) throw new AssertionException(
$"Script {filename} expected line {line} to fail, but it succeeded");
if (!expectFailure && !result.Success) throw new AssertionException(
$"Script {filename} error at line {line}: {result.Message}");
if (result.NextHandler is null) throw new AssertionException(
$"Script {filename} quit unexpectedly at line {line}: \"{input}\"");
2024-09-06 16:04:25 +00:00
handler = result.NextHandler;
2024-09-06 16:04:25 +00:00
expectFailure = false;
2024-08-18 04:24:59 +00:00
}
}
}