PorygonSharp/PorygonSharpTests/UnitTest1.cs

173 lines
5.0 KiB
C#

using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using PorygonSharp;
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(script.Diagnostics.GetFullDiagnosticMessage(diag));
}
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"))
{
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Number, result.GetTypeClass());
var val = result.EvaluateInteger();
Assert.AreEqual(11, val);
}
}
[Test]
public void Test4()
{
using (var script = new Script("1+10.24"))
{
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Number, result.GetTypeClass());
var val = result.EvaluateFloat();
Assert.AreEqual(11.24d, val, 5);
}
}
[Test]
public void Test5()
{
using (var script = new Script("true and true"))
{
var result = script.Evaluate();
Assert.AreEqual(TypeClass.Bool, result.GetTypeClass());
var val = result.EvaluateBool();
Assert.AreEqual(true, val);
}
}
[Test]
public void Test6()
{
using (var script = new Script("'foo' + 'bar'"))
{
var result = script.Evaluate();
Assert.AreEqual(TypeClass.String, result.GetTypeClass());
var val = result.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();
Assert.That(script.HasFunction("add"));
script.CallFunction("add", 100, 50);
var variable = script.GetVariable("result");
Assert.AreEqual(TypeClass.Number, variable.GetTypeClass());
var val = variable.EvaluateInteger();
Assert.AreEqual(150, val);
}
}
[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]));
}
}
[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);
}
}
}
[Test]
public void TestExceptions()
{
using (var script = new Script("error('test')"))
{
Assert.Throws<EvaluationException>(() => script.Evaluate());
}
}
[Test]
public void TestHash()
{
var hash = "Foo".ScriptHash();
Assert.AreEqual(193501609, hash);
}
[Test]
public void TestBoundTreeString()
{
using (var script = new Script("return 10 + 500"))
{
var tree = script.GetBoundTreeString();
const string expectedString = @"BlockStatement
ReturnStatement
BinaryExpression: addition (number)
LiteralInteger: 10 (number)
LiteralInteger: 500 (number)";
Assert.AreEqual(expectedString, tree);
}
}
}
}