Upsilon/UpsilonTests/GeneralTests/UserDataTests.cs

163 lines
4.6 KiB
C#
Raw Normal View History

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
// 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;
}
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-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)
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);
}
[Fact]
public void RunVoidMethod()
{
var obj = new UserDataHelper();
const string input = @"
function test(o)
o.testMethod()
end
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(120, result);
}
[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);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages);
}
[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);
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);
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
}
}