2018-11-23 12:34:46 +00:00
|
|
|
using System;
|
2018-11-26 15:55:10 +00:00
|
|
|
using Upsilon;
|
2018-11-21 19:59:27 +00:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace UpsilonTests.StandardLibraryTests
|
|
|
|
{
|
2018-11-23 11:55:28 +00:00
|
|
|
public class BasicFunctionsTests : TestClass
|
2018-11-21 19:59:27 +00:00
|
|
|
{
|
2018-11-23 11:55:28 +00:00
|
|
|
public BasicFunctionsTests(StaticScriptFixture fix) : base(fix)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-21 19:59:27 +00:00
|
|
|
[Fact]
|
2018-11-23 12:34:46 +00:00
|
|
|
public void AssertTest()
|
2018-11-21 19:59:27 +00:00
|
|
|
{
|
2018-11-26 15:55:10 +00:00
|
|
|
Executor.EvaluateScript("assert(true)", Options);
|
2018-11-26 16:23:56 +00:00
|
|
|
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-21 19:59:27 +00:00
|
|
|
}
|
2018-11-23 11:55:28 +00:00
|
|
|
|
2018-11-24 12:35:40 +00:00
|
|
|
[Fact]
|
2018-11-24 14:11:33 +00:00
|
|
|
public void UpTillNilPairsTest()
|
2018-11-24 12:35:40 +00:00
|
|
|
{
|
|
|
|
const string input = @"
|
2018-11-24 13:39:53 +00:00
|
|
|
arr = {100, 56, 28, nil, 100}
|
2018-11-24 12:35:40 +00:00
|
|
|
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);
|
2018-11-24 12:35:40 +00:00
|
|
|
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);
|
2018-11-24 12:35:40 +00:00
|
|
|
Assert.Equal(100, result);
|
|
|
|
}
|
|
|
|
|
2018-11-24 13:39:53 +00:00
|
|
|
[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);
|
2018-11-24 13:39:53 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-11-21 19:59:27 +00:00
|
|
|
}
|
|
|
|
}
|