Upsilon/UpsilonTests/GeneralTests/UserDataOperatorTests.cs

174 lines
5.4 KiB
C#
Raw Normal View History

using System;
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
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
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
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);
}
[Fact]
public void TestAdditionOverloading()
{
const string input = @"
function add(o1, o2)
return o1 + o2
end
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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);
}
[Fact]
public void TestNegation()
{
const string input = @"
function negate(o1)
return -o1
end
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
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-23 11:55:28 +00:00
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
{
}
}
}