Upsilon/UpsilonTests/GeneralTests/TableTests.cs

182 lines
4.5 KiB
C#

using Upsilon.Evaluator;
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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(200, evaluated);
}
[Fact]
public void BasicStringTable()
{
const string input = @"
table = {
test = 100,
val = 400,
another = 10_000,
}
return table[""another""]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(10_000, evaluated);
}
[Fact]
public void HidesLocalKeys()
{
const string input = @"
table = {
local test = 100,
val = 400,
another = 10_000,
}
return table[""test""]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate();
Assert.Single(script.Diagnostics.Messages);
}
[Fact]
public void NestedTables()
{
const string input = @"
table = {
{
{
100, 600, 900
},
{}
}
}
return table[1][1][2]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
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 script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(400, evaluated);
}
[Fact]
public void UnnamedFunctionInTable()
{
const string input = @"
table = {
function()
return 400
end
}
return table[1]()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(400, evaluated);
}
[Fact]
public void AssignToTable()
{
const string input = @"
table = {}
table[1] = 400
return table[1]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(400, evaluated);
}
}
}