2018-11-26 15:55:10 +00:00
|
|
|
using Upsilon;
|
2018-11-15 14:51:05 +00:00
|
|
|
using Upsilon.Evaluator;
|
2018-11-26 16:23:56 +00:00
|
|
|
using Upsilon.Exceptions;
|
2018-11-15 14:51:05 +00:00
|
|
|
using Xunit;
|
|
|
|
|
2018-11-23 13:38:45 +00:00
|
|
|
namespace UpsilonTests.GeneralTests
|
2018-11-15 14:51:05 +00:00
|
|
|
{
|
2018-11-23 11:55:28 +00:00
|
|
|
public class FunctionTests : TestClass
|
2018-11-15 14:51:05 +00:00
|
|
|
{
|
2018-11-23 11:55:28 +00:00
|
|
|
|
|
|
|
public FunctionTests(StaticScriptFixture fix) : base(fix)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:51:05 +00:00
|
|
|
[Fact]
|
|
|
|
public void BasicFunctionTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc ()
|
|
|
|
a = 100
|
|
|
|
end
|
|
|
|
a = 50
|
|
|
|
testFunc()
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
var a = script.GetVariable<long>("a");
|
|
|
|
Assert.Equal(100, a);
|
2018-11-15 14:51:05 +00:00
|
|
|
}
|
2018-11-15 19:13:53 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ParameterTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc (var1)
|
|
|
|
a = var1
|
|
|
|
end
|
|
|
|
a = 50
|
|
|
|
testFunc(100)
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
var a = script.GetVariable<long>("a");
|
|
|
|
Assert.Equal(100, a);
|
2018-11-15 19:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ParameterBindTest()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc (var1)
|
2018-11-15 19:48:52 +00:00
|
|
|
var1 == true
|
2018-11-15 19:13:53 +00:00
|
|
|
end
|
|
|
|
testFunc(100)
|
|
|
|
";
|
2018-11-26 16:23:56 +00:00
|
|
|
Assert.Throws<ParseException>(() => Executor.ParseInputAndEvaluate(input, Options));
|
2018-11-15 19:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:48:52 +00:00
|
|
|
[Fact]
|
|
|
|
public void BindUnusedFunctions()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc (var1)
|
|
|
|
var1 == true
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.True(script.HasVariable("testFunc"));
|
2018-11-15 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:45:03 +00:00
|
|
|
[Fact]
|
|
|
|
public void ReturnFromFunction()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc ()
|
|
|
|
return 5
|
|
|
|
end
|
|
|
|
a = testFunc()
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
var a = script.GetVariable<long>("a");
|
|
|
|
Assert.Equal(5, a);
|
2018-11-16 12:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ReturnFromFunctionOnce()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc ()
|
|
|
|
return 5
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
a = testFunc()
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
var a = script.GetVariable<long>("a");
|
|
|
|
Assert.Equal(5, a);
|
2018-11-16 12:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ReturnFromFunctionNested()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc ()
|
|
|
|
if true then
|
|
|
|
return 5
|
|
|
|
end
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
a = testFunc()
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var script = Executor.ParseInputAndEvaluate(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
var a = script.GetVariable<long>("a");
|
|
|
|
Assert.Equal(5, a);
|
2018-11-16 12:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ReturnFromScriptAsFunction()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
a = 100
|
|
|
|
return 60
|
|
|
|
a = 87
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
|
|
|
|
var result = Executor.EvaluateScript<long>(input, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.Equal(60, result);
|
2018-11-16 12:45:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 13:46:12 +00:00
|
|
|
[Fact]
|
|
|
|
public void ReturnFromCSharpCall()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc ()
|
|
|
|
return 100
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<long>(input, "testFunc", options: Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.Equal(100, result);
|
2018-11-16 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 13:58:15 +00:00
|
|
|
[Fact]
|
|
|
|
public void ReturnFromCSharpCallWithParameter()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function testFunc (b)
|
|
|
|
if b then
|
|
|
|
return 100
|
|
|
|
else
|
|
|
|
return 50
|
|
|
|
end
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {true}, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.Equal(100, result);
|
2018-11-26 15:55:10 +00:00
|
|
|
var result2 = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {false}, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.Equal(50, result2);
|
2018-11-16 13:58:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void ReturnFromCSharpCallWithParameters()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function add (a, b)
|
|
|
|
return a + b
|
|
|
|
end
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateFunction<long>(input, "add", new object[] {400,128}, Options);
|
2018-11-17 11:40:28 +00:00
|
|
|
Assert.Equal(528, result);
|
2018-11-24 11:55:51 +00:00
|
|
|
}
|
2018-11-16 13:58:15 +00:00
|
|
|
|
2018-11-24 11:55:51 +00:00
|
|
|
[Fact]
|
|
|
|
public void HandleFunctionsInsideBinaryExpressions()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
arr = {100, 56, 28}
|
|
|
|
value = 0
|
2018-11-24 13:35:23 +00:00
|
|
|
for key, val in ipairs(arr) do
|
2018-11-24 11:55:51 +00:00
|
|
|
value = value + tonumber(key)
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
";
|
2018-11-26 15:55:10 +00:00
|
|
|
var result = Executor.EvaluateScript<long>(input, Options);
|
2018-11-24 11:55:51 +00:00
|
|
|
Assert.Equal(6, result);
|
2018-11-16 13:58:15 +00:00
|
|
|
}
|
2019-01-20 14:01:18 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void HandleMultipleFunctionOptions()
|
|
|
|
{
|
|
|
|
const string input = @"
|
|
|
|
function a(number v)
|
|
|
|
return v + 10
|
|
|
|
end
|
|
|
|
function a(string s)
|
|
|
|
return s == ""test""
|
|
|
|
end
|
|
|
|
|
|
|
|
return a(50) == 60 and a(""test"")
|
|
|
|
";
|
|
|
|
Assert.Equal(60, Executor.EvaluateFunction<long>(input, "a", new object[] {50}));
|
|
|
|
Assert.True(Executor.EvaluateFunction<bool>(input, "a", new object[] {"test"}));
|
|
|
|
Assert.True(Executor.EvaluateScript<bool>(input, Options));
|
|
|
|
}
|
2018-11-15 14:51:05 +00:00
|
|
|
}
|
|
|
|
}
|