-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathInteropTests.cs
More file actions
601 lines (476 loc) · 14.2 KB
/
InteropTests.cs
File metadata and controls
601 lines (476 loc) · 14.2 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
using System;
using System.IO;
using Xunit;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Tests.Interop;
using JavaScriptEngineSwitcher.Tests.Interop.Animals;
using JavaScriptEngineSwitcher.Tests.Interop.Logging;
namespace JavaScriptEngineSwitcher.Tests.Yantra
{
public class InteropTests : InteropTestsBase
{
protected override string EngineName
{
get { return "YantraJsEngine"; }
}
#region Embedding of objects
#region Objects with fields
[Fact]
public override void EmbeddingOfInstanceOfCustomValueTypeWithFields()
{
// Arrange
var date = new Date(2015, 12, 29);
const string input1 = "date.Year";
const int targetOutput1 = 2015;
const string input2 = "date.Month";
const int targetOutput2 = 12;
const string input3 = "date.Day";
const int targetOutput3 = 29;
// Act
int output1;
int output2;
int output3;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostObject("date", date);
output1 = jsEngine.Evaluate<int>(input1);
output2 = jsEngine.Evaluate<int>(input2);
output3 = jsEngine.Evaluate<int>(input3);
}
// Assert
Assert.Equal(targetOutput1, output1);
Assert.Equal(targetOutput2, output2);
Assert.Equal(targetOutput3, output3);
}
[Fact]
public override void EmbeddingOfInstanceOfCustomValueTypeWithReadonlyField()
{
// Arrange
var age = new Age(1979);
const string updateCode = "age.Year = 1982;";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
jsEngine.EmbedHostObject("age", age);
jsEngine.Execute(updateCode);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.Equal("Cannot modify property Year of 47 which has only a getter", exception.Description);
}
[Fact]
public override void EmbeddingOfInstanceOfCustomReferenceTypeWithFields()
{
// Arrange
var product = new Product
{
Name = "Red T-shirt",
Description = string.Empty,
Price = 995.00
};
const string updateCode = @"product.Description = '';
product.Price *= 1.15;";
const string input1 = "product.Name";
const string targetOutput1 = "Red T-shirt";
const string input2 = "product.Price";
const double targetOutput2 = 1144.25;
// Act
string output1;
double output2;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostObject("product", product);
jsEngine.Execute(updateCode);
output1 = jsEngine.Evaluate<string>(input1);
output2 = jsEngine.Evaluate<double>(input2);
}
// Assert
Assert.Equal(targetOutput1, output1);
Assert.Equal(targetOutput2, output2);
}
#endregion
#region Objects with properties
[Fact]
public override void EmbeddingOfInstanceOfBuiltinValueTypeWithProperties()
{ }
[Fact]
public override void EmbeddingOfInstanceOfCustomValueTypeWithProperties()
{ }
[Fact]
public override void EmbeddingOfInstanceOfCustomReferenceTypeWithProperties()
{
// Arrange
var person = new Person("Vanya", "Ivanov");
const string updateCode = @"person.LastName = person.LastName.substr(0, 5) + 'ff';
person.Patronymic = '';";
const string input1 = "person.FirstName";
const string targetOutput1 = "Vanya";
const string input2 = "person.LastName";
const string targetOutput2 = "Ivanoff";
// Act
string output1;
string output2;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostObject("person", person);
jsEngine.Execute(updateCode);
output1 = jsEngine.Evaluate<string>(input1);
output2 = jsEngine.Evaluate<string>(input2);
}
// Assert
Assert.Equal(targetOutput1, output1);
Assert.Equal(targetOutput2, output2);
}
#endregion
#region Objects with methods
[Fact]
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
{
// Arrange
var date = new Date();
const string input = "date.GetType();";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
jsEngine.EmbedHostObject("date", date);
jsEngine.Evaluate<string>(input);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.Equal("Method GetType not found in JavaScriptEngineSwitcher.Tests.Interop.Date", exception.Description);
}
[Fact]
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
{
// Arrange
var cat = new Cat();
const string input = @"cat.GetType();";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
jsEngine.EmbedHostObject("cat", cat);
jsEngine.Evaluate<string>(input);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.Equal("Method GetType not found in JavaScriptEngineSwitcher.Tests.Interop.Animals.Cat", exception.Description);
}
#endregion
#region Recursive calls
#region Mapping of errors
[Fact]
public void MappingCompilationErrorDuringRecursiveEvaluationOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/compilation-error";
const string input = "evaluateFile('index').calculateResult();";
const double targetOutput = 132;
// Act
double output;
using (var jsEngine = CreateJsEngine())
{
Func<string, object> evaluateFile = path => {
string absolutePath = Path.Combine(directoryPath, $"{path}.js");
string code = File.ReadAllText(absolutePath);
object result = jsEngine.Evaluate(code, absolutePath);
return result;
};
jsEngine.EmbedHostObject("evaluateFile", evaluateFile);
output = jsEngine.Evaluate<double>(input);
}
// Assert
Assert.Equal(targetOutput, output);
}
[Fact]
public void MappingRuntimeErrorDuringRecursiveEvaluationOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/runtime-error";
const string input = "evaluateFile('index').calculateResult();";
const double targetOutput = double.NaN;
// Act
double output;
using (var jsEngine = CreateJsEngine())
{
Func<string, object> evaluateFile = path => {
string absolutePath = Path.Combine(directoryPath, $"{path}.js");
string code = File.ReadAllText(absolutePath);
object result = jsEngine.Evaluate(code, absolutePath);
return result;
};
jsEngine.EmbedHostObject("evaluateFile", evaluateFile);
output = jsEngine.Evaluate<double>(input);
}
// Assert
Assert.Equal(targetOutput, output);
}
[Fact]
public void MappingHostErrorDuringRecursiveEvaluationOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/host-error";
const string input = "evaluateFile('index').calculateResult();";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
Func<string, object> evaluateFile = path => {
string absolutePath = Path.Combine(directoryPath, $"{path}.js");
string code = File.ReadAllText(absolutePath);
object result = jsEngine.Evaluate(code, absolutePath);
return result;
};
jsEngine.EmbedHostObject("evaluateFile", evaluateFile);
double output = jsEngine.Evaluate<double>(input);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.StartsWith("System.IO.FileNotFoundException: Could not find file ", exception.Description);
Assert.Equal("Error", exception.Type);
Assert.Equal("index.js", exception.DocumentName);
Assert.Equal(6, exception.LineNumber);
Assert.Equal(2, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
Assert.Equal(
" at calculateResult (index.js:6:2)" + Environment.NewLine +
" at Global code (Script Document:1:1)",
exception.CallStack
);
}
[Fact]
public void MappingCompilationErrorDuringRecursiveExecutionOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-execution/compilation-error";
const string variableName = "num";
// Act
JsCompilationException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
Action<string> executeFile = path => jsEngine.ExecuteFile(path);
jsEngine.SetVariableValue("directoryPath", directoryPath);
jsEngine.EmbedHostObject("executeFile", executeFile);
jsEngine.ExecuteFile(Path.Combine(directoryPath, "main-file.js"));
int output = jsEngine.GetVariableValue<int>(variableName);
}
catch (JsCompilationException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Compilation error", exception.Category);
Assert.Equal("Unexpected token Hash: #", exception.Description);
Assert.Equal("SyntaxError", exception.Type);
Assert.Equal("second-file.js", exception.DocumentName);
Assert.Equal(1, exception.LineNumber);
Assert.Equal(6, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
}
[Fact]
public void MappingRuntimeErrorDuringRecursiveExecutionOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-execution/runtime-error";
const string variableName = "num";
const int targetOutput = 15;
// Act
int output;
using (var jsEngine = CreateJsEngine())
{
Action<string> executeFile = path => jsEngine.ExecuteFile(path);
jsEngine.SetVariableValue("directoryPath", directoryPath);
jsEngine.EmbedHostObject("executeFile", executeFile);
jsEngine.ExecuteFile(Path.Combine(directoryPath, "main-file.js"));
output = jsEngine.GetVariableValue<int>(variableName);
}
// Assert
Assert.Equal(targetOutput, output);
}
[Fact]
public void MappingHostErrorDuringRecursiveExecutionOfFiles()
{
// Arrange
const string directoryPath = "Files/recursive-execution/host-error";
const string variableName = "num";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
Action<string> executeFile = path => jsEngine.ExecuteFile(path);
jsEngine.SetVariableValue("directoryPath", directoryPath);
jsEngine.EmbedHostObject("executeFile", executeFile);
jsEngine.ExecuteFile(Path.Combine(directoryPath, "main-file.js"));
int output = jsEngine.GetVariableValue<int>(variableName);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.StartsWith("System.IO.FileNotFoundException: File ", exception.Description);
Assert.Equal("Error", exception.Type);
Assert.Equal("first-file.js", exception.DocumentName);
Assert.Equal(2, exception.LineNumber);
Assert.Equal(0, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
Assert.Equal(
" at Global code (first-file.js:2)" + Environment.NewLine +
" at Global code (main-file.js:2)",
exception.CallStack
);
}
#endregion
#endregion
#endregion
#region Embedding of types
#region Creating of instances
[Fact]
public override void CreatingAnInstanceOfEmbeddedBuiltinValueType()
{ }
[Fact]
public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
{
// Arrange
Type loginFailedExceptionType = typeof(LoginFailedException);
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
string targetOutput = "function LoginFailedException() { [clr-native] }";
// Act
string output;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
output = jsEngine.Evaluate<string>(input);
}
// Assert
Assert.Equal(targetOutput, output);
}
#endregion
#region Types with fields
[Fact]
public override void EmbeddingOfCustomReferenceTypeWithField()
{
// Arrange
Type defaultLoggerType = typeof(DefaultLogger);
const string input = "DefaultLogger.Current.ToString()";
const string targetOutput = "[null logger]";
// Act
string output;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostType("DefaultLogger", defaultLoggerType);
lock (DefaultLogger.SyncRoot)
{
output = jsEngine.Evaluate<string>(input);
}
}
// Assert
Assert.Equal(targetOutput, output);
}
[Fact]
public override void EmbeddingOfCustomReferenceTypeWithReadonlyFields()
{
// Arrange
Type runtimeConstantsType = typeof(RuntimeConstants);
const string updateCode = @"RuntimeConstants.MinValue = 1;
RuntimeConstants.MaxValue = 100;";
// Act
JsRuntimeException exception = null;
using (var jsEngine = CreateJsEngine())
{
try
{
jsEngine.EmbedHostType("RuntimeConstants", runtimeConstantsType);
lock (RuntimeConstants.SyncRoot)
{
jsEngine.Execute(updateCode);
}
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.Equal(
"Cannot modify property MinValue of function RuntimeConstants() { [clr-native] }" +
" which has only a getter",
exception.Description
);
}
#endregion
#region Types with methods
[Fact]
public override void EmbeddingOfBuiltinReferenceTypeWithMethods()
{
// Arrange
Type mathType = typeof(Math);
const string input1 = "Math2.Max(5.37, 5.56)";
const double targetOutput1 = 5;
const string input2 = "Math2.Log10(23)";
const double targetOutput2 = 1.36172783601759;
// Act
double output1;
double output2;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostType("Math2", mathType);
output1 = jsEngine.Evaluate<double>(input1);
output2 = Math.Round(jsEngine.Evaluate<double>(input2), 14);
}
// Assert
Assert.Equal(targetOutput1, output1);
Assert.Equal(targetOutput2, output2);
}
[Fact]
public override void EmbeddingOfTypeAndCallingOfItsGetTypeMethod()
{ }
#endregion
#endregion
}
}