示例#1
0
        protected string GenericTest(
            string fileName, string csharpOutput,
            string javascriptOutput, string[] stubbedAssemblies = null,
            TypeInfoProvider typeInfo = null
            )
        {
            long   elapsed, temp;
            string generatedJs = 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 generatedJs, out temp, out elapsed, MakeConfiguration);

                    Assert.AreEqual(Portability.NormalizeNewLines(csharpOutput), csOutput.Trim(), "Did not get expected output from C# test");
                    Assert.AreEqual(Portability.NormalizeNewLines(javascriptOutput), jsOutput.Trim(), "Did not get expected output from JavaScript test");
                } catch {
                    Console.Error.WriteLine("// Generated JS: \r\n{0}", generatedJs);
                    throw;
                }
            }

            return(generatedJs);
        }
示例#2
0
        public void EscapesOutputFilenames()
        {
            using (var test = new ComparisonTest(
                       EvaluatorPool,
                       new[] { @"TestCases\HelloWorld.cs" },
                       Portability.NormalizeDirectorySeparators(@"MetadataTests\EscapesOutputFilenames")
                       )) {
                var filenames = test.Translate((tr) => {
                    return((from file in tr.OrderedFiles select file.Filename).ToArray());
                }, () => {
                    var configuration = MakeConfiguration();
                    configuration.FilenameEscapeRegex = "[^A-Za-z0-9 _]";
                    // We don't escape manifest
                    configuration.SkipManifestCreation = true;
                    return(configuration);
                });

                Assert.AreEqual(1, filenames.Length);

                foreach (var filename in filenames)
                {
                    Assert.IsTrue(Regex.IsMatch(filename, @"^([A-Za-z0-9 _]*)\.js$"), "Filename '{0}' does not match regex.", filename);
                    Console.WriteLine(filename);
                }
            }
        }
示例#3
0
 protected ComparisonTest MakeTest(
     string filename, string[] stubbedAssemblies = null,
     TypeInfoProvider typeInfo   = null,
     AssemblyCache assemblyCache = null
     )
 {
     return(new ComparisonTest(
                EvaluatorPool,
                Portability.NormalizeDirectorySeparators(filename), stubbedAssemblies,
                typeInfo, assemblyCache
                ));
 }
示例#4
0
        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());
        }
示例#5
0
        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());
        }
示例#6
0
        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);
            }
        }
示例#7
0
        protected string GetJavascript(string fileName, string expectedText = null, Func <Configuration> makeConfiguration = null, bool dumpJsOnFailure = true)
        {
            long   elapsed, temp;
            string generatedJs = null, output;

            using (var test = MakeTest(fileName)) {
                try {
                    output = test.RunJavascript(new string[0], out generatedJs, out temp, out elapsed, makeConfiguration ?? MakeConfiguration);
                } catch {
                    if (dumpJsOnFailure)
                    {
                        Console.Error.WriteLine("// Generated JS: \r\n{0}", generatedJs);
                    }
                    throw;
                }

                if (expectedText != null)
                {
                    Assert.AreEqual(Portability.NormalizeNewLines(expectedText), output.Trim());
                }
            }

            return(generatedJs);
        }
示例#8
0
        private CompileResult RunComparisonTest(
            string filename, string[] stubbedAssemblies = null,
            TypeInfoProvider typeInfo = null, Action <string, 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
            )
        {
            CompileResult 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);
                }

                using (var test = new ComparisonTest(
                           EvaluatorPool,
                           testFilenames,
                           Path.Combine(
                               ComparisonTest.TestSourceFolder,
                               ComparisonTest.MapSourceFileToTestFile(filename)
                               ),
                           stubbedAssemblies, typeInfo, asmCache,
                           compilerOptions: compilerOptions
                           )) {
                    test.GetTestRunnerQueryString = getTestRunnerQueryString ?? test.GetTestRunnerQueryString;
                    result = test.CompileResult;

                    if (shouldRunJs)
                    {
                        test.Run(
                            makeConfiguration: makeConfiguration,
                            evaluationConfig: evaluationConfig,
                            onTranslationFailure: onTranslationFailure,
                            initializeTranslator: initializeTranslator,
                            scanForProxies: scanForProxies
                            );
                    }
                    else
                    {
                        string js;
                        long   elapsed;
                        try {
                            var csOutput = test.RunCSharp(new string[0], out elapsed);
                            test.GenerateJavascript(
                                new string[0], out js, out elapsed,
                                makeConfiguration,
                                evaluationConfig == null || evaluationConfig.ThrowOnUnimplementedExternals,
                                onTranslationFailure,
                                initializeTranslator
                                );

                            Console.WriteLine("generated");

                            if (errorCheckPredicate != null)
                            {
                                errorCheckPredicate(csOutput, js);
                            }
                        } 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);
        }
示例#9
0
        public ComparisonTest(
            EvaluatorPool pool,
            IEnumerable <string> filenames, string outputPath,
            string[] stubbedAssemblies  = null, TypeInfoProvider typeInfo = null,
            AssemblyCache assemblyCache = null, string compilerOptions    = ""
            )
        {
            var started = DateTime.UtcNow.Ticks;

            OutputPath    = outputPath;
            EvaluatorPool = pool;

            var extensions        = (from f in filenames select Path.GetExtension(f).ToLower()).Distinct().ToArray();
            var absoluteFilenames = (from f in filenames select Path.Combine(TestSourceFolder, Portability.NormalizeDirectorySeparators(f)));

            if (extensions.Length != 1)
            {
                throw new InvalidOperationException("Mixture of different source languages provided.");
            }

            SourceDirectory = Path.GetDirectoryName(absoluteFilenames.First());

            var assemblyNamePrefix = Path.GetDirectoryName(outputPath).Split(new char[] { '\\', '/' }).Last();
            var assemblyName       = Path.Combine(
                assemblyNamePrefix,
                Path.GetFileName(outputPath).Replace(".js", "")
                );

            JSFilenames = null;

            if (UseAppDomains)
            {
                AssemblyAppDomain = AppDomain.CreateDomain("TestAssemblyDomain", null, new AppDomainSetup {
                    ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
                });
            }
            else
            {
                AssemblyAppDomain = AppDomain.CurrentDomain;
            }

            switch (extensions[0])
            {
            case ".exe":
            case ".dll":
                var fns = absoluteFilenames.ToArray();
                if (fns.Length > 1)
                {
                    throw new InvalidOperationException("Multiple binary assemblies provided.");
                }
                AssemblyUtility = CrossDomainHelper.CreateFromAssemblyPathOnRemoteDomain(AssemblyAppDomain, fns[0], extensions[0] == ".exe").AssemblyUtility;
                break;

            case ".js":
                JSFilenames     = absoluteFilenames.ToArray();
                Metacomments    = null;
                AssemblyUtility = null;
                break;

            default:
                bool ignore = false;
                try
                {
                    var helper = CrossDomainHelper.CreateFromCompileResultOnRemoteDomain(AssemblyAppDomain,
                                                                                         absoluteFilenames, assemblyName,
                                                                                         compilerOptions, CurrentMetaRevision);
                    Metacomments        = helper.Metacomments;
                    AssemblyUtility     = helper.AssemblyUtility;
                    CompilationCacheHit = helper.WasCached;
                }
                catch (TargetInvocationException exception)
                {
                    if (exception.InnerException is CompilerNotFoundException)
                    {
                        Assert.Ignore(exception.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (CompilerNotFoundException exception)
                {
                    Assert.Ignore(exception.Message);
                }
                break;
            }

            if (typeInfo != null)
            {
                typeInfo.ClearCaches();
            }

            StubbedAssemblies = stubbedAssemblies;
            TypeInfo          = typeInfo;
            AssemblyCache     = assemblyCache;

            var ended = DateTime.UtcNow.Ticks;

            CompilationElapsed = TimeSpan.FromTicks(ended - started);
        }
示例#10
0
        public ComparisonTest(
            EvaluatorPool pool,
            IEnumerable <string> filenames, string outputPath,
            string[] stubbedAssemblies  = null, TypeInfoProvider typeInfo = null,
            AssemblyCache assemblyCache = null, string compilerOptions    = ""
            )
        {
            var started = DateTime.UtcNow.Ticks;

            OutputPath    = outputPath;
            EvaluatorPool = pool;

            var extensions        = (from f in filenames select Path.GetExtension(f).ToLower()).Distinct().ToArray();
            var absoluteFilenames = (from f in filenames select Path.Combine(TestSourceFolder, Portability.NormalizeDirectorySeparators(f)));

            if (extensions.Length != 1)
            {
                throw new InvalidOperationException("Mixture of different source languages provided.");
            }

            var assemblyNamePrefix = Path.GetDirectoryName(outputPath).Split(new char[] { '\\', '/' }).Last();
            var assemblyName       = Path.Combine(
                assemblyNamePrefix,
                Path.GetFileName(outputPath).Replace(".js", "")
                );

            switch (extensions[0])
            {
            case ".exe":
            case ".dll":
                var fns = absoluteFilenames.ToArray();
                if (fns.Length > 1)
                {
                    throw new InvalidOperationException("Multiple binary assemblies provided.");
                }

                Assembly = Assembly.LoadFile(fns[0]);
                break;

            default:
                CompileResult = CompilerUtil.Compile(absoluteFilenames, assemblyName, compilerOptions: compilerOptions);
                Assembly      = CompileResult.Assembly;
                break;
            }

            if (typeInfo != null)
            {
                typeInfo.ClearCaches();
            }

            StubbedAssemblies = stubbedAssemblies;
            TypeInfo          = typeInfo;
            AssemblyCache     = assemblyCache;

            var ended = DateTime.UtcNow.Ticks;

            CompilationElapsed = TimeSpan.FromTicks(ended - started);
        }
示例#11
0
        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);
        }