示例#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);
      }
    }
示例#2
0
 static void Main(string[] args) {
   HostEnvironment hostEnvironment = new HostEnvironment();
   hostEnvironment.Errors += hostEnvironment.HandleErrors;
   SpecSharpOptions commandLineOptions = OptionParser.ParseCommandLineArguments(hostEnvironment, args);
   if (hostEnvironment.hasError) return;
   if (commandLineOptions.DisplayCommandLineHelp)
     DisplayCommandLineHelp(OptionParser.GetOptionUsage());
   else if (commandLineOptions.RunTestSuite)
     RunTestSuite(commandLineOptions);
   else
     TranslateToExe(commandLineOptions);
 }
示例#3
0
 private static List<IAssemblyReference> GetAssemblyReferences(SpecSharpOptions commandLineOptions, HostEnvironment hostEnvironment) {
   List<IAssemblyReference> assemblyReferences = new List<IAssemblyReference>();
   assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
   foreach (string assemblyReference in commandLineOptions.ReferencedAssemblies) {
     IUnit unit = hostEnvironment.LoadUnitFrom(assemblyReference);
     if (unit == Dummy.Unit) {
       unit = hostEnvironment.LoadUnitFrom(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), assemblyReference));
     }
     IAssembly/*?*/ aref = unit as IAssembly;
     if (aref != null)
       assemblyReferences.Add(aref);
     else {
       //TODO: error message
     }
   }
   return assemblyReferences;
 }
示例#4
0
        static void Main(string[] args)
        {
            HostEnvironment hostEnvironment = new HostEnvironment();

            hostEnvironment.Errors += hostEnvironment.HandleErrors;
            SpecSharpOptions commandLineOptions = OptionParser.ParseCommandLineArguments(hostEnvironment, args);

            if (hostEnvironment.hasError)
            {
                return;
            }
            if (commandLineOptions.DisplayCommandLineHelp)
            {
                DisplayCommandLineHelp(OptionParser.GetOptionUsage());
            }
            else if (commandLineOptions.RunTestSuite)
            {
                RunTestSuite(commandLineOptions);
            }
            else
            {
                TranslateToExe(commandLineOptions);
            }
        }
示例#5
0
        private static List <IAssemblyReference> GetAssemblyReferences(SpecSharpOptions commandLineOptions, HostEnvironment hostEnvironment)
        {
            List <IAssemblyReference> assemblyReferences = new List <IAssemblyReference>();

            assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
            foreach (string assemblyReference in commandLineOptions.ReferencedAssemblies)
            {
                IUnit unit = hostEnvironment.LoadUnitFrom(assemblyReference);
                if (unit == Dummy.Unit)
                {
                    unit = hostEnvironment.LoadUnitFrom(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), assemblyReference));
                }
                IAssembly /*?*/ aref = unit as IAssembly;
                if (aref != null)
                {
                    assemblyReferences.Add(aref);
                }
                else
                {
                    //TODO: error message
                }
            }
            return(assemblyReferences);
        }
示例#6
0
        public static bool RunTestSuite(string suiteName, StreamReader instream)
        {
            System.Diagnostics.Debug.Listeners.Remove("Default");
            HostEnvironment hostEnvironment = new HostEnvironment();

            hostEnvironment.Errors += hostEnvironment.HandleErrors;
            StringBuilder source             = null;
            StringBuilder expectedOutput     = null;
            StringBuilder actualOutput       = null;
            List <string> suiteParameters    = new List <string>();
            List <string> compilerParameters = null;
            List <string> testCaseParameters = null;
            int           errors             = 0;

            try {
                int ch   = instream.Read();
                int line = 1;
                while (ch >= 0)
                {
                    compilerParameters = new List <string>(suiteParameters);
                    bool skipTest = false;
                    if (ch == '`')
                    {
                        ch = instream.Read();
                        bool parametersAreForEntireSuite = false;
                        if (ch == '`')
                        {
                            parametersAreForEntireSuite = true;
                            ch = instream.Read();
                        }
                        while (ch == '/')
                        {
                            //compiler parameters
                            StringBuilder cParam = new StringBuilder();
                            do
                            {
                                cParam.Append((char)ch);
                                ch = instream.Read();
                            } while (ch != '/' && ch != 0 && ch != 10 && ch != 13);
                            for (int i = cParam.Length - 1; i >= 0; i--)
                            {
                                if (!Char.IsWhiteSpace(cParam[i]))
                                {
                                    break;
                                }
                                cParam.Length = i;
                            }
                            string cp = cParam.ToString();
                            compilerParameters.Add(cp);
                        }
                        if (parametersAreForEntireSuite)
                        {
                            suiteParameters.AddRange(compilerParameters);
                        }
                        if (ch == 13)
                        {
                            ch = instream.Read();
                        }
                        if (ch == 10)
                        {
                            line++;
                            ch = instream.Read();
                            if (parametersAreForEntireSuite && ch == '`')
                            {
                                continue;
                            }
                        }
                    }
                    if (ch == ':')
                    {
                        ch = instream.Read();
                        while (ch == '=')
                        {
                            //test case parameters
                            StringBuilder tcParam = new StringBuilder();
                            ch = instream.Read(); //discard =
                            while (ch != '=' && ch != 0 && ch != 10 && ch != 13)
                            {
                                tcParam.Append((char)ch);
                                ch = instream.Read();
                            }
                            for (int i = tcParam.Length - 1; i >= 0; i--)
                            {
                                if (!Char.IsWhiteSpace(tcParam[i]))
                                {
                                    break;
                                }
                                tcParam.Length = i;
                            }
                            if (testCaseParameters == null)
                            {
                                testCaseParameters = new List <string>();
                            }
                            testCaseParameters.Add(tcParam.ToString());
                        }
                        if (ch == 13)
                        {
                            ch = instream.Read();
                        }
                        if (ch == 10)
                        {
                            ch = instream.Read();
                            line++;
                        }
                    }
                    source = new StringBuilder();
                    while (ch >= 0 && ch != '`')
                    {
                        source.Append((char)ch);
                        ch = instream.Read();
                        if (ch == 10)
                        {
                            line++;
                        }
                    }
                    if (ch < 0)
                    {
                        Console.WriteLine("The last test case in the suite has not been provided with expected output");
                        errors++;
                        break;
                    }
                    ch = instream.Read();
                    if (ch == 13)
                    {
                        ch = instream.Read();
                    }
                    if (ch == 10)
                    {
                        line++;
                        ch = instream.Read();
                    }
                    int errLine = line;
                    expectedOutput = new StringBuilder();
                    while (ch >= 0 && ch != '`')
                    {
                        expectedOutput.Append((char)ch);
                        ch = instream.Read();
                        if (ch == 10)
                        {
                            line++;
                        }
                    }
                    if (expectedOutput.Length > 0 && expectedOutput[expectedOutput.Length - 1] == 10)
                    {
                        expectedOutput.Length -= 1;
                    }
                    if (expectedOutput.Length > 0 && expectedOutput[expectedOutput.Length - 1] == 13)
                    {
                        expectedOutput.Length -= 1;
                    }
                    ch = instream.Read();
                    if (ch == 13)
                    {
                        ch = instream.Read();
                    }
                    if (ch == 10)
                    {
                        ch = instream.Read();
                        line++;
                    }
                    if (skipTest)
                    {
                        continue;
                    }
                    actualOutput = new StringBuilder();
                    TextWriter savedOut = Console.Out;
                    Console.SetOut(new StringWriter(actualOutput));
                    System.Diagnostics.TextWriterTraceListener myWriter = new System.Diagnostics.TextWriterTraceListener(System.Console.Out);
                    System.Diagnostics.Debug.Listeners.Add(myWriter);
                    try {
                        int returnCode = RunTest(hostEnvironment, Path.GetFileNameWithoutExtension(suiteName), source.ToString(), actualOutput, compilerParameters, testCaseParameters);
                        if (returnCode != 0)
                        {
                            actualOutput.Append("Non zero return code: " + returnCode);
                        }
                    } catch (System.Reflection.TargetInvocationException e) {
                        actualOutput.Append(e.InnerException.Message);
                    } catch (Exception e) {
                        actualOutput.Append(e.Message);
                    }
                    compilerParameters = null;
                    testCaseParameters = null;
                    Console.SetOut(savedOut);
                    System.Diagnostics.Debug.Listeners.Remove(myWriter);
                    if (actualOutput.Length > 0 && actualOutput[actualOutput.Length - 1] == 10)
                    {
                        actualOutput.Length -= 1;
                    }
                    if (actualOutput.Length > 0 && actualOutput[actualOutput.Length - 1] == 13)
                    {
                        actualOutput.Length -= 1;
                    }
                    if (!expectedOutput.ToString().Equals(actualOutput.ToString()))
                    {
                        if (errors++ == 0)
                        {
                            Console.WriteLine(suiteName + " failed\n");
                        }
                        Console.WriteLine("source({0}):", errLine);
                        if (source != null)
                        {
                            Console.WriteLine(source);
                        }
                        Console.WriteLine("actual output:");
                        Console.WriteLine(actualOutput);
                        Console.WriteLine("expected output:");
                        if (expectedOutput != null)
                        {
                            Console.WriteLine(expectedOutput);
                        }
                    }
                }
                instream.Close();
                if (errors == 0)
                {
                    Console.WriteLine(suiteName + " passed");
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(suiteName + " had " + errors + (errors > 1 ? " failures" : " failure"));
                }
            } catch {
                Console.WriteLine(suiteName + " failed\n");
                Console.WriteLine("source:");
                if (source != null)
                {
                    Console.WriteLine(source);
                }
                Console.WriteLine("actual output:");
                Console.WriteLine(actualOutput);
                Console.WriteLine("expected output:");
                if (expectedOutput != null)
                {
                    Console.WriteLine(expectedOutput);
                }
            }
            return(errors == 0);
        }
示例#7
0
 public static bool RunTestSuite(string suiteName, StreamReader instream) {
   System.Diagnostics.Debug.Listeners.Remove("Default");
   HostEnvironment hostEnvironment = new HostEnvironment();
   hostEnvironment.Errors += hostEnvironment.HandleErrors;
   StringBuilder source = null;
   StringBuilder expectedOutput = null;
   StringBuilder actualOutput = null;
   List<string> suiteParameters = new List<string>();
   List<string> compilerParameters = null;
   List<string> testCaseParameters = null;
   int errors = 0;
   try {
     int ch = instream.Read();
     int line = 1;
     while (ch >= 0) {
       compilerParameters = new List<string>(suiteParameters);
       bool skipTest = false;
       if (ch == '`') {
         ch = instream.Read();
         bool parametersAreForEntireSuite = false;
         if (ch == '`') {
           parametersAreForEntireSuite = true;
           ch = instream.Read();
         }
         while (ch == '/') {
           //compiler parameters
           StringBuilder cParam = new StringBuilder();
           do {
             cParam.Append((char)ch);
             ch = instream.Read();
           } while (ch != '/' && ch != 0 && ch != 10 && ch != 13);
           for (int i = cParam.Length-1; i >= 0; i--) {
             if (!Char.IsWhiteSpace(cParam[i])) break;
             cParam.Length = i;
           }
           string cp = cParam.ToString();
           compilerParameters.Add(cp);
         }
         if (parametersAreForEntireSuite)
           suiteParameters.AddRange(compilerParameters);
         if (ch == 13) ch = instream.Read();
         if (ch == 10) {
           line++;
           ch = instream.Read();
           if (parametersAreForEntireSuite && ch == '`') continue;
         }
       }
       if (ch == ':') {
         ch = instream.Read();
         while (ch == '=') {
           //test case parameters
           StringBuilder tcParam = new StringBuilder();
           ch = instream.Read(); //discard =
           while (ch != '=' && ch != 0 && ch != 10 && ch != 13) {
             tcParam.Append((char)ch);
             ch = instream.Read();
           }
           for (int i = tcParam.Length-1; i >= 0; i--) {
             if (!Char.IsWhiteSpace(tcParam[i])) break;
             tcParam.Length = i;
           }
           if (testCaseParameters == null) testCaseParameters = new List<string>();
           testCaseParameters.Add(tcParam.ToString());
         }
         if (ch == 13) ch = instream.Read();
         if (ch == 10) {
           ch = instream.Read();
           line++;
         }
       }
       source = new StringBuilder();
       while (ch >= 0 && ch != '`') {
         source.Append((char)ch);
         ch = instream.Read();
         if (ch == 10) line++;
       }
       if (ch < 0) {
         Console.WriteLine("The last test case in the suite has not been provided with expected output");
         errors++;
         break;
       }
       ch = instream.Read();
       if (ch == 13) ch = instream.Read();
       if (ch == 10) {
         line++;
         ch = instream.Read();
       }
       int errLine = line;
       expectedOutput = new StringBuilder();
       while (ch >= 0 && ch != '`') {
         expectedOutput.Append((char)ch);
         ch = instream.Read();
         if (ch == 10) line++;
       }
       if (expectedOutput.Length > 0 && expectedOutput[expectedOutput.Length-1] == 10)
         expectedOutput.Length -= 1;
       if (expectedOutput.Length > 0 && expectedOutput[expectedOutput.Length-1] == 13)
         expectedOutput.Length -= 1;
       ch = instream.Read();
       if (ch == 13) ch = instream.Read();
       if (ch == 10) {
         ch = instream.Read();
         line++;
       }
       if (skipTest) continue;
       actualOutput = new StringBuilder();
       TextWriter savedOut = Console.Out;
       Console.SetOut(new StringWriter(actualOutput));
       System.Diagnostics.TextWriterTraceListener myWriter = new System.Diagnostics.TextWriterTraceListener(System.Console.Out);
       System.Diagnostics.Debug.Listeners.Add(myWriter);
       try {
         int returnCode = RunTest(hostEnvironment, Path.GetFileNameWithoutExtension(suiteName), source.ToString(), actualOutput, compilerParameters, testCaseParameters);
         if (returnCode != 0)
           actualOutput.Append("Non zero return code: "+returnCode);
       } catch (System.Reflection.TargetInvocationException e) {
         actualOutput.Append(e.InnerException.Message);
       } catch (Exception e) {
         actualOutput.Append(e.Message);
       }
       compilerParameters = null;
       testCaseParameters = null;
       Console.SetOut(savedOut);
       System.Diagnostics.Debug.Listeners.Remove(myWriter);
       if (actualOutput.Length > 0 && actualOutput[actualOutput.Length - 1] == 10)
         actualOutput.Length -= 1;
       if (actualOutput.Length > 0 && actualOutput[actualOutput.Length - 1] == 13)
         actualOutput.Length -= 1;
       if (!expectedOutput.ToString().Equals(actualOutput.ToString())) {
         if (errors++ == 0) Console.WriteLine(suiteName+" failed\n");
         Console.WriteLine("source({0}):", errLine);
         if (source != null)
           Console.WriteLine(source);
         Console.WriteLine("actual output:");
         Console.WriteLine(actualOutput);
         Console.WriteLine("expected output:");
         if (expectedOutput != null)
           Console.WriteLine(expectedOutput);
       }
     }
     instream.Close();
     if (errors == 0)
       Console.WriteLine(suiteName+" passed");
     else {
       Console.WriteLine();
       Console.WriteLine(suiteName+" had "+errors+ (errors > 1 ? " failures" : " failure"));
     }
   } catch {
     Console.WriteLine(suiteName+" failed\n");
     Console.WriteLine("source:");
     if (source != null)
       Console.WriteLine(source);
     Console.WriteLine("actual output:");
     Console.WriteLine(actualOutput);
     Console.WriteLine("expected output:");
     if (expectedOutput != null)
       Console.WriteLine(expectedOutput);
   }
   return errors == 0;
 }
示例#8
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;
    }