forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecompilationTests.cs
More file actions
210 lines (174 loc) · 4.79 KB
/
PrecompilationTests.cs
File metadata and controls
210 lines (174 loc) · 4.79 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
using System;
using Xunit;
using JavaScriptEngineSwitcher.Core;
namespace JavaScriptEngineSwitcher.Tests.V8
{
public class PrecompilationTests : PrecompilationTestsBase
{
protected override string EngineName
{
get { return "V8JsEngine"; }
}
#region Error handling
#region Mapping of errors
[Fact]
public void MappingCompilationErrorDuringPrecompilationOfCode()
{
// Arrange
const string input = @"function guid() {
function s4() {
return Math.floor((1 + Math.random() * 0x10000)
.toString(16)
.substring(1)
;
}
var result = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
return result;
}";
IPrecompiledScript precompiledScript = null;
JsCompilationException exception = null;
// Act
using (var jsEngine = CreateJsEngine())
{
try
{
precompiledScript = jsEngine.Precompile(input, "guid.js");
}
catch (JsCompilationException e)
{
exception = e;
}
}
// Assert
Assert.Null(precompiledScript);
Assert.NotNull(exception);
Assert.Equal("Compilation error", exception.Category);
Assert.Equal("missing ) after argument list", exception.Description);
Assert.Equal("SyntaxError", exception.Type);
Assert.Equal("guid.js", exception.DocumentName);
Assert.Equal(5, exception.LineNumber);
Assert.Equal(16, exception.ColumnNumber);
Assert.Equal(" .substring(1)", exception.SourceFragment);
}
[Fact]
public void MappingRuntimeErrorDuringExecutionOfPrecompiledCode()
{
// Arrange
const string input = @"function getItem(items, itemIndex) {
var item = items[itemIndex];
return item;
}
(function (getItem) {
var items = null,
item = getItem(items, 5)
;
return item;
})(getItem);";
JsRuntimeException exception = null;
// Act
using (var jsEngine = CreateJsEngine())
{
try
{
IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-item.js");
jsEngine.Execute(precompiledScript);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
// Assert
Assert.NotNull(exception);
Assert.Equal("Runtime error", exception.Category);
Assert.Equal("Cannot read properties of null (reading '5')", exception.Description);
Assert.Equal("TypeError", exception.Type);
Assert.Equal("get-item.js", exception.DocumentName);
Assert.Equal(2, exception.LineNumber);
Assert.Equal(18, exception.ColumnNumber);
Assert.Equal(" var item = items[itemIndex];", exception.SourceFragment);
Assert.Equal(
" at getItem (get-item.js:2:18)" + Environment.NewLine +
" at get-item.js:9:10" + Environment.NewLine +
" at get-item.js:13:3",
exception.CallStack
);
}
#endregion
#region Generation of error messages
[Fact]
public void GenerationOfCompilationErrorMessage()
{
// Arrange
const string input = @"function makeId(length) {
var result = '',
possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
charIndex
;
for (charIndex = 0; charIndex < length; charIndex++)
result += possible.charAt(Math.floor(Math.random() * possible.length));
}
return result;
}";
string targetOutput = "SyntaxError: Illegal return statement" + Environment.NewLine +
" at make-id.js:11:2 -> return result;"
;
IPrecompiledScript precompiledScript = null;
JsCompilationException exception = null;
// Act
using (var jsEngine = CreateJsEngine())
{
try
{
precompiledScript = jsEngine.Precompile(input, "make-id.js");
}
catch (JsCompilationException e)
{
exception = e;
}
}
Assert.Null(precompiledScript);
Assert.NotNull(exception);
Assert.Equal(targetOutput, exception.Message);
}
[Fact]
public void GenerationOfRuntimeErrorMessage()
{
// Arrange
const string input = @"function getFullName(firstName, lastName) {
var fullName = firstName + ' ' + middleName + ' ' + lastName;
return fullName;
}
(function (getFullName) {
var firstName = 'Vasya',
lastName = 'Pupkin'
;
return getFullName(firstName, lastName);
})(getFullName);";
string targetOutput = "ReferenceError: middleName is not defined" + Environment.NewLine +
" at getFullName (get-full-name.js:2:35) -> var fullName = firstName + " +
"' ' + middleName + ' ' + lastName;" + Environment.NewLine +
" at get-full-name.js:12:9" + Environment.NewLine +
" at get-full-name.js:13:3"
;
JsRuntimeException exception = null;
// Act
using (var jsEngine = CreateJsEngine())
{
try
{
IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-full-name.js");
jsEngine.Execute(precompiledScript);
}
catch (JsRuntimeException e)
{
exception = e;
}
}
Assert.NotNull(exception);
Assert.Equal(targetOutput, exception.Message);
}
#endregion
#endregion
}
}