public void ConsoleWriteLineTest() { // http://pytools.codeplex.com/workitem/649 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("import System"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); replWindow.ClearScreen(); execute = replEval.ExecuteText("System.Console.WriteLine(42)"); execute.Wait(); Assert.AreEqual(replWindow.Output, "42\r\n"); replWindow.ClearScreen(); Assert.AreEqual(execute.Result, ExecutionResult.Success); execute = replEval.ExecuteText("System.Console.Write(42)"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); Assert.AreEqual(replWindow.Output, "42"); }
public void AttachSupportMultiThreaded() { // http://pytools.codeplex.com/workitem/663 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var code = new[] { "import threading", "def sayHello():\r\n pass", "t1 = threading.Thread(target=sayHello)", "t1.start()", "t2 = threading.Thread(target=sayHello)", "t2.start()" }; foreach (var line in code) { var execute = replEval.ExecuteText(line); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); } replWindow.ClearScreen(); var finalExecute = replEval.ExecuteText("42"); finalExecute.Wait(); Assert.AreEqual(finalExecute.Result, ExecutionResult.Success); Assert.AreEqual(replWindow.Output, "42\r\n"); }
public void GenericMethodCompletions() { // http://pytools.codeplex.com/workitem/661 var fact = IronPythonInterpreter; var replEval = new PythonReplEvaluator(fact, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("from System.Threading.Tasks import Task"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); replWindow.ClearScreen(); execute = replEval.ExecuteText("def func1(): print 'hello world'\r\n\r\n"); execute.Wait(); replWindow.ClearScreen(); Assert.AreEqual(execute.Result, ExecutionResult.Success); execute = replEval.ExecuteText("t = Task.Factory.StartNew(func1)"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); using (var analyzer = new VsProjectAnalyzer(PythonToolsTestUtilities.CreateMockServiceProvider(), fact, new[] { fact })) { replWindow.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer); var names = replEval.GetMemberNames("t"); foreach (var name in names) { Debug.WriteLine(name.Name); } } }
public void BadInterpreterPath() { // http://pytools.codeplex.com/workitem/662 var emptyFact = InterpreterFactoryCreator.CreateInterpreterFactory( new InterpreterFactoryCreationOptions() { Description = "Test Interpreter", InterpreterPath = "C:\\Does\\Not\\Exist\\Some\\Interpreter.exe" } ); var replEval = new PythonReplEvaluator(emptyFact, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow); var execute = replEval.ExecuteText("42"); var errorText = replWindow.Error; const string expected = "The interactive window could not be started because the associated Python environment could not be found.\r\n" + "If this version of Python has recently been uninstalled, you can close this window.\r\n" + "Current interactive window is disconnected."; if (!errorText.Contains(expected)) { Assert.Fail(string.Format( "Did not find:\n{0}\n\nin:\n{1}", expected, errorText )); } }
public void NoTraceFunction() { // http://pytools.codeplex.com/workitem/662 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("import sys"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); replWindow.ClearScreen(); execute = replEval.ExecuteText("sys.gettrace()"); execute.Wait(); Assert.AreEqual(replWindow.Output, ""); replWindow.ClearScreen(); }
private static void TestOutput(MockReplWindow window, PythonReplEvaluator evaluator, string code, bool success, Action <bool> afterExecute, params string[] expectedOutput) { StringBuilder output = window.Output; output.Clear(); bool completed = false; evaluator.ExecuteText(code, (result) => { Assert.AreEqual(result.Success, success); if (output.Length == 0) { Assert.IsTrue(expectedOutput.Length == 0); } else { // don't count ending \n as new empty line output.Replace("\r\n", "\n"); if (output[output.Length - 1] == '\n') { output.Remove(output.Length - 1, 1); } var lines = output.ToString().Split('\n'); if (lines.Length != expectedOutput.Length) { Console.WriteLine(output.ToString()); } Assert.AreEqual(lines.Length, expectedOutput.Length); for (int i = 0; i < expectedOutput.Length; i++) { Assert.AreEqual(lines[i], expectedOutput[i]); } } completed = true; }); if (afterExecute != null) { afterExecute(completed); } for (int i = 0; i < 30 && !completed; i++) { Thread.Sleep(100); } if (!completed) { Assert.Fail("command didn't complete in 3 seconds"); } }
public void IronPythonCommentInput() { // http://pytools.codeplex.com/workitem/649 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("#fob\n1+2"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); }
public void NoTraceFunction() { // http://pytools.codeplex.com/workitem/662 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("import sys"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); execute = replEval.ExecuteText("sys.gettrace()"); execute.Wait(); AssertUtil.AreEqual( new Regex(@"\<bound method Thread.trace_func of \<Thread.+\>\>"), replWindow.Output ); replWindow.ClearScreen(); }
public void CommentFollowedByBlankLine() { // http://pytools.codeplex.com/workitem/659 var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("# fob\r\n\r\n \r\n\t\t\r\na = 42"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); replWindow.ClearScreen(); }
public void IronPythonModuleName() { var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); replWindow.ClearScreen(); var execute = replEval.ExecuteText("__name__"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); Assert.AreEqual(replWindow.Output, "'__main__'\r\n"); replWindow.ClearScreen(); }
public void IronPythonSignatures() { var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("from System import Array"); execute.Wait(); Assert.AreEqual(execute.Result, ExecutionResult.Success); var sigs = replEval.GetSignatureDocumentation("Array[int]"); Assert.AreEqual(sigs.Length, 1); Assert.AreEqual("Array[int](: int)\r\n", sigs[0].Documentation); }
public void IronPythonSignatures() { var replEval = new PythonReplEvaluator(IronPythonInterpreter, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("from System import Array"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); OverloadDoc[] sigs = null; for (int retries = 0; retries < 5 && sigs == null; retries += 1) { sigs = replEval.GetSignatureDocumentation("Array[int]"); } Assert.IsNotNull(sigs, "GetSignatureDocumentation timed out"); Assert.AreEqual(sigs.Length, 1); Assert.AreEqual("Array[int](: int)\r\n", sigs[0].Documentation); }
public void NoInterpreterPath() { // http://pytools.codeplex.com/workitem/662 var emptyFact = InterpreterFactoryCreator.CreateInterpreterFactory( new InterpreterFactoryCreationOptions() { Description = "Test Interpreter" } ); var replEval = new PythonReplEvaluator(emptyFact, PythonToolsTestUtilities.CreateMockServiceProvider(), new ReplTestReplOptions()); var replWindow = new MockReplWindow(replEval); replEval.Initialize(replWindow); var execute = replEval.ExecuteText("42"); Console.WriteLine(replWindow.Error); Assert.IsTrue( replWindow.Error.Contains("Test Interpreter cannot be started"), "Expected: <Test Interpreter cannot be started>\r\nActual: <" + replWindow.Error + ">" ); }
private static void TestOutput(MockReplWindow window, PythonReplEvaluator evaluator, string code, bool success, Action <bool> afterExecute, bool equalOutput, int timeout = 3000, params string[] expectedOutput) { window.ClearScreen(); bool completed = false; var task = evaluator.ExecuteText(code).ContinueWith(completedTask => { Assert.AreEqual(success, completedTask.Result.IsSuccessful); var output = success ? window.Output : window.Error; if (equalOutput) { if (output.Length == 0) { Assert.IsTrue(expectedOutput.Length == 0); } else { // don't count ending \n as new empty line output = output.Replace("\r\n", "\n"); if (output[output.Length - 1] == '\n') { output = output.Remove(output.Length - 1, 1); } var lines = output.Split('\n'); if (lines.Length != expectedOutput.Length) { for (int i = 0; i < lines.Length; i++) { Console.WriteLine("{0}: {1}", i, lines[i].ToString()); } } Assert.AreEqual(lines.Length, expectedOutput.Length); for (int i = 0; i < expectedOutput.Length; i++) { Assert.AreEqual(lines[i], expectedOutput[i]); } } } else { foreach (var line in expectedOutput) { Assert.IsTrue(output.Contains(line), string.Format("'{0}' does not contain '{1}'", output, line)); } } completed = true; }); if (afterExecute != null) { afterExecute(completed); } try { task.Wait(timeout); } catch (AggregateException ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw; } if (!completed) { Assert.Fail(string.Format("command didn't complete in {0} seconds", timeout / 1000.0)); } }