Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Nov 16, 2024
1 parent fca42b7 commit 8e0bf0d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void Start()
{
StartCoroutine(Lifespan(
_settings.lifespanSec,
_settings.ExitCodeWhenLifespanExpired,
_settings.ExitCode,
_settings.exitMessage));
}

Expand Down
11 changes: 7 additions & 4 deletions Runtime/Settings/AutopilotSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,25 @@ public class AutopilotSettings : ScriptableObject

/// <summary>
/// Autopilot exit code options when lifespan expired.
/// When using it from within code, use the <code>ExitCode</code> property.
/// </summary>
public ExitCodeWhenLifespanExpired exitCode = Settings.ExitCodeWhenLifespanExpired.Normally;
public ExitCodeWhenLifespanExpired exitCode = ExitCodeWhenLifespanExpired.Normally;

/// <summary>
/// Custom exit code to be used if selected <code>Custom</code> for <c>exitCode</c>.
/// Please enter an integer value.
/// </summary>
public string customExitCode;

public ExitCode ExitCodeWhenLifespanExpired
public ExitCode ExitCode
{
get
{
if (exitCode == Settings.ExitCodeWhenLifespanExpired.Custom)
if (exitCode == ExitCodeWhenLifespanExpired.Custom)
{
return (ExitCode)int.Parse(customExitCode);
return int.TryParse(customExitCode, out var intExitCode)
? (ExitCode)intExitCode
: (ExitCode)exitCode;
}

return (ExitCode)exitCode;
Expand Down
38 changes: 38 additions & 0 deletions Tests/Runtime/AutopilotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ private static AutopilotSettings CreateAutopilotSettings(int lifespanSec)
return settings;
}

[Test]
public async Task Lifespan_Expire_CallTerminateAsync()
{
var spyReporter = ScriptableObject.CreateInstance<SpyReporter>();
var settings = CreateAutopilotSettings(1);
settings.reporters.Add(spyReporter);
settings.exitCode = ExitCodeWhenLifespanExpired.Normally;
settings.customExitCode = "100"; // dummy
settings.exitMessage = "Lifespan expired";

await Launcher.LaunchAutopilotAsync(settings);
await UniTask.NextFrame(); // wait reporter

Assert.That(spyReporter.IsCalled, Is.True);
Assert.That(spyReporter.Arguments["exitCode"], Is.EqualTo(ExitCode.Normally.ToString()));
Assert.That(spyReporter.Arguments["message"], Is.EqualTo("Lifespan expired"));
}

[Test]
public async Task Lifespan_Expire_CallTerminateAsyncWithExitCodeAndMessage()
{
var spyReporter = ScriptableObject.CreateInstance<SpyReporter>();
var settings = CreateAutopilotSettings(1);
settings.reporters.Add(spyReporter);
settings.exitCode = ExitCodeWhenLifespanExpired.Custom;
settings.customExitCode = "100";
settings.exitMessage = "Lifespan expired";

await Launcher.LaunchAutopilotAsync(settings);
await UniTask.NextFrame(); // wait reporter

Assert.That(spyReporter.IsCalled, Is.True);
Assert.That(spyReporter.Arguments["exitCode"], Is.EqualTo("100"));
Assert.That(spyReporter.Arguments["message"], Is.EqualTo("Lifespan expired"));

LogAssert.Expect(LogType.Exception, "AssertionException: Autopilot failed with exit code 100");
}

[Test]
public async Task TerminateAsync_DestroyedAutopilotAndAgentObjects()
{
Expand Down
30 changes: 30 additions & 0 deletions Tests/Runtime/Settings/AutopilotSettingsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ private static Arguments CreateNotCapturedArguments()
return arguments;
}

[Test]
public void ExitCodeWhenLifespanExpired_NotCustom_ReturnsExitCode()
{
var settings = CreateAutopilotSettings();
settings.exitCode = ExitCodeWhenLifespanExpired.AutopilotLifespanExpired;
settings.customExitCode = "100"; // dummy

Assert.That(settings.ExitCode, Is.EqualTo(ExitCode.AutopilotLifespanExpired));
}

[Test]
public void ExitCodeWhenLifespanExpired_Custom_ReturnsCustomExitCode()
{
var settings = CreateAutopilotSettings();
settings.exitCode = ExitCodeWhenLifespanExpired.Custom;
settings.customExitCode = "100";

Assert.That(settings.ExitCode, Is.EqualTo((ExitCode)100));
}

[Test]
public void ExitCodeWhenLifespanExpired_CustomButNotValid_ReturnsExitCodeWhenLifespanExpiredCustom()
{
var settings = CreateAutopilotSettings();
settings.exitCode = ExitCodeWhenLifespanExpired.Custom;
settings.customExitCode = "not valid";

Assert.That(settings.ExitCode, Is.EqualTo((ExitCode)ExitCodeWhenLifespanExpired.Custom));
}

[Test]
public void OverwriteByCommandLineArguments_HasNotCommandlineArguments_KeepScriptableObjectValues()
{
Expand Down

0 comments on commit 8e0bf0d

Please sign in to comment.