diff --git a/Runtime/Autopilot.cs b/Runtime/Autopilot.cs index bd1ba98..441e410 100644 --- a/Runtime/Autopilot.cs +++ b/Runtime/Autopilot.cs @@ -94,7 +94,7 @@ private void Start() { StartCoroutine(Lifespan( _settings.lifespanSec, - _settings.ExitCodeWhenLifespanExpired, + _settings.ExitCode, _settings.exitMessage)); } diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index e61f88a..db80d33 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -116,8 +116,9 @@ public class AutopilotSettings : ScriptableObject /// /// Autopilot exit code options when lifespan expired. + /// When using it from within code, use the ExitCode property. /// - public ExitCodeWhenLifespanExpired exitCode = Settings.ExitCodeWhenLifespanExpired.Normally; + public ExitCodeWhenLifespanExpired exitCode = ExitCodeWhenLifespanExpired.Normally; /// /// Custom exit code to be used if selected Custom for exitCode. @@ -125,13 +126,15 @@ public class AutopilotSettings : ScriptableObject /// 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; diff --git a/Tests/Runtime/AutopilotTest.cs b/Tests/Runtime/AutopilotTest.cs index ccff54b..ccf1ece 100644 --- a/Tests/Runtime/AutopilotTest.cs +++ b/Tests/Runtime/AutopilotTest.cs @@ -39,6 +39,44 @@ private static AutopilotSettings CreateAutopilotSettings(int lifespanSec) return settings; } + [Test] + public async Task Lifespan_Expire_CallTerminateAsync() + { + var spyReporter = ScriptableObject.CreateInstance(); + 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(); + 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() { diff --git a/Tests/Runtime/Settings/AutopilotSettingsTest.cs b/Tests/Runtime/Settings/AutopilotSettingsTest.cs index 1c3488f..a60f812 100644 --- a/Tests/Runtime/Settings/AutopilotSettingsTest.cs +++ b/Tests/Runtime/Settings/AutopilotSettingsTest.cs @@ -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() {