2018-11-21 12:08:41 +00:00
|
|
|
using System;
|
|
|
|
using Upsilon.BaseTypes.UserData;
|
|
|
|
using Upsilon.Evaluator;
|
|
|
|
using Xunit;
|
|
|
|
// ReSharper disable UnusedMember.Local
|
|
|
|
// ReSharper disable ClassNeverInstantiated.Global
|
|
|
|
|
|
|
|
namespace UpsilonTests
|
|
|
|
{
|
|
|
|
public class UserDataOperatorTests : IClassFixture<UserDataOperatorTests.UserDataOperatorTestsFixture>
|
|
|
|
{
|
|
|
|
public class UserDataOperatorTestsFixture : IDisposable
|
|
|
|
{
|
|
|
|
public UserDataOperatorTestsFixture()
|
|
|
|
{
|
|
|
|
UserDataTypeHandler.LoadType<UserDataHelper>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma warning disable 414, 649
|
|
|
|
private class UserDataHelper
|
|
|
|
{
|
|
|
|
public UserDataHelper(double value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double Value { get; }
|
|
|
|
|
|
|
|
public static UserDataHelper operator +(UserDataHelper a, UserDataHelper b)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(a.Value + b.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static UserDataHelper operator +(UserDataHelper a, double b)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(a.Value + b);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:47:16 +00:00
|
|
|
public static UserDataHelper operator -(UserDataHelper a, UserDataHelper b)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(a.Value - b.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:54:41 +00:00
|
|
|
public static UserDataHelper operator -(UserDataHelper a)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(-a.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:47:16 +00:00
|
|
|
public static UserDataHelper operator *(UserDataHelper a, UserDataHelper b)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(a.Value * b.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static UserDataHelper operator /(UserDataHelper a, UserDataHelper b)
|
|
|
|
{
|
|
|
|
return new UserDataHelper(a.Value / b.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:08:41 +00:00
|
|
|
}
|
|
|
|
#pragma warning restore 414, 649
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestAddition()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function add(o1, o2)
|
|
|
|
return o1 + o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
2018-11-21 12:47:16 +00:00
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(215);
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("add", new[] {o1, o2});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(315, result.Value);
|
2018-11-21 12:08:41 +00:00
|
|
|
}
|
2018-11-21 12:47:16 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestAdditionOverloading()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function add(o1, o2)
|
|
|
|
return o1 + o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
const double o2 = 1.5;
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("add", new object[] {o1, o2});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(101.5, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestSubtraction()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function subtract(o1, o2)
|
|
|
|
return o1 - o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(1.5);
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("subtract", new object[] {o1, o2});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(98.5, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestMultiplication()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function multiply(o1, o2)
|
|
|
|
return o1 * o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(4);
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("multiply", new object[] {o1, o2});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(400, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestDivision()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function divide(o1, o2)
|
|
|
|
return o1 / o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(10);
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("divide", new object[] {o1, o2});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(10, result.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:54:41 +00:00
|
|
|
[Fact]
|
|
|
|
public void TestNegation()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function negate(o1)
|
|
|
|
return -o1
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var script = new Script(input);
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var result = script.EvaluateFunction<UserDataHelper>("negate", new object[] {o1});
|
|
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
|
|
Assert.Equal(-100, result.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:08:41 +00:00
|
|
|
}
|
|
|
|
}
|