150 lines
4.2 KiB
C#
150 lines
4.2 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;
|
|
}
|
|
|
|
[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(diag.GetCode().ToString());
|
|
}
|
|
|
|
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(diag.GetCode().ToString());
|
|
}
|
|
|
|
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(diag.GetCode().ToString());
|
|
}
|
|
|
|
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(diag.GetCode().ToString());
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |