186 lines
4.2 KiB
C#
186 lines
4.2 KiB
C#
using Upsilon;
|
|
using Upsilon.Evaluator;
|
|
using Upsilon.Exceptions;
|
|
using Xunit;
|
|
|
|
namespace UpsilonTests.GeneralTests
|
|
{
|
|
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 = Executor.ParseInputAndEvaluate(input, Options);
|
|
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 = Executor.ParseInputAndEvaluate(input, Options);
|
|
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)
|
|
";
|
|
Assert.Throws<ParseException>(() => Executor.ParseInputAndEvaluate(input, Options));
|
|
}
|
|
|
|
[Fact]
|
|
public void BindUnusedFunctions()
|
|
{
|
|
const string input = @"
|
|
function testFunc (var1)
|
|
var1 == true
|
|
end
|
|
";
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
|
Assert.True(script.HasVariable("testFunc"));
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnFromFunction()
|
|
{
|
|
const string input = @"
|
|
function testFunc ()
|
|
return 5
|
|
end
|
|
a = testFunc()
|
|
";
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
|
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 = Executor.ParseInputAndEvaluate(input, Options);
|
|
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 = Executor.ParseInputAndEvaluate(input, Options);
|
|
var a = script.GetVariable<long>("a");
|
|
Assert.Equal(5, a);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnFromScriptAsFunction()
|
|
{
|
|
const string input = @"
|
|
a = 100
|
|
return 60
|
|
a = 87
|
|
";
|
|
|
|
var result = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(60, result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnFromCSharpCall()
|
|
{
|
|
const string input = @"
|
|
function testFunc ()
|
|
return 100
|
|
end
|
|
";
|
|
var result = Executor.EvaluateFunction<long>(input, "testFunc", options: Options);
|
|
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 result = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {true}, Options);
|
|
Assert.Equal(100, result);
|
|
var result2 = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {false}, Options);
|
|
Assert.Equal(50, result2);
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnFromCSharpCallWithParameters()
|
|
{
|
|
const string input = @"
|
|
function add (a, b)
|
|
return a + b
|
|
end
|
|
";
|
|
var result = Executor.EvaluateFunction<long>(input, "add", new object[] {400,128}, Options);
|
|
Assert.Equal(528, result);
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleFunctionsInsideBinaryExpressions()
|
|
{
|
|
const string input = @"
|
|
arr = {100, 56, 28}
|
|
value = 0
|
|
for key, val in ipairs(arr) do
|
|
value = value + tonumber(key)
|
|
end
|
|
return value
|
|
";
|
|
var result = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(6, result);
|
|
}
|
|
}
|
|
} |