Support for calling userdata functions
This commit is contained in:
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using PorygonSharp;
|
||||
using PorygonSharp.UserData;
|
||||
using PorygonSharp.Utilities;
|
||||
|
||||
namespace PorygonSharpTests
|
||||
|
||||
@@ -15,6 +15,16 @@ namespace PorygonSharpTests
|
||||
public int Bar { get; set; }
|
||||
public int GetOnly { get; } = 865;
|
||||
public readonly int ReadOnly = 684;
|
||||
|
||||
public int TestFunc()
|
||||
{
|
||||
return 345435;
|
||||
}
|
||||
|
||||
public int Add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -146,5 +156,59 @@ end
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanCallUserdataFunction()
|
||||
{
|
||||
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
||||
using (var script = new Script(@"
|
||||
function test(testObject v)
|
||||
result = v.TestFunc()
|
||||
end
|
||||
"))
|
||||
{
|
||||
var diags = script.Diagnostics.GetDiagnostics();
|
||||
foreach (var diag in diags)
|
||||
{
|
||||
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
||||
}
|
||||
|
||||
script.Evaluate();
|
||||
|
||||
var parameter = new UserDataTestObject();
|
||||
|
||||
script.CallFunction("test", parameter);
|
||||
var variable = script.GetVariable("result");
|
||||
Assert.AreEqual(TypeClass.Number ,variable.GetTypeClass());
|
||||
Assert.AreEqual(345435, script.GetVariable("result").EvaluateInteger());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanCallUserdataFunctionWithArguments()
|
||||
{
|
||||
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
||||
using (var script = new Script(@"
|
||||
function test(testObject v)
|
||||
result = v.Add(5, 200)
|
||||
end
|
||||
"))
|
||||
{
|
||||
var diags = script.Diagnostics.GetDiagnostics();
|
||||
foreach (var diag in diags)
|
||||
{
|
||||
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
||||
}
|
||||
|
||||
script.Evaluate();
|
||||
|
||||
var parameter = new UserDataTestObject();
|
||||
|
||||
script.CallFunction("test", parameter);
|
||||
var variable = script.GetVariable("result");
|
||||
Assert.AreEqual(TypeClass.Number ,variable.GetTypeClass());
|
||||
Assert.AreEqual(205, script.GetVariable("result").EvaluateInteger());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user