forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonTestsBase.cs
More file actions
627 lines (497 loc) · 14.4 KB
/
CommonTestsBase.cs
File metadata and controls
627 lines (497 loc) · 14.4 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
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
namespace JavaScriptEngineSwitcher.Tests
{
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
using Core;
public abstract class CommonTestsBase
{
protected IJsEngine _jsEngine;
[TestFixtureSetUp]
public abstract void SetUp();
#region Evaluation of code
[Test]
public virtual void EvaluationOfExpressionWithUndefinedResultIsCorrect()
{
// Arrange
const string input = "undefined";
var targetOutput = Undefined.Value;
// Act
var output = _jsEngine.Evaluate(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void EvaluationOfExpressionWithNullResultIsCorrect()
{
// Arrange
const string input = "null";
const object targetOutput = null;
// Act
var output = _jsEngine.Evaluate(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void EvaluationOfExpressionWithBooleanResultIsCorrect()
{
// Arrange
const string input1 = "7 > 5";
const bool targetOutput1 = true;
const string input2 = "null === undefined";
const bool targetOutput2 = false;
// Act
var output1 = _jsEngine.Evaluate<bool>(input1);
var output2 = _jsEngine.Evaluate<bool>(input2);
// Assert
Assert.AreEqual(targetOutput1, output1);
Assert.AreEqual(targetOutput2, output2);
}
[Test]
public virtual void EvaluationOfExpressionWithIntegerResultIsCorrect()
{
// Arrange
const string input = "7 * 8 - 20";
const int targetOutput = 36;
// Act
var output = _jsEngine.Evaluate<int>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void EvaluationOfExpressionWithDoubleResultIsCorrect()
{
// Arrange
const string input = "Math.PI + 0.22";
const double targetOutput = 3.36;
// Act
var output = Math.Round(_jsEngine.Evaluate<double>(input), 2);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void EvaluationOfExpressionWithStringResultIsCorrect()
{
// Arrange
const string input = "'Hello, ' + \"Vasya\" + '?';";
const string targetOutput = "Hello, Vasya?";
// Act
var output = _jsEngine.Evaluate<string>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
#endregion
#region Execution of code
[Test]
public virtual void ExecutionOfCodeIsCorrect()
{
// Arrange
const string functionCode = @"function add(num1, num2) {
return (num1 + num2);
}";
const string input = "add(7, 9);";
const int targetOutput = 16;
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.Evaluate<int>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void ExecutionOfFileIsCorrect()
{
// Arrange
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../Resources/square.js");
const string input = "square(6);";
const int targetOutput = 36;
// Act
_jsEngine.ExecuteFile(filePath);
var output = _jsEngine.Evaluate<int>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void ExecutionOfResourceByTypeIsCorrect()
{
// Arrange
const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.cube.js";
const string input = "cube(5);";
const int targetOutput = 125;
// Act
_jsEngine.ExecuteResource(resourceName, GetType());
var output = _jsEngine.Evaluate<int>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void ExecutionOfResourceByAssemblyIsCorrect()
{
// Arrange
const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.power.js";
const string input = "power(4, 3);";
const int targetOutput = 64;
// Act
_jsEngine.ExecuteResource(resourceName, Assembly.GetExecutingAssembly());
var output = _jsEngine.Evaluate<int>(input);
// Assert
Assert.AreEqual(targetOutput, output);
}
#endregion
#region Calling of functions
[Test]
public void CallingOfFunctionWithoutParametersIsCorrect()
{
// Arrange
const string functionCode = @"function hooray() {
return 'Hooray!';
}";
const string targetOutput = "Hooray!";
// Act
_jsEngine.Execute(functionCode);
var output = (string)_jsEngine.CallFunction("hooray");
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void CallingOfFunctionWithUndefinedResultIsCorrect()
{
// Arrange
const string functionCode = @"function testUndefined(value) {
if (typeof value !== 'undefined') {
throw new TypeError();
}
return undefined;
}";
object input = Undefined.Value;
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction("testUndefined", input);
// Assert
Assert.AreEqual(input, output);
}
[Test]
public virtual void CallingOfFunctionWithNullResultIsCorrect()
{
// Arrange
const string functionCode = @"function testNull(value) {
if (value !== null) {
throw new TypeError();
}
return null;
}";
const object input = null;
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction("testNull", input);
// Assert
Assert.AreEqual(input, output);
}
[Test]
public virtual void CallingOfFunctionWithBooleanResultIsCorrect()
{
// Arrange
const string functionCode = @"function inverse(value) {
return !value;
}";
const bool input = false;
const bool targetOutput = true;
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<bool>("inverse", input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void CallingOfFunctionWithIntegerResultIsCorrect()
{
// Arrange
const string functionCode = @"function negate(value) {
return -1 * value;
}";
const int input = 28;
const int targetOutput = -28;
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<int>("negate", input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void CallingOfFunctionWithDoubleResultIsCorrect()
{
// Arrange
const string functionCode = @"function triple(value) {
return 3 * value;
}";
const double input = 3.2;
const double targetOutput = 9.6;
// Act
_jsEngine.Execute(functionCode);
var output = Math.Round(_jsEngine.CallFunction<double>("triple", input), 1);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void CallingOfFunctionWithStringResultIsCorrect()
{
// Arrange
const string functionCode = @"function greeting(name) {
return 'Hello, ' + name + '!';
}";
const string input = "Vovan";
const string targetOutput = "Hello, Vovan!";
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<string>("greeting", input);
// Assert
Assert.AreEqual(targetOutput, output);
}
[Test]
public virtual void CallingOfFunctionWithManyParametersIsCorrect()
{
// Arrange
const string functionCode = @"function determineArgumentsTypes() {
var result = '',
argumentIndex,
argumentCount = arguments.length
;
for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
if (argumentIndex > 0) {
result += ', ';
}
result += typeof arguments[argumentIndex];
}
return result;
}";
// Act
_jsEngine.Execute(functionCode);
var output = (string)_jsEngine.CallFunction("determineArgumentsTypes", Undefined.Value, null,
true, 12, 3.14, "test");
// Assert
Assert.AreEqual("undefined, object, boolean, number, number, string", output);
}
[Test]
public virtual void CallingOfFunctionWithManyParametersAndBooleanResultIsCorrect()
{
// Arrange
const string functionCode = @"function and() {
var result = null,
argumentIndex,
argumentCount = arguments.length,
argumentValue
;
for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
argumentValue = arguments[argumentIndex];
if (result !== null) {
result = result && argumentValue;
}
else {
result = argumentValue;
}
}
return result;
}";
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<bool>("and", true, true, false, true);
// Assert
Assert.AreEqual(false, output);
}
[Test]
public virtual void CallingOfFunctionWithManyParametersAndIntegerResultIsCorrect()
{
// Arrange
const string functionCode = @"function sum() {
var result = 0,
argumentIndex,
argumentCount = arguments.length
;
for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
result += arguments[argumentIndex];
}
return result;
}";
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<int>("sum", 120, 5, 18, 63);
// Assert
Assert.AreEqual(206, output);
}
[Test]
public virtual void CallingOfFunctionWithManyParametersAndDoubleResultIsCorrect()
{
// Arrange
const string functionCode = @"function sum() {
var result = 0,
argumentIndex,
argumentCount = arguments.length
;
for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
result += arguments[argumentIndex];
}
return result;
}";
// Act
_jsEngine.Execute(functionCode);
var output = Math.Round(_jsEngine.CallFunction<double>("sum", 22000, 8.5, 0.05, 3), 2);
// Assert
Assert.AreEqual(22011.55, output);
}
[Test]
public virtual void CallingOfFunctionWithManyParametersAndStringResultIsCorrect()
{
// Arrange
const string functionCode = @"function concatenate() {
var result = '',
argumentIndex,
argumentCount = arguments.length
;
for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
result += arguments[argumentIndex];
}
return result;
}";
// Act
_jsEngine.Execute(functionCode);
var output = _jsEngine.CallFunction<string>("concatenate", "Hello", ",", " ", "Petya", "!");
// Assert
Assert.AreEqual("Hello, Petya!", output);
}
#endregion
#region Getting, setting and removing variables
[Test]
public virtual void SettingAndGettingVariableWithUndefinedValueIsCorrect()
{
// Arrange
const string variableName = "myVar1";
object input = Undefined.Value;
// Act
_jsEngine.SetVariableValue(variableName, input);
bool variableExists = _jsEngine.HasVariable(variableName);
var output = _jsEngine.GetVariableValue(variableName);
// Assert
Assert.IsFalse(variableExists);
Assert.AreEqual(input, output);
}
[Test]
public virtual void SettingAndGettingVariableWithNullValueIsCorrect()
{
// Arrange
const string variableName = "myVar2";
const object input = null;
// Act
_jsEngine.SetVariableValue(variableName, input);
bool variableExists = _jsEngine.HasVariable(variableName);
var output = _jsEngine.GetVariableValue(variableName);
// Assert
Assert.IsTrue(variableExists);
Assert.AreEqual(input, output);
}
[Test]
public virtual void SettingAndGettingVariableWithBooleanValueIsCorrect()
{
// Arrange
const string variableName = "isVisible";
const bool input1 = true;
const bool targetOutput1 = false;
const bool input2 = true;
// Act
_jsEngine.SetVariableValue(variableName, input1);
bool variableExists = _jsEngine.HasVariable(variableName);
_jsEngine.Execute(string.Format("{0} = !{0};", variableName));
var output1 = _jsEngine.GetVariableValue<bool>(variableName);
_jsEngine.SetVariableValue(variableName, input2);
var output2 = _jsEngine.GetVariableValue<bool>(variableName);
// Assert
Assert.IsTrue(variableExists);
Assert.AreEqual(targetOutput1, output1);
Assert.AreEqual(input2, output2);
}
[Test]
public virtual void SettingAndGettingVariableWithIntegerValueIsCorrect()
{
// Arrange
const string variableName = "amount";
const int input1 = 38;
const int targetOutput1 = 41;
const int input2 = 711;
// Act
_jsEngine.SetVariableValue(variableName, input1);
bool variableExists = _jsEngine.HasVariable(variableName);
_jsEngine.Execute(string.Format("{0} += 3;", variableName));
var output1 = _jsEngine.GetVariableValue<int>(variableName);
_jsEngine.SetVariableValue(variableName, input2);
var output2 = _jsEngine.GetVariableValue<int>(variableName);
// Assert
Assert.IsTrue(variableExists);
Assert.AreEqual(targetOutput1, output1);
Assert.AreEqual(input2, output2);
}
[Test]
public virtual void SettingAndGettingVariableWithDoubleValueIsCorrect()
{
// Arrange
const string variableName = "price";
const double input1 = 2.20;
const double targetOutput1 = 2.17;
const double input2 = 3.50;
// Act
_jsEngine.SetVariableValue(variableName, input1);
bool variableExists = _jsEngine.HasVariable(variableName);
_jsEngine.Execute(string.Format("{0} -= 0.03;", variableName));
var output1 = Math.Round(_jsEngine.GetVariableValue<double>(variableName), 2);
_jsEngine.SetVariableValue(variableName, input2);
var output2 = Math.Round(_jsEngine.GetVariableValue<double>(variableName), 2);
// Assert
Assert.IsTrue(variableExists);
Assert.AreEqual(targetOutput1, output1);
Assert.AreEqual(input2, output2);
}
[Test]
public virtual void SettingAndGettingVariableWithStringValueIsCorrect()
{
// Arrange
const string variableName = "word";
const string input1 = "Hooray";
const string targetOutput1 = "Hooray!";
const string input2 = "Hurrah";
// Act
_jsEngine.SetVariableValue(variableName, input1);
bool variableExists = _jsEngine.HasVariable(variableName);
_jsEngine.Execute(string.Format("{0} += '!';", variableName));
var output1 = _jsEngine.GetVariableValue<string>(variableName);
_jsEngine.SetVariableValue(variableName, input2);
var output2 = _jsEngine.GetVariableValue<string>(variableName);
// Assert
Assert.IsTrue(variableExists);
Assert.AreEqual(targetOutput1, output1);
Assert.AreEqual(input2, output2);
}
[Test]
public virtual void RemovingVariableIsCorrect()
{
// Arrange
const string variableName = "price";
const double input = 120.55;
// Act
_jsEngine.SetVariableValue(variableName, input);
bool variableBeforeRemovingExists = _jsEngine.HasVariable(variableName);
_jsEngine.RemoveVariable(variableName);
bool variableAfterRemovingExists = _jsEngine.HasVariable(variableName);
// Assert
Assert.IsTrue(variableBeforeRemovingExists);
Assert.IsFalse(variableAfterRemovingExists);
}
#endregion
[TestFixtureTearDown]
public virtual void TearDown()
{
if (_jsEngine != null)
{
_jsEngine.Dispose();
_jsEngine = null;
}
}
}
}