PorygonSharp/PorygonSharpTests/UnitTest1.cs

157 lines
4.6 KiB
C#
Raw Normal View History

2019-06-05 15:50:01 +00:00
using System;
using System.IO;
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"))
{
2019-07-27 17:45:39 +00:00
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Number, result.GetTypeClass());
var val = result.EvaluateInteger();
2019-06-05 15:50:01 +00:00
Assert.AreEqual(11, val);
}
}
[Test]
public void Test4()
{
using (var script = new Script("1+10.24"))
{
2019-07-27 17:45:39 +00:00
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Number, result.GetTypeClass());
var val = result.EvaluateFloat();
2019-06-05 15:50:01 +00:00
Assert.AreEqual(11.24d, val, 5);
}
}
[Test]
public void Test5()
{
using (var script = new Script("true and true"))
{
2019-07-27 17:45:39 +00:00
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Bool, result.GetTypeClass());
var val = result.EvaluateBool();
2019-06-05 15:50:01 +00:00
Assert.AreEqual(true, val);
}
}
[Test]
public void Test6()
{
using (var script = new Script("'foo' + 'bar'"))
{
2019-07-27 17:45:39 +00:00
var result = script.Evaluate();
Assert.AreEqual(TypeClass.String, result.GetTypeClass());
var val = result.EvaluateString();
2019-06-05 15:50:01 +00:00
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();
2019-07-27 17:45:39 +00:00
Assert.That(script.HasFunction("add"));
script.CallFunction("add", 100, 50);
2019-07-27 17:45:39 +00:00
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] (2, 2) '\\x' is not a valid control character.",
script.Diagnostics.GetFullDiagnosticMessage(diagnostics[0]));
2019-06-19 13:50:11 +00:00
}
}
[Test]
public void TestPrint()
{
using (var script = new Script("print('test')"))
{
using (var sw = new StringWriter())
{
Console.SetOut(sw);
Assert.False(script.Diagnostics.HasErrors());
script.Evaluate();
var s = sw.ToString();
Assert.AreEqual("test\n", s);
}
}
}
2019-06-19 13:50:11 +00:00
2019-07-27 17:45:39 +00:00
[Test]
public void TestExceptions()
{
using (var script = new Script("error('test')"))
{
Assert.Throws<EvaluationException>(() => script.Evaluate());
}
}
2019-06-14 20:22:42 +00:00
[Test]
public void TestHash()
{
var hash = "Foo".ScriptHash();
2019-06-14 20:22:42 +00:00
Assert.AreEqual(193501609, hash);
}
2019-06-05 15:50:01 +00:00
}
}