Upsilon/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs

89 lines
2.1 KiB
C#
Raw Normal View History

2018-11-23 12:34:46 +00:00
using System;
2018-11-26 15:55:10 +00:00
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests.StandardLibraryTests
{
2018-11-23 11:55:28 +00:00
public class BasicFunctionsTests : TestClass
{
2018-11-23 11:55:28 +00:00
public BasicFunctionsTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact]
2018-11-23 12:34:46 +00:00
public void AssertTest()
{
2018-11-26 15:55:10 +00:00
Executor.EvaluateScript("assert(true)", Options);
Assert.Throws<Exception>(() => Executor.EvaluateScript("assert(false)", Options));
2018-11-23 12:34:46 +00:00
}
[Fact]
public void Error()
{
2018-11-26 15:55:10 +00:00
var e = Assert.Throws<Exception>(() => Executor.EvaluateScript(@"error(""test_error"")", Options));
2018-11-23 12:34:46 +00:00
Assert.Equal("test_error", e.Message);
}
2018-11-23 11:55:28 +00:00
[Fact]
2018-11-24 14:11:33 +00:00
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
";
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, result);
}
[Fact]
public void ToNumberTest()
{
const string input = @"
return tonumber(""100"")
";
2018-11-26 15:55:10 +00:00
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
";
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(284, result);
}
2018-11-24 13:49:20 +00:00
[Fact]
public void ToStringTest()
{
const string input = @"
return tostring(100)
";
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateScript<string>(input, Options);
2018-11-24 13:49:20 +00:00
Assert.Equal("100", result);
}
[Fact]
public void TypeTest()
{
const string input = @"
return type(100)
";
2018-11-26 15:55:10 +00:00
var result = Executor.EvaluateScript<string>(input, Options);
2018-11-24 13:49:20 +00:00
Assert.Equal("Number", result);
}
}
}