forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteropTests.cs
More file actions
383 lines (308 loc) · 9.45 KB
/
InteropTests.cs
File metadata and controls
383 lines (308 loc) · 9.45 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
#if !NET452
using System;
using System.IO;
using Xunit;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Tests.Interop;
namespace JavaScriptEngineSwitcher.Tests.Jint
{
public class InteropTests : InteropTestsBase
{
protected override string EngineName
{
get { return "JintJsEngine"; }
}
#region Embedding of objects
#region Objects with fields
[Fact]
public override void EmbeddingOfInstanceOfCustomReferenceTypeWithFieldsIsCorrect()
{
// Arrange
var product = new Product
{
Name = "Red T-shirt",
Description = string.Empty,
Price = 995.00
};
const string updateCode = "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 Delegates
[Fact]
public override void CallingOfEmbeddedDelegateWithMissingParameter()
{
// Arrange
var sumFunc = new Func<int, int, int>((a, b) => a + b);
const string input = "sum(678)";
const int targetOutput = 678;
// Act
int output;
using (var jsEngine = CreateJsEngine())
{
jsEngine.EmbedHostObject("sum", sumFunc);
output = jsEngine.Evaluate<int>(input);
}
// Assert
Assert.Equal(targetOutput, output);
}
#endregion
#region Recursive calls
#region Mapping of errors
[Fact]
public void MappingCompilationErrorDuringRecursiveEvaluationOfFilesIsCorrect()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/compilation-error";
const string input = "evaluateFile('index').calculateResult();";
// Act
JsCompilationException 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 (JsCompilationException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Compilation error", exception.Category);
Assert.Equal("Unexpected token ,", exception.Description);
Assert.Equal("SyntaxError", exception.Type);
Assert.Equal("math.js", exception.DocumentName);
Assert.Equal(25, exception.LineNumber);
Assert.Equal(11, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
}
[Fact]
public void MappingRuntimeErrorDuringRecursiveEvaluationOfFilesIsCorrect()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/runtime-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.Equal("argumens is not defined", exception.Description);
Assert.Equal("ReferenceError", exception.Type);
Assert.Equal("math.js", exception.DocumentName);
Assert.Equal(10, exception.LineNumber);
Assert.Equal(4, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
Assert.Equal(
" at sum (math.js:10:14)" + Environment.NewLine +
" at calculateResult (index.js:7:13)" + Environment.NewLine +
" at Global code (Script Document:1:1)",
exception.CallStack
);
}
[Fact]
public void MappingHostErrorDuringRecursiveEvaluationOfFilesIsCorrect()
{
// Arrange
const string directoryPath = "Files/recursive-evaluation/host-error";
const string input = "evaluateFile('index').calculateResult();";
// Act
FileNotFoundException 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 (FileNotFoundException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.StartsWith("Could not find file '", exception.Message);
}
[Fact]
public void MappingCompilationErrorDuringRecursiveExecutionOfFilesIsCorrect()
{
// 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 number", exception.Description);
Assert.Equal("SyntaxError", exception.Type);
Assert.Equal("second-file.js", exception.DocumentName);
Assert.Equal(1, exception.LineNumber);
Assert.Equal(8, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
}
[Fact]
public void MappingRuntimeErrorDuringRecursiveExecutionOfFilesIsCorrect()
{
// Arrange
const string directoryPath = "Files/recursive-execution/runtime-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.Equal("nuм is not defined", exception.Description);
Assert.Equal("ReferenceError", exception.Type);
Assert.Equal("second-file.js", exception.DocumentName);
Assert.Equal(1, exception.LineNumber);
Assert.Equal(1, exception.ColumnNumber);
Assert.Empty(exception.SourceFragment);
Assert.Equal(
" at Global code (second-file.js:1:1)" + Environment.NewLine +
" at Global code (first-file.js:2:1)" + Environment.NewLine +
" at Global code (main-file.js:2:1)",
exception.CallStack
);
}
[Fact]
public void MappingHostErrorDuringRecursiveExecutionOfFilesIsCorrect()
{
// Arrange
const string directoryPath = "Files/recursive-execution/host-error";
const string variableName = "num";
// Act
FileNotFoundException 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 (FileNotFoundException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("File '" + directoryPath + "/second-file.jsx' not exist.", exception.Message);
}
#endregion
#endregion
#endregion
#region Embedding of types
#region Types with methods
#if NET471
[Fact]
public override void EmbeddingOfBuiltinReferenceTypeWithMethodsIsCorrect()
{
// Arrange
Type mathType = typeof(Math);
const string input1 = "Math2.Max(5.37, 5.56)";
const double targetOutput1 = 5.56;
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 = Math.Round(jsEngine.Evaluate<double>(input1), 2);
output2 = Math.Round(jsEngine.Evaluate<double>(input2), 14);
}
// Assert
Assert.Equal(targetOutput1, output1);
Assert.Equal(targetOutput2, output2);
}
#endif
#endregion
#endregion
}
}
#endif