PorygonSharp/PorygonSharpTests/UserDataTests.cs

214 lines
6.3 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 TestFunc()
{
return 345435;
}
public int Add(int a, int b)
{
return a + b;
}
}
[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());
}
}
}
}