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;
|
2018-11-26 16:23:56 +00:00
|
|
|
using Upsilon.Exceptions;
|
2018-11-19 16:22:25 +00:00
|
|
|
using Xunit;
|
2018-11-23 13:38:45 +00:00
|
|
|
|
2018-11-20 11:55:41 +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;
|
2018-11-20 16:33:51 +00:00
|
|
|
private string _privateTestField = "hidden";
|
2018-11-21 13:04:43 +00:00
|
|
|
public bool GetOnly { get; } = false;
|
|
|
|
public bool PrivateSet { get; private set; } = false;
|
2018-11-20 11:55:41 +00:00
|
|
|
|
|
|
|
public bool TestMethodHasRun { get; private set; }
|
|
|
|
public void TestMethod()
|
|
|
|
{
|
|
|
|
TestMethodHasRun = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long Add(long a, long b)
|
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
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)
|
2018-11-20 13:17:15 +00:00
|
|
|
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)
|
2018-11-20 13:17:15 +00:00
|
|
|
o.FieldStringSet = ""Test""
|
2018-11-19 16:22:25 +00:00
|
|
|
end
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Executor.EvaluateFunction(input, "test", new[] {obj}, Options);
|
2018-11-19 16:22:25 +00:00
|
|
|
Assert.Equal("Test", obj.FieldStringSet);
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:55:41 +00:00
|
|
|
[Fact]
|
|
|
|
public void RunVoidMethod()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
2018-11-20 13:17:15 +00:00
|
|
|
o.testMethod()
|
2018-11-20 11:55:41 +00:00
|
|
|
end
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Executor.EvaluateFunction(input, "test", new[] {obj}, Options);
|
2018-11-20 11:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ReturnMethodWithParameters()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
2018-11-20 13:17:15 +00:00
|
|
|
return o.add(100, 20)
|
2018-11-20 11:55:41 +00:00
|
|
|
end
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<long>(input, "test", new[] {obj}, Options);
|
2018-11-25 18:30:18 +00:00
|
|
|
Assert.Equal(120, result);
|
2018-11-20 11:55:41 +00:00
|
|
|
}
|
2018-11-20 16:33:51 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CantAccessHiddenFields()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
|
|
|
return o._privateTestField
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Assert.Throws<ParseException>(() => Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options));
|
2018-11-20 16:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:04:43 +00:00
|
|
|
[Fact]
|
|
|
|
public void CantSetToFieldsWithNoSetter()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
|
|
|
o.GetOnly = true
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Assert.Throws<ParseException>(() => Executor.EvaluateFunction(input, "test", new[] {obj}, Options));
|
2018-11-21 13:04:43 +00:00
|
|
|
Assert.False(obj.GetOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CantSetToFieldsWithPrivateSetter()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
|
|
|
o.PrivateSet = true
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Assert.Throws<ParseException>(() => Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options));
|
2018-11-21 13:04:43 +00:00
|
|
|
Assert.False(obj.PrivateSet);
|
|
|
|
}
|
|
|
|
|
2018-11-23 11:55:28 +00:00
|
|
|
public UserDataTests(StaticScriptFixture fix) : base(fix)
|
|
|
|
{
|
|
|
|
}
|
2018-11-19 16:22:25 +00:00
|
|
|
}
|
|
|
|
}
|