-
-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathEngineTests.cs
3101 lines (2688 loc) · 108 KB
/
EngineTests.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
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Globalization;
using System.Reflection;
using Jint.Native;
using Jint.Native.Array;
using Jint.Native.Number;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Debugger;
using Jint.Tests.Runtime.Debugger;
using Xunit.Abstractions;
#pragma warning disable 618
namespace Jint.Tests.Runtime;
public partial class EngineTests : IDisposable
{
private readonly Engine _engine;
private int countBreak = 0;
private StepMode stepMode;
private static readonly TimeZoneInfo _pacificTimeZone;
private static readonly TimeZoneInfo _tongaTimeZone;
private static readonly TimeZoneInfo _easternTimeZone;
static EngineTests()
{
// https://stackoverflow.com/questions/47848111/how-should-i-fetch-timezoneinfo-in-a-platform-agnostic-way
// should be natively supported soon https://github.com/dotnet/runtime/issues/18644
try
{
_pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
}
catch (TimeZoneNotFoundException)
{
_pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
}
try
{
_tongaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific/Tongatapu");
}
catch (TimeZoneNotFoundException)
{
_tongaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Tonga Standard Time");
}
try
{
_easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}
catch (TimeZoneNotFoundException)
{
_easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
}
}
public EngineTests(ITestOutputHelper output)
{
_engine = new Engine()
.SetValue("log", new Action<object>(o => output.WriteLine(o.ToString())))
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("equal", new Action<object, object>(Assert.Equal))
;
}
void IDisposable.Dispose()
{
}
private void RunTest(string source)
{
_engine.Execute(source);
}
internal static string GetEmbeddedFile(string filename)
{
const string Prefix = "Jint.Tests.Runtime.Scripts.";
var assembly = typeof(EngineTests).GetTypeInfo().Assembly;
var scriptPath = Prefix + filename;
using var stream = assembly.GetManifestResourceStream(scriptPath);
using var sr = new StreamReader(stream);
return sr.ReadToEnd();
}
[Theory]
[InlineData(42d, "42")]
[InlineData("Hello", "'Hello'")]
public void ShouldInterpretLiterals(object expected, string source)
{
var engine = new Engine();
var result = engine.Evaluate(source).ToObject();
Assert.Equal(expected, result);
}
[Fact]
public void ShouldInterpretVariableDeclaration()
{
var engine = new Engine();
var result = engine
.Evaluate("var foo = 'bar'; foo;")
.ToObject();
Assert.Equal("bar", result);
}
[Theory]
[InlineData(4d, "1 + 3")]
[InlineData(-2d, "1 - 3")]
[InlineData(3d, "1 * 3")]
[InlineData(2d, "6 / 3")]
[InlineData(9d, "15 & 9")]
[InlineData(15d, "15 | 9")]
[InlineData(6d, "15 ^ 9")]
[InlineData(36d, "9 << 2")]
[InlineData(2d, "9 >> 2")]
[InlineData(4d, "19 >>> 2")]
public void ShouldInterpretBinaryExpression(object expected, string source)
{
var engine = new Engine();
var result = engine.Evaluate(source).ToObject();
Assert.Equal(expected, result);
}
[Theory]
[InlineData(-59d, "~58")]
[InlineData(58d, "~~58")]
public void ShouldInterpretUnaryExpression(object expected, string source)
{
var engine = new Engine();
var result = engine.Evaluate(source).ToObject();
Assert.Equal(expected, result);
}
[Fact]
public void ShouldHaveProperReferenceErrorMessage()
{
RunTest(@"
'use strict';
var arr = [1, 2];
try {
for (i in arr) { }
assert(false);
}
catch (ex) {
assert(ex.message === 'i is not defined');
}
");
}
[Fact]
public void ShouldHaveProperNotAFunctionErrorMessage()
{
RunTest(@"
try {
var example = {};
example();
assert(false);
}
catch (ex) {
assert(ex.message === 'example is not a function');
}
");
}
[Fact]
public void ShouldEvaluateHasOwnProperty()
{
RunTest(@"
var x = {};
x.Bar = 42;
assert(x.hasOwnProperty('Bar'));
");
}
[Fact]
public void ShouldAllowNullAsStringValue()
{
var engine = new Engine().SetValue("name", (string) null);
Assert.True(engine.Evaluate("name").IsNull());
}
[Fact]
public void FunctionConstructorsShouldCreateNewObjects()
{
RunTest(@"
var Vehicle = function () {};
var vehicle = new Vehicle();
assert(vehicle != undefined);
");
}
[Fact]
public void NewObjectsInheritFunctionConstructorProperties()
{
RunTest(@"
var Vehicle = function () {};
var vehicle = new Vehicle();
Vehicle.prototype.wheelCount = 4;
assert(vehicle.wheelCount == 4);
assert((new Vehicle()).wheelCount == 4);
");
}
[Fact]
public void PrototypeFunctionIsInherited()
{
RunTest(@"
function Body(mass){
this.mass = mass;
}
Body.prototype.offsetMass = function(dm) {
this.mass += dm;
return this;
}
var b = new Body(36);
b.offsetMass(6);
assert(b.mass == 42);
");
}
[Fact]
public void FunctionConstructorCall()
{
RunTest(@"
function Body(mass){
this.mass = mass;
}
var john = new Body(36);
assert(john.mass == 36);
");
}
[Fact]
public void ArrowFunctionCall()
{
RunTest(@"
var add = (a, b) => {
return a + b;
}
var x = add(1, 2);
assert(x == 3);
");
}
[Fact]
public void ArrowFunctionExpressionCall()
{
RunTest(@"
var add = (a, b) => a + b;
var x = add(1, 2);
assert(x === 3);
");
}
[Fact]
public void ArrowFunctionScope()
{
RunTest(@"
var bob = {
_name: ""Bob"",
_friends: [""Alice""],
printFriends() {
this._friends.forEach(f => assert(this._name === ""Bob"" && f === ""Alice""))
}
};
bob.printFriends();
");
}
[Fact]
public void NewObjectsShouldUsePrivateProperties()
{
RunTest(@"
var Vehicle = function (color) {
this.color = color;
};
var vehicle = new Vehicle('tan');
assert(vehicle.color == 'tan');
");
}
[Fact]
public void FunctionConstructorsShouldDefinePrototypeChain()
{
RunTest(@"
function Vehicle() {};
var vehicle = new Vehicle();
assert(vehicle.hasOwnProperty('constructor') == false);
");
}
[Fact]
public void NewObjectsConstructorIsObject()
{
RunTest(@"
var o = new Object();
assert(o.constructor == Object);
");
}
[Fact]
public void NewObjectsIntanceOfConstructorObject()
{
RunTest(@"
var o = new Object();
assert(o instanceof Object);
");
}
[Fact]
public void NewObjectsConstructorShouldBeConstructorObject()
{
RunTest(@"
var Vehicle = function () {};
var vehicle = new Vehicle();
assert(vehicle.constructor == Vehicle);
");
}
[Fact]
public void NewObjectsIntanceOfConstructorFunction()
{
RunTest(@"
var Vehicle = function () {};
var vehicle = new Vehicle();
assert(vehicle instanceof Vehicle);
");
}
[Fact]
public void ShouldEvaluateForLoops()
{
RunTest(@"
var foo = 0;
for (var i = 0; i < 5; i++) {
foo += i;
}
assert(foo == 10);
");
}
[Fact]
public void ShouldEvaluateRecursiveFunctions()
{
RunTest(@"
function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
var result = fib(6);
assert(result == 8);
");
}
[Fact]
public void ShouldAccessObjectProperties()
{
RunTest(@"
var o = {};
o.Foo = 'bar';
o.Baz = 42;
o.Blah = o.Foo + o.Baz;
assert(o.Blah == 'bar42');
");
}
[Fact]
public void ShouldConstructArray()
{
RunTest(@"
var o = [];
assert(o.length == 0);
");
}
[Fact]
public void ArrayPushShouldIncrementLength()
{
RunTest(@"
var o = [];
o.push(1);
assert(o.length == 1);
");
}
[Fact]
public void ArrayFunctionInitializesLength()
{
RunTest(@"
assert(Array(3).length == 3);
assert(Array('3').length == 1);
");
}
[Fact]
public void ArrayIndexerIsAssigned()
{
RunTest(@"
var n = 8;
var o = Array(n);
for (var i = 0; i < n; i++) o[i] = i;
equal(0, o[0]);
equal(7, o[7]);
");
}
[Fact]
public void DenseArrayTurnsToSparseArrayWhenSizeGrowsTooMuch()
{
RunTest(@"
var n = 1024*10+2;
var o = Array(n);
for (var i = 0; i < n; i++) o[i] = i;
equal(0, o[0]);
equal(n -1, o[n - 1]);
");
}
[Fact]
public void DenseArrayTurnsToSparseArrayWhenSparseIndexed()
{
RunTest(@"
var o = Array();
o[100] = 1;
assert(o[100] == 1);
");
}
[Fact]
public void ArrayPopShouldDecrementLength()
{
RunTest(@"
var o = [42, 'foo'];
var pop = o.pop();
assert(o.length == 1);
assert(pop == 'foo');
");
}
[Fact]
public void ArrayConstructor()
{
RunTest(@"
var o = [];
assert(o.constructor == Array);
");
}
[Fact]
public void DateConstructor()
{
RunTest(@"
var o = new Date();
assert(o.constructor == Date);
assert(o.hasOwnProperty('constructor') == false);
");
}
[Fact]
public void DateConstructorWithInvalidParameters()
{
RunTest(@"
var dt = new Date (1, Infinity);
assert(isNaN(dt.getTime()));
");
}
[Fact]
public void ShouldConvertDateToNumber()
{
RunTest(@"
assert(Number(new Date(0)) === 0);
");
}
[Fact]
public void MathObjectIsDefined()
{
RunTest(@"
var o = Math.abs(-1)
assert(o == 1);
");
}
[Fact]
public void VoidShouldReturnUndefined()
{
RunTest(@"
assert(void 0 === undefined);
var x = '1';
assert(void x === undefined);
x = 'x';
assert (isNaN(void x) === true);
x = new String('-1');
assert (void x === undefined);
");
}
[Fact]
public void TypeofObjectShouldReturnString()
{
RunTest(@"
assert(typeof x === 'undefined');
assert(typeof 0 === 'number');
var x = 0;
assert (typeof x === 'number');
var x = new Object();
assert (typeof x === 'object');
");
}
[Fact]
public void MathAbsReturnsAbsolute()
{
RunTest(@"
assert(1 == Math.abs(-1));
");
}
[Fact]
public void NaNIsNan()
{
RunTest(@"
var x = NaN;
assert(isNaN(NaN));
assert(isNaN(Math.abs(x)));
");
}
[Theory]
[InlineData(2147483647, 1, 2147483648)]
[InlineData(-2147483647, -2, -2147483649)]
public void IntegerAdditionShouldNotOverflow(int lhs, int rhs, long result)
{
RunTest($"assert({lhs} + {rhs} == {result})");
}
[Theory]
[InlineData(2147483647, -1, 2147483648)]
[InlineData(-2147483647, 2, -2147483649)]
public void IntegerSubtractionShouldNotOverflow(int lhs, int rhs, long result)
{
RunTest($"assert({lhs} - {rhs} == {result})");
}
[Fact]
public void ToNumberHandlesStringObject()
{
RunTest(@"
x = new String('1');
x *= undefined;
assert(isNaN(x));
");
}
[Fact]
public void FunctionScopesAreChained()
{
RunTest(@"
var x = 0;
function f1(){
function f2(){
return x;
};
return f2();
var x = 1;
}
assert(f1() === undefined);
");
}
[Fact]
public void EvalFunctionParseAndExecuteCode()
{
RunTest(@"
var x = 0;
eval('assert(x == 0)');
");
}
[Fact]
public void ForInStatement()
{
var engine = new Engine();
var result = engine.Evaluate("""
var x, y, str = '';
for(var z in this) {
str += z;
}
return str;
""");
Assert.Equal("xystrz", result);
}
[Fact]
public void ForInStatementEnumeratesKeys()
{
RunTest(@"
for(var i in 'abc');
log(i);
assert(i === '2');
");
}
[Fact]
public void WithStatement()
{
RunTest(@"
with (Math) {
assert(cos(0) == 1);
}
");
}
[Fact]
public void ObjectExpression()
{
RunTest(@"
var o = { x: 1 };
assert(o.x == 1);
");
}
[Fact]
public void StringFunctionCreatesString()
{
RunTest(@"
assert(String(NaN) === 'NaN');
");
}
[Fact]
public void ScopeChainInWithStatement()
{
RunTest(@"
var x = 0;
var myObj = {x : 'obj'};
function f1(){
var x = 1;
function f2(){
with(myObj){
return x;
}
};
return f2();
}
assert(f1() === 'obj');
");
}
[Fact]
public void TryCatchBlockStatement()
{
RunTest(@"
var x, y, z;
try {
x = 1;
throw new TypeError();
x = 2;
}
catch(e) {
assert(x == 1);
assert(e instanceof TypeError);
y = 1;
}
finally {
assert(x == 1);
z = 1;
}
assert(x == 1);
assert(y == 1);
assert(z == 1);
");
}
[Fact]
public void FunctionsCanBeAssigned()
{
RunTest(@"
var sin = Math.sin;
assert(sin(0) == 0);
");
}
[Fact]
public void FunctionArgumentsIsDefined()
{
RunTest(@"
function f() {
assert(arguments.length > 0);
}
f(42);
");
}
[Fact]
public void PrimitiveValueFunctions()
{
RunTest(@"
var s = (1).toString();
assert(s == '1');
");
}
[Theory]
[InlineData(true, "'ab' == 'a' + 'b'")]
public void OperatorsPrecedence(object expected, string source)
{
var engine = new Engine();
var result = engine.Evaluate(source).ToObject();
Assert.Equal(expected, result);
}
[Fact]
public void FunctionPrototypeShouldHaveApplyMethod()
{
RunTest(@"
var numbers = [5, 6, 2, 3, 7];
var max = Math.max.apply(null, numbers);
assert(max == 7);
");
}
[Theory]
[InlineData(double.NaN, "parseInt(NaN)")]
[InlineData(double.NaN, "parseInt(null)")]
[InlineData(double.NaN, "parseInt(undefined)")]
[InlineData(double.NaN, "parseInt(new Boolean(true))")]
[InlineData(double.NaN, "parseInt(Infinity)")]
[InlineData(-1d, "parseInt(-1)")]
[InlineData(-1d, "parseInt('-1')")]
[InlineData(double.NaN, "parseInt(new Array(100000).join('Z'))")]
public void ShouldEvaluateParseInt(object expected, string source)
{
var engine = new Engine();
var result = engine.Evaluate(source).ToObject();
Assert.Equal(expected, result);
}
[Fact]
public void ShouldNotExecuteDebuggerStatement()
{
new Engine().Evaluate("debugger");
}
[Fact]
public void ShouldConvertDoubleToStringWithoutLosingPrecision()
{
RunTest(@"
assert(String(14.915832707045631) === '14.915832707045631');
assert(String(-14.915832707045631) === '-14.915832707045631');
assert(String(0.5) === '0.5');
assert(String(0.00000001) === '1e-8');
assert(String(0.000001) === '0.000001');
assert(String(-1.0) === '-1');
assert(String(30.0) === '30');
assert(String(0.2388906159889881) === '0.2388906159889881');
");
}
[Fact]
public void ShouldWriteNumbersUsingBases()
{
RunTest(@"
assert(15.0.toString() === '15');
assert(15.0.toString(2) === '1111');
assert(15.0.toString(8) === '17');
assert(15.0.toString(16) === 'f');
assert(15.0.toString(17) === 'f');
assert(15.0.toString(36) === 'f');
assert(15.1.toString(36) === 'f.3llllllllkau6snqkpygsc3di');
");
}
[Fact]
public void ShouldNotAlterSlashesInRegex()
{
RunTest(@"
equal('/\\//', new RegExp('/').toString());
");
}
[Fact]
public void ShouldHandleEscapedSlashesInRegex()
{
RunTest(@"
var regex = /[a-z]\/[a-z]/;
assert(regex.test('a/b') === true);
assert(regex.test('a\\/b') === false);
");
}
[Fact]
public void ShouldComputeFractionInBase()
{
Assert.Equal("011", NumberPrototype.ToFractionBase(0.375, 2));
Assert.Equal("14141414141414141414141414141414141414141414141414", NumberPrototype.ToFractionBase(0.375, 5));
}
[Fact]
public void ShouldInvokeAFunctionValue()
{
RunTest(@"
function add(x, y) { return x + y; }
");
var add = _engine.GetValue("add");
Assert.Equal(3, _engine.Invoke(add, 1, 2));
}
[Fact]
public void ShouldAllowInvokeAFunctionValueWithNullValueAsArgument()
{
RunTest(@"
function get(x) { return x; }
");
var add = _engine.GetValue("get");
string str = null;
Assert.Equal(Native.JsValue.Null, _engine.Invoke(add, str));
}
[Fact]
public void ShouldNotInvokeNonFunctionValue()
{
RunTest(@"
var x= 10;
");
var x = _engine.GetValue("x");
var exception = Assert.Throws<JavaScriptException>(() => _engine.Invoke(x, 1, 2));
Assert.Equal("Can only invoke functions", exception.Message);
}
[Fact]
public void ShouldInvokeAFunctionValueThatBelongsToAnObject()
{
RunTest(@"
var obj = { foo: 5, getFoo: function (bar) { return 'foo is ' + this.foo + ', bar is ' + bar; } };
");
var obj = _engine.GetValue("obj").AsObject();
var getFoo = obj.Get("getFoo");
Assert.Equal("foo is 5, bar is 7", _engine.Invoke(getFoo, obj, new object[] { 7 }).AsString());
}
[Fact]
public void ShouldNotInvokeNonFunctionValueThatBelongsToAnObject()
{
RunTest(@"
var obj = { foo: 2 };
");
var obj = _engine.GetValue("obj").AsObject();
var foo = obj.Get("foo");
Assert.Throws<JavaScriptException>(() => _engine.Invoke(foo, obj, new object[] { }));
}
[Fact]
public void ShouldNotAllowModifyingSharedUndefinedDescriptor()
{
var e = new Engine();
e.Evaluate("var x = { literal: true };");
var pd = e.GetValue("x").AsObject().GetOwnProperty("doesNotExist");
Assert.Throws<InvalidOperationException>(() => pd.Value = "oh no, assigning this breaks things");
}
[Theory]
[InlineData("0", 0, 16)]
[InlineData("1", 1, 16)]
[InlineData("100", 100, 10)]
[InlineData("1100100", 100, 2)]
[InlineData("2s", 100, 36)]
[InlineData("2qgpckvng1s", 10000000000000000L, 36)]
public void ShouldConvertNumbersToDifferentBase(string expected, long number, int radix)
{
var result = NumberPrototype.ToBase(number, radix);
Assert.Equal(expected, result);
}
[Fact]
public void JsonParserShouldParseNegativeNumber()
{
RunTest(@"
var a = JSON.parse('{ ""x"":-1 }');
assert(a.x === -1);
var b = JSON.parse('{ ""x"": -1 }');
assert(b.x === -1);
");
}
[Fact]
public void JsonParserShouldUseToString()
{
RunTest(@"
var a = JSON.parse(null); // Equivalent to JSON.parse('null')
assert(a === null);
");
RunTest(@"
var a = JSON.parse(true); // Equivalent to JSON.parse('true')
assert(a === true);
");
RunTest(@"
var a = JSON.parse(false); // Equivalent to JSON.parse('false')
assert(a === false);
");
RunTest(@"
try {
JSON.parse(undefined); // Equivalent to JSON.parse('undefined')
assert(false);
}
catch(ex) {
assert(ex instanceof SyntaxError);
}
");
RunTest(@"
try {
JSON.parse({}); // Equivalent to JSON.parse('[object Object]')
assert(false);
}
catch(ex) {
assert(ex instanceof SyntaxError);
}
");
RunTest(@"
try {
JSON.parse(function() { }); // Equivalent to JSON.parse('function () {}')
assert(false);
}
catch(ex) {
assert(ex instanceof SyntaxError);
}
");
}
[Fact]
public void JsonParserShouldDetectInvalidNegativeNumberSyntax()
{
RunTest(@"
try {
JSON.parse('{ ""x"": -.1 }'); // Not allowed
assert(false);
}
catch(ex) {
assert(ex instanceof SyntaxError);
}
");
RunTest(@"
try {
JSON.parse('{ ""x"": - 1 }'); // Not allowed
assert(false);
}
catch(ex) {
assert(ex instanceof SyntaxError);
}
");
}
[Fact]
public void JsonParserShouldUseReviverFunction()
{
RunTest(@"