Upsilon/UpsilonTests/GeneralTests/ScopeTests.cs

67 lines
1.6 KiB
C#
Raw Normal View History

2018-11-14 15:39:52 +00:00
using Upsilon.Evaluator;
using Xunit;
2018-11-23 13:38:45 +00:00
namespace UpsilonTests.GeneralTests
2018-11-14 15:39:52 +00:00
{
2018-11-23 11:55:28 +00:00
public class ScopeTests : TestClass
2018-11-14 15:39:52 +00:00
{
2018-11-23 11:55:28 +00:00
public ScopeTests(StaticScriptFixture fix) : base(fix)
{
}
2018-11-14 15:39:52 +00:00
[Fact]
public void LocalInnerScopeDoesNotOverrideGlobal()
{
const string input = @"
a = 10
2018-11-23 14:27:48 +00:00
function testFunc()
2018-11-14 15:39:52 +00:00
local a = 100
end
2018-11-23 14:27:48 +00:00
testFunc()
2018-11-14 15:39:52 +00:00
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
2018-11-14 15:39:52 +00:00
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
2018-11-14 15:39:52 +00:00
Assert.Empty(script.Diagnostics.Messages);
var a = script.GetVariable<long>("a");
Assert.Equal(10, a);
2018-11-14 15:39:52 +00:00
}
[Fact]
public void InnerScopeDoesOverrideGlobal()
{
const string input = @"
a = 10
if true then
a = 100
end
b = a
";
2018-11-23 11:55:28 +00:00
var script = new Script(input, BoundScope, StaticScope);
2018-11-14 15:39:52 +00:00
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
2018-11-14 15:39:52 +00:00
Assert.Empty(script.Diagnostics.Messages);
var a = script.GetVariable<long>("a");
Assert.Equal(100, a);
2018-11-14 15:39:52 +00:00
}
2018-11-24 11:42:54 +00:00
[Fact]
public void InnerScopeDefinesToUpperLocalIfLogical()
{
const string input = @"
arr = {100, 56, 28}
local value = 0
for key, val in ipairs(arr) do
2018-11-24 11:42:54 +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);
}
2018-11-14 15:39:52 +00:00
}
}