Upsilon/UpsilonTests/GeneralTests/UserDataTests.cs

167 lines
4.2 KiB
C#
Raw Normal View History

2018-11-19 16:22:25 +00:00
using System;
2018-11-26 15:55:10 +00:00
using Upsilon;
2018-11-19 16:22:25 +00:00
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
using Upsilon.Exceptions;
2018-11-19 16:22:25 +00:00
using Xunit;
2018-11-23 13:38:45 +00:00
// ReSharper disable UnusedMember.Local
2018-11-19 16:22:25 +00:00
2018-11-23 13:38:45 +00:00
namespace UpsilonTests.GeneralTests
2018-11-19 16:22:25 +00:00
{
2018-11-23 11:55:28 +00:00
public class UserDataTests : TestClass, IClassFixture<UserDataTests.UserDataTestsFixture>
2018-11-19 16:22:25 +00:00
{
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;
}
2018-11-19 16:22:25 +00:00
}
#pragma warning restore 414, 649
[Fact]
public void AccessFieldsGet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
return o.fieldString
2018-11-19 16:22:25 +00:00
end
";
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
2018-11-19 16:22:25 +00:00
Assert.Equal("TestField", result);
}
[Fact]
public void AccessFieldsSet()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.FieldStringSet = ""Test""
2018-11-19 16:22:25 +00:00
end
";
Executor.EvaluateFunction(input, "test", new[] {obj}, Options);
2018-11-19 16:22:25 +00:00
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
";
2018-11-26 15:55:10 +00:00
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));
}
2018-11-23 11:55:28 +00:00
public UserDataTests(StaticScriptFixture fix) : base(fix)
{
}
2018-11-19 16:22:25 +00:00
}
}