Skip to content

Commit

Permalink
refactor: Migrate Assert-commands to new game-info system
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Feb 21, 2025
1 parent 12d690b commit 1728967
Showing 1 changed file with 104 additions and 101 deletions.
205 changes: 104 additions & 101 deletions CelesteTAS-EverestInterop/Source/TAS/Input/Commands/AssertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
namespace TAS.Input.Commands;

public static class AssertCommand {
private const string CommandName = "Assert";
private class Meta : ITasCommandMeta {
public string Insert => $"Assert{CommandInfo.Separator}[0;Condition]{CommandInfo.Separator}\"[1;Expected]\"{CommandInfo.Separator}\"[2;Actual]\"";
public string Insert => $"{CommandName}{CommandInfo.Separator}[0;Condition]{CommandInfo.Separator}\"[1;Expected]\"{CommandInfo.Separator}\"[2;Actual]\"";
public bool HasArguments => true;

public IEnumerator<CommandAutoCompleteEntry> GetAutoCompleteEntries(string[] args, string filePath, int fileLine) {
Expand All @@ -23,129 +24,131 @@ public IEnumerator<CommandAutoCompleteEntry> GetAutoCompleteEntries(string[] arg
}
}

private enum AssertCondition {
Equal,
NotEqual,
Contain,
NotContain,
StartWith,
NotStartWith,
EndWith,
NotEndWith,
public enum AssertCondition {
Equal, NotEqual,
Contain, NotContain,
StartWith, NotStartWith,
EndWith, NotEndWith,
}

public static bool Running { get; private set; }
private readonly record struct AssertData(string Expected, InfoCustom.TemplateComponent[] ActualComponents, AssertCondition Condition, string? FailureMessage);

private static readonly Dictionary<int, List<AssertData>> asserts = new();

// Assert, Condition, Expected, Actual, FailureMessage
[TasCommand("Assert", ExecuteTiming = ExecuteTiming.Parse | ExecuteTiming.Runtime, MetaDataProvider = typeof(Meta))]
[TasCommand(CommandName, ExecuteTiming = ExecuteTiming.Parse | ExecuteTiming.Runtime, MetaDataProvider = typeof(Meta))]
private static void Assert(CommandLine commandLine, int studioLine, string filePath, int fileLine) {
string[] args = commandLine.Arguments;
string prefix = $"""
Assert '{Path.GetFileName(filePath)}' line {fileLine} failed
""";

if (Command.Parsing) {
string[] args = commandLine.Arguments;

// Validate arguments
if (args.IsEmpty()) {
AbortTas($"{prefix}Lack of assert condition");
} else if (!Enum.TryParse<AssertCondition>(args[0], true, out _)) {
}
if (!Enum.TryParse<AssertCondition>(args[0], true, out var condition)) {
AbortTas($"{prefix}{args[0]} is not a valid assert condition");
} else if (args.Length < 2 || args[1].IsEmpty()) {
AbortTas($"{prefix}Lack of expected value");
} else if (args.Length < 3 || args[2].IsEmpty()) {
}
if (args.Length < 2 || args[1].IsEmpty()) {
AbortTas($"{prefix}Lack of assert.Expected value");
}
if (args.Length < 3 || args[2].IsEmpty()) {
AbortTas($"{prefix}Lack of actual value");
}

var components = InfoCustom.ParseTemplate(args[2]);
asserts.AddToKey(Manager.Controller.CurrentParsingFrame, new AssertData(args[1], components, condition, args.Length >= 4 ? args[3] : null));
} else {
var condition = Enum.Parse<AssertCondition>(args[0], ignoreCase: true); // Must succeed, since this was checked in Parse
string expected = args[1];
string actualTemplate = args[2];
string? failureMessage = args.Length >= 4 ? args[3] : null;
if (!asserts.TryGetValue(Manager.Controller.CurrentFrameInTas, out var currentAssets)) {
return;
}

Running = true;
// FIXME
string actual = string.Empty;//string.Join("\n", InfoCustom.ParseTemplateLine(actualTemplate, 0, forceAllowCodeExecution: true));
Running = false;
return;
foreach (var assert in currentAssets) {
string actual = InfoCustom.EvaluateTemplate(assert.ActualComponents, 0, forceAllowCodeExecution: true);

switch (condition) {
case AssertCondition.Equal: {
if (actual != expected) {
failureMessage ??= $"""
Expected equal: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
}
case AssertCondition.NotEqual: {
if (actual == expected) {
failureMessage ??= $"""
Expected not equal: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
}
case AssertCondition.Contain:
if (!actual.Contains(expected)) {
failureMessage ??= $"""
Expected contain: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.NotContain:
if (actual.Contains(expected)) {
failureMessage ??= $"""
Expected not contain: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
switch (assert.Condition) {
case AssertCondition.Equal: {
if (actual != assert.Expected) {
string failureMessage = assert.FailureMessage ?? $"""
Expected equal: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
}
break;
case AssertCondition.StartWith:
if (!actual.StartsWith(expected)) {
failureMessage ??= $"""
Expected starts with: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
case AssertCondition.NotEqual: {
if (actual == assert.Expected) {
string failureMessage = assert.FailureMessage ?? $"""
Expected not equal: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
}
break;
case AssertCondition.NotStartWith:
if (actual.StartsWith(expected)) {
failureMessage ??= $"""
Expected not starts with: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.EndWith:
if (!actual.EndsWith(expected)) {
failureMessage ??= $"""
Expected ends with: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.NotEndWith:
if (actual.EndsWith(expected)) {
failureMessage ??= $"""
Expected not ends with: {expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.Contain:
if (!actual.Contains(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected contain: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.NotContain:
if (actual.Contains(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected not contain: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.StartWith:
if (!actual.StartsWith(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected starts with: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.NotStartWith:
if (actual.StartsWith(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected not starts with: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.EndWith:
if (!actual.EndsWith(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected ends with: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
case AssertCondition.NotEndWith:
if (actual.EndsWith(assert.Expected)) {
string failureMessage = assert.FailureMessage ?? $"""
Expected not ends with: {assert.Expected}
But was: {actual}
""";
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;

default:
throw new ArgumentOutOfRangeException();
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
Expand Down

0 comments on commit 1728967

Please sign in to comment.