forked from nikhilk/scriptsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationTests.cs
More file actions
287 lines (229 loc) · 12.3 KB
/
Copy pathValidationTests.cs
File metadata and controls
287 lines (229 loc) · 12.3 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
// ValidationTests.cs
// Script#/Tests/ScriptSharp
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScriptSharp.Tests.Core;
namespace ScriptSharp.Tests {
[TestClass]
public sealed class ValidationTests : CompilationTest {
[TestMethod]
public void TestConflictingTypes() {
string expectedErrors =
"The type 'test.App' conflicts with another existing type with the same full name. " +
"This might be because a referenced assembly uses the same type, or you have multiple " +
"types with the same name across namespaces mapped to the same script namespace. ";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestCreateInstance() {
string expectedErrors =
"You must store the type returned from a method or property into a local variable to use with Type.CreateInstance. Code.cs(37, 25)" + Environment.NewLine +
"You must store the type returned from a method or property into a local variable to use with Type.CreateInstance. Code.cs(38, 25)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestExceptions() {
string expectedErrors =
"Try/Catch statements are limited to a single catch clause. Code.cs(12, 13)" + Environment.NewLine +
"Throw statements must specify an exception object. Code.cs(26, 17)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestImplicitEnums() {
string expectedErrors =
"Enumeration fields must have an explicit constant value specified. Code.cs(13, 9)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestInlineScript() {
string expectedErrors =
"The argument to Script.Literal must be a constant string. Code.cs(15, 28)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestKeywords() {
string expectedErrors =
"ss is a reserved word. Code.cs(12, 17)" + Environment.NewLine +
"instanceof is a reserved word. Code.cs(16, 17)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestNestedTypes() {
string expectedErrors =
"Only members are allowed inside types. Nested types are not supported. Code.cs(9, 5)" + Environment.NewLine +
"Only members are allowed inside types. Nested types are not supported. Code.cs(9, 5)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestOverloads() {
string expectedErrors =
"Extern methods used to declare alternate signatures should have a corresponding non-extern implementation as well. Code.cs(11, 9)" + Environment.NewLine +
"The implemenation method and associated alternate signature methods should have the same access type. Code.cs(14, 9)" + Environment.NewLine +
"The implemenation method and associated alternate signature methods should have the same access type. Code.cs(17, 9)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestProperties() {
string expectedErrors =
"Set-only properties are not supported. Use a set method instead. Code.cs(11, 9)";
Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
bool result = compilation.Execute();
Assert.IsFalse(result, "Expected compilation to fail.");
Assert.IsTrue(compilation.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation.ErrorMessages, expectedErrors) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
[TestMethod]
public void TestUnsupported() {
string expectedErrors1 =
"Type destructors are not supported. Code1.cs(14, 9)" + Environment.NewLine +
"Using statements are not supported. Code1.cs(18, 13)" + Environment.NewLine +
"Derived interface types are not supported. Code1.cs(26, 5)";
string expectedErrors2 =
"Check that your C# source compiles and that you are not using an unsupported feature. Common things to check for include use of fully-qualified names (use a using statement to import namespaces instead) or accessing private members of a type from a static member of the same type. Code2.cs(12, 13)";
Compilation compilation1 = CreateCompilation();
compilation1.AddSource("Code1.cs");
bool result1 = compilation1.Execute();
Assert.IsFalse(result1, "Expected compilation to fail.");
Assert.IsTrue(compilation1.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation1.ErrorMessages, expectedErrors1) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors1);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation1.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
Compilation compilation2 = CreateCompilation();
compilation2.AddSource("Code2.cs");
bool result2 = compilation2.Execute();
Assert.IsFalse(result2, "Expected compilation to fail.");
Assert.IsTrue(compilation2.HasErrors, "Expected compilation to fail with errors.");
if (String.CompareOrdinal(compilation2.ErrorMessages, expectedErrors2) != 0) {
Console.WriteLine("Expected Errors:");
Console.WriteLine(expectedErrors2);
Console.WriteLine();
Console.WriteLine("Actual Errors:");
Console.WriteLine(compilation2.ErrorMessages);
Console.WriteLine();
Assert.Fail("Unexpected errors.");
}
}
}
}