|
|
@@ -9,6 +9,12 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
[TestFixture]
|
|
|
public class SimpleTests
|
|
|
{
|
|
|
+ [Test]
|
|
|
+ public void EmptyChunk()
|
|
|
+ {
|
|
|
+ Script S = new Script(CoreModules.None);
|
|
|
+ DynValue res = S.DoString("");
|
|
|
+ }
|
|
|
|
|
|
[Test]
|
|
|
public void CSharpStaticFunctionCallStatement()
|
|
|
@@ -55,6 +61,58 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
Assert.AreEqual(DataType.Void, res.Type);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void CSharpStaticFunctionCall4()
|
|
|
+ {
|
|
|
+ string script = "return callback()();";
|
|
|
+
|
|
|
+ var callback2 = DynValue.NewCallback(new CallbackFunction((_x, a) => { return DynValue.NewNumber(1234.0); }));
|
|
|
+ var callback = DynValue.NewCallback(new CallbackFunction((_x, a) => { return callback2; }));
|
|
|
+
|
|
|
+ var S = new Script();
|
|
|
+ S.Globals.Set("callback", callback);
|
|
|
+
|
|
|
+ DynValue res = S.DoString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(1234.0, res.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void CSharpStaticFunctionCall3()
|
|
|
+ {
|
|
|
+ string script = "return callback();";
|
|
|
+
|
|
|
+ var callback = DynValue.NewCallback(new CallbackFunction((_x, a) => { return DynValue.NewNumber(1234.0); }));
|
|
|
+
|
|
|
+ var S = new Script();
|
|
|
+ S.Globals.Set("callback", callback);
|
|
|
+
|
|
|
+ DynValue res = S.DoString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(1234.0, res.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void CSharpStaticFunctionCall2()
|
|
|
+ {
|
|
|
+ IList<DynValue> args = null;
|
|
|
+
|
|
|
+ string script = "return callback 'hello';";
|
|
|
+
|
|
|
+ var S = new Script();
|
|
|
+ S.Globals.Set("callback", DynValue.NewCallback(new CallbackFunction((_x, a) => { args = a.GetArray(); return DynValue.NewNumber(1234.0); })));
|
|
|
+
|
|
|
+ DynValue res = S.DoString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(1, args.Count);
|
|
|
+ Assert.AreEqual(DataType.String, args[0].Type);
|
|
|
+ Assert.AreEqual("hello", args[0].String);
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(1234.0, res.Number);
|
|
|
+ }
|
|
|
+
|
|
|
[Test]
|
|
|
public void CSharpStaticFunctionCall()
|
|
|
{
|
|
|
@@ -77,6 +135,7 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
}
|
|
|
|
|
|
[Test]
|
|
|
+ //!!! DO NOT REFORMAT THIS METHOD !!!
|
|
|
public void LongStrings()
|
|
|
{
|
|
|
string script = @"
|
|
|
@@ -96,7 +155,7 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
Assert.AreEqual(DataType.String, res.Tuple[0].Type);
|
|
|
Assert.AreEqual(DataType.String, res.Tuple[1].Type);
|
|
|
Assert.AreEqual(DataType.String, res.Tuple[2].Type);
|
|
|
- Assert.AreEqual("\t\t\t\t\tciao\r\n\t\t\t\t", res.Tuple[0].String);
|
|
|
+ Assert.AreEqual("\t\t\t\t\tciao\n\t\t\t\t", res.Tuple[0].String);
|
|
|
Assert.AreEqual(" [[uh]] ", res.Tuple[1].String);
|
|
|
Assert.AreEqual("[==[[=[[[eheh]]=]=]", res.Tuple[2].String);
|
|
|
}
|
|
|
@@ -145,6 +204,7 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
[Test]
|
|
|
public void ParserErrorMessage()
|
|
|
{
|
|
|
+ bool caught = false;
|
|
|
string script = @"
|
|
|
return 'It's a wet floor warning saying wheat flour instead. \
|
|
|
Probably, the cook thought it was funny. \
|
|
|
@@ -156,10 +216,25 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
}
|
|
|
catch (SyntaxErrorException ex)
|
|
|
{
|
|
|
+ caught = true;
|
|
|
Assert.IsNotNullOrEmpty(ex.Message);
|
|
|
}
|
|
|
+
|
|
|
+ Assert.IsTrue(caught);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void StringsWithBackslashLineEndings2()
|
|
|
+ {
|
|
|
+ string script = @"
|
|
|
+ return 'a\
|
|
|
+ b\
|
|
|
+ c'";
|
|
|
+
|
|
|
+ DynValue res = Script.RunString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.String, res.Type);
|
|
|
+ }
|
|
|
|
|
|
[Test]
|
|
|
public void StringsWithBackslashLineEndings()
|
|
|
@@ -193,6 +268,27 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
}
|
|
|
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void ReturnSimpleUnop()
|
|
|
+ {
|
|
|
+ string script = @"return -42";
|
|
|
+
|
|
|
+ DynValue res = Script.RunString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(-42, res.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void ReturnSimple()
|
|
|
+ {
|
|
|
+ string script = @"return 42";
|
|
|
+
|
|
|
+ DynValue res = Script.RunString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(42, res.Number);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
@@ -616,7 +712,29 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
Assert.AreEqual(12, res.Number);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void OperatorPrecedence6()
|
|
|
+ {
|
|
|
+ string script = @"return -2^2";
|
|
|
+ Script S = new Script(CoreModules.None);
|
|
|
+
|
|
|
+ DynValue res = S.DoString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(-4, res.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void OperatorPrecedence7()
|
|
|
+ {
|
|
|
+ string script = @"return -7 / 0.5";
|
|
|
+ Script S = new Script(CoreModules.None);
|
|
|
+
|
|
|
+ DynValue res = S.DoString(script);
|
|
|
|
|
|
+ Assert.AreEqual(DataType.Number, res.Type);
|
|
|
+ Assert.AreEqual(-14, res.Number);
|
|
|
+ }
|
|
|
|
|
|
[Test]
|
|
|
public void OperatorPrecedenceAndAssociativity()
|
|
|
@@ -792,6 +910,28 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
Assert.AreEqual(DataType.Function, res.Type);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void FunctionWithStringArg2()
|
|
|
+ {
|
|
|
+ string script = @"
|
|
|
+ x = 0;
|
|
|
+
|
|
|
+ fact = function(y)
|
|
|
+ x = y
|
|
|
+ end
|
|
|
+
|
|
|
+ fact 'ciao';
|
|
|
+
|
|
|
+ return x;
|
|
|
+ ";
|
|
|
+
|
|
|
+
|
|
|
+ DynValue res = Script.RunString(script);
|
|
|
+
|
|
|
+ Assert.AreEqual(DataType.String, res.Type);
|
|
|
+ Assert.AreEqual("ciao", res.String);
|
|
|
+ }
|
|
|
+
|
|
|
[Test]
|
|
|
public void FunctionWithStringArg()
|
|
|
{
|
|
|
@@ -934,21 +1074,6 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
Assert.AreEqual(1, res.Number);
|
|
|
}
|
|
|
|
|
|
- [Test]
|
|
|
- [ExpectedException(ExpectedException=typeof(SyntaxErrorException))]
|
|
|
- public void HexFloatsReportError()
|
|
|
- {
|
|
|
- string script = @"
|
|
|
- function x(a, b)
|
|
|
-
|
|
|
- end
|
|
|
-
|
|
|
- x(0x0.1E, 0x1E / 0x100);
|
|
|
- ";
|
|
|
-
|
|
|
- DynValue res = Script.RunString(script);
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
[Test]
|
|
|
public void ExpressionReducesTuples2()
|
|
|
@@ -1262,7 +1387,7 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
}
|
|
|
|
|
|
[Test]
|
|
|
- [ExpectedException(ExpectedException = typeof(SyntaxErrorException))]
|
|
|
+ [ExpectedException(typeof(SyntaxErrorException))]
|
|
|
public void VarArgsInNoVarArgsReturnsError()
|
|
|
{
|
|
|
string script = @"
|
|
|
@@ -1283,6 +1408,31 @@ namespace MoonSharp.Interpreter.Tests
|
|
|
DynValue res = Script.RunString(script);
|
|
|
}
|
|
|
|
|
|
+ [Test]
|
|
|
+ public void HexFloats_1()
|
|
|
+ {
|
|
|
+ string script = "return 0x0.1E";
|
|
|
+ DynValue result = Script.RunString(script);
|
|
|
+ Assert.AreEqual((double)0x1E / (double)0x100, result.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void HexFloats_2()
|
|
|
+ {
|
|
|
+ string script = "return 0xA23p-4";
|
|
|
+ DynValue result = Script.RunString(script);
|
|
|
+ Assert.AreEqual((double)0xA23 / 16.0, result.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void HexFloats_3()
|
|
|
+ {
|
|
|
+ string script = "return 0X1.921FB54442D18P+1";
|
|
|
+ DynValue result = Script.RunString(script);
|
|
|
+ Assert.AreEqual((1 + (double)0x921FB54442D18 / (double)0x10000000000000) * 2, result.Number);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|