Upsilon/UpsilonTests/GeneralTests/UserDataTests.cs

167 lines
4.2 KiB
C#

using System;
using Upsilon;
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
using Upsilon.Exceptions;
using Xunit;
// ReSharper disable UnusedMember.Local
namespace UpsilonTests.GeneralTests
{
public class UserDataTests : TestClass, 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;
private string _privateTestField = "hidden";
public bool GetOnly { get; } = false;
public bool PrivateSet { get; private set; } = false;
public bool TestMethodHasRun { get; private set; }
public void TestMethod()
{
TestMethodHasRun = true;
}
public long Add(long a, long b)
{
return a + b;
}
public bool OverloadMethod(string s)
{
return s == "test";
}
public int OverloadMethod(int i)
{
return i + 10;
}
}
#pragma warning restore 414, 649
[Fact]
public void AccessFieldsGet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.fieldString
end
";
var result = Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
Assert.Equal("TestField", result);
}
[Fact]
public void AccessFieldsSet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.FieldStringSet = ""Test""
end
";
Executor.EvaluateFunction(input, "test", new[] {obj}, Options);
Assert.Equal("Test", obj.FieldStringSet);
}
[Fact]
public void RunVoidMethod()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.testMethod()
end
";
Executor.EvaluateFunction(input, "test", new[] {obj}, Options);
}
[Fact]
public void ReturnMethodWithParameters()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.add(100, 20)
end
";
var result = Executor.EvaluateFunction<long>(input, "test", new[] {obj}, Options);
Assert.Equal(120, result);
}
[Fact]
public void CantAccessHiddenFields()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o._privateTestField
end
";
Assert.Throws<ParseException>(() => Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options));
}
[Fact]
public void CantSetToFieldsWithNoSetter()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.GetOnly = true
end
";
Assert.Throws<ParseException>(() => Executor.EvaluateFunction(input, "test", new[] {obj}, Options));
Assert.False(obj.GetOnly);
}
[Fact]
public void CantSetToFieldsWithPrivateSetter()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.PrivateSet = true
end
";
Assert.Throws<ParseException>(() => Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options));
Assert.False(obj.PrivateSet);
}
[Fact]
public void OverloadMethods()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.overloadMethod(""test"") and o.OverloadMethod(10) == 20
end
";
Assert.True(Executor.EvaluateFunction<bool>(input, "test", new[] {obj}, Options));
}
public UserDataTests(StaticScriptFixture fix) : base(fix)
{
}
}
}