157 lines
3.2 KiB
C#
157 lines
3.2 KiB
C#
using Upsilon;
|
|
using Upsilon.Evaluator;
|
|
using Upsilon.Exceptions;
|
|
using Xunit;
|
|
|
|
namespace UpsilonTests.GeneralTests
|
|
{
|
|
public class TableTests : TestClass
|
|
{
|
|
public TableTests(StaticScriptFixture fix) : base(fix)
|
|
{
|
|
}
|
|
|
|
[Fact]
|
|
public void BasicNumberTable()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
100, 200, 300
|
|
}
|
|
return table[2]
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(200, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void BasicStringTable()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
test = 100,
|
|
val = 400,
|
|
another = 10_000,
|
|
}
|
|
return table[""another""]
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(10_000, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void HidesLocalKeys()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
local test = 100,
|
|
val = 400,
|
|
another = 10_000,
|
|
}
|
|
return table[""test""]
|
|
";
|
|
Assert.Throws<ParseException>(() => Executor.EvaluateScript<long>(input, Options));
|
|
}
|
|
|
|
[Fact]
|
|
public void NestedTables()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
{
|
|
{
|
|
100, 600, 900
|
|
},
|
|
{}
|
|
}
|
|
}
|
|
return table[1][1][2]
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(600, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void FunctionsInTable()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
function test()
|
|
return 100
|
|
end,
|
|
val = 400,
|
|
another = 10_000,
|
|
}
|
|
return table[""test""]()
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(100, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void DumbassTableFunctionNesting()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
function func()
|
|
return function()
|
|
return {
|
|
function()
|
|
return 100
|
|
end
|
|
}
|
|
end
|
|
end,
|
|
}
|
|
return table[""func""]()()[1]()
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(100, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void ScopeInTables()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
local val = 400,
|
|
function test()
|
|
return val
|
|
end,
|
|
another = 10_000,
|
|
}
|
|
return table[""test""]()
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(400, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void UnnamedFunctionInTable()
|
|
{
|
|
const string input = @"
|
|
table = {
|
|
function()
|
|
return 400
|
|
end
|
|
}
|
|
return table[1]()
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(400, evaluated);
|
|
}
|
|
|
|
[Fact]
|
|
public void AssignToTable()
|
|
{
|
|
const string input = @"
|
|
table = {}
|
|
table[1] = 400
|
|
return table[1]
|
|
";
|
|
var evaluated = Executor.EvaluateScript<long>(input, Options);
|
|
Assert.Equal(400, evaluated);
|
|
}
|
|
|
|
}
|
|
} |