Support properties and readonly fields
This commit is contained in:
@@ -109,59 +109,6 @@ namespace PorygonSharpTests
|
||||
}
|
||||
}
|
||||
|
||||
private class UserDataTestObject
|
||||
{
|
||||
public int Foo = 200;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test9()
|
||||
{
|
||||
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
||||
using (var script = Script.CreateScript(@"
|
||||
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 Test10()
|
||||
{
|
||||
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
||||
using (var script = Script.CreateScript(@"
|
||||
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 TestHash()
|
||||
{
|
||||
|
||||
150
PorygonSharpTests/UserDataTests.cs
Normal file
150
PorygonSharpTests/UserDataTests.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
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 = Script.CreateScript(@"
|
||||
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 = Script.CreateScript(@"
|
||||
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 = Script.CreateScript(@"
|
||||
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 = Script.CreateScript(@"
|
||||
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 = Script.CreateScript(@"
|
||||
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 = Script.CreateScript(@"
|
||||
function test(testObject v)
|
||||
v['ReadOnly'] = 10000
|
||||
end
|
||||
"))
|
||||
{
|
||||
var diags = script.Diagnostics.GetDiagnostics().ToArray();
|
||||
Assert.IsNotEmpty(diags);
|
||||
Assert.AreEqual(1, diags.Length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user