forked from microsoft/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostFunctionsTest.cs
940 lines (818 loc) · 33.8 KB
/
HostFunctionsTest.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.V8;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
[TestClass]
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Test classes use TestCleanupAttribute for deterministic teardown.")]
public class HostFunctionsTest : ClearScriptTest
{
#region setup / teardown
private ScriptEngine engine;
private HostFunctions host;
[TestInitialize]
public void TestInitialize()
{
engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
engine.AddHostObject("host", host = new HostFunctions());
}
[TestCleanup]
public void TestCleanup()
{
engine.Dispose();
BaseTestCleanup();
}
#endregion
#region test methods
// ReSharper disable InconsistentNaming
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_asType()
{
var value = Enumerable.Range(1, 5).ToArray();
VerifyAsType<IList>(value);
VerifyAsType<Array>(value);
VerifyAsType<Object>(value);
Assert.IsNull(host.asType<IDictionary>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_asType_Scalar()
{
const int value = 123;
VerifyAsType<IComparable>(value);
VerifyAsType<ValueType>(value);
VerifyAsType<Object>(value);
Assert.IsNull(host.asType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_asType_Enum()
{
const DayOfWeek value = DayOfWeek.Wednesday;
VerifyAsType<IComparable>(value);
VerifyAsType<Enum>(value);
VerifyAsType<ValueType>(value);
VerifyAsType<Object>(value);
Assert.IsNull(host.asType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_asType_Struct()
{
var value = new DateTime(2007, 5, 22, 6, 15, 43);
VerifyAsType<IComparable>(value);
VerifyAsType<ValueType>(value);
VerifyAsType<Object>(value);
Assert.IsNull(host.asType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_cast()
{
var value = Enumerable.Range(1, 5).ToArray();
CastVerifier<IList>.VerifySymmetric(host, value);
CastVerifier<Array>.VerifySymmetric(host, value);
CastVerifier<object>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
[ExpectedException(typeof(RuntimeBinderException))]
public void HostFunctions_cast_Fail()
{
var value = Enumerable.Range(1, 5).ToArray();
CastVerifier<IDictionary>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_cast_Scalar()
{
const double value = 123.75;
CastVerifier<IComparable>.VerifySymmetric(host, value);
CastVerifier<ValueType>.VerifySymmetric(host, value);
CastVerifier<object>.VerifySymmetric(host, value);
CastVerifier<int>.VerifyAsymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
[ExpectedException(typeof(RuntimeBinderException))]
public void HostFunctions_cast_Scalar_Fail()
{
const double value = 123.75;
CastVerifier<IList>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_cast_Enum()
{
const DayOfWeek value = DayOfWeek.Wednesday;
CastVerifier<IComparable>.VerifySymmetric(host, value);
CastVerifier<Enum>.VerifySymmetric(host, value);
CastVerifier<ValueType>.VerifySymmetric(host, value);
CastVerifier<object>.VerifySymmetric(host, value);
CastVerifier<int>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
[ExpectedException(typeof(RuntimeBinderException))]
public void HostFunctions_cast_Enum_Fail()
{
const DayOfWeek value = DayOfWeek.Wednesday;
CastVerifier<IList>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_cast_Struct()
{
var value = new DateTime(2007, 5, 22, 6, 15, 43);
CastVerifier<IComparable>.VerifySymmetric(host, value);
CastVerifier<ValueType>.VerifySymmetric(host, value);
CastVerifier<object>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
[ExpectedException(typeof(RuntimeBinderException))]
public void HostFunctions_cast_Struct_Fail()
{
var value = new DateTime(2007, 5, 22, 6, 15, 43);
CastVerifier<IList>.VerifySymmetric(host, value);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_cast_UserDefined()
{
Assert.AreEqual(intVal, CastVerifier<int>.VerifyCast(host, this));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Action()
{
var fooVal = Enumerable.Range(1, 5).ToArray();
const double barVal = Math.PI;
const DayOfWeek bazVal = DayOfWeek.Wednesday;
var quxVal = new DateTime(2007, 5, 22, 6, 15, 43);
int[] foo = null;
var bar = 0d;
var baz = DayOfWeek.Sunday;
var qux = DateTime.UtcNow;
Action<int[], double, DayOfWeek, DateTime> method = (fooArg, barArg, bazArg, quxArg) =>
{
foo = fooArg;
bar = barArg;
baz = bazArg;
qux = quxArg;
};
host.del<Action<int[], double, DayOfWeek, DateTime>>(method)(fooVal, barVal, bazVal, quxVal);
Assert.AreEqual(fooVal, foo);
Assert.AreEqual(barVal, bar);
Assert.AreEqual(bazVal, baz);
Assert.AreEqual(quxVal, qux);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Action_0()
{
var methodInvoked = false;
Action method = () => { methodInvoked = true; };
host.del<Action>(method)();
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Action_1()
{
var methodInvoked = false;
Action<bool> method = value => { methodInvoked = value; };
host.del<Action<bool>>(method)(true);
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Func()
{
var fooVal = Enumerable.Range(1, 5).ToArray();
const double barVal = Math.PI;
const DayOfWeek bazVal = DayOfWeek.Wednesday;
var quxVal = new DateTime(2007, 5, 22, 6, 15, 43);
const int retVal = 42;
int[] foo = null;
var bar = 0d;
var baz = DayOfWeek.Sunday;
var qux = DateTime.UtcNow;
Func<int[], double, DayOfWeek, DateTime, int> method = (fooArg, barArg, bazArg, quxArg) =>
{
foo = fooArg;
bar = barArg;
baz = bazArg;
qux = quxArg;
return retVal;
};
Assert.AreEqual(retVal, host.del<Func<int[], double, DayOfWeek, DateTime, int>>(method)(fooVal, barVal, bazVal, quxVal));
Assert.AreEqual(fooVal, foo);
Assert.AreEqual(barVal, bar);
Assert.AreEqual(bazVal, baz);
Assert.AreEqual(quxVal, qux);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Func_0()
{
var methodInvoked = false;
const int retVal = 42;
Func<int> method = () => { methodInvoked = true; return retVal; };
Assert.AreEqual(retVal, host.del<Func<int>>(method)());
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_Func_1()
{
var methodInvoked = false;
const int retVal = 42;
Func<bool, int> method = value => { methodInvoked = value; return retVal; };
Assert.AreEqual(retVal, host.del<Func<bool, int>>(method)(true));
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_del_CustomDelegate()
{
engine.EnableAutoHostVariables = true;
var fooVal = Enumerable.Range(1, 5).ToArray();
const double barVal = Math.PI;
const DayOfWeek bazVal = DayOfWeek.Wednesday;
var quxVal = new DateTime(2007, 5, 22, 6, 15, 43);
const int retVal = 42;
CustomDelegateHandler method = (fooVar, fooArg, barVar, barArg, bazVar, bazArg, quxVar, quxArg, retArg) =>
{
fooVar.Value = fooArg;
barVar.Value = barArg;
bazVar.Value = bazArg;
quxVar.Value = quxArg;
return retArg;
};
var bar = 0d;
var qux = DateTime.UtcNow;
Assert.AreEqual(retVal, host.del<CustomDelegate>(method)(out var foo, fooVal, ref bar, barVal, out var baz, bazVal, ref qux, quxVal, retVal));
Assert.AreEqual(fooVal, foo);
Assert.AreEqual(barVal, bar);
Assert.AreEqual(bazVal, baz);
Assert.AreEqual(quxVal, qux);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_0()
{
var methodInvoked = false;
const int retVal = 42;
Func<int> method = () => { methodInvoked = true; return retVal; };
Assert.AreEqual(retVal, ((Func<int>)host.func<int>(0, method))());
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_1()
{
var methodInvoked = false;
const int retVal = 42;
Func<object, int> method = value => { methodInvoked = (bool)value; return retVal; };
Assert.AreEqual(retVal, ((Func<object, int>)host.func<int>(1, method))(true));
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_2()
{
var methodInvoked = false;
const int retVal = 42;
Func<object, object, int> method = (value1, value2) => { methodInvoked = (bool)value1 && (bool)value2; return retVal; };
Assert.AreEqual(retVal, ((Func<object, object, int>)host.func<int>(2, method))(true, true));
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_0_NonGeneric()
{
var methodInvoked = false;
const int retVal = 42;
Func<object> method = () => { methodInvoked = true; return retVal; };
Assert.AreEqual(retVal, ((Func<object>)host.func(0, method))());
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_1_NonGeneric()
{
var methodInvoked = false;
const int retVal = 42;
Func<object, object> method = value => { methodInvoked = (bool)value; return retVal; };
Assert.AreEqual(retVal, ((Func<object, object>)host.func(1, method))(true));
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_func_2_NonGeneric()
{
var methodInvoked = false;
const int retVal = 42;
Func<object, object, object> method = (value1, value2) => { methodInvoked = (bool)value1 && (bool)value2; return retVal; };
Assert.AreEqual(retVal, ((Func<object, object, object>)host.func(2, method))(true, true));
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isType()
{
var value = Enumerable.Range(1, 5).ToArray();
Assert.IsTrue(host.isType<IList>(value));
Assert.IsTrue(host.isType<Array>(value));
Assert.IsTrue(host.isType<Object>(value));
Assert.IsFalse(host.isType<IDictionary>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isType_Scalar()
{
const int value = 123;
Assert.IsTrue(host.isType<IComparable>(value));
Assert.IsTrue(host.isType<ValueType>(value));
Assert.IsTrue(host.isType<Object>(value));
Assert.IsFalse(host.isType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isType_Enum()
{
const DayOfWeek value = DayOfWeek.Wednesday;
Assert.IsTrue(host.isType<IComparable>(value));
Assert.IsTrue(host.isType<Enum>(value));
Assert.IsTrue(host.isType<ValueType>(value));
Assert.IsTrue(host.isType<Object>(value));
Assert.IsFalse(host.isType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isType_Struct()
{
var value = new DateTime(2007, 5, 22, 6, 15, 43);
Assert.IsTrue(host.isType<IComparable>(value));
Assert.IsTrue(host.isType<ValueType>(value));
Assert.IsTrue(host.isType<Object>(value));
Assert.IsFalse(host.isType<IList>(value));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isTypeObj_True_TypeArg()
{
engine.AddHostType("Int32", typeof(int));
Assert.IsTrue((bool)engine.Evaluate("host.isTypeObj(Int32)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isTypeObj_True_NonTypeArg()
{
engine.AddHostType("List", typeof(List<>));
engine.AddHostType("Console", typeof(Console));
Assert.IsTrue((bool)engine.Evaluate("host.isTypeObj(List)"));
Assert.IsTrue((bool)engine.Evaluate("host.isTypeObj(Console)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_isTypeObj_False()
{
Assert.IsFalse((bool)engine.Evaluate("host.isTypeObj(5)"));
Assert.IsFalse((bool)engine.Evaluate("host.isTypeObj({})"));
Assert.IsFalse((bool)engine.Evaluate("host.isTypeObj(host)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newArr()
{
VerifyNewArr<Random>(5);
VerifyNewArr<Random>(5, 3);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newArr_Enum()
{
VerifyNewArr<DayOfWeek>(5);
VerifyNewArr<DayOfWeek>(5, 3);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newArr_Scalar()
{
VerifyNewArr<double>(5);
VerifyNewArr<double>(5, 3);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newArr_Struct()
{
VerifyNewArr<DateTime>(5);
VerifyNewArr<DateTime>(5, 3);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newArr_NonGeneric()
{
VerifyNewArr(5);
VerifyNewArr(5, 3);
}
[TestMethod, TestCategory("HostFunctions")]
[ExpectedException(typeof(ArgumentException))]
public void HostFunctions_newArr_Fail()
{
VerifyNewArr<Random>();
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newObj()
{
Assert.IsInstanceOfType(host.newObj<Random>(), typeof(Random));
Assert.IsInstanceOfType(host.newObj<Random>(100), typeof(Random));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newObj_Struct()
{
Assert.AreEqual(new DateTime(), host.newObj<DateTime>());
Assert.AreEqual(new DateTime(2007, 5, 22), host.newObj<DateTime>(2007, 5, 22));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newObj_PropertyBag()
{
Assert.IsInstanceOfType(host.newObj(), typeof(PropertyBag));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newVar()
{
VerifyNewVar<Random>();
VerifyNewVar(new Random(100));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newVar_Enum()
{
VerifyNewVar<DayOfWeek>();
VerifyNewVar(DayOfWeek.Wednesday);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newVar_Scalar()
{
VerifyNewVar<double>();
VerifyNewVar(0.125);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_newVar_Struct()
{
VerifyNewVar<DateTime>();
VerifyNewVar(DateTime.Now);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_proc_0()
{
var methodInvoked = false;
Action method = () => methodInvoked = true;
((Action)host.proc(0, method))();
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_proc_1()
{
var methodInvoked = false;
Action<object> method = value => methodInvoked = (bool)value;
((Action<object>)host.proc(1, method))(true);
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_proc_2()
{
var methodInvoked = false;
Action<object, object> method = (value1, value2) => methodInvoked = (bool)value1 && (bool)value2;
((Action<object, object>)host.proc(2, method))(true, true);
Assert.IsTrue(methodInvoked);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_typeOf_TypeArg()
{
engine.AllowReflection = true;
engine.AddHostType("Int32", typeof(int));
Assert.AreEqual(typeof(int), engine.Evaluate("host.typeOf(Int32)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_typeOf_NonTypeArg()
{
engine.AllowReflection = true;
engine.AddHostType("List", typeof(List<>));
engine.AddHostType("Console", typeof(Console));
Assert.AreEqual(typeof(List<>), engine.Evaluate("host.typeOf(List)"));
Assert.AreEqual(typeof(Console), engine.Evaluate("host.typeOf(Console)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_typeOf_TypeArg_Blocked()
{
engine.AddHostType("Int32", typeof(int));
TestUtil.AssertException<UnauthorizedAccessException>(() => engine.Execute("host.typeOf(Int32)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_typeOf_NonTypeArg_Blocked()
{
engine.AddHostType("Console", typeof(Console));
TestUtil.AssertException<UnauthorizedAccessException>(() => engine.Execute("host.typeOf(Console)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_typeOf_NonType()
{
engine.AllowReflection = true;
TestUtil.AssertException<ArgumentException>(() => engine.Execute("host.typeOf(5)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_flags_Unsigned()
{
engine.AddHostType("UnsignedFlags", HostItemFlags.PrivateAccess, typeof(UnsignedFlags));
var result = engine.Evaluate("host.flags(UnsignedFlags.Second, UnsignedFlags.Fourth, UnsignedFlags.Sixth, UnsignedFlags.Eighth)");
Assert.AreEqual(UnsignedFlags.Second | UnsignedFlags.Fourth | UnsignedFlags.Sixth | UnsignedFlags.Eighth, result);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_flags_Signed()
{
engine.AddHostType("SignedFlags", HostItemFlags.PrivateAccess, typeof(SignedFlags));
// include Eighth member to force overflow path in host.flags()
var result = engine.Evaluate("host.flags(SignedFlags.Second, SignedFlags.Fourth, SignedFlags.Sixth, SignedFlags.Eighth)");
Assert.AreEqual(SignedFlags.Second | SignedFlags.Fourth | SignedFlags.Sixth | SignedFlags.Eighth, result);
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_flags_Mismatch()
{
engine.AddHostType("UnsignedFlags", HostItemFlags.PrivateAccess, typeof(UnsignedFlags));
engine.AddHostType("SignedFlags", HostItemFlags.PrivateAccess, typeof(SignedFlags));
TestUtil.AssertMethodBindException(() => engine.Execute("host.flags(SignedFlags.Second, SignedFlags.Fourth, UnsignedFlags.Sixth, UnsignedFlags.Eighth)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_flags_NonFlags()
{
engine.AddHostType("NonFlags", HostItemFlags.PrivateAccess, typeof(NonFlags));
TestUtil.AssertException<InvalidOperationException>(() => engine.Execute("host.flags(NonFlags.Second, NonFlags.Fourth, NonFlags.Sixth, NonFlags.Eighth)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toSByte()
{
Assert.AreEqual(Convert.ToSByte(127), engine.Evaluate("host.toSByte(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toByte()
{
Assert.AreEqual(Convert.ToByte(127), engine.Evaluate("host.toByte(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt16()
{
Assert.AreEqual(Convert.ToInt16(127), engine.Evaluate("host.toInt16(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt16()
{
Assert.AreEqual(Convert.ToUInt16(127), engine.Evaluate("host.toUInt16(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toChar()
{
Assert.AreEqual(Convert.ToChar(127), engine.Evaluate("host.toChar(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt32()
{
Assert.AreEqual(Convert.ToInt32(127), engine.Evaluate("host.toInt32(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt32()
{
Assert.AreEqual(Convert.ToUInt32(127), engine.Evaluate("host.toUInt32(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt64()
{
Assert.AreEqual(Convert.ToInt64(127), engine.Evaluate("host.toInt64(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt64()
{
Assert.AreEqual(Convert.ToUInt64(127), engine.Evaluate("host.toUInt64(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toSingle()
{
Assert.AreEqual(Convert.ToSingle(127), engine.Evaluate("host.toSingle(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toDouble()
{
Assert.AreEqual(Convert.ToDouble(127), engine.Evaluate("host.toDouble(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toDecimal()
{
Assert.AreEqual(Convert.ToDecimal(127), engine.Evaluate("host.toDecimal(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toSByte_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToSByte(127), engine.Evaluate("host.toSByte(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toByte_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToByte(127), engine.Evaluate("host.toByte(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt16_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToInt16(127), engine.Evaluate("host.toInt16(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt16_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToUInt16(127), engine.Evaluate("host.toUInt16(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toChar_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToChar(127), engine.Evaluate("host.toChar(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt32_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToInt32(127), engine.Evaluate("host.toInt32(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt32_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToUInt32(127), engine.Evaluate("host.toUInt32(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toInt64_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToInt64(127), engine.Evaluate("host.toInt64(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toUInt64_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToUInt64(127), engine.Evaluate("host.toUInt64(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toSingle_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToSingle(127), engine.Evaluate("host.toSingle(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toDouble_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToDouble(127), engine.Evaluate("host.toDouble(127)"));
}
[TestMethod, TestCategory("HostFunctions")]
public void HostFunctions_toDecimal_V8()
{
engine.Dispose();
engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
Assert.AreEqual(Convert.ToDecimal(127), engine.Evaluate("host.toDecimal(127)"));
}
// ReSharper restore InconsistentNaming
#endregion
#region miscellaneous
private void VerifyAsType<T>(object value) where T : class
{
var hostItem = host.asType<T>(value) as HostItem;
Assert.IsNotNull(hostItem);
Assert.AreEqual(typeof(T), hostItem.Target.Type);
Assert.AreEqual(value, hostItem.Unwrap());
}
private void VerifyNewArr<T>(params int[] lengths)
{
var value = (Array)host.newArr<T>(lengths);
Assert.IsInstanceOfType(value, typeof(T).MakeArrayType(lengths.Length));
lengths.ForEach((_, index) => Assert.AreEqual(lengths[index], value.GetLength(index)));
}
private void VerifyNewArr(params int[] lengths)
{
VerifyNewArr<object>(lengths);
}
private void VerifyNewVar<T>()
{
var variable = host.newVar<T>();
Assert.IsInstanceOfType(variable, typeof(HostVariable<T>));
if (typeof(T).IsValueType)
{
Assert.AreEqual(default, ((HostVariable<T>)variable).Value);
}
else
{
Assert.AreSame(default(T), ((HostVariable<T>)variable).Value);
}
}
private void VerifyNewVar<T>(T initValue)
{
var variable = host.newVar(initValue);
Assert.IsInstanceOfType(variable, typeof(HostVariable<T>));
if (typeof(T).IsValueType)
{
Assert.AreEqual(initValue, ((HostVariable<T>)variable).Value);
}
else
{
Assert.AreSame(initValue, ((HostVariable<T>)variable).Value);
}
}
private const int intVal = 12345;
public static explicit operator int(HostFunctionsTest value)
{
return intVal;
}
private static class CastVerifier<TTarget>
{
public static void VerifySymmetric<TValue>(HostFunctions host, TValue value)
{
var result1 = VerifyCast<TTarget>(host, value);
var result2 = VerifyCast<TValue>(host, result1);
Assert.AreEqual(value, result2);
}
public static void VerifyAsymmetric<TValue>(HostFunctions host, TValue value)
{
var result1 = VerifyCast<TTarget>(host, value);
var result2 = VerifyCast<TValue>(host, result1);
Assert.AreEqual(value.DynamicCast<TTarget>().DynamicCast<TValue>(), result2);
}
public static object VerifyCast(HostFunctions host, object value)
{
return VerifyCast<TTarget>(host, value);
}
private static object VerifyCast<T>(HostFunctions host, object value)
{
var expectedResult = value.DynamicCast<T>();
var result = host.cast<T>(value);
if (result is HostItem hostItem)
{
Assert.AreEqual(typeof(T), hostItem.Target.Type);
result = hostItem.Unwrap();
}
Assert.AreEqual(expectedResult, result);
return result;
}
}
private delegate int CustomDelegate(
out int[] foo, int[] fooVal,
ref double bar, double barVal,
out DayOfWeek baz, DayOfWeek bazVal,
ref DateTime qux, DateTime quxVal,
int retVal
);
private delegate int CustomDelegateHandler(
OutArg<int[]> fooVar, int[] fooVal,
RefArg<double> barVar, double barVal,
OutArg<DayOfWeek> bazVar, DayOfWeek bazVal,
RefArg<DateTime> quxVar, DateTime quxVal,
int retVal
);
[Flags]
public enum UnsignedFlags : byte
{
None = 0,
First = 1,
Second = 1 << 1,
Third = 1 << 2,
Fourth = 1 << 3,
Fifth = 1 << 4,
Sixth = 1 << 5,
Seventh = 1 << 6,
Eighth = 1 << 7,
}
[Flags]
public enum SignedFlags : sbyte
{
None = 0,
First = 1,
Second = 1 << 1,
Third = 1 << 2,
Fourth = 1 << 3,
Fifth = 1 << 4,
Sixth = 1 << 5,
Seventh = 1 << 6,
Eighth = -128,
// negative member forces overflow path in host.flags()
}
public enum NonFlags
{
None = 0,
First = 1,
Second = 1 << 1,
Third = 1 << 2,
Fourth = 1 << 3,
Fifth = 1 << 4,
Sixth = 1 << 5,
Seventh = 1 << 6,
Eighth = 1 << 7,
}
#endregion
}
}