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 AttachSupportMultiThreaded() { // http://pytools.codeplex.com/workitem/663 using (var replEval = Evaluator) { 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.IsTrue(execute.Result.IsSuccessful); } replWindow.ClearScreen(); var finalExecute = replEval.ExecuteText("42"); finalExecute.Wait(); Assert.IsTrue(finalExecute.Result.IsSuccessful); Assert.AreEqual(replWindow.Output, "42\n"); } }
public void BadInterpreterPath() { // http://pytools.codeplex.com/workitem/662 var replEval = new PythonInteractiveEvaluator(PythonToolsTestUtilities.CreateMockServiceProvider()) { DisplayName = "Test Interpreter", Configuration = new LaunchConfiguration(new VisualStudioInterpreterConfiguration("InvalidInterpreter", "Test Interpreter", pythonExePath: "C:\\Does\\Not\\Exist\\Some\\Interpreter.exe")) }; var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow); var execute = replEval.ExecuteText("42"); var errorText = replWindow.Error; const string expected = "the associated Python environment could not be found."; if (!errorText.Contains(expected)) { Assert.Fail(string.Format( "Did not find:\n{0}\n\nin:\n{1}", expected, errorText )); } }
public void GenericMethodCompletions() { // http://pytools.codeplex.com/workitem/661 using (var replEval = Evaluator) { var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("from System.Threading.Tasks import Task"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); execute = replEval.ExecuteText("def func1(): print 'hello world'\r\n\r\n"); execute.Wait(); replWindow.ClearScreen(); Assert.IsTrue(execute.Result.IsSuccessful); execute = replEval.ExecuteText("t = Task.Factory.StartNew(func1)"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); replWindow.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), replEval.Analyzer); CompletionResult[] names = null; for (int retries = 0; retries < 5 && names == null; retries += 1) { names = replEval.GetMemberNames("t"); } Assert.IsNotNull(names, "GetMemberNames call timed out"); foreach (var name in names) { Debug.WriteLine(name.Name); } } }
public void Reset() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("1"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("1", window.Output); res = window.Reset(); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("The process has exited" + Environment.NewLine, window.Error); window.ClearScreen(); Assert.AreEqual("", window.Output); Assert.AreEqual("", window.Error); //Check to ensure the REPL continues to work after Reset res = eval.ExecuteText("var a = 1"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined", window.Output); res = eval.ExecuteText("a"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined1", window.Output); } }
public void TestAbort() { using (var evaluator = MakeEvaluator()) { var window = new MockReplWindow(evaluator); evaluator._Initialize(window); TestOutput( window, evaluator, "while True: pass\n", false, (completed) => { Assert.IsTrue(!completed); Thread.Sleep(1000); evaluator.AbortExecution(); }, false, 20000, "KeyboardInterrupt" ); } }
public void ConsoleWriteLineTest() { // http://pytools.codeplex.com/workitem/649 using (var replEval = Evaluator) { var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("import System"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); execute = replEval.ExecuteText("System.Console.WriteLine(42)"); execute.Wait(); Assert.AreEqual(replWindow.Output, "42\n"); replWindow.ClearScreen(); Assert.IsTrue(execute.Result.IsSuccessful); execute = replEval.ExecuteText("System.Console.Write(42)"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); Assert.AreEqual(replWindow.Output, "42"); } }
public void Save() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval, NodejsConstants.JavaScript); window.ClearScreen(); var res = window.Execute("function f() { }"); Assert.IsTrue(res.Wait(10000)); res = window.Execute("function g() { }"); Assert.IsTrue(res.Wait(10000)); var path = Path.GetTempFileName(); File.Delete(path); new SaveReplCommand().Execute(window, path).Wait(10000); Assert.IsTrue(File.Exists(path)); var saved = File.ReadAllText(path); Assert.IsTrue(saved.IndexOf("function f") != -1); Assert.IsTrue(saved.IndexOf("function g") != -1); Assert.IsTrue(window.Output.Contains("Session saved to:")); } }
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 RequireInProject() { string testDir; do { testDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); } while (Directory.Exists(testDir)); Directory.CreateDirectory(testDir); var moduleDir = Path.Combine(testDir, "node_modules"); Directory.CreateDirectory(moduleDir); File.WriteAllText(Path.Combine(moduleDir, "foo.js"), "exports.foo = function(a, b, c) { }"); File.WriteAllText(Path.Combine(testDir, "bar.js"), "exports.bar = function(a, b, c) { }"); try { using (var eval = new NodejsReplEvaluator(new TestNodejsReplSite(null, testDir))) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("require('foo.js');"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual(window.Output, "{ foo: [Function] }"); window.ClearScreen(); res = eval.ExecuteText("require('./bar.js');"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual(window.Output, "{ bar: [Function] }"); } } finally { try { Directory.Delete(testDir, true); } catch (IOException) { } } }
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"); }
private static void TestOutput(MockReplWindow window, PythonInteractiveEvaluator 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) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } throw; } if (!completed) { Assert.Fail(string.Format("command didn't complete in {0} seconds", timeout / 1000.0)); } }
public void TestInit() { Version.AssertInstalled(); var serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider(); _evaluator = new PythonDebugReplEvaluator(serviceProvider); _window = new MockReplWindow(_evaluator); _evaluator._Initialize(_window); _processes = new List <PythonProcess>(); }
public void Number() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("42"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual(window.Output, "42"); } }
public void ObjectLiteral() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("{x:42}"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("{ x: 42 }", window.Output); } }
public void Require() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("require('http').constructor"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("[Function: Object]", window.Output); } }
public void ConsoleWarn() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("console.warn('hi')"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("hi\r\n", window.Error); } }
public void ConsoleLog() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("console.log('hi')"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("hi\r\nundefined", window.Output); } }
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 using (var replEval = Evaluator) { var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); var execute = replEval.ExecuteText("#fob\n1+2"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); } }
public void ConsoleDir() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("console.dir({'abc': {'foo': [1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]}})"); var expected = @"{ abc: { foo: [ 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ] } } undefined"; Assert.IsTrue(res.Wait(10000)); var received = window.Output; AreEqual(expected, received); } }
public void Exception() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("throw 'an error';"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("an error", window.Error); } }
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 CommentFollowedByBlankLine() { // http://pytools.codeplex.com/workitem/659 using (var replEval = Evaluator) { 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.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); } }
public void ExceptionUndefined() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("throw undefined;"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined", window.Output); } }
public async Task TestGetAllMembers() { using (var evaluator = MakeEvaluator()) { var window = new MockReplWindow(evaluator); await evaluator.Initialize(window); await evaluator.ExecuteText("globals()['my_new_value'] = 123"); var names = evaluator.GetMemberNames(""); Assert.IsNotNull(names); AssertUtil.ContainsAtLeast(names.Select(m => m.Name), "my_new_value"); } }
public void IronPythonModuleName() { using (var replEval = Evaluator) { var replWindow = new MockReplWindow(replEval); replEval._Initialize(replWindow).Wait(); replWindow.ClearScreen(); var execute = replEval.ExecuteText("__name__"); execute.Wait(); Assert.IsTrue(execute.Result.IsSuccessful); Assert.AreEqual(replWindow.Output, "'__main__'\n"); 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(); }