Upsilon/UpsilonTests/GeneralTests/UserDataOperatorTests.cs

156 lines
4.4 KiB
C#

using System;
using Upsilon;
using Upsilon.BaseTypes.UserData;
using Xunit;
// ReSharper disable UnusedMember.Local
// ReSharper disable ClassNeverInstantiated.Global
namespace UpsilonTests.GeneralTests
{
public class UserDataOperatorTests : TestClass, 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);
}
public static UserDataHelper operator -(UserDataHelper a, UserDataHelper b)
{
return new UserDataHelper(a.Value - b.Value);
}
public static UserDataHelper operator -(UserDataHelper a)
{
return new UserDataHelper(-a.Value);
}
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);
}
}
#pragma warning restore 414, 649
[Fact]
public void TestAddition()
{
const string input = @"
function add(o1, o2)
return o1 + o2
end
";
var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(215);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
Assert.Equal(315, result.Value);
}
[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;
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
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);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "subtract", new object[] {o1, o2}, Options);
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);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "multiply", new object[] {o1, o2}, Options);
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);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "divide", new object[] {o1, o2}, Options);
Assert.Equal(10, result.Value);
}
[Fact]
public void TestNegation()
{
const string input = @"
function negate(o1)
return -o1
end
";
var o1 = new UserDataHelper(100);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "negate", new object[] {o1}, Options);
Assert.Equal(-100, result.Value);
}
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
{
}
}
}