2019-06-05 15:50:01 +00:00
|
|
|
using System;
|
2019-06-19 13:50:11 +00:00
|
|
|
using System.Linq;
|
2019-06-05 15:50:01 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
using PorygonSharp;
|
2019-06-14 20:22:42 +00:00
|
|
|
using PorygonSharp.Utilities;
|
2019-06-05 15:50:01 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-06-19 13:50:11 +00:00
|
|
|
throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
|
2019-06-05 15:50:01 +00:00
|
|
|
}
|
|
|
|
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-19 13:50:11 +00:00
|
|
|
[Test]
|
|
|
|
public void TestDiagnostic()
|
|
|
|
{
|
|
|
|
using (var script = new Script("\n'\\x'"))
|
|
|
|
{
|
|
|
|
var diagnostics = script.Diagnostics.GetDiagnostics().ToArray();
|
|
|
|
Assert.IsNotEmpty(diagnostics);
|
|
|
|
Assert.AreEqual("[Error] (1, 3) '\\x' is not a valid control character.", script.Diagnostics.GetFullDiagnosticMessage(diagnostics[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-14 20:22:42 +00:00
|
|
|
[Test]
|
|
|
|
public void TestHash()
|
|
|
|
{
|
|
|
|
var hash = HashedString.ScriptHash("Foo");
|
|
|
|
Assert.AreEqual(193501609, hash);
|
|
|
|
}
|
2019-06-05 15:50:01 +00:00
|
|
|
}
|
|
|
|
}
|