-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScriptMachineTests.cs
More file actions
598 lines (510 loc) · 13 KB
/
ScriptMachineTests.cs
File metadata and controls
598 lines (510 loc) · 13 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System.Collections.Concurrent;
using Preagonal.Scripting.GS2Engine.Extensions;
using Preagonal.Scripting.GS2Engine.GS2.Script;
using Preagonal.Scripting.GS2Engine.Models;
using Preagonal.Scripting.GS2Engine.UnitTests.Objects;
using Xunit.Abstractions;
using static Preagonal.Scripting.GS2Engine.GS2.Script.Script;
namespace Preagonal.Scripting.GS2Engine.UnitTests;
public class ScriptMachineTests
{
private int _calledTimes;
private readonly Dictionary<int, string> _receivedStrings = new();
public ScriptMachineTests(ITestOutputHelper testOutputHelper)
{
Tools.SetDebugFuncWrite(testOutputHelper.WriteLine);
Tools.SetDebugFuncWriteLine(testOutputHelper.WriteLine);
Tools.DEBUG_ON = true;
ConcurrentDictionary<int, Drawing> drawings = new();
ScriptProperties<ScriptMachineTests>.AddProperties(
null,
new()
{
{ "screenwidth", "The width of the game screen", _ => 1024 },
{ "screenheight", "The height of the game screen", _ => 1024 },
}
);
ScriptProperties<ScriptMachineTests>.AddFunctions(
null,
new()
{
{
"echo",
"",
EchoCallback
},
{
"showimg",
"",
(_, args) =>
{
if (!(args?.Length > 3)) return 0;
try
{
var index = (int)args[0]!.GetValue<double>();
string? image = args[1]?.GetValue<TString>() ?? string.Empty;
var x = (int)args[2]!.GetValue<double>();
var y = (int)args[3]!.GetValue<double>();
if (drawings.TryGetValue(index, out var value))
{
value.ShowImg(image, x, y);
}
else
{
value = new(image, x, y);
drawings.AddOrUpdate(index, value, (_, _) => value);
}
}
catch (Exception)
{
//_logger.LogDebug(e.Message);
}
return 0;
}
},
{
"findimg",
"",
(_, args) =>
{
if (!(args?.Length > 0)) return null;
try
{
var index = (int)args[0]!.GetValue<double>();
if (drawings.TryGetValue(index, out var value))
{
return value;
}
}
catch (Exception)
{
//_logger.LogDebug(e.Message);
}
return null;
}
},
{
"getimgwidth",
"",
(_, args) =>
{
if (!(args?.Length > 0)) return 0;
try
{
var image = args[0]!.GetValue<TString>();
if (image != null) Console.WriteLine(image);
return 1;
}
catch (Exception)
{
//_logger.LogDebug(e.Message);
}
return 0;
}
},
}
);
foreach (var property in GlobalProperties.Where(x => !x.Value.Compiled))
{
property.Value.Compile();
}
}
private int EchoCallback(ScriptMachineTests _, IStackEntry[] args)
{
_receivedStrings[_calledTimes] = args[0]?.GetValue()?.ToString()??"";
Console.WriteLine(_receivedStrings[_calledTimes]);
_calledTimes++;
return 0;
}
private static Script CompileScript(string scriptText)
{
var response = GS2Compiler.Interface.CompileCode(
scriptText,
"weapon",
"testScript"
);
if (response.Success)
{
// Arrange
return new("testScript", response.ByteCode);
}
throw new($"Script failure: {response.ErrMsg}");
}
private static Script InitializePrebakedScript(string fileName) => new(fileName);
[Fact]
public void When_script_is_faulty_Then_exception_is_thrown()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated()
}
""";
//Act
var result = Assert.Throws<Exception>(() => CompileScript(scriptText));;
//Assert
Assert.Equal("Script failure: malformed input at line 3: \t\t\t}\n", result.Message);
}
private static void RegisterGlobalObject(string name, ScriptVariable collection) => GlobalObjects[name] = collection;
[Fact]
public async Task When_calling_built_in_sin_Then_correct_sin_value_is_returned()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
var expectedSin = new List<double> { 1, 0, -1, 0 };
var sin = new List<double>();
const string scriptText =
"""
//#CLIENTSIDE
function test(dir) {
temp.angle = (pi/2 * (dir+1));
return sin(temp.angle);
}
""";
var script = CompileScript(scriptText);
//Act
for (var i = 0; i < 4; i++) sin.Add((await script.Call("test", i)).GetValue<double>());
//Assert
for (var i = 0; i < 4; i++) Assert.Equal(expectedSin[i], sin[i]);
}
[Fact]
public async Task When_calling_built_in_cos_Then_correct_cos_value_is_returned()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
var expectedCos = new List<double> { 0, -1, 0, 1 };
var cos = new List<double>();
const string scriptText =
"""
//#CLIENTSIDE
function test(dir) {
temp.angle = (pi/2) * (dir+1);
return cos(temp.angle);
}
""";
var script = CompileScript(scriptText);
//Act
for (var i = 0; i < 4; i++) cos.Add((await script.Call("test", i)).GetValue<double>());
//Assert
for (var i = 0; i < 4; i++) Assert.Equal(expectedCos[i], cos[i]);
}
[Fact]
public async Task Given_temp_var_When_returning_without_temp_prefix_Then_temp_var_should_be_returned()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.var = "test";
temp.var2 = var;
return var2;
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.Equal("test", result.GetValue<TString>()!);
}
[Fact]
public async Task Given_function_in_script2_When_calling_public_function_in_script1_Then_value_should_be_returned()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText1 =
"""
//#CLIENTSIDE
public function PubFun() {
temp.var = "PubFun";
temp.var2 = var;
return var2;
}
""";
var script1 = CompileScript(scriptText1);
const string scriptText2 =
"""
//#CLIENTSIDE
function onCreated() {
return (@"script1").PubFun();
}
""";
var script2 = CompileScript(scriptText2);
GlobalVariables["script1"] = script1.ToStackEntry();
//Act
var result = await script2.Call("onCreated");
//Assert
Assert.Equal("PubFun", result.GetValue()!.ToString());
}
[Fact]
public async Task Given_this_var3_When_returning_without_this_prefix_Then_0_should_be_returned()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
this.var3 = "test2";
return var3;
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.Equal(0d, result.GetValue()!);
}
[Fact]
public async Task Given_temp_var_When_creating_new_array_object_Then_result_should_be_empty_array_object()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
GlobalVariables.Clear();
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.var = new[2];
return temp.var == {0,0};
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.Equal(1.0d, result.GetValue()!);
}
[Fact(Skip = "Waiting for fix in GS2Compiler")]
public async Task Given_temp_update_When_comparing_multiple_or_and_one_variable_is_updated_Then_result_should_be_true()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
GlobalVariables.Clear();
GlobalObjects.Clear();
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.var1 = 30.5;
temp.var2 = 30;
temp.var3 = 2;
temp.var4 = 0;
temp.var5 = "myvar";
this.oldData = {var1,var2,var3,var4,var5};
var3 = -1;
temp.update = var1 != this.oldData[0] ||
var2 != this.oldData[1] ||
(var3 == -1 && this.oldData[2] >=0);
return temp.update;
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.True(Convert.ToBoolean(result.GetValue()!));
}
[Fact(Skip = "Waiting for fix in GS2Compiler")]
public async Task Given_temp_var_When_at_comparing_Then_result_should_be_true()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
GlobalVariables.Clear();
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.var = {true,true};
echo(@var);
echo(""@{1,1});
return @var == @{1,1};
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.Equal(true, result.GetValue()!);
}
[Fact]
public async Task Given_temp_var3_is_array_object_When_comparing_values_with_another_array_object_with_identical_values_Then_result_should_be_true()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.var3 = {1,"asd",3};
return temp.var3 == {1,"asd",3};
}
""";
var script = CompileScript(scriptText);
//Act
var result = await script.Call("onCreated");
//Assert
Assert.Equal(1.0d, result.GetValue()!);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_items_Then_properly_for_loop_through_items()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
temp.i = 0;
temp.sounds = {
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
"text_" @ temp.i++,
};
for (temp.sound : temp.sounds) {
echo(temp.sound);
}
return "done!";
}
""";
var script = CompileScript(scriptText);
//Act
var result = (await script.Call("onCreated")).GetValue<TString>();
//Assert
Assert.Equal("done!", result?.ToString());
Assert.Equal(15, _calledTimes);
Assert.Equal("text_11", _receivedStrings[3]);
Assert.Equal("text_0", _receivedStrings[14]);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_8_loops_Then_echo_is_called_8_times()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
for(this.i=0;this.i<8;this.i++) {
echo(((this.i==6)?"test2":"test") @ "_text_" @ this.i);
}
}
""";
var script = CompileScript(scriptText);
//Act
_ = await script.Call("onCreated");
//Assert
Assert.Equal("test_text_3", _receivedStrings[3]);
Assert.Equal("test2_text_6", _receivedStrings[6]);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_8_loops_Then_echo_is_called_8_times_()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
test = new GuiControl("test");
with(test) {
width = 12;
};
echo(test.width);
}
""";
var script = CompileScript(scriptText);
//Act
_ = await script.Call("onCreated");
//Assert
Assert.Equal("12", _receivedStrings[0]);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_8_loops_Then_echo_is_called_8_times__()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
echo(""@screenwidth);
}
""";
var script = CompileScript(scriptText);
//Act
_ = await script.Call("onCreated");
//Assert
Assert.Equal("1024", _receivedStrings[0]);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_8_loops_Then_echo_is_called_8_times___()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
echo(1.01+screenwidth);
}
""";
var script = CompileScript(scriptText);
//Act
_ = await script.Call("onCreated");
//Assert
Assert.Equal("1025.01", _receivedStrings[0]);
}
[Fact(Skip = "fix later")]
public async Task When_for_loop_with_8_loops_Then_echo_is_called_8_times____()
{
//Arrange
_receivedStrings.Clear();
_calledTimes = 0;
const string scriptText =
"""
//#CLIENTSIDE
function onCreated() {
showimg(1402, "cog2.png", 85, 60);
findimg(1402).rotation = 234;
temp.rot2 = findimg(1402).rotation;
echo(temp.rot2);
}
""";
var script = CompileScript(scriptText);
//Act
_ = await script.Call("onCreated");
//Assert
Assert.Equal("234", _receivedStrings[0]);
}
}