Upsilon/UpsilonTests/TableTests.cs

165 lines
3.8 KiB
C#
Raw Normal View History

using System;
2018-11-18 13:18:24 +00:00
using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests
{
public class TableTests
{
[Fact]
public void BasicNumberTable()
{
const string input = @"
table = {
100, 200, 300
}
return table[2]
";
var script = new Script(input);
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);
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);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
Assert.Null(evaluated);
}
[Fact]
public void NestedTables()
{
const string input = @"
table = {
{
{
100, 600, 900
},
{}
}
}
return table[1][1][2]
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(600, evaluated);
}
2018-11-18 13:18:24 +00:00
[Fact]
public void FunctionsInTable()
{
const string input = @"
table = {
2018-11-18 19:20:03 +00:00
function test()
2018-11-18 13:18:24 +00:00
return 100
2018-11-18 19:20:03 +00:00
end,
2018-11-18 13:18:24 +00:00
val = 400,
another = 10_000,
}
return table[""test""]()
2018-11-18 19:20:03 +00:00
";
var script = new Script(input);
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()
2018-11-18 19:20:03 +00:00
return {
function()
2018-11-18 19:20:03 +00:00
return 100
end
}
end
end,
}
return table[""func""]()()[1]()
2018-11-18 13:18:24 +00:00
";
var script = new Script(input);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(100, evaluated);
}
2018-11-19 11:17:21 +00:00
[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);
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);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(400, evaluated);
}
2018-11-18 13:18:24 +00:00
}
}