General fixes for Tests
This commit is contained in:
63
UpsilonTests/GeneralTests/BasicMathExpressions.cs
Normal file
63
UpsilonTests/GeneralTests/BasicMathExpressions.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class BasicMathExpressions : TestClass
|
||||
{
|
||||
public BasicMathExpressions(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1+1", 2)]
|
||||
[InlineData("1000+1", 1001)]
|
||||
[InlineData("1+1000", 1001)]
|
||||
[InlineData("1 + 1000", 1001)]
|
||||
[InlineData("8612648+6153205", 14765853)]
|
||||
[InlineData("0.5 + 2", 2.5)]
|
||||
[InlineData("0.005 + 2.2", 2.205)]
|
||||
public void Addition(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1-1", 0)]
|
||||
[InlineData("100-45", 55)]
|
||||
[InlineData("1-1200", -1199)]
|
||||
[InlineData("341564-5646843", -5305279)]
|
||||
[InlineData("1-0.5", 0.5)]
|
||||
[InlineData("10.256-2.8546", 7.4014)]
|
||||
public void Subtraction(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1*1", 1)]
|
||||
[InlineData("100 * 100", 10000)]
|
||||
[InlineData("21312 * 41684", 888369408)]
|
||||
public void Multiplication(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1/1", 1)]
|
||||
[InlineData("1000 / 10", 100)]
|
||||
[InlineData("656486 / 5146", 127)]
|
||||
[InlineData("656486 / 5146.0", 127.57209483)]
|
||||
public void Divison(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
206
UpsilonTests/GeneralTests/FunctionTests.cs
Normal file
206
UpsilonTests/GeneralTests/FunctionTests.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class FunctionTests : TestClass
|
||||
{
|
||||
|
||||
public FunctionTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicFunctionTest()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc ()
|
||||
a = 100
|
||||
end
|
||||
a = 50
|
||||
testFunc()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(100, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParameterTest()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc (var1)
|
||||
a = var1
|
||||
end
|
||||
a = 50
|
||||
testFunc(100)
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(100, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParameterBindTest()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc (var1)
|
||||
var1 == true
|
||||
end
|
||||
testFunc(100)
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Single(script.Diagnostics.Messages);
|
||||
Assert.True(script.HasVariable("testFunc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BindUnusedFunctions()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc (var1)
|
||||
var1 == true
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var val = script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.True(script.HasVariable("testFunc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromFunction()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc ()
|
||||
return 5
|
||||
end
|
||||
a = testFunc()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(5, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromFunctionOnce()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc ()
|
||||
return 5
|
||||
return 10
|
||||
end
|
||||
a = testFunc()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(5, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromFunctionNested()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc ()
|
||||
if true then
|
||||
return 5
|
||||
end
|
||||
return 10
|
||||
end
|
||||
a = testFunc()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(5, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromScriptAsFunction()
|
||||
{
|
||||
const string input = @"
|
||||
a = 100
|
||||
return 60
|
||||
a = 87
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(60, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromCSharpCall()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc ()
|
||||
return 100
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.EvaluateFunction<long>("testFunc");
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(100, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromCSharpCallWithParameter()
|
||||
{
|
||||
const string input = @"
|
||||
function testFunc (b)
|
||||
if b then
|
||||
return 100
|
||||
else
|
||||
return 50
|
||||
end
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.EvaluateFunction<long>("testFunc", new object[] {true});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(100, result);
|
||||
var result2 = script.EvaluateFunction<long>("testFunc", new object[] {false});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(50, result2);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnFromCSharpCallWithParameters()
|
||||
{
|
||||
const string input = @"
|
||||
function add (a, b)
|
||||
return a + b
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.EvaluateFunction<long>("add", new object[] {400, 128});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(528, result);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
68
UpsilonTests/GeneralTests/IfTests.cs
Normal file
68
UpsilonTests/GeneralTests/IfTests.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class IfTests : TestClass
|
||||
{
|
||||
public IfTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicIfTest()
|
||||
{
|
||||
const string input = @"
|
||||
if true then
|
||||
return true
|
||||
end
|
||||
return false";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var actual = script.Evaluate<bool>();
|
||||
Assert.True(actual);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("true", 3, 8, 3)]
|
||||
[InlineData("false", 3, 8, 8)]
|
||||
[InlineData("5 == 5", 500, 349, 500)]
|
||||
public void BasicIfElseTests(string condition, int in1, int in2, long expected)
|
||||
{
|
||||
var input =
|
||||
$@"
|
||||
if {condition} then
|
||||
val = {in1}
|
||||
else
|
||||
val = {in2}
|
||||
end";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var actual = script.Evaluate<long>();
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("true", "false", 3, 8, 5, 3)]
|
||||
[InlineData("false", "true", 3, 8, 5, 8)]
|
||||
public void BasicIfElseIfElseTests(string condition1, string condition2, int in1, int in2, int in3, long expected)
|
||||
{
|
||||
var input =
|
||||
$@"
|
||||
if {condition1} then
|
||||
val = {in1}
|
||||
elseif {condition2} then
|
||||
val = {in2}
|
||||
else
|
||||
val = {in3}
|
||||
end";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var actual = script.Evaluate<long>();
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
33
UpsilonTests/GeneralTests/MathPrecedence.cs
Normal file
33
UpsilonTests/GeneralTests/MathPrecedence.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Evaluator;
|
||||
using Upsilon.Parser;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class MathPrecedence : TestClass
|
||||
{
|
||||
public MathPrecedence(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("5 * (10 + 5)", 75)]
|
||||
[InlineData("(10 + 5) * 5", 75)]
|
||||
public void Parenthesis(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("5 * 10 + 5", 55)]
|
||||
[InlineData("5 + 10 * 5", 55)]
|
||||
public void MultiplicationBeforeAddition(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
49
UpsilonTests/GeneralTests/ScopeTests.cs
Normal file
49
UpsilonTests/GeneralTests/ScopeTests.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class ScopeTests : TestClass
|
||||
{
|
||||
public ScopeTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LocalInnerScopeDoesNotOverrideGlobal()
|
||||
{
|
||||
const string input = @"
|
||||
a = 10
|
||||
if true then
|
||||
local a = 100
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(10, a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InnerScopeDoesOverrideGlobal()
|
||||
{
|
||||
const string input = @"
|
||||
a = 10
|
||||
if true then
|
||||
a = 100
|
||||
end
|
||||
b = a
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var a = script.GetVariable<long>("a");
|
||||
Assert.Equal(100, a);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
55
UpsilonTests/GeneralTests/StringTests.cs
Normal file
55
UpsilonTests/GeneralTests/StringTests.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class StringTests : TestClass
|
||||
{
|
||||
public StringTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicStringVariable()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test""
|
||||
return string
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("test", evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringIndexable()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test""
|
||||
return string[3]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("s", evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringAddition()
|
||||
{
|
||||
const string input = @"
|
||||
string = ""test"" + ""123""
|
||||
return string
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<string>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("test123", evaluated);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
182
UpsilonTests/GeneralTests/TableTests.cs
Normal file
182
UpsilonTests/GeneralTests/TableTests.cs
Normal file
@@ -0,0 +1,182 @@
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class TableTests : TestClass
|
||||
{
|
||||
public TableTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicNumberTable()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
100, 200, 300
|
||||
}
|
||||
return table[2]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(200, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicStringTable()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
test = 100,
|
||||
val = 400,
|
||||
another = 10_000,
|
||||
}
|
||||
return table[""another""]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(10_000, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HidesLocalKeys()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
local test = 100,
|
||||
val = 400,
|
||||
another = 10_000,
|
||||
}
|
||||
return table[""test""]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate();
|
||||
Assert.Single(script.Diagnostics.Messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NestedTables()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
{
|
||||
{
|
||||
100, 600, 900
|
||||
},
|
||||
{}
|
||||
}
|
||||
}
|
||||
return table[1][1][2]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(600, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FunctionsInTable()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
function test()
|
||||
return 100
|
||||
end,
|
||||
val = 400,
|
||||
another = 10_000,
|
||||
}
|
||||
return table[""test""]()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(100, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DumbassTableFunctionNesting()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
function func()
|
||||
return function()
|
||||
return {
|
||||
function()
|
||||
return 100
|
||||
end
|
||||
}
|
||||
end
|
||||
end,
|
||||
}
|
||||
return table[""func""]()()[1]()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(100, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ScopeInTables()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
local val = 400,
|
||||
function test()
|
||||
return val
|
||||
end,
|
||||
another = 10_000,
|
||||
}
|
||||
return table[""test""]()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(400, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnnamedFunctionInTable()
|
||||
{
|
||||
const string input = @"
|
||||
table = {
|
||||
function()
|
||||
return 400
|
||||
end
|
||||
}
|
||||
return table[1]()
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(400, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AssignToTable()
|
||||
{
|
||||
const string input = @"
|
||||
table = {}
|
||||
table[1] = 400
|
||||
return table[1]
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(400, evaluated);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
34
UpsilonTests/GeneralTests/UserDataDictionaryTests.cs
Normal file
34
UpsilonTests/GeneralTests/UserDataDictionaryTests.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class UserDataDictionaryTests : TestClass
|
||||
{
|
||||
[Fact]
|
||||
public void BasicStringKeyed()
|
||||
{
|
||||
var arr = new Dictionary<string, int>
|
||||
{
|
||||
{"test", 100},
|
||||
{"1234", 90},
|
||||
{"here", 1683},
|
||||
};
|
||||
const string input = @"
|
||||
function getValue(arr)
|
||||
return arr[""here""]
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(1683, evaluated);
|
||||
}
|
||||
|
||||
public UserDataDictionaryTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
45
UpsilonTests/GeneralTests/UserDataListTests.cs
Normal file
45
UpsilonTests/GeneralTests/UserDataListTests.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class UserDataListTests : TestClass
|
||||
{
|
||||
[Fact]
|
||||
public void BasicArrayTest()
|
||||
{
|
||||
var arr = new[] {100, 30, 56, 213, 76787};
|
||||
const string input = @"
|
||||
function getValue(arr)
|
||||
return arr[3]
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(56, evaluated);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicListTest()
|
||||
{
|
||||
var arr = new List<int> {100, 30, 56, 213, 76787};
|
||||
const string input = @"
|
||||
function getValue(arr)
|
||||
return arr[2]
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(30, evaluated);
|
||||
}
|
||||
|
||||
public UserDataListTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
173
UpsilonTests/GeneralTests/UserDataOperatorTests.cs
Normal file
173
UpsilonTests/GeneralTests/UserDataOperatorTests.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using System;
|
||||
using Upsilon.BaseTypes.UserData;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
// ReSharper disable UnusedMember.Local
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
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 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
|
||||
";
|
||||
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
|
||||
";
|
||||
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
|
||||
";
|
||||
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
|
||||
";
|
||||
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
|
||||
";
|
||||
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);
|
||||
}
|
||||
|
||||
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
162
UpsilonTests/GeneralTests/UserDataTests.cs
Normal file
162
UpsilonTests/GeneralTests/UserDataTests.cs
Normal file
@@ -0,0 +1,162 @@
|
||||
using System;
|
||||
using Upsilon.BaseTypes.UserData;
|
||||
using Upsilon.Evaluator;
|
||||
using Xunit;
|
||||
// ReSharper disable UnusedMember.Local
|
||||
|
||||
namespace UpsilonTests
|
||||
{
|
||||
public class UserDataTests : TestClass, IClassFixture<UserDataTests.UserDataTestsFixture>
|
||||
{
|
||||
public class UserDataTestsFixture : IDisposable
|
||||
{
|
||||
public UserDataTestsFixture()
|
||||
{
|
||||
UserDataTypeHandler.LoadType<UserDataHelper>();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable 414, 649
|
||||
private class UserDataHelper
|
||||
{
|
||||
public string FieldString = "TestField";
|
||||
public string FieldStringSet;
|
||||
private string _privateTestField = "hidden";
|
||||
public bool GetOnly { get; } = false;
|
||||
public bool PrivateSet { get; private set; } = false;
|
||||
|
||||
public bool TestMethodHasRun { get; private set; }
|
||||
public void TestMethod()
|
||||
{
|
||||
TestMethodHasRun = true;
|
||||
}
|
||||
|
||||
public long Add(long a, long b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
}
|
||||
#pragma warning restore 414, 649
|
||||
|
||||
|
||||
[Fact]
|
||||
public void AccessFieldsGet()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
return o.fieldString
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.EvaluateFunction<string>("test", new[] {obj});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("TestField", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AccessFieldsSet()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
o.FieldStringSet = ""Test""
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.EvaluateFunction("test", new[] {obj});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal("Test", obj.FieldStringSet);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RunVoidMethod()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
o.testMethod()
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.EvaluateFunction("test", new[] {obj});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.True(obj.TestMethodHasRun);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnMethodWithParameters()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
return o.add(100, 20)
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.EvaluateFunction<long>("test", new[] {obj});
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(result, 120);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantAccessHiddenFields()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
return o._privateTestField
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.EvaluateFunction("test", new[] {obj});
|
||||
Assert.Single(script.Diagnostics.Messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantSetToFieldsWithNoSetter()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
o.GetOnly = true
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.EvaluateFunction("test", new[] {obj});
|
||||
Assert.Single(script.Diagnostics.Messages);
|
||||
Assert.False(obj.GetOnly);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantSetToFieldsWithPrivateSetter()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
o.PrivateSet = true
|
||||
end
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
script.EvaluateFunction("test", new[] {obj});
|
||||
Assert.Single(script.Diagnostics.Messages);
|
||||
Assert.False(obj.PrivateSet);
|
||||
}
|
||||
|
||||
public UserDataTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user