/
DynamicTests.cs
105 lines (79 loc) · 1.79 KB
/
DynamicTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using MoonSharp.Interpreter.Execution;
using NUnit.Framework;
using MoonSharp.Interpreter.CoreLib;
namespace MoonSharp.Interpreter.Tests
{
[TestFixture]
public class DynamicTests
{
[Test]
public void DynamicAccessEval()
{
string script = @"
return dynamic.eval('5+1');
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(6, res.Number);
}
[Test]
public void DynamicAccessPrepare()
{
string script = @"
x = dynamic.prepare('5+1');
return dynamic.eval(x);
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(6, res.Number);
}
[Test]
public void DynamicAccessScope()
{
string script = @"
a = 3;
x = dynamic.prepare('a+1');
function f()
a = 5;
return dynamic.eval(x);
end
return f();
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(6, res.Number);
}
[Test]
public void DynamicAccessScopeSecurity()
{
string script = @"
a = 5;
local x = dynamic.prepare('a');
local eval = dynamic.eval;
local _ENV = { }
function f()
return eval(x);
end
return f();
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.Nil, res.Type);
//Assert.AreEqual(6, res.Number);
}
[Test]
public void DynamicAccessFromCSharp()
{
string code = @"
t = { ciao = { 'hello' } }
";
Script script = new Script();
script.DoString(code);
DynValue v = script.CreateDynamicExpression("t.ciao[1] .. ' world'").Evaluate();
Assert.AreEqual(v.String, "hello world");
}
}
}