Upsilon/UpsilonTests/GeneralTests/ForLoopTests.cs

119 lines
2.5 KiB
C#

using Upsilon;
using Upsilon.BaseTypes;
using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests.GeneralTests
{
public class ForLoopTests : TestClass
{
public ForLoopTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact]
public void BasicNumericForLoopTest()
{
const string input = @"
a = 0
for i=0,5 do
a = a + i
end
return a
";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
public void NumericForLoopWithDifferentStepTest()
{
const string input = @"
a = 0
for i=0,10,2 do
a = a + i
end
return a
";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(30, actual);
}
[Fact]
public void NumericForLoopWithNegativeStepTest()
{
const string input = @"
a = 0
for i=5,0,-1 do
a = a + i
end
return a";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
public void NumericForLoopBreakTest()
{
const string input = @"
a = 0
for i=0,10 do
a = a + i
if i == 5 then
break
end
end
return a
";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
public void GenericForLoopValueTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
value = value + val
end
return value
";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, actual);
}
[Fact]
public void GenericForLoopKeyTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
local keyInt = tonumber(key)
value = value + keyInt
end
return value
";
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(6, actual);
}
[Fact]
public void GenericForLoopValueTypeBindTest()
{
const string input = @"
arr = {100, 56, 28}
for key, val in ipairs(arr) do
a = val
break
end
";
var script = Executor.ParseInputAndEvaluate(input, Options);
Assert.True(script.Bind().Scope.TryGetVariable("a", true, out var variable));
Assert.Equal(Type.Number, variable.TypeContainer.Type);
}
}
}