Upsilon/UpsilonTests/GeneralTests/ForLoopTests.cs

83 lines
2.0 KiB
C#

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);
}
[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);
}
}
}