Upsilon/UpsilonTests/UserDataTests.cs

108 lines
2.9 KiB
C#

using System;
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
using Xunit;
// ReSharper disable UnusedMember.Local
namespace UpsilonTests
{
public class UserDataTests : IClassFixture<UserDataTests.UserDataTestsFixture>
{
public class UserDataTestsFixture : IDisposable
{
public UserDataTestsFixture()
{
UserDataTypeHandler.LoadType<UserDataHelper>();
}
public void Dispose()
{
}
}
#pragma warning disable 414, 649
private class UserDataHelper
{
public string FieldString = "TestField";
public string FieldStringSet;
public bool TestMethodHasRun { get; private set; }
public void TestMethod()
{
TestMethodHasRun = true;
}
public long Add(long a, long b)
{
return a + b;
}
}
#pragma warning restore 414, 649
[Fact]
public void AccessFieldsGet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.fieldString
end
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<string>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal("TestField", result);
}
[Fact]
public void AccessFieldsSet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.FieldStringSet = ""Test""
end
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal("Test", obj.FieldStringSet);
}
[Fact]
public void RunVoidMethod()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.testMethod()
end
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.True(obj.TestMethodHasRun);
}
[Fact]
public void ReturnMethodWithParameters()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.add(100, 20)
end
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(result, 120);
}
}
}