375 lines
12 KiB
C#
375 lines
12 KiB
C#
using System;
|
|
using System.Linq;
|
|
using NUnit.Framework;
|
|
using PorygonSharp;
|
|
using PorygonSharp.UserData;
|
|
|
|
namespace PorygonSharpTests
|
|
{
|
|
[TestFixture]
|
|
public class UserDataTests
|
|
{
|
|
private class UserDataTestObject
|
|
{
|
|
public int Foo = 200;
|
|
public int Bar { get; set; }
|
|
public int GetOnly { get; } = 865;
|
|
public readonly int ReadOnly = 684;
|
|
|
|
public int[] IntArray = {55, 60, 846, 346846};
|
|
|
|
public int TestFunc()
|
|
{
|
|
return 345435;
|
|
}
|
|
|
|
public int Add(int a, int b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
public static UserDataTestObject operator + (UserDataTestObject a, UserDataTestObject b)
|
|
{
|
|
a.Foo += b.Foo;
|
|
return a;
|
|
}
|
|
|
|
public static UserDataTestObject operator + (UserDataTestObject a, int b)
|
|
{
|
|
a.Foo += b;
|
|
return a;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanGetFromUserDataField()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
result = v['Foo']
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
|
|
script.CallFunction("test", new UserDataTestObject());
|
|
var variable = script.GetVariable("result");
|
|
Assert.AreEqual(200, variable.EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanSetToUserDataField()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
v['Foo'] = 20000
|
|
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);
|
|
Assert.AreEqual(20000, parameter.Foo);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanGetFromPropertyWithoutSetter()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
result = v['GetOnly']
|
|
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);
|
|
Assert.AreEqual(865, script.GetVariable("result").EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CantSetToPropertyWithoutSetter()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
v['GetOnly'] = 10000
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics().ToArray();
|
|
Assert.IsNotEmpty(diags);
|
|
Assert.AreEqual(1, diags.Length);
|
|
}
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void CanGetFromReadonlyField()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
result = v['ReadOnly']
|
|
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);
|
|
Assert.AreEqual(684, script.GetVariable("result").EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CantSetToReadonlyField()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
v['ReadOnly'] = 10000
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics().ToArray();
|
|
Assert.IsNotEmpty(diags);
|
|
Assert.AreEqual(1, diags.Length);
|
|
}
|
|
}
|
|
|
|
[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());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanGetFromArray()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
return v.IntArray[3]
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
var parameter = new UserDataTestObject();
|
|
var result = script.CallFunction("test", parameter);
|
|
Assert.AreEqual(846, result.EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanSetToArray()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
v.IntArray[2] = 256
|
|
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);
|
|
Assert.AreEqual(256, parameter.IntArray[1]);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanIterateOverArrayKeys()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
value = 0
|
|
for i in v.IntArray do
|
|
value = value + i
|
|
end
|
|
return value
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
var parameter = new UserDataTestObject();
|
|
var result = script.CallFunction("test", parameter);
|
|
Assert.AreEqual(10, result.EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanIterateOverArrayValues()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject v)
|
|
value = 0
|
|
for i, j in v.IntArray do
|
|
value = value + j
|
|
end
|
|
return value
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
var parameter = new UserDataTestObject();
|
|
var result = script.CallFunction("test", parameter);
|
|
Assert.AreEqual(347807, result.EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanUseBinaryOperations()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject a, testObject b)
|
|
return a + b
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
var a = new UserDataTestObject();
|
|
var b = new UserDataTestObject();
|
|
var result = script.CallFunction("test", a, b);
|
|
Assert.AreEqual(400, ((UserDataTestObject)result.GetObjectValue()).Foo);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void CanUseBinaryOperationsWithScriptTypes()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = new Script(@"
|
|
function test(testObject a)
|
|
return a + 500
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
|
}
|
|
|
|
script.Evaluate();
|
|
var a = new UserDataTestObject();
|
|
var result = script.CallFunction("test", a);
|
|
Assert.AreEqual(700, ((UserDataTestObject)result.GetObjectValue()).Foo);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |