Upsilon/UpsilonTests/GeneralTests/TableTests.cs

157 lines
3.2 KiB
C#
Raw Normal View History

2018-11-26 15:55:10 +00:00
using Upsilon;
2018-11-18 13:18:24 +00:00
using Upsilon.Evaluator;
using Upsilon.Exceptions;
2018-11-18 13:18:24 +00:00
using Xunit;
2018-11-23 13:38:45 +00:00
namespace UpsilonTests.GeneralTests
2018-11-18 13:18:24 +00:00
{
2018-11-23 11:55:28 +00:00
public class TableTests : TestClass
2018-11-18 13:18:24 +00:00
{
2018-11-23 11:55:28 +00:00
public TableTests(StaticScriptFixture fix) : base(fix)
{
}
2018-11-18 13:18:24 +00:00
[Fact]
public void BasicNumberTable()
{
const string input = @"
table = {
100, 200, 300
}
return table[2]
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-18 13:18:24 +00:00
Assert.Equal(200, evaluated);
}
[Fact]
public void BasicStringTable()
{
const string input = @"
table = {
test = 100,
val = 400,
another = 10_000,
}
return table[""another""]
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-18 13:18:24 +00:00
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));
2018-11-18 13:18:24 +00:00
}
[Fact]
public void NestedTables()
{
const string input = @"
table = {
{
{
100, 600, 900
},
{}
}
}
return table[1][1][2]
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
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
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-18 19:20:03 +00:00
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
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-18 13:18:24 +00:00
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""]()
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-19 11:17:21 +00:00
Assert.Equal(400, evaluated);
}
[Fact]
public void UnnamedFunctionInTable()
{
const string input = @"
table = {
function()
return 400
end
}
return table[1]()
2018-11-19 15:22:13 +00:00
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-19 15:22:13 +00:00
Assert.Equal(400, evaluated);
}
[Fact]
public void AssignToTable()
{
const string input = @"
table = {}
table[1] = 400
return table[1]
2018-11-19 11:17:21 +00:00
";
2018-11-26 15:55:10 +00:00
var evaluated = Executor.EvaluateScript<long>(input, Options);
2018-11-19 11:17:21 +00:00
Assert.Equal(400, evaluated);
}
2018-11-18 13:18:24 +00:00
}
}