Upsilon/UpsilonTests/GeneralTests/ForLoopTests.cs

119 lines
3.0 KiB
C#
Raw Normal View History

2018-11-23 13:38:45 +00:00
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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
[Fact]
public void NumericForLoopWithDifferentStepTest()
{
const string input = @"
a = 0
for i=0,10,2 do
a = a + i
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(30, result);
}
[Fact]
public void NumericForLoopWithNegativeStepTest()
{
const string input = @"
a = 0
for i=5,0,-1 do
a = a + i
end
return a";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
2018-11-23 14:27:48 +00:00
[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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
}
2018-11-23 17:18:07 +00:00
[Fact]
public void GenericForLoopValueTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
2018-11-23 17:18:07 +00:00
value = value + val
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(184, result);
}
[Fact]
public void GenericForLoopKeyTest()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in ipairs(arr) do
2018-11-23 17:18:07 +00:00
local keyInt = tonumber(key)
value = value + keyInt
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(6, result);
}
2018-11-23 13:38:45 +00:00
}
}