2018-11-19 16:22:25 +00:00
|
|
|
using System;
|
|
|
|
using Upsilon.BaseTypes.UserData;
|
|
|
|
using Upsilon.Evaluator;
|
|
|
|
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-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-19 16:22:25 +00:00
|
|
|
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)
|
2018-11-20 13:17:15 +00:00
|
|
|
o.FieldStringSet = ""Test""
|
2018-11-19 16:22:25 +00:00
|
|
|
end
|
|
|
|
";
|
2018-11-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-19 16:22:25 +00:00
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
script.EvaluateFunction("test", new[] {obj});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
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-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-20 11:55:41 +00:00
|
|
|
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)
|
2018-11-20 13:17:15 +00:00
|
|
|
return o.add(100, 20)
|
2018-11-20 11:55:41 +00:00
|
|
|
end
|
|
|
|
";
|
2018-11-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-20 11:55:41 +00:00
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var result = script.EvaluateFunction<long>("test", new[] {obj});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(result, 120);
|
|
|
|
}
|
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-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-20 16:33:51 +00:00
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
script.EvaluateFunction("test", new[] {obj});
|
|
|
|
Assert.Single(script.Diagnostics.Messages);
|
|
|
|
}
|
|
|
|
|
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-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-21 13:04:43 +00:00
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
script.EvaluateFunction("test", new[] {obj});
|
|
|
|
Assert.Single(script.Diagnostics.Messages);
|
|
|
|
Assert.False(obj.GetOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CantSetToFieldsWithPrivateSetter()
|
|
|
|
{
|
|
|
|
var obj = new UserDataHelper();
|
|
|
|
const string input = @"
|
|
|
|
function test(o)
|
|
|
|
o.PrivateSet = true
|
|
|
|
end
|
|
|
|
";
|
2018-11-23 11:55:28 +00:00
|
|
|
var script = new Script(input, BoundScope, StaticScope);
|
2018-11-21 13:04:43 +00:00
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
script.EvaluateFunction("test", new[] {obj});
|
|
|
|
Assert.Single(script.Diagnostics.Messages);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|