Exemple #1
0
        private static void TranslateToExe(SpecSharpOptions commandLineOptions)
        //^ requires commandLineOptions.FileNames.Count > 0;
        {
            HostEnvironment hostEnvironment = new HostEnvironment();

            hostEnvironment.Errors         += hostEnvironment.HandleErrors;
            hostEnvironment.displayFileName = true;
            List <IAssemblyReference>      assemblyReferences = GetAssemblyReferences(commandLineOptions, hostEnvironment);
            List <IModuleReference>        moduleReferences   = new List <IModuleReference>();
            List <SpecSharpSourceDocument> programSources     = new List <SpecSharpSourceDocument>(1);
            IName                      name   = hostEnvironment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(commandLineOptions.FileNames[0]));
            SpecSharpAssembly          assem  = new SpecSharpAssembly(name, Path.GetFullPath(name.Value), hostEnvironment, commandLineOptions, assemblyReferences, moduleReferences, programSources);
            SpecSharpCompilationHelper helper = new SpecSharpCompilationHelper(assem.Compilation);

            foreach (string fileName in commandLineOptions.FileNames)
            {
                name = hostEnvironment.NameTable.GetNameFor(fileName);
                StreamReader instream = File.OpenText(fileName);
                programSources.Add(new SpecSharpSourceDocument(helper, name, Path.GetFullPath(fileName), instream));
            }

            if (assem.Compilation.HasErrors)
            {
                return;
            }
            var sourceLocationProvider = assem.Compilation.SourceLocationProvider;
            var localScopeProvider     = assem.Compilation.LocalScopeProvider;

            using (var pdbWriter = new PdbWriter(Path.ChangeExtension(assem.Location, "pdb"), sourceLocationProvider)) {
                PeWriter.WritePeToStream(assem, hostEnvironment, File.Create(Path.ChangeExtension(assem.Location, "exe")), sourceLocationProvider, localScopeProvider, pdbWriter);
            }
        }
Exemple #2
0
        private static int RunTest(HostEnvironment hostEnvironment, string suiteName, string test, StringBuilder actualOutput, List <string> compilerParameters, List <string> testCaseParameters)
        {
            hostEnvironment.hasError = false;
            IName                     name               = hostEnvironment.NameTable.GetNameFor(suiteName);
            SpecSharpOptions          options            = new SpecSharpOptions(); //TODO: extract from params
            List <IAssemblyReference> assemblyReferences = new List <IAssemblyReference>();
            List <IModuleReference>   moduleReferences   = new List <IModuleReference>();

            assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
            IUnit unit;
            SpecSharpAssembly /*?*/    assem = null;
            SpecSharpCompilationHelper helper;

            if (hostEnvironment.previousDocument != null && compilerParameters.Contains("/incremental"))
            {
                unit   = hostEnvironment.GetIncrementalUnit(test);
                helper = (SpecSharpCompilationHelper)hostEnvironment.previousDocument.SpecSharpCompilationPart.Helper;
            }
            else
            {
                List <SpecSharpSourceDocument> programSources = new List <SpecSharpSourceDocument>(1);
                assem  = new SpecSharpAssembly(name, "", hostEnvironment, options, assemblyReferences, moduleReferences, programSources);
                helper = new SpecSharpCompilationHelper(assem.Compilation);
                programSources.Add(hostEnvironment.previousDocument = new SpecSharpSourceDocument(helper, name, "", test));
                unit = assem;
            }
            if (assem != null && assem.Compilation.HasErrors)
            {
                return(0);
            }
            if (assem != null && assem.EntryPoint.ResolvedMethod != Dummy.Method)
            {
                var memStream = new MemoryStream();
                PeWriter.WritePeToStream(assem, hostEnvironment, memStream);
                if (hostEnvironment.hasError)
                {
                    return(0);
                }
                var runtimeAssembly = System.Reflection.Assembly.Load(memStream.ToArray());
                var result          = runtimeAssembly.EntryPoint.Invoke(null, null);
                if (result is int)
                {
                    return((int)result);
                }
                return(0);
            }

            BaseCodeTraverser traverser = new BaseCodeTraverser();

            unit.Dispatch(traverser);
            return(0);
        }