102 lines
2.3 KiB
C#
102 lines
2.3 KiB
C#
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);
|
|
}
|
|
|
|
[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);
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
var evaluated = script.Evaluate<long>();
|
|
Assert.Empty(script.Diagnostics.Messages);
|
|
Assert.Equal(100, evaluated);
|
|
}
|
|
|
|
}
|
|
} |