Upsilon/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs

89 lines
2.1 KiB
C#

using System;
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests.StandardLibraryTests
{
public class BasicFunctionsTests : TestClass
{
public BasicFunctionsTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact]
public void AssertTest()
{
Executor.EvaluateScript("assert(true)", Options);
Assert.Throws<Exception>(() => Executor.EvaluateScript("assert(false)", Options));
}
[Fact]
public void Error()
{
var e = Assert.Throws<Exception>(() => Executor.EvaluateScript(@"error(""test_error"")", Options));
Assert.Equal("test_error", e.Message);
}
[Fact]
public void UpTillNilPairsTest()
{
const string input = @"
arr = {100, 56, 28, nil, 100}
value = 0
for key, val in ipairs(arr) do
value = value + val
end
return value
";
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, result);
}
[Fact]
public void ToNumberTest()
{
const string input = @"
return tonumber(""100"")
";
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(100, result);
}
[Fact]
public void PairsTest()
{
const string input = @"
arr = {100, 56, 28, nil, 100}
value = 0
for key, val in pairs(arr) do
value = value + val
end
return value
";
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(284, result);
}
[Fact]
public void ToStringTest()
{
const string input = @"
return tostring(100)
";
var result = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("100", result);
}
[Fact]
public void TypeTest()
{
const string input = @"
return type(100)
";
var result = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("Number", result);
}
}
}