2019-06-05 15:50:01 +00:00
|
|
|
using System;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using PorygonSharp;
|
|
|
|
|
|
|
|
namespace PorygonSharpTests
|
|
|
|
{
|
|
|
|
public class Tests
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void Test1()
|
|
|
|
{
|
|
|
|
using (var script = new Script("true"))
|
|
|
|
{
|
|
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
|
|
foreach (var diag in diags)
|
|
|
|
{
|
|
|
|
throw new Exception(diag.GetCode().ToString());
|
|
|
|
}
|
|
|
|
Assert.False(script.Diagnostics.HasErrors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[Test]
|
|
|
|
public void Test2()
|
|
|
|
{
|
|
|
|
using (var script = new Script("1+true"))
|
|
|
|
{
|
|
|
|
Assert.True(script.Diagnostics.HasErrors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test3()
|
|
|
|
{
|
|
|
|
using (var script = new Script("1+10"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
var lastVal = script.GetLastValue();
|
|
|
|
Assert.AreEqual(TypeClass.Number, lastVal.GetTypeClass());
|
|
|
|
var val = lastVal.EvaluateInteger();
|
|
|
|
Assert.AreEqual(11, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test4()
|
|
|
|
{
|
|
|
|
using (var script = new Script("1+10.24"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
var lastVal = script.GetLastValue();
|
|
|
|
Assert.AreEqual(TypeClass.Number, lastVal.GetTypeClass());
|
|
|
|
var val = lastVal.EvaluateFloat();
|
|
|
|
Assert.AreEqual(11.24d, val, 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test5()
|
|
|
|
{
|
|
|
|
using (var script = new Script("true and true"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
var lastVal = script.GetLastValue();
|
|
|
|
Assert.AreEqual(TypeClass.Bool, lastVal.GetTypeClass());
|
|
|
|
var val = lastVal.EvaluateBool();
|
|
|
|
Assert.AreEqual(true, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test6()
|
|
|
|
{
|
|
|
|
using (var script = new Script("'foo' + 'bar'"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
var lastVal = script.GetLastValue();
|
|
|
|
Assert.AreEqual(TypeClass.String, lastVal.GetTypeClass());
|
|
|
|
var val = lastVal.EvaluateString();
|
|
|
|
Assert.AreEqual("foobar", val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test7()
|
|
|
|
{
|
|
|
|
using (var script = new Script("a = 100 + 50"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
var variable = script.GetVariable("a");
|
|
|
|
Assert.AreEqual(TypeClass.Number, variable.GetTypeClass());
|
|
|
|
var val = variable.EvaluateInteger();
|
|
|
|
Assert.AreEqual(150, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:52:28 +00:00
|
|
|
[Test]
|
|
|
|
public void Test8()
|
|
|
|
{
|
|
|
|
using (var script = new Script("function add(number a, number b) result = a + b end"))
|
|
|
|
{
|
|
|
|
script.Evaluate();
|
|
|
|
script.CallFunction("add", 100, 50);
|
|
|
|
var variable = script.GetVariable("result");
|
|
|
|
Assert.AreEqual(TypeClass.Number, variable.GetTypeClass());
|
|
|
|
var val = variable.EvaluateInteger();
|
|
|
|
Assert.AreEqual(150, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:50:01 +00:00
|
|
|
}
|
|
|
|
}
|