172 lines
5.0 KiB
C#
172 lines
5.0 KiB
C#
using System;
|
|
using NUnit.Framework;
|
|
using PorygonSharp;
|
|
using PorygonSharp.UserData;
|
|
using PorygonSharp.Utilities;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
|
|
private class UserDataTestObject
|
|
{
|
|
public int Foo = 200;
|
|
}
|
|
|
|
[Test]
|
|
public void Test9()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = Script.CreateScript(@"
|
|
function test(testObject v)
|
|
result = v['Foo']
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(diag.GetCode().ToString());
|
|
}
|
|
|
|
script.Evaluate();
|
|
|
|
script.CallFunction("test", new UserDataTestObject());
|
|
var variable = script.GetVariable("result");
|
|
Assert.AreEqual(200, variable.EvaluateInteger());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Test10()
|
|
{
|
|
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
|
|
using (var script = Script.CreateScript(@"
|
|
function test(testObject v)
|
|
v['Foo'] = 20000
|
|
end
|
|
"))
|
|
{
|
|
var diags = script.Diagnostics.GetDiagnostics();
|
|
foreach (var diag in diags)
|
|
{
|
|
throw new Exception(diag.GetCode().ToString());
|
|
}
|
|
|
|
script.Evaluate();
|
|
|
|
var parameter = new UserDataTestObject();
|
|
script.CallFunction("test", parameter);
|
|
Assert.AreEqual(20000, parameter.Foo);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestHash()
|
|
{
|
|
var hash = HashedString.ScriptHash("Foo");
|
|
Assert.AreEqual(193501609, hash);
|
|
}
|
|
}
|
|
} |