Ejemplo n.º 1
0
    private static int RunTest(string suiteName, string test, StringBuilder actualOutput, ArrayList compilerParameters, ArrayList testCaseParameters, bool xamlSuite)
    {
        Microsoft.SpecSharp.Compiler compiler = new Microsoft.SpecSharp.Compiler();
        compiler.CompileAsXaml = xamlSuite;
        CompilerOptions options = new Microsoft.SpecSharp.SpecSharpCompilerOptions();

        if (compilerParameters != null)
        {
            ErrorNodeList compilerParameterErrors = new ErrorNodeList(0);
            compiler.ParseCompilerParameters(options, (string[])compilerParameters.ToArray(typeof(string)), compilerParameterErrors, false);
            for (int i = 0, n = compilerParameterErrors.Count; i < n; i++)
            {
                ErrorNode err = compilerParameterErrors[i];
                Console.WriteLine(err.GetMessage());
            }
        }
        options.OutputAssembly     = "assembly for suite " + suiteName + " test case " + main.assemblyNameCounter++;
        options.GenerateExecutable = true;
        options.MayLockFiles       = true;
        options.GenerateInMemory   = true;
        // Code that is not marked as unsafe should be verifiable. This catches cases where the compiler generates bad IL.
        if (!options.AllowUnsafeCode)
        {
            options.Evidence = new System.Security.Policy.Evidence(new object[] { new System.Security.Policy.Zone(System.Security.SecurityZone.Internet) }, null);
        }
        CompilerResults results = compiler.CompileAssemblyFromSource(options, test);

        foreach (CompilerError e in results.Errors)
        {
            Console.Write('(');
            Console.Write(e.Line);
            Console.Write(',');
            Console.Write(e.Column);
            Console.Write("): ");
            string warningString = null;
            string errorString   = null;
            if (e.IsWarning)
            {
                if (!e.ErrorNumber.StartsWith("CS") || e.ErrorNumber.Length == 6)
                {
                    if (warningString == null)
                    {
                        warningString = compiler.GetWarningString();
                    }
                    Console.Write(warningString, e.ErrorNumber);
                }
            }
            else
            {
                if (!e.ErrorNumber.StartsWith("CS") || e.ErrorNumber.Length == 6)
                {
                    if (errorString == null)
                    {
                        errorString = compiler.GetErrorString();
                    }
                    Console.Write(errorString, e.ErrorNumber);
                }
            }
            Console.WriteLine(e.ErrorText);
        }
        if (results.NativeCompilerReturnValue != 0)
        {
            return(0);
        }
        object returnVal = null;

        try{
            if (testCaseParameters == null)
            {
                if (results.CompiledAssembly.EntryPoint.GetParameters().Length == 0)
                {
                    returnVal = results.CompiledAssembly.EntryPoint.Invoke(null, null);
                }
                else
                {
                    returnVal = results.CompiledAssembly.EntryPoint.Invoke(null, new object[] { new string[0] });
                }
            }
            else
            {
                returnVal = results.CompiledAssembly.EntryPoint.Invoke(null, new object[] { (string[])testCaseParameters.ToArray(typeof(string)) });
            }
        }catch (System.Reflection.TargetInvocationException e) {
            throw e.InnerException;
        }
        if (returnVal is int)
        {
            return((int)returnVal);
        }
        return(0);
    }