using System; using Upsilon.Evaluator; using Xunit; namespace UpsilonTests.StandardLibraryTests { public class BasicFunctionsTests : TestClass { public BasicFunctionsTests(StaticScriptFixture fix) : base(fix) { } [Fact] public void AssertTest() { new Script("assert(true)", BoundScope, StaticScope).Evaluate(); Assert.Throws(() => new Script("assert(false)", BoundScope, StaticScope).Evaluate()); } [Fact] public void Error() { var e = Assert.Throws(() => new Script(@"error(""test_error"")", BoundScope, StaticScope).Evaluate()); 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 script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); Assert.Equal(184, result); } [Fact] public void ToNumberTest() { const string input = @" return tonumber(""100"") "; var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); 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 script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); Assert.Equal(284, result); } [Fact] public void ToStringTest() { const string input = @" return tostring(100) "; var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); Assert.Equal("100", result); } [Fact] public void TypeTest() { const string input = @" return type(100) "; var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); Assert.Equal("Number", result); } } }