67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
using Upsilon.Evaluator;
|
|
using Xunit;
|
|
|
|
namespace UpsilonTests.GeneralTests
|
|
{
|
|
public class ScopeTests : TestClass
|
|
{
|
|
public ScopeTests(StaticScriptFixture fix) : base(fix)
|
|
{
|
|
}
|
|
|
|
[Fact]
|
|
public void LocalInnerScopeDoesNotOverrideGlobal()
|
|
{
|
|
const string input = @"
|
|
a = 10
|
|
function testFunc()
|
|
local a = 100
|
|
end
|
|
testFunc()
|
|
";
|
|
var script = new Script(input, BoundScope, StaticScope);
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
script.Evaluate();
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
var a = script.GetVariable<long>("a");
|
|
Assert.Equal(10, a);
|
|
}
|
|
|
|
[Fact]
|
|
public void InnerScopeDoesOverrideGlobal()
|
|
{
|
|
const string input = @"
|
|
a = 10
|
|
if true then
|
|
a = 100
|
|
end
|
|
b = a
|
|
";
|
|
var script = new Script(input, BoundScope, StaticScope);
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
script.Evaluate();
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
var a = script.GetVariable<long>("a");
|
|
Assert.Equal(100, a);
|
|
}
|
|
|
|
[Fact]
|
|
public void InnerScopeDefinesToUpperLocalIfLogical()
|
|
{
|
|
const string input = @"
|
|
arr = {100, 56, 28}
|
|
local value = 0
|
|
for key, val in arr do
|
|
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);
|
|
}
|
|
|
|
}
|
|
} |