forked from microsoft/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVBScriptEngineTest.cs
3380 lines (2882 loc) · 158 KB
/
VBScriptEngineTest.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.Windows;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
[TestClass]
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Test classes use TestCleanupAttribute for deterministic teardown.")]
[SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "Typos in test code are acceptable.")]
public class VBScriptEngineTest : ClearScriptTest
{
#region setup / teardown
private VBScriptEngine engine;
[TestInitialize]
public void TestInitialize()
{
engine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
engine.Execute("function pi : pi = 4 * atn(1) : end function");
engine.Execute("function e : e = exp(1) : end function");
}
[TestCleanup]
public void TestCleanup()
{
engine.Dispose();
BaseTestCleanup();
}
#endregion
#region test methods
// ReSharper disable InconsistentNaming
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostObject()
{
var host = new HostFunctions();
engine.AddHostObject("host", host);
Assert.AreSame(host, engine.Evaluate("host"));
}
[TestMethod, TestCategory("VBScriptEngine")]
[ExpectedException(typeof(InvalidOperationException))]
public void VBScriptEngine_AddHostObject_Scalar()
{
engine.AddHostObject("value", 123);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostObject_Enum()
{
const DayOfWeek value = DayOfWeek.Wednesday;
engine.AddHostObject("value", value);
Assert.AreEqual(value, engine.Evaluate("value"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostObject_Struct()
{
var date = new DateTime(2007, 5, 22, 6, 15, 43);
engine.AddHostObject("date", date);
Assert.AreEqual(date, engine.Evaluate("date"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostObject_GlobalMembers()
{
var host = new HostFunctions();
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
engine.Execute("TestProperty = newObj()");
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
}
[TestMethod, TestCategory("VBScriptEngine")]
[ExpectedException(typeof(ScriptEngineException))]
public void VBScriptEngine_AddHostObject_DefaultAccess()
{
engine.AddHostObject("test", this);
engine.Execute("test.PrivateMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostObject_PrivateAccess()
{
engine.AddHostObject("test", HostItemFlags.PrivateAccess, this);
engine.Execute("test.PrivateMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddRestrictedHostObject_BaseClass()
{
var host = new ExtendedHostFunctions() as HostFunctions;
engine.AddRestrictedHostObject("host", host);
Assert.IsInstanceOfType(engine.Evaluate("host.newObj()"), typeof(PropertyBag));
TestUtil.AssertException<ScriptEngineException>(() => engine.Evaluate("host.type(\"System.Int32\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddRestrictedHostObject_Interface()
{
const double value = 123.45;
engine.AddRestrictedHostObject("convertible", value as IConvertible);
engine.AddHostObject("culture", CultureInfo.InvariantCulture);
Assert.AreEqual(value, engine.Evaluate("convertible.ToDouble(culture)"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType("Random", typeof(Random));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Random)"), typeof(Random));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_GlobalMembers()
{
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
engine.Execute("StaticTestProperty = NewGuid()");
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
}
[TestMethod, TestCategory("VBScriptEngine")]
[ExpectedException(typeof(ScriptEngineException))]
public void VBScriptEngine_AddHostType_DefaultAccess()
{
engine.AddHostType("Test", GetType());
engine.Execute("Test.PrivateStaticMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_PrivateAccess()
{
engine.AddHostType("Test", HostItemFlags.PrivateAccess, GetType());
engine.Execute("Test.PrivateStaticMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_Static()
{
engine.AddHostType("Enumerable", typeof(Enumerable));
Assert.IsInstanceOfType(engine.Evaluate("Enumerable.Range(0, 5).ToArray()"), typeof(int[]));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_OpenGeneric()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType("List", typeof(List<>));
engine.AddHostType("Guid", typeof(Guid));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(List(Guid))"), typeof(List<Guid>));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_ByName()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType("Random", "System.Random");
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Random)"), typeof(Random));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_ByNameWithAssembly()
{
engine.AddHostType("Enumerable", "System.Linq.Enumerable", "System.Core");
Assert.IsInstanceOfType(engine.Evaluate("Enumerable.Range(0, 5).ToArray()"), typeof(int[]));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_ByNameWithTypeArgs()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType("Dictionary", "System.Collections.Generic.Dictionary", typeof(string), typeof(int));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Dictionary)"), typeof(Dictionary<string, int>));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_DefaultName()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType(typeof(Random));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Random)"), typeof(Random));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostType_DefaultNameGeneric()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType(typeof(List<int>));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(List)"), typeof(List<int>));
engine.AddHostType(typeof(Dictionary<,>));
engine.AddHostType(typeof(int));
engine.AddHostType(typeof(decimal));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Dictionary(Int32, Decimal), 100)"), typeof(Dictionary<int, decimal>));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AddHostTypes()
{
engine.AddHostObject("host", new HostFunctions());
engine.AddHostTypes(typeof(Dictionary<,>), typeof(int), typeof(decimal));
Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Dictionary(Int32, Decimal), 100)"), typeof(Dictionary<int, decimal>));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate()
{
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate("e * pi"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_Array()
{
// ReSharper disable ImplicitlyCapturedClosure
var lengths = new[] { 3, 5, 7 };
var formatParams = string.Join(", ", Enumerable.Range(0, lengths.Length).Select(position => "{" + position + "}"));
var hosts = Array.CreateInstance(typeof(object), lengths);
hosts.Iterate(indices => hosts.SetValue(new HostFunctions(), indices));
engine.AddHostObject("hostArray", hosts);
engine.Execute(MiscHelpers.FormatInvariant("dim hosts(" + formatParams + ")", lengths.Select(length => (object)(length - 1)).ToArray()));
hosts.Iterate(indices => engine.Execute(MiscHelpers.FormatInvariant("set hosts(" + formatParams + ") = hostArray.GetValue(" + formatParams + ")", indices.Select(index => (object)index).ToArray())));
hosts.Iterate(indices => Assert.AreSame(hosts.GetValue(indices), engine.Evaluate(MiscHelpers.FormatInvariant("hosts(" + formatParams + ")", indices.Select(index => (object)index).ToArray()))));
var result = engine.Evaluate("hosts");
Assert.IsInstanceOfType(result, typeof(object[,,]));
var hostArray = (object[,,])result;
hosts.Iterate(indices => Assert.AreSame(hosts.GetValue(indices), hostArray.GetValue(indices)));
// ReSharper restore ImplicitlyCapturedClosure
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_WithDocumentName()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(documentName, "e * pi"));
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DiscardDocument()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(documentName, true, "e * pi"));
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_RetainDocument()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(documentName, false, "e * pi"));
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DocumentInfo_WithDocumentName()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(new DocumentInfo(documentName), "e * pi"));
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DocumentInfo_WithDocumentUri()
{
const string documentName = "DoTheMath";
var documentUri = new Uri(@"c:\foo\bar\baz\" + documentName);
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(new DocumentInfo(documentUri) { Flags = DocumentFlags.None }, "e * pi"));
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DocumentInfo_WithDocumentUri_Relative()
{
const string documentName = "DoTheMath";
var documentUri = new Uri(documentName, UriKind.Relative);
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(new DocumentInfo(documentUri) { Flags = DocumentFlags.None }, "e * pi"));
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DocumentInfo_DiscardDocument()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(new DocumentInfo(documentName) { Flags = DocumentFlags.IsTransient }, "e * pi"));
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Evaluate_DocumentInfo_RetainDocument()
{
const string documentName = "DoTheMath";
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate(new DocumentInfo(documentName) { Flags = DocumentFlags.None }, "e * pi"));
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute()
{
engine.Execute("epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_WithDocumentName()
{
const string documentName = "DoTheMath";
engine.Execute(documentName, "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DiscardDocument()
{
const string documentName = "DoTheMath";
engine.Execute(documentName, true, "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_RetainDocument()
{
const string documentName = "DoTheMath";
engine.Execute(documentName, false, "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DocumentInfo_WithDocumentName()
{
const string documentName = "DoTheMath";
engine.Execute(new DocumentInfo(documentName), "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DocumentInfo_WithDocumentUri()
{
const string documentName = "DoTheMath";
var documentUri = new Uri(@"c:\foo\bar\baz\" + documentName);
engine.Execute(new DocumentInfo(documentUri), "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DocumentInfo_WithDocumentUri_Relative()
{
const string documentName = "DoTheMath";
var documentUri = new Uri(documentName, UriKind.Relative);
engine.Execute(new DocumentInfo(documentUri), "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DocumentInfo_DiscardDocument()
{
const string documentName = "DoTheMath";
engine.Execute(new DocumentInfo(documentName) { Flags = DocumentFlags.IsTransient }, "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsFalse(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Execute_DocumentInfo_RetainDocument()
{
const string documentName = "DoTheMath";
engine.Execute(new DocumentInfo(documentName) { Flags = DocumentFlags.None }, "epi = e * pi");
Assert.AreEqual(Math.E * Math.PI, engine.Script.epi);
Assert.IsTrue(engine.GetDebugDocumentNames().Any(name => name.StartsWith(documentName, StringComparison.Ordinal)));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ExecuteCommand_EngineConvert()
{
Assert.AreEqual("[ScriptObject:EngineInternalImpl]", engine.ExecuteCommand("eval EngineInternal"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ExecuteCommand_HostConvert()
{
var dateHostItem = HostItem.Wrap(engine, new DateTime(2007, 5, 22, 6, 15, 43));
engine.AddHostObject("date", dateHostItem);
Assert.AreEqual(dateHostItem.ToString(), engine.ExecuteCommand("eval date"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ExecuteCommand_HostVariable()
{
engine.Script.host = new HostFunctions();
Assert.AreEqual("[HostVariable:String]", engine.ExecuteCommand("eval host.newVar(\"foo\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Invoke_ScriptFunction()
{
engine.Execute("function foo(x) : foo = x * pi : end function");
Assert.AreEqual(Math.E * Math.PI, engine.Invoke("foo", Math.E));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Invoke_HostDelegate()
{
engine.Script.foo = new Func<double, double>(x => x * Math.PI);
Assert.AreEqual(Math.E * Math.PI, engine.Invoke("foo", Math.E));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Interrupt()
{
var checkpoint = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(_ =>
{
checkpoint.WaitOne();
engine.Interrupt();
});
engine.AddHostObject("checkpoint", checkpoint);
TestUtil.AssertException<OperationCanceledException>(() => engine.Execute("call checkpoint.Set() : while true : foo = \"hello\" : wend"));
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate("e * pi"));
}
[TestMethod, TestCategory("VBScriptEngine")]
[ExpectedException(typeof(ScriptEngineException))]
public void VBScriptEngine_AccessContext_Default()
{
engine.AddHostObject("test", this);
engine.Execute("test.PrivateMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_AccessContext_Private()
{
engine.AddHostObject("test", this);
engine.AccessContext = GetType();
engine.Execute("test.PrivateMethod()");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ContinuationCallback()
{
engine.ContinuationCallback = () => false;
TestUtil.AssertException<OperationCanceledException>(() => engine.Execute("while true : foo = \"hello\" : wend"));
engine.ContinuationCallback = null;
Assert.AreEqual(Math.E * Math.PI, engine.Evaluate("e * pi"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_FileNameExtension()
{
Assert.AreEqual("vbs", engine.FileNameExtension);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable()
{
var host = new HostFunctions();
engine.Script.host = host;
Assert.AreSame(host, engine.Script.host);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Scalar()
{
const int value = 123;
engine.Script.value = value;
Assert.AreEqual(value, engine.Script.value);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Enum()
{
const DayOfWeek value = DayOfWeek.Wednesday;
engine.Script.value = value;
Assert.AreEqual(value, engine.Script.value);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Array()
{
// ReSharper disable ImplicitlyCapturedClosure
var lengths = new[] { 3, 5, 7 };
var formatParams = string.Join(", ", Enumerable.Range(0, lengths.Length).Select(position => "{" + position + "}"));
var hosts = Array.CreateInstance(typeof(object), lengths);
hosts.Iterate(indices => hosts.SetValue(new HostFunctions(), indices));
engine.Script.hostArray = hosts;
engine.Execute(MiscHelpers.FormatInvariant("dim hosts(" + formatParams + ")", lengths.Select(length => (object)(length - 1)).ToArray()));
hosts.Iterate(indices => engine.Execute(MiscHelpers.FormatInvariant("set hosts(" + formatParams + ") = hostArray.GetValue(" + formatParams + ")", indices.Select(index => (object)index).ToArray())));
hosts.Iterate(indices => Assert.AreSame(hosts.GetValue(indices), engine.Script.hosts.GetValue(indices)));
var result = engine.Script.hosts;
Assert.IsInstanceOfType(result, typeof(object[,,]));
var hostArray = (object[,,])result;
hosts.Iterate(indices => Assert.AreSame(hosts.GetValue(indices), hostArray.GetValue(indices)));
// ReSharper restore ImplicitlyCapturedClosure
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Struct()
{
var stamp = new DateTime(2007, 5, 22, 6, 15, 43);
engine.Script.stamp = stamp;
Assert.AreEqual(stamp, engine.Script.stamp);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Function()
{
engine.Execute("function test(x, y) : test = x * y : end function");
Assert.AreEqual(Math.E * Math.PI, engine.Script.test(Math.E, Math.PI));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Sub()
{
var callbackInvoked = false;
Action callback = () => callbackInvoked = true;
engine.Execute("sub test(x) : call x() : end sub");
engine.Script.test(callback);
Assert.IsTrue(callbackInvoked);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim host As New HostFunctions
engine.Script.host = host
Assert.AreSame(host, engine.Script.host)
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Scalar_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim value = 123
engine.Script.value = value
Assert.AreEqual(value, engine.Script.value)
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Enum_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim value = DayOfWeek.Wednesday
engine.Script.value = value
Assert.AreEqual(value, engine.Script.value)
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Array_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim lengths As Integer() = { 3, 5, 7 }
Dim formatParams = String.Join("", "", Enumerable.Range(0, lengths.Length).Select(Function(position) ""{"" & position & ""}""))
Dim hosts = Array.CreateInstance(GetType(Object), lengths)
TestUtil.Iterate(hosts, Sub(indices) hosts.SetValue(New HostFunctions, indices))
engine.Script.hostArray = hosts
engine.Execute(TestUtil.FormatInvariant(""dim hosts("" & formatParams & "")"", lengths.Select(Function(length) CType(length - 1, Object)).ToArray()))
TestUtil.Iterate(hosts, Sub(indices) engine.Execute(TestUtil.FormatInvariant(""set hosts("" & formatParams & "") = hostArray.GetValue("" & formatParams & "")"", indices.Select(Function(index) CType(index, Object)).ToArray())))
TestUtil.Iterate(hosts, Sub(indices) Assert.AreSame(hosts.GetValue(indices), engine.Script.hosts.GetValue(indices)))
Dim result = engine.Script.hosts
Assert.IsInstanceOfType(result, GetType(Object(,,)))
Dim hostArray As Object(,,) = result
TestUtil.Iterate(hosts, Sub(indices) Assert.AreSame(hosts.GetValue(indices), hostArray.GetValue(indices)))
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Variable_Struct_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim stamp = New DateTime(2007, 5, 22, 6, 15, 43)
engine.Script.stamp = stamp
Assert.AreEqual(stamp, engine.Script.stamp)
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Function_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
engine.Execute(""function test(x, y) : test = x * y : end function"")
Assert.AreEqual(Math.E * Math.PI, engine.Script.test(Math.E, Math.PI))
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_Script_Sub_VB()
{
TestUtil.InvokeVBTestSub(@"
Using engine As New VBScriptEngine
Dim callbackInvoked = False
Dim callback = Sub() callbackInvoked = True
engine.Execute(""sub test(x) : call x() : end sub"")
engine.Script.test(callback)
Assert.IsTrue(callbackInvoked)
End Using
");
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_CollectGarbage()
{
// VBScript doesn't support GC
engine.CollectGarbage(true);
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_General()
{
using (var console = new StringWriter())
{
var clr = new HostTypeCollection(type => type != typeof(Console), "mscorlib", "System", "System.Core");
clr.GetNamespaceNode("System").SetPropertyNoCheck("Console", console);
engine.AddHostObject("host", new ExtendedHostFunctions());
engine.AddHostObject("clr", clr);
engine.Execute(generalScript);
Assert.AreEqual(MiscHelpers.FormatCode(generalScriptOutput), console.ToString().Replace("\r\n", "\n"));
}
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ErrorHandling_ScriptError()
{
TestUtil.AssertException<ScriptEngineException>(() =>
{
try
{
engine.Execute("foo = {}; foo();");
}
catch (ScriptEngineException exception)
{
TestUtil.AssertValidException(engine, exception);
Assert.IsNull(exception.InnerException);
throw;
}
});
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ErrorHandling_HostException()
{
engine.AddHostObject("host", new HostFunctions());
TestUtil.AssertException<ScriptEngineException>(() =>
{
try
{
engine.Evaluate("host.proc(0)");
}
catch (ScriptEngineException exception)
{
TestUtil.AssertValidException(engine, exception);
Assert.IsNotNull(exception.InnerException);
var hostException = exception.InnerException;
Assert.IsTrue((hostException is RuntimeBinderException) || (hostException is MissingMethodException));
TestUtil.AssertValidException(hostException);
Assert.IsNull(hostException.InnerException);
Assert.AreEqual(hostException.Message, exception.Message);
throw;
}
});
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ErrorHandling_IgnoredHostException()
{
engine.AddHostObject("host", new HostFunctions());
TestUtil.AssertException<ScriptEngineException>(() =>
{
try
{
engine.Execute("on error resume next : host.newObj(null) : on error goto 0 : foo = \"foo\" : foo()");
}
catch (ScriptEngineException exception)
{
TestUtil.AssertValidException(engine, exception);
Assert.IsNull(exception.InnerException);
throw;
}
});
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ErrorHandling_NestedScriptError()
{
using (var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging))
{
engine.AddHostObject("engine", innerEngine);
TestUtil.AssertException<ScriptEngineException>(() =>
{
try
{
engine.Execute("engine.Execute(\"foo = {}; foo();\")");
}
catch (ScriptEngineException exception)
{
TestUtil.AssertValidException(engine, exception);
Assert.IsNotNull(exception.InnerException);
var hostException = exception.InnerException;
Assert.IsInstanceOfType(hostException, typeof(TargetInvocationException));
TestUtil.AssertValidException(hostException);
Assert.IsNotNull(hostException.InnerException);
var nestedException = hostException.InnerException as ScriptEngineException;
Assert.IsNotNull(nestedException);
// ReSharper disable once AccessToDisposedClosure
TestUtil.AssertValidException(innerEngine, nestedException);
Assert.IsNull(nestedException.InnerException);
Assert.AreEqual(hostException.GetBaseException().Message, exception.Message);
throw;
}
});
}
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_ErrorHandling_NestedHostException()
{
using (var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging))
{
innerEngine.AddHostObject("host", new HostFunctions());
engine.AddHostObject("engine", innerEngine);
TestUtil.AssertException<ScriptEngineException>(() =>
{
try
{
engine.Execute("engine.Evaluate(\"host.proc(0)\")");
}
catch (ScriptEngineException exception)
{
TestUtil.AssertValidException(engine, exception);
Assert.IsNotNull(exception.InnerException);
var hostException = exception.InnerException;
Assert.IsInstanceOfType(hostException, typeof(TargetInvocationException));
TestUtil.AssertValidException(hostException);
Assert.IsNotNull(hostException.InnerException);
var nestedException = hostException.InnerException as ScriptEngineException;
Assert.IsNotNull(nestedException);
// ReSharper disable once AccessToDisposedClosure
TestUtil.AssertValidException(innerEngine, nestedException);
Assert.IsNotNull(nestedException.InnerException);
var nestedHostException = nestedException.InnerException;
Assert.IsTrue((nestedHostException is RuntimeBinderException) || (nestedHostException is MissingMethodException));
TestUtil.AssertValidException(nestedHostException);
Assert.IsNull(nestedHostException.InnerException);
Assert.AreEqual(nestedHostException.GetBaseException().Message, nestedException.Message);
Assert.AreEqual(hostException.GetBaseException().Message, exception.Message);
throw;
}
});
}
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_CreateInstance()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.AreEqual("foo bar baz qux", engine.Evaluate("host.newObj(testObject, \"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_CreateInstance_Fail()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
TestUtil.AssertException<InvalidOperationException>(() => engine.Evaluate("host.newObj(testObject)"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Invoke()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("foo,bar,baz,qux", engine.Evaluate("testObject(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Invoke_Fail()
{
engine.Script.testObject = new DynamicTestObject();
TestUtil.AssertException<InvalidOperationException>(() => engine.Evaluate("testObject()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("foo-bar-baz-qux", engine.Evaluate("testObject.DynamicMethod(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_Fail()
{
engine.Script.testObject = new DynamicTestObject();
TestUtil.AssertException<MissingMemberException>(() => engine.Evaluate("testObject.DynamicMethod()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_FieldOverride()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("foo.bar.baz.qux", engine.Evaluate("testObject.SomeField(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_FieldOverride_Fail()
{
engine.Script.testObject = new DynamicTestObject();
TestUtil.AssertException<MissingMemberException>(() => engine.Evaluate("testObject.SomeField()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_PropertyOverride()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("foo:bar:baz:qux", engine.Evaluate("testObject.SomeProperty(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_PropertyOverride_Fail()
{
engine.Script.testObject = new DynamicTestObject();
TestUtil.AssertException<MissingMemberException>(() => engine.Evaluate("testObject.SomeProperty()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_DynamicOverload()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("foo;bar;baz;qux", engine.Evaluate("testObject.SomeMethod(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_NonDynamicOverload()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual(Math.PI, engine.Evaluate("testObject.SomeMethod()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_InvokeMethod_NonDynamic()
{
engine.Script.testObject = new DynamicTestObject();
Assert.AreEqual("Super Bass-O-Matic '76", engine.Evaluate("testObject.ToString()"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_StaticType_Field()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.IsInstanceOfType(engine.Evaluate("testObject.SomeField"), typeof(HostMethod));
Assert.AreEqual(12345, engine.Evaluate("host.toStaticType(testObject).SomeField"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_StaticType_Property()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.IsInstanceOfType(engine.Evaluate("testObject.SomeProperty"), typeof(HostMethod));
Assert.AreEqual("Bogus", engine.Evaluate("host.toStaticType(testObject).SomeProperty"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_StaticType_Method()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.AreEqual("bar+baz+qux", engine.Evaluate("host.toStaticType(testObject).SomeMethod(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_StaticType_Invoke()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
TestUtil.AssertException<NotSupportedException>(() => engine.Evaluate("host.toStaticType(testObject)(\"foo\", \"bar\", \"baz\", \"qux\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Property()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.IsInstanceOfType(engine.Evaluate("host.getProperty(testObject, \"foo\")"), typeof(Undefined));
Assert.AreEqual(123, engine.Evaluate("host.setProperty(testObject, \"foo\", clng(123))"));
Assert.AreEqual(123, engine.Evaluate("testObject.foo"));
Assert.IsTrue((bool)engine.Evaluate("host.removeProperty(testObject, \"foo\")"));
Assert.IsInstanceOfType(engine.Evaluate("host.getProperty(testObject, \"foo\")"), typeof(Undefined));
Assert.IsFalse((bool)engine.Evaluate("host.removeProperty(testObject, \"foo\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Property_Fail()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.IsInstanceOfType(engine.Evaluate("host.getProperty(testObject, \"Zfoo\")"), typeof(Undefined));
TestUtil.AssertException<InvalidOperationException>(() => engine.Evaluate("host.setProperty(testObject, \"Zfoo\", clng(123))"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Property_Invoke()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
engine.Execute("function secret(x) : secret = len(x) : end function");
Assert.IsInstanceOfType(engine.Evaluate("host.getProperty(testObject, \"foo\")"), typeof(Undefined));
Assert.IsInstanceOfType(engine.Evaluate("host.setProperty(testObject, \"foo\", GetRef(\"secret\"))"), typeof(DynamicObject));
Assert.AreEqual("floccinaucinihilipilification".Length, engine.Evaluate("testObject.foo(\"floccinaucinihilipilification\")"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_DynamicHostObject_Property_Invoke_Nested()
{
engine.Script.testObject = new DynamicTestObject();
engine.Script.host = new HostFunctions();
Assert.IsInstanceOfType(engine.Evaluate("host.getProperty(testObject, \"foo\")"), typeof(Undefined));
Assert.IsInstanceOfType(engine.Evaluate("host.setProperty(testObject, \"foo\", testObject)"), typeof(DynamicObject));