-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
330 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
NWN.Anvil.Tests/src/main/Services/API/Utils/VirtualMachineTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Anvil.API; | ||
using Anvil.Services; | ||
using NUnit.Framework; | ||
|
||
namespace Anvil.Tests.Services.API.Utils | ||
{ | ||
[TestFixture(Category = "Services.API")] | ||
public sealed class VirtualMachineTests | ||
{ | ||
[Inject] | ||
private static VirtualMachine VirtualMachine { get; set; } = null!; | ||
|
||
[Inject] | ||
private static ScriptHandleFactory ScriptHandleFactory { get; set; } = null!; | ||
|
||
[Test(Description = "Running ExecuteScript correctly executes the specified script with the right object context.")] | ||
[Timeout(5000)] | ||
public void ExecuteScriptAssignsCorrectObjectSelf() | ||
{ | ||
foreach (NwObject nwObject in GetTestObjects()) | ||
{ | ||
ExecuteScript(nwObject); | ||
} | ||
} | ||
|
||
[Test(Description = "Running ExecuteScript in the main loop correctly executes the specified script with the right object context.")] | ||
[Timeout(5000)] | ||
public async Task ExecuteScriptInvalidContextAssignsCorrectObjectSelf() | ||
{ | ||
await NwTask.NextFrame(); | ||
foreach (NwObject nwObject in GetTestObjects()) | ||
{ | ||
ExecuteScript(nwObject); | ||
} | ||
} | ||
|
||
private void ExecuteScript(NwObject nwObject) | ||
{ | ||
string scriptParamKey = "someParamKey"; | ||
string scriptParamValue = "someParamValue"; | ||
|
||
ScriptCallbackHandle handle = ScriptHandleFactory.CreateUniqueHandler(info => | ||
{ | ||
Assert.That(info.ObjectSelf, Is.EqualTo(nwObject)); | ||
Assert.That(info.ScriptParams[scriptParamKey], Is.EqualTo(scriptParamValue)); | ||
return ScriptHandleResult.Handled; | ||
}); | ||
|
||
VirtualMachine.Execute(handle.ScriptName, nwObject, (scriptParamKey, scriptParamValue)); | ||
} | ||
|
||
private static IEnumerable<NwObject> GetTestObjects() | ||
{ | ||
yield return NwModule.Instance; | ||
|
||
foreach (NwGameObject gameObject in NwModule.Instance.Areas.First().Objects) | ||
{ | ||
yield return gameObject; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.