-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTestFixture.cs
More file actions
598 lines (512 loc) · 25 KB
/
Copy pathGenericTestFixture.cs
File metadata and controls
598 lines (512 loc) · 25 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using JSIL.Compiler.Extensibility;
using JSIL.Internal;
using JSIL.Translator;
using Microsoft.Win32;
using NUnit.Framework;
namespace JSIL.Tests {
public class GenericTestFixture : IDisposable {
protected TypeInfoProvider DefaultTypeInfoProvider;
protected string NameSuffix = "";
public EvaluatorPool EvaluatorPool {
get;
private set;
}
protected virtual Dictionary<string, string> SetupEvaluatorEnvironment () {
return null;
}
protected virtual string JSShellOptions {
get {
return "";
}
}
[TestFixtureSetUp]
public void FixtureSetUp () {
var setupCodePath =
EvaluatorPool = new EvaluatorPool(
ComparisonTest.JSShellPath,
JSShellOptions,
(e) =>
{
var initCode =
ComparisonTest.EvaluatorSetupCode +
Environment.NewLine +
// When we'll find option to read environment variables in SpiderMonkey, delete this.
ComparisonTest.EvaluatorPrepareEnvironmentCode(SetupEvaluatorEnvironment()) +
Environment.NewLine +
ComparisonTest.EvaluatorRunCode;
e.WriteInput(initCode);
},
SetupEvaluatorEnvironment()
);
}
[TestFixtureTearDown]
public void FixtureTearDown () {
Dispose();
}
public void Dispose () {
if (EvaluatorPool != null) {
EvaluatorPool.Dispose();
EvaluatorPool = null;
}
if (DefaultTypeInfoProvider != null) {
DefaultTypeInfoProvider.Dispose();
DefaultTypeInfoProvider = null;
}
}
protected ComparisonTest MakeTest (
string filename, string[] stubbedAssemblies = null,
TypeInfoProvider typeInfo = null,
AssemblyCache assemblyCache = null
) {
return new ComparisonTest(
EvaluatorPool,
Portability.NormalizeDirectorySeparators(filename), stubbedAssemblies,
typeInfo, assemblyCache
);
}
protected virtual Configuration MakeConfiguration () {
return ComparisonTest.MakeDefaultConfiguration();
}
protected TypeInfoProvider MakeDefaultProvider () {
if (DefaultTypeInfoProvider == null)
// Construct a type info provider with default proxies loaded (kind of a hack)
DefaultTypeInfoProvider = (new AssemblyTranslator(MakeConfiguration())).GetTypeInfoProvider();
return DefaultTypeInfoProvider.Clone();
}
/// <summary>
/// Runs one or more comparison tests by compiling the source C# or VB.net file,
/// running the compiled test method, translating the compiled test method to JS,
/// then running the translated JS and comparing the outputs.
/// </summary>
/// <param name="filenames">The path to one or more test files. If a test file is named 'Common.cs' it will be linked into all tests.</param>
/// <param name="stubbedAssemblies">The paths of assemblies to stub during translation, if any.</param>
/// <param name="typeInfo">A TypeInfoProvider to use for type info. Using this parameter is not advised if you use proxies or JSIL.Meta attributes in your tests.</param>
/// <param name="testPredicate">A predicate to invoke before running each test. If the predicate returns false, the JS version of the test will not be run (though it will be translated).</param>
protected void RunComparisonTests (
string[] filenames, string[] stubbedAssemblies = null,
TypeInfoProvider typeInfo = null,
Func<string, bool> testPredicate = null,
Action<string, Func<string>> errorCheckPredicate = null,
Func<Configuration> getConfiguration = null
) {
var started = DateTime.UtcNow.Ticks;
string commonFile = null;
for (var i = 0; i < filenames.Length; i++) {
if (filenames[i].Contains(Path.Combine ("", "Common."))) {
commonFile = filenames[i];
break;
}
}
const string keyName = @"Software\Squared\JSIL\Tests\PreviousFailures";
StackFrame callingTest = null;
for (int i = 1; i < 10; i++) {
callingTest = new StackFrame(i);
var method = callingTest.GetMethod();
if ((method != null) && method.GetCustomAttributes(true).Any(
(ca) => ca.GetType().FullName == "NUnit.Framework.TestAttribute"
)) {
break;
} else {
callingTest = null;
}
}
var previousFailures = new HashSet<string>();
MethodBase callingMethod = null;
if ((callingTest != null) && ((callingMethod = callingTest.GetMethod()) != null)) {
try {
using (var rk = Registry.CurrentUser.CreateSubKey(keyName)) {
var names = rk.GetValue(callingMethod.Name) as string;
if (names != null) {
foreach (var name in names.Split(',')) {
previousFailures.Add(name);
}
}
}
} catch (Exception ex) {
Console.WriteLine("Warning: Could not open registry key: {0}", ex);
}
}
var failureList = new List<string>();
var sortedFilenames = new List<string>(filenames);
sortedFilenames.Sort(
(lhs, rhs) => {
var lhsShort = Path.GetFileNameWithoutExtension(lhs);
var rhsShort = Path.GetFileNameWithoutExtension(rhs);
int result =
(previousFailures.Contains(lhsShort) ? 0 : 1).CompareTo(
previousFailures.Contains(rhsShort) ? 0 : 1
);
if (result == 0)
result = lhsShort.CompareTo(rhsShort);
return result;
}
);
var asmCache = new AssemblyCache();
foreach (var filename in sortedFilenames) {
if (filename == commonFile)
continue;
bool shouldRunJs = true;
if (testPredicate != null)
shouldRunJs = testPredicate(filename);
RunComparisonTest(
filename, stubbedAssemblies, typeInfo,
errorCheckPredicate, failureList,
commonFile, shouldRunJs, asmCache,
getConfiguration ?? MakeConfiguration
);
}
if (callingMethod != null) {
try {
using (var rk = Registry.CurrentUser.CreateSubKey(keyName))
rk.SetValue(callingMethod.Name, String.Join(",", failureList.ToArray()));
} catch (Exception ex) {
Console.WriteLine("Warning: Could not open registry key: {0}", ex);
}
}
var ended = DateTime.UtcNow.Ticks;
var elapsedTotalSeconds = TimeSpan.FromTicks(ended - started).TotalSeconds;
Console.WriteLine("// Ran {0} test(s) in {1:000.00}s.", sortedFilenames.Count, elapsedTotalSeconds);
Assert.AreEqual(0, failureList.Count,
String.Format("{0} test(s) failed:\r\n{1}", failureList.Count, String.Join("\r\n", failureList.ToArray()))
);
}
private IEnumerable<Metacomment> RunComparisonTest (
string filename, string[] stubbedAssemblies = null,
TypeInfoProvider typeInfo = null, Action<string, Func<string>> errorCheckPredicate = null,
List<string> failureList = null, string commonFile = null,
bool shouldRunJs = true, AssemblyCache asmCache = null,
Func<Configuration> makeConfiguration = null,
Action<Exception> onTranslationFailure = null,
JSEvaluationConfig evaluationConfig = null,
string compilerOptions = "",
Action<AssemblyTranslator> initializeTranslator = null,
Func<string> getTestRunnerQueryString = null,
bool? scanForProxies = null,
string[] extraDependencies = null,
string testFolderNameOverride = null
) {
IEnumerable<Metacomment> result = null;
Console.WriteLine("// {0} ... ", Path.GetFileName(filename));
filename = Portability.NormalizeDirectorySeparators(filename);
try {
var testFilenames = new List<string>() { filename };
if (commonFile != null)
testFilenames.Add(commonFile);
var testDirectoryName = testFolderNameOverride??
TestContext.CurrentContext.Test.FullName.Replace(
"." + TestContext.CurrentContext.Test.Name, String.Empty);
var testFileDirectory = Path.Combine(
ComparisonTest.TestSourceFolder,
Path.GetDirectoryName(filename));
var testDirectory = Path.Combine(testFileDirectory, testDirectoryName);
Directory.CreateDirectory(testDirectory);
using (var test = new ComparisonTest(
EvaluatorPool,
testFilenames,
Path.Combine(
testDirectory,
ComparisonTest.MapSourceFileToTestFile(Path.GetFileName(filename))
),
stubbedAssemblies, typeInfo, asmCache,
compilerOptions: compilerOptions
)) {
test.GetTestRunnerQueryString = getTestRunnerQueryString ?? test.GetTestRunnerQueryString;
result = test.Metacomments;
if (extraDependencies != null) {
var destDir = Path.GetDirectoryName(test.AssemblyUtility.AssemblyLocation);
foreach (var dependency in extraDependencies)
File.Copy(dependency, Path.Combine(destDir, Path.GetFileName(dependency)), true);
}
if (shouldRunJs) {
test.Run(
makeConfiguration: makeConfiguration,
evaluationConfig: evaluationConfig,
onTranslationFailure: onTranslationFailure,
initializeTranslator: initializeTranslator,
scanForProxies: scanForProxies
);
} else {
Func<string> getJs;
long elapsed;
try {
var csOutput = test.RunCSharp(new string[0], out elapsed);
test.GenerateJavascript(
new string[0], out getJs, out elapsed,
makeConfiguration,
evaluationConfig == null || evaluationConfig.ThrowOnUnimplementedExternals,
onTranslationFailure,
initializeTranslator,
shouldWritePrologue: false
);
Console.WriteLine("generated");
if (errorCheckPredicate != null) {
errorCheckPredicate(csOutput, getJs);
}
} catch (Exception) {
Console.WriteLine("error");
throw;
}
}
}
} catch (Exception ex) {
if (ex.Message == "JS test failed")
Debug.WriteLine(ex.InnerException);
else
Debug.WriteLine(ex);
if (failureList != null) {
failureList.Add(Path.GetFileNameWithoutExtension(filename));
} else
throw;
}
return result;
}
protected string GetJavascript (
string fileName, string expectedText = null,
Func<Configuration> makeConfiguration = null,
bool dumpJsOnFailure = true,
Action<AssemblyTranslator> initializeTranslator = null,
IEnumerable<IAnalyzer> analyzers = null
) {
long elapsed, temp;
Func<string> generateJs = null;
string output;
using (var test = MakeTest(fileName)) {
try {
output = test.RunJavascript(
new string[0], out generateJs,
out temp, out elapsed,
makeConfiguration ?? MakeConfiguration,
initializeTranslator: initializeTranslator,
analyzers: analyzers
);
} catch {
if (dumpJsOnFailure) {
// Failures in very large programs can totally choke the test runner
const int limit = 1024 * 16;
var truncated = generateJs != null ? generateJs() : string.Empty;
if (truncated.Length > limit)
truncated = truncated.Substring(0, limit);
Console.Error.WriteLine("// Generated JS: \r\n{0}", truncated);
}
throw;
}
if (expectedText != null)
Assert.AreEqual(Portability.NormalizeNewLines(expectedText), output.Trim());
}
return generateJs();
}
protected string GenericTest (
string fileName, string csharpOutput,
string javascriptOutput, string[] stubbedAssemblies = null,
TypeInfoProvider typeInfo = null
) {
long elapsed, temp;
Func<string> generateJs = null;
using (var test = new ComparisonTest(EvaluatorPool, Portability.NormalizeDirectorySeparators(fileName), stubbedAssemblies, typeInfo)) {
var csOutput = test.RunCSharp(new string[0], out elapsed);
try {
var jsOutput = test.RunJavascript(new string[0], out generateJs, out temp, out elapsed, MakeConfiguration);
try {
Assert.AreEqual(Portability.NormalizeNewLines(csharpOutput), csOutput.Trim(), "Did not get expected output from C# test");
} catch {
var cso = csOutput;
if (cso.Length > 8192)
cso = cso.Substring(0, 8192);
Console.Error.WriteLine("// C# stdout: \r\n{0}", cso);
throw;
}
Assert.AreEqual(Portability.NormalizeNewLines(javascriptOutput), jsOutput.Trim(), "Did not get expected output from JavaScript test");
} catch {
Console.Error.WriteLine("// Generated JS: \r\n{0}", generateJs != null ? generateJs() : string.Empty);
throw;
}
}
return generateJs();
}
protected string GenericIgnoreTest (string fileName, string workingOutput, string jsErrorSubstring, string[] stubbedAssemblies = null) {
long elapsed, temp;
Func<string> generateJs = null;
string jsOutput = null;
using (var test = new ComparisonTest(EvaluatorPool, Portability.NormalizeDirectorySeparators(fileName), stubbedAssemblies)) {
var csOutput = test.RunCSharp(new string[0], out elapsed);
Assert.AreEqual(Portability.NormalizeNewLines(workingOutput), csOutput.Trim());
try {
jsOutput = test.RunJavascript(new string[0], out generateJs, out temp, out elapsed, MakeConfiguration);
Assert.Fail("Expected javascript to throw an exception containing the string \"" + jsErrorSubstring + "\".");
} catch (JavaScriptEvaluatorException jse) {
bool foundMatch = false;
foreach (var exc in jse.Exceptions) {
if (exc.Message.Contains(jsErrorSubstring)) {
foundMatch = true;
break;
}
}
if (!foundMatch) {
Console.Error.WriteLine("// Was looking for a JS exception containing the string '{0}' but didn't find it.", jsErrorSubstring);
Console.Error.WriteLine("// Generated JS: \r\n{0}", generateJs != null ? generateJs() : string.Empty);
if (jsOutput != null)
Console.Error.WriteLine("// JS output: \r\n{0}", jsOutput);
throw;
}
} catch {
Console.Error.WriteLine("// Generated JS: \r\n{0}", generateJs != null ? generateJs() : string.Empty);
if (jsOutput != null)
Console.Error.WriteLine("// JS output: \r\n{0}", jsOutput);
throw;
}
}
return generateJs();
}
protected IEnumerable<Metacomment> RunSingleComparisonTestCase (
object[] parameters,
Func<Configuration> makeConfiguration = null,
JSEvaluationConfig evaluationConfig = null,
Action<Exception> onTranslationFailure = null,
string compilerOptions = "",
Action<AssemblyTranslator> initializeTranslator = null,
Func<string> getTestRunnerQueryString = null,
bool? scanForProxies = null,
string[] extraDependencies = null,
bool shouldRunJs = true,
string testFolderNameOverride = null
) {
if (parameters.Length != 5)
throw new ArgumentException("Wrong number of test case data parameters.");
var provider = (TypeInfoProvider)parameters[1];
var cache = (AssemblyCache)parameters[2];
try {
return RunComparisonTest(
(string)parameters[0], null, provider, null, null, (string)parameters[3], shouldRunJs, cache,
makeConfiguration: makeConfiguration,
evaluationConfig: evaluationConfig,
onTranslationFailure: onTranslationFailure,
compilerOptions: compilerOptions,
initializeTranslator: initializeTranslator,
getTestRunnerQueryString: getTestRunnerQueryString,
scanForProxies: scanForProxies,
extraDependencies: extraDependencies,
testFolderNameOverride: testFolderNameOverride
);
} finally {
if ((bool)parameters[4]) {
if (provider != null)
provider.Dispose();
if (cache != null)
cache.Dispose();
}
}
}
protected IEnumerable<TestCaseData> FolderTestSource (string folderName, TypeInfoProvider typeInfo = null, AssemblyCache asmCache = null, bool markLastTest = true)
{
var cases = FolderTestSource(folderName, String.Empty, typeInfo, asmCache);
if (!markLastTest)
{
return cases;
}
//Let's mark last test.
var array = cases.ToArray();
((object[]) array[array.Length - 1].Arguments[0])[4] = true;
return array;
}
private IEnumerable<TestCaseData> FolderTestSource(string folderName, string subfolders, TypeInfoProvider typeInfo, AssemblyCache asmCache)
{
if (folderName.StartsWith("exclude_", StringComparison.InvariantCultureIgnoreCase))
{
yield break;
}
var testPath = Path.GetFullPath(Path.Combine(ComparisonTest.TestSourceFolder, subfolders, folderName));
if (!Directory.Exists(testPath))
{
Console.WriteLine("WARNING: Folder {0} doesn't exist.", testPath);
yield break;
}
subfolders = Path.Combine(subfolders, folderName);
var testNames = Directory.GetFiles(testPath, "*.cs")
.Concat(Directory.GetFiles(testPath, "*.vb"))
.Concat(Directory.GetFiles(testPath, "*.fs"))
.Concat(Directory.GetFiles(testPath, "*.js"))
.Concat(Directory.GetFiles(testPath, "*.il"))
.Concat(Directory.GetFiles(testPath, "*.cpp"))
.OrderBy((s) => s).ToArray();
string commonFile = null;
foreach (var testName in testNames)
{
if (Path.GetFileNameWithoutExtension(testName) == "Common")
{
commonFile = testName;
break;
}
}
for (int i = 0, l = testNames.Length; i < l; i++)
{
var testName = testNames[i];
if (Path.GetFileNameWithoutExtension(testName) == "Common")
continue;
var item = (new TestCaseData(new object[] {new object[] {testName, typeInfo, asmCache, commonFile, false}}))
.SetName(PickTestNameForFilename(testName) + NameSuffix)
.SetDescription(String.Format("{0}\\{1}", subfolders, Path.GetFileName(testName)));
foreach (var category in subfolders.Split(Path.DirectorySeparatorChar))
{
item.SetCategory(category);
}
yield return item;
}
string[] subdirectoryEntries = Directory.GetDirectories(testPath);
foreach (string subdirectory in subdirectoryEntries)
foreach (var item in FolderTestSource(subdirectory.Split(Path.DirectorySeparatorChar).Last(), subfolders, typeInfo, asmCache))
yield return item;
}
protected IEnumerable<TestCaseData> FilenameTestSource (string[] filenames, TypeInfoProvider typeInfo = null, AssemblyCache asmCache = null, bool markLastTest = true) {
var testNames = filenames.OrderBy((s) => s).ToArray();
for (int i = 0, l = testNames.Length; i < l; i++) {
var testName = testNames[i];
bool isIgnored = testName.StartsWith("ignored:", StringComparison.OrdinalIgnoreCase);
var actualTestName = testName;
if (isIgnored)
actualTestName = actualTestName.Substring(actualTestName.IndexOf(":") + 1);
var item = (new TestCaseData(new object[] { new object[] { actualTestName, typeInfo, asmCache, null, markLastTest && i == (l - 1) } }))
.SetName(PickTestNameForFilename(actualTestName) + NameSuffix);
var normalTestPathName = Portability.NormalizeDirectorySeparators(actualTestName);
var testFileName = Path.GetFileName(normalTestPathName);
foreach (var part in normalTestPathName.Split(Path.DirectorySeparatorChar))
{
if (part != testFileName)
{
item.SetCategory(part);
}
}
if (isIgnored)
item.Ignore();
yield return item;
}
}
public static string PickTestNameForFilename (string filename) {
var result = Path.GetFileNameWithoutExtension(filename);
var containingFolder = Path.GetFileName(Path.GetDirectoryName(filename));
switch (Path.GetExtension(filename).ToLowerInvariant()) {
case ".cs":
return result + " (C#)";
case ".vb":
return result + " (VB)";
case ".fs":
return result + " (F#)";
case ".js":
return result + " (JavaScript)";
case ".il":
return result + " (CIL)";
case ".cpp":
return result + " (C++/CLI)";
default:
return result;
}
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class FailsOnMonoAttribute : CategoryAttribute { }
}