public void TestStdOutputAndStdError() { using (AssemblyRunner runner = new AssemblyRunner(Exe)) { StringWriter wtr = new StringWriter(); StringWriter err = new StringWriter(); ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs e) { if (e.Error) { err.WriteLine(e.Data); } else { wtr.WriteLine(e.Data); } }; runner.OutputReceived += handler; Assert.AreEqual(1, runner.Run("command-line-argrument")); Assert.AreEqual("std-err", err.ToString().Trim()); StringReader rdr = new StringReader(wtr.ToString()); Assert.AreEqual("WorkingDirectory = " + Environment.CurrentDirectory, rdr.ReadLine()); Assert.AreEqual("argument[0] = command-line-argrument", rdr.ReadLine()); Assert.AreEqual("std-input:", rdr.ReadLine()); Assert.AreEqual(null, rdr.ReadLine()); } }
public void TestUnhandledExceptionRun() { using (IRunner runner = new AssemblyRunner(Exe)) { runner.Run("-throw", "System.ArgumentOutOfRangeException"); Assert.Fail(); } }
/// <summary> /// Run the provided donatello program and return the result. /// </summary> /// <typeparam name="T">The expected return type</typeparam> /// <param name="program">The donatello source code</param> /// <returns>the return value of the program</returns> public static T Run <T>(string program, string assemblyName = null, params string[] references) { const string namespaceName = "DonatelloRun"; const string className = "Runner"; const string methodName = "Run"; CompilationUnitSyntax syntaxTree = AntlrParser.ParseAsClass(program, namespaceName, className, methodName); Stream result = Compiler.Compile(assemblyName ?? namespaceName, references, OutputType.DynamicallyLinkedLibrary, syntaxTree); return(AssemblyRunner.Run <T>(result, namespaceName, className, methodName)); }
public void TestRunWithInput() { using (AssemblyRunner runner = new AssemblyRunner(Exe)) { List <string> lines = new List <string>(); runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); }; int exitCode = runner.Run(new StringReader("Hello World\r\nWhatever!\r\nAnother line.")); Assert.AreEqual(0, exitCode); // 0 == WorkingDirectory = // 1 == std-input: Assert.AreEqual("Hello World", lines[2]); Assert.AreEqual("Whatever!", lines[3]); Assert.AreEqual("Another line.", lines[4]); } }
public void TestOutputEventUnsubscribe() { using (AssemblyRunner runner = new AssemblyRunner(Exe)) { bool outputReceived = false; ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs e) { outputReceived = true; }; runner.OutputReceived += handler; runner.OutputReceived -= handler; Assert.AreEqual(0, runner.Run()); Assert.IsFalse(outputReceived); } }
public void TestExitedEvent() { using (AssemblyRunner runner = new AssemblyRunner(Exe)) { int exitCode = -1; bool receivedExit = false; ProcessExitedEventHandler handler = delegate(object o, ProcessExitedEventArgs e) { receivedExit = true; exitCode = e.ExitCode; }; runner.ProcessExited += handler; Assert.AreEqual(0, runner.Run()); Assert.IsTrue(receivedExit); Assert.AreEqual(0, exitCode); } }
public void TestRunWithWorkingDirectory() { using (TempDirectory dir = new TempDirectory()) using (AssemblyRunner runner = new AssemblyRunner(Exe)) { List <string> lines = new List <string>(); runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); }; Assert.AreNotEqual(dir.TempPath, runner.WorkingDirectory); runner.WorkingDirectory = dir.TempPath; Assert.AreEqual(dir.TempPath, runner.WorkingDirectory); int exitCode = runner.Run(); Assert.AreEqual(0, exitCode); Assert.AreEqual("WorkingDirectory = " + dir.TempPath, lines[0]); } }