-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathLuaObjectTests.cs
More file actions
259 lines (210 loc) · 7.18 KB
/
LuaObjectTests.cs
File metadata and controls
259 lines (210 loc) · 7.18 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Lua.Standard;
namespace Lua.Tests;
[LuaObject]
public partial class LuaTestObj
{
int x;
int y;
[LuaMember("x")]
public int X
{
get => x;
set => x = value;
}
[LuaMember("y")]
public int Y
{
get => y;
set => y = value;
}
[LuaMember("create")]
public static LuaTestObj Create(int x, int y)
{
return new LuaTestObj() { x = x, y = y };
}
[LuaMetamethod(LuaObjectMetamethod.Add)]
public static LuaTestObj Add(LuaTestObj a, LuaTestObj b)
{
return new LuaTestObj() { x = a.x + b.x, y = a.y + b.y };
}
[LuaMetamethod(LuaObjectMetamethod.Sub)]
public static async Task<LuaTestObj> Sub(LuaTestObj a, LuaTestObj b)
{
await Task.Delay(1);
return new LuaTestObj() { x = a.x - b.x, y = a.y - b.y };
}
[LuaMember]
public object GetObj() => this;
[LuaMember]
public static double Sum(double a, ReadOnlySpan<LuaValue> values, CancellationToken ct)
{
var sum = a;
foreach (var v in values)
{
sum += v.Read<double>();
}
return sum;
}
}
[LuaObject]
public partial class TestUserData
{
[LuaMember] public int Property { get; init; }
[LuaMember] public int ReadOnlyProperty { get; }
[LuaMember] public int SetOnlyProperty { set { } }
[LuaMember] public LuaValue LuaValueProperty { get; set; }
[LuaMember("p2")] public string PropertyWithName { get; set; } = "";
[LuaMember]
public static void MethodVoid()
{
Console.WriteLine("HEY!");
}
[LuaMember]
public static async Task MethodAsync()
{
await Task.CompletedTask;
}
[LuaMember]
public static double StaticMethodWithReturnValue(double a, double b)
{
Console.WriteLine($"HEY! {a} {b}");
return a + b;
}
[LuaMember]
public double InstanceMethodWithReturnValue()
{
return Property;
}
[LuaMember]
public async ValueTask<LuaValue> InstanceMethodWithReturnValueAsync(LuaValue value, CancellationToken ct)
{
await Task.Delay(1, ct);
return value;
}
[LuaMetamethod(LuaObjectMetamethod.Call)]
public string Call()
{
return "Called!";
}
}
public class LuaObjectTests
{
[Test]
public async Task Test_Property()
{
var userData = new TestUserData { Property = 1 };
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test.Property");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
}
[Test]
public async Task Test_PropertyWithName()
{
var userData = new TestUserData { PropertyWithName = "foo" };
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test.p2");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue("foo")));
}
[Test]
public async Task Test_MethodVoid()
{
var userData = new TestUserData();
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test.MethodVoid()");
Assert.That(results, Has.Length.EqualTo(0));
}
[Test]
public async Task Test_MethodAsync()
{
var userData = new TestUserData();
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test.MethodAsync()");
Assert.That(results, Has.Length.EqualTo(0));
}
[Test]
public async Task Test_StaticMethodWithReturnValue()
{
var userData = new TestUserData();
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test.StaticMethodWithReturnValue(1, 2)");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue(3)));
}
[Test]
public async Task Test_InstanceMethodWithReturnValue()
{
var userData = new TestUserData { Property = 1 };
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test:InstanceMethodWithReturnValue()");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
}
[Test]
public async Task Test_InstanceMethodWithReturnValueAsync()
{
var userData = new TestUserData { Property = 1 };
var state = LuaState.Create();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("return test:InstanceMethodWithReturnValueAsync(2)");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue(2)));
}
[Test]
public async Task Test_CallMetamethod()
{
var userData = new TestUserData();
var state = LuaState.Create();
state.OpenBasicLibrary();
state.Environment["test"] = userData;
var results = await state.DoStringAsync("""
assert(test() == 'Called!')
return test()
""");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0], Is.EqualTo(new LuaValue("Called!")));
}
[Test]
public async Task Test_ArithMetamethod()
{
var userData = new LuaTestObj();
var state = LuaState.Create();
state.OpenBasicLibrary();
state.Environment["TestObj"] = userData;
var results = await state.DoStringAsync("""
local a = TestObj.create(1, 2)
local b = TestObj.create(3, 4)
return a + b, a - b
""");
Assert.That(results, Has.Length.EqualTo(2));
Assert.That(results[0].Read<object>(), Is.TypeOf<LuaTestObj>());
var objAdd = results[0].Read<LuaTestObj>();
Assert.That(objAdd.X, Is.EqualTo(4));
Assert.That(objAdd.Y, Is.EqualTo(6));
Assert.That(results[1].Read<object>(), Is.TypeOf<LuaTestObj>());
var objSub = results[1].Read<LuaTestObj>();
Assert.That(objSub.X, Is.EqualTo(-2));
Assert.That(objSub.Y, Is.EqualTo(-2));
}
[Test]
public async Task Test_Params()
{
var userData = new LuaTestObj();
var state = LuaState.Create();
state.OpenBasicLibrary();
state.Environment["TestObj"] = userData;
var results = await state.DoStringAsync("""
local a = TestObj.Sum(1, 2, 3)
return TestObj.Sum(1, 2, 3)
""");
Assert.That(results, Has.Length.EqualTo(1));
Assert.That(results[0].Read<double>(), Is.EqualTo(6.0));
}
}