-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathEngineTests.cs
More file actions
827 lines (707 loc) · 19 KB
/
EngineTests.cs
File metadata and controls
827 lines (707 loc) · 19 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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*****************************************************************************
*
* ReoScript - .NET Script Language Engine
*
* https://github.com/unvell/ReoScript
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
* This software released under MIT license.
* Copyright (c) 2012-2019 Jingwood, unvell.com, all rights reserved.
*
****************************************************************************/
using System;
using System.IO;
using Xunit;
using unvell.ReoScript;
using unvell.ReoScript.Diagnostics;
namespace unvell.ReoScript.TestCase
{
/// <summary>
/// Tests for engine-level features: loop protection, exception safety,
/// truthy/falsy semantics, error source location.
/// </summary>
public class EngineTests
{
private ScriptRunningMachine CreateSRM()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
return srm;
}
#region Loop Protection
[Fact]
public void InfiniteWhileLoop_ThrowsTimeout()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 1000;
Assert.Throws<ScriptExecutionTimeoutException>(() =>
{
srm.Run("var i = 0; while (true) { i++; }");
});
}
[Fact]
public void InfiniteForLoop_ThrowsTimeout()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 1000;
Assert.Throws<ScriptExecutionTimeoutException>(() =>
{
srm.Run("for (var i = 0; ; i++) { }");
});
}
[Fact]
public void NormalLoop_CompletesWithinLimit()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 10000;
// Should complete without error
srm.Run("var sum = 0; for (var i = 0; i < 5000; i++) { sum += i; }");
}
[Fact]
public void LoopProtectionDisabled_WhenZero()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 0;
// With limit disabled, a bounded loop should still work fine
srm.Run("var sum = 0; for (var i = 0; i < 100; i++) { sum += i; }");
}
#endregion
#region Error Source Location
[Fact]
public void ErrorObject_IncludesLineNumber()
{
var srm = CreateSRM();
var ex = Assert.Throws<ReoScriptRuntimeException>(() =>
{
srm.Run("var x = 1;\nvar y = 2;\nundefinedFunction();");
});
Assert.NotNull(ex.ErrorObject);
Assert.True(ex.ErrorObject.Line > 0, "Error should include line number");
}
[Fact]
public void ErrorObject_IncludesFilePathWhenLoaded()
{
var srm = CreateSRM();
// Run with a file path context via ScriptContext
var ctx = srm.CreateContext();
ctx.SourceFilePath = "test_script.reo";
var ex = Assert.Throws<ReoScriptRuntimeException>(() =>
{
srm.Run("undefinedFunction();", ctx);
});
Assert.NotNull(ex.ErrorObject);
Assert.Equal("test_script.reo", ex.ErrorObject.FilePath);
}
[Fact]
public void GetFullErrorInfo_IncludesFileAndLine()
{
var srm = CreateSRM();
var ctx = srm.CreateContext();
ctx.SourceFilePath = "demo.reo";
var ex = Assert.Throws<ReoScriptRuntimeException>(() =>
{
srm.Run("undefinedFunction();", ctx);
});
string info = ex.ErrorObject.GetFullErrorInfo();
Assert.Contains("demo.reo", info);
}
#endregion
#region Truthy/Falsy
[Fact]
public void WhileTruthyCount_Terminates()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 100;
srm.Run("var count = 5; var sum = 0; while (count > 0) { sum += count; count--; }");
var sum = srm.CalcExpression("sum;");
Assert.Equal(15.0, sum);
}
[Fact]
public void GetBoolValue_NumberZero_IsFalsy()
{
Assert.False(ScriptRunningMachine.GetBoolValue(0));
Assert.False(ScriptRunningMachine.GetBoolValue(0.0));
Assert.False(ScriptRunningMachine.GetBoolValue(0L));
Assert.True(ScriptRunningMachine.GetBoolValue(1));
Assert.True(ScriptRunningMachine.GetBoolValue(-1.5));
}
[Fact]
public void GetBoolValue_Strings()
{
Assert.False(ScriptRunningMachine.GetBoolValue(""));
Assert.True(ScriptRunningMachine.GetBoolValue("hello"));
Assert.True(ScriptRunningMachine.GetBoolValue(" "));
}
[Fact]
public void GetBoolValue_Null_IsFalsy()
{
Assert.False(ScriptRunningMachine.GetBoolValue(null));
}
[Fact]
public void IfTruthy_WithNumber()
{
var srm = CreateSRM();
srm.Run("var result = 'no'; if (1) result = 'yes';");
Assert.Equal("yes", srm.CalcExpression("result;"));
}
[Fact]
public void IfFalsy_WithNull()
{
var srm = CreateSRM();
srm.Run("var result = 'no'; var x = null; if (x) result = 'yes';");
Assert.Equal("no", srm.CalcExpression("result;"));
}
[Fact]
public void LogicalOr_ReturnsFirstTruthy()
{
var srm = CreateSRM();
srm.Run("var result = null || 'fallback';");
Assert.Equal("fallback", srm.CalcExpression("result;"));
}
[Fact]
public void NotOperator_OnNull()
{
var srm = CreateSRM();
srm.Run("var result = !null;");
Assert.Equal(true, srm.CalcExpression("result;"));
}
#endregion
#region ScriptError Event
[Fact]
public void LoopTimeout_ErrorIncludesLineInfo()
{
var srm = CreateSRM();
srm.MaxIterationsPerLoop = 100;
var ex = Assert.Throws<ScriptExecutionTimeoutException>(() =>
{
srm.Run("while (true) { }");
});
string info = ex.GetFullErrorInfo();
Assert.Contains("Loop exceeded maximum iteration limit", info);
}
#endregion
#region Async (setTimeout / setInterval)
[Fact]
public void SetTimeout_CallbackExecutes()
{
var srm = CreateSRM();
srm.Run("var a = 0; setTimeout(function(){ a = 10; }, 10);");
// Wait enough time for the callback to fire
System.Threading.Thread.Sleep(200);
Assert.Equal(10.0, srm.CalcExpression("a;"));
}
[Fact]
public void SetInterval_CallbackFiresMultipleTimes()
{
var srm = CreateSRM();
srm.Run("var count = 0; var id = setInterval(function(){ count++; }, 20);");
// Wait for several intervals to fire
System.Threading.Thread.Sleep(300);
srm.Run("clearInterval(id);");
var count = Convert.ToDouble(srm.CalcExpression("count;"));
// Should have fired multiple times (at least 3 in 300ms with 20ms interval)
Assert.True(count >= 3, $"Expected count >= 3, but was {count}");
}
[Fact]
public void ClearInterval_StopsCallback()
{
var srm = CreateSRM();
srm.Run("var count = 0; var id = setInterval(function(){ count++; }, 20);");
System.Threading.Thread.Sleep(150);
srm.Run("clearInterval(id);");
var countAfterClear = Convert.ToDouble(srm.CalcExpression("count;"));
// Wait more and confirm count didn't increase
System.Threading.Thread.Sleep(200);
var countLater = Convert.ToDouble(srm.CalcExpression("count;"));
Assert.Equal(countAfterClear, countLater);
}
[Fact]
public void SetInterval_ReturnsValidId()
{
var srm = CreateSRM();
srm.Run("var id = setInterval(function(){}, 100);");
var id = Convert.ToDouble(srm.CalcExpression("id;"));
Assert.True(id > 0, "setInterval should return a positive id");
srm.Run("clearInterval(id);");
}
#endregion
#region Module Import
private string WriteTempScript(string content)
{
string path = Path.Combine(Path.GetTempPath(), "reoscript_test_" + Guid.NewGuid().ToString("N") + ".reo");
File.WriteAllText(path, content);
return path;
}
[Fact]
public void ImportModule_FunctionsAvailable()
{
string modulePath = WriteTempScript(@"
function add(a, b) { return a + b; }
function mul(a, b) { return a * b; }
");
try
{
var srm = CreateSRM();
srm.Run(string.Format(
"var math = importModule(\"{0}\"); var r = math.add(3, 4);",
modulePath.Replace("\\", "\\\\")));
Assert.Equal(7.0, srm.CalcExpression("r;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportModule_VariablesAvailable()
{
string modulePath = WriteTempScript(@"
var PI = 3.14159;
var name = 'mathlib';
");
try
{
var srm = CreateSRM();
srm.Run(string.Format(
"var m = importModule(\"{0}\");",
modulePath.Replace("\\", "\\\\")));
Assert.Equal("mathlib", srm.CalcExpression("m.name;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportModule_IsolatedScope()
{
string modulePath = WriteTempScript(@"
var secret = 42;
function getSecret() { return secret; }
");
try
{
var srm = CreateSRM();
srm.Run(string.Format(
"var m = importModule(\"{0}\"); var s = m.getSecret();",
modulePath.Replace("\\", "\\\\")));
// Module function should work
Assert.Equal(42.0, srm.CalcExpression("s;"));
// Module's 'secret' should NOT be in global scope
Assert.Null(srm.CalcExpression("secret;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportModule_CachedOnSecondImport()
{
string modulePath = WriteTempScript(@"
var counter = 0;
counter++;
");
try
{
var srm = CreateSRM();
srm.Run(string.Format(@"
var m1 = importModule(""{0}"");
var m2 = importModule(""{0}"");
",
modulePath.Replace("\\", "\\\\")));
// Both references should be the same cached object
// counter should be 1, not 2 (file executed only once)
Assert.Equal(1.0, srm.CalcExpression("m1.counter;"));
Assert.Equal(1.0, srm.CalcExpression("m2.counter;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportModule_FileNotFound_Throws()
{
var srm = CreateSRM();
Assert.Throws<ReoScriptException>(() =>
{
srm.Run("var m = importModule('nonexistent_file.reo');");
});
}
[Fact]
public void ImportModuleFile_FromCSharp()
{
string modulePath = WriteTempScript(@"
function greet(name) { return 'hello ' + name; }
");
try
{
var srm = CreateSRM();
var module = srm.ImportModuleFile(modulePath);
Assert.NotNull(module);
Assert.True(module["greet"] is AbstractFunctionObject);
}
finally
{
File.Delete(modulePath);
}
}
#endregion
#region ASI (Automatic Semicolon Insertion)
[Fact]
public void ASI_SimpleStatements()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
// Variable declaration and assignment without semicolons
object result = srm.Run(@"
var a = 10
var b = 20
a + b
");
Assert.Equal(30d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_FunctionCallWithoutSemicolon()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
object result = srm.Run(@"
function add(x, y) {
return x + y
}
add(3, 4)
");
Assert.Equal(7d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_ReturnWithNewline()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
// return on its own line should return null (like JS)
object result = srm.Run(@"
function f() {
return
42
}
f()
");
Assert.Null(result);
}
[Fact]
public void ASI_ReturnWithValueOnSameLine()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
object result = srm.Run(@"
function f() {
return 42
}
f()
");
Assert.Equal(42d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_MixedWithExplicitSemicolons()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
// Mixing explicit semicolons and ASI should work
object result = srm.Run(@"
var a = 1;
var b = 2
var c = a + b;
c
");
Assert.Equal(3d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_ForLoopStillRequiresSemicolons()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
// for loop header semicolons are mandatory
object result = srm.Run(@"
var sum = 0
for (var i = 0; i < 5; i++) {
sum = sum + i
}
sum
");
Assert.Equal(10d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_ObjectPropertyAccess()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
object result = srm.Run(@"
var obj = { x: 10, y: 20 }
obj.x + obj.y
");
Assert.Equal(30d, ScriptRunningMachine.GetNumberValue(result));
}
[Fact]
public void ASI_BeforeClosingBrace()
{
var srm = new ScriptRunningMachine();
new ScriptDebugger(srm);
// ASI before } (no newline or semicolon needed)
object result = srm.Run("function f() { return 5 } f()");
Assert.Equal(5d, ScriptRunningMachine.GetNumberValue(result));
}
#endregion
#region Import As
[Fact]
public void ImportAs_FunctionsAvailable()
{
string modulePath = WriteTempScript(@"
function add(a, b) { return a + b; }
function sub(a, b) { return a - b; }
");
try
{
var srm = CreateSRM();
srm.WorkPath = Path.GetDirectoryName(modulePath);
srm.Run(string.Format(
"import \"{0}\" as math; var r = math.add(10, 3);",
Path.GetFileName(modulePath)));
Assert.Equal(13.0, srm.CalcExpression("r;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportAs_IsolatedScope()
{
string modulePath = WriteTempScript(@"
var secret = 99;
function getSecret() { return secret; }
");
try
{
var srm = CreateSRM();
srm.WorkPath = Path.GetDirectoryName(modulePath);
srm.Run(string.Format(
"import \"{0}\" as m;",
Path.GetFileName(modulePath)));
// Module function works via alias
Assert.Equal(99.0, srm.CalcExpression("m.getSecret();"));
// secret is NOT in global scope
Assert.Null(srm.CalcExpression("typeof secret == 'undefined' ? null : secret;"));
}
finally
{
File.Delete(modulePath);
}
}
[Fact]
public void ImportAs_WithoutAs_LegacyBehavior()
{
string modulePath = WriteTempScript(@"
var x = 42;
");
try
{
var srm = CreateSRM();
srm.WorkPath = Path.GetDirectoryName(modulePath);
srm.Run(string.Format(
"import \"{0}\";",
Path.GetFileName(modulePath)));
// Legacy import: x should be in global scope
Assert.Equal(42.0, srm.CalcExpression("x;"));
}
finally
{
File.Delete(modulePath);
}
}
#endregion
#region Date Enhancement
[Fact]
public void DateNow_ReturnsNumber()
{
var srm = CreateSRM();
object result = srm.CalcExpression("Date.now();");
Assert.IsType<double>(result);
Assert.True((double)result > 0);
}
[Fact]
public void DateGetTime_MatchesTicks()
{
var srm = CreateSRM();
srm.Run("var d = new Date(); var t = d.getTime();");
object result = srm.CalcExpression("t;");
Assert.IsType<double>(result);
Assert.True((double)result > 0);
}
[Fact]
public void DateNow_ElapsedPattern()
{
var srm = CreateSRM();
srm.Run(@"
var start = Date.now();
var sum = 0;
for (var i = 0; i < 1000; i++) { sum += i; }
var elapsed = Date.now() - start;
");
object elapsed = srm.CalcExpression("elapsed;");
Assert.IsType<double>(elapsed);
Assert.True((double)elapsed >= 0);
}
#endregion
#region Shorthand Property
[Fact]
public void ShorthandProperty_Basic()
{
var srm = CreateSRM();
srm.Run("var a = 1, b = 2; var obj = { a, b };");
Assert.Equal(1.0, srm.CalcExpression("obj.a;"));
Assert.Equal(2.0, srm.CalcExpression("obj.b;"));
}
[Fact]
public void ShorthandProperty_MixedWithNormal()
{
var srm = CreateSRM();
srm.Run("var x = 10; var obj = { x, y: 20 };");
Assert.Equal(10.0, srm.CalcExpression("obj.x;"));
Assert.Equal(20.0, srm.CalcExpression("obj.y;"));
}
#endregion
#region Object Spread
[Fact]
public void ObjectSpread_CopiesProperties()
{
var srm = CreateSRM();
srm.Run("var obj1 = { a: 1, b: 2 }; var obj2 = { ...obj1 };");
Assert.Equal(1.0, srm.CalcExpression("obj2.a;"));
Assert.Equal(2.0, srm.CalcExpression("obj2.b;"));
}
[Fact]
public void ObjectSpread_WithAdditionalProperties()
{
var srm = CreateSRM();
srm.Run("var obj1 = { a: 1 }; var obj2 = { ...obj1, b: 2 };");
Assert.Equal(1.0, srm.CalcExpression("obj2.a;"));
Assert.Equal(2.0, srm.CalcExpression("obj2.b;"));
}
[Fact]
public void ObjectSpread_OverridesProperties()
{
var srm = CreateSRM();
srm.Run("var obj1 = { a: 1, b: 2 }; var obj2 = { ...obj1, b: 99 };");
Assert.Equal(1.0, srm.CalcExpression("obj2.a;"));
Assert.Equal(99.0, srm.CalcExpression("obj2.b;"));
}
#endregion
#region Destructuring
[Fact]
public void Destructuring_Basic()
{
var srm = CreateSRM();
srm.Run("var obj = { a: 10, b: 20 }; var { a, b } = obj;");
Assert.Equal(10.0, srm.CalcExpression("a;"));
Assert.Equal(20.0, srm.CalcExpression("b;"));
}
[Fact]
public void Destructuring_PartialExtract()
{
var srm = CreateSRM();
srm.Run("var obj = { x: 1, y: 2, z: 3 }; var { x, z } = obj;");
Assert.Equal(1.0, srm.CalcExpression("x;"));
Assert.Equal(3.0, srm.CalcExpression("z;"));
}
[Fact]
public void Destructuring_MissingProperty_IsNull()
{
var srm = CreateSRM();
srm.Run("var obj = { a: 1 }; var { a, b } = obj;");
Assert.Equal(1.0, srm.CalcExpression("a;"));
Assert.Null(srm.CalcExpression("b;"));
}
#endregion
#region CalcExpression Without Semicolon
[Fact]
public void CalcExpression_WithoutSemicolon()
{
var srm = CreateSRM();
srm.Run("var x = 42;");
// Should work without trailing semicolon
Assert.Equal(42.0, srm.CalcExpression("x"));
}
[Fact]
public void CalcExpression_ComplexExprWithoutSemicolon()
{
var srm = CreateSRM();
srm.Run("var a = 10; var b = 20;");
Assert.Equal(30.0, srm.CalcExpression("a + b"));
}
#endregion
#region Int32/Double Type Conversion
[Fact]
public void BoxedInt32_Arithmetic()
{
// Simulate ExternalProperty returning boxed Int32
var srm = CreateSRM();
srm.SetGlobalVariable("w", (object)(int)800);
srm.Run("var x = (w - 16) / 3;");
Assert.Equal((800.0 - 16) / 3, srm.CalcExpression("x;"));
}
[Fact]
public void BoxedInt32_MathFloor()
{
var srm = CreateSRM();
srm.SetGlobalVariable("w", (object)(int)800);
object result = srm.CalcExpression("Math.floor(w);");
Assert.Equal(800.0, result);
}
[Fact]
public void BoxedInt32_GetIntValue()
{
// GetIntValue should handle boxed int, float, byte etc.
Assert.Equal(42, ScriptRunningMachine.GetIntValue((object)(int)42));
Assert.Equal(3, ScriptRunningMachine.GetIntValue((object)(float)3.7f));
Assert.Equal(5, ScriptRunningMachine.GetIntValue((object)(byte)5));
Assert.Equal(99, ScriptRunningMachine.GetIntValue((object)(short)99));
}
[Fact]
public void BoxedInt32_GetNumberValue()
{
Assert.Equal(42.0, ScriptRunningMachine.GetNumberValue((object)(int)42));
Assert.Equal(3.0, ScriptRunningMachine.GetNumberValue((object)(float)3.0f));
}
[Fact]
public void BoxedInt32_Comparison()
{
var srm = CreateSRM();
srm.SetGlobalVariable("w", (object)(int)800);
Assert.Equal(true, srm.CalcExpression("w > 100;"));
Assert.Equal(true, srm.CalcExpression("w == 800;"));
}
[Fact]
public void ParseInt_WithBoxedInt32Radix()
{
var srm = CreateSRM();
object result = srm.CalcExpression("parseInt('FF', 16);");
Assert.Equal(255, result);
}
[Fact]
public void MathAtan2_CorrectArgs()
{
var srm = CreateSRM();
object result = srm.CalcExpression("Math.atan2(1, 0);");
Assert.IsType<double>(result);
// atan2(1, 0) = PI/2
Assert.Equal(Math.PI / 2, (double)result, 10);
}
#endregion
}
}