Upsilon/UpsilonTests/GeneralTests/UserDataOperatorTests.cs

156 lines
4.4 KiB
C#
Raw Normal View History

using System;
2018-11-26 15:55:10 +00:00
using Upsilon;
using Upsilon.BaseTypes.UserData;
using Xunit;
2018-11-23 13:38:45 +00:00
// ReSharper disable UnusedMember.Local
// ReSharper disable ClassNeverInstantiated.Global
2018-11-23 13:38:45 +00:00
namespace UpsilonTests.GeneralTests
{
2018-11-23 11:55:28 +00:00
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);
2018-11-26 15:55:10 +00:00
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;
2018-11-26 15:55:10 +00:00
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);
2018-11-26 15:55:10 +00:00
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);
2018-11-26 15:55:10 +00:00
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);
2018-11-26 15:55:10 +00:00
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);
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateFunction<UserDataHelper>(input, "negate", new object[] {o1}, Options);
Assert.Equal(-100, result.Value);
}
2018-11-23 11:55:28 +00:00
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
{
}
}
}