2018-11-21 12:08:41 +00:00
|
|
|
using System;
|
2018-11-26 15:55:10 +00:00
|
|
|
using Upsilon;
|
2018-11-21 12:08:41 +00:00
|
|
|
using Upsilon.BaseTypes.UserData;
|
|
|
|
using Xunit;
|
2018-11-23 13:38:45 +00:00
|
|
|
|
2018-11-21 12:08:41 +00:00
|
|
|
// ReSharper disable UnusedMember.Local
|
|
|
|
// ReSharper disable ClassNeverInstantiated.Global
|
|
|
|
|
2018-11-23 13:38:45 +00:00
|
|
|
namespace UpsilonTests.GeneralTests
|
2018-11-21 12:08:41 +00:00
|
|
|
{
|
2018-11-23 11:55:28 +00:00
|
|
|
public class UserDataOperatorTests : TestClass, IClassFixture<UserDataOperatorTests.UserDataOperatorTestsFixture>
|
2018-11-21 12:08:41 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
";
|
2018-11-21 12:47:16 +00:00
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(215);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
|
2018-11-21 12:47:16 +00:00
|
|
|
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 o1 = new UserDataHelper(100);
|
|
|
|
const double o2 = 1.5;
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
|
2018-11-21 12:47:16 +00:00
|
|
|
Assert.Equal(101.5, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestSubtraction()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function subtract(o1, o2)
|
|
|
|
return o1 - o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(1.5);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "subtract", new object[] {o1, o2}, Options);
|
2018-11-21 12:47:16 +00:00
|
|
|
Assert.Equal(98.5, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestMultiplication()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function multiply(o1, o2)
|
|
|
|
return o1 * o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(4);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "multiply", new object[] {o1, o2}, Options);
|
2018-11-21 12:47:16 +00:00
|
|
|
Assert.Equal(400, result.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestDivision()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function divide(o1, o2)
|
|
|
|
return o1 / o2
|
|
|
|
end
|
|
|
|
";
|
|
|
|
var o1 = new UserDataHelper(100);
|
|
|
|
var o2 = new UserDataHelper(10);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "divide", new object[] {o1, o2}, Options);
|
2018-11-21 12:47:16 +00:00
|
|
|
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 o1 = new UserDataHelper(100);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<UserDataHelper>(input, "negate", new object[] {o1}, Options);
|
2018-11-21 12:54:41 +00:00
|
|
|
Assert.Equal(-100, result.Value);
|
|
|
|
}
|
|
|
|
|
2018-11-23 11:55:28 +00:00
|
|
|
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
|
|
|
|
{
|
|
|
|
}
|
2018-11-21 12:08:41 +00:00
|
|
|
}
|
|
|
|
}
|