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.IsTrue(execute.Result.IsSuccessful); Assert.AreEqual(replWindow.Output, "'__main__'\r\n"); replWindow.ClearScreen(); }
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 TestNumber() { 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 TestRequire() { 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 TestFunctionDefinition() { var whitespaces = new[] { "", "\r\n", " ", "\r\n " }; using (var eval = ProjectlessEvaluator()) { foreach (var whitespace in whitespaces) { Console.WriteLine("Whitespace: {0}", whitespace); var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText(whitespace + "function f() { }"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined", window.Output); window.ClearScreen(); res = eval.ExecuteText("f"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("[Function: f]", window.Output); } } }
public void TestSave() { 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 TestSaveBadFile() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("function f() { }"); Assert.IsTrue(res.Wait(10000)); res = eval.ExecuteText("function g() { }"); Assert.IsTrue(res.Wait(10000)); new SaveReplCommand().Execute(window, "<foo>").Wait(10000); Assert.IsTrue(window.Error.Contains("Invalid filename: <foo>")); } }
public void TestReset() { 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 TestProcessExit() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("process.exit(0);"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("The process has exited" + Environment.NewLine, window.Error); window.ClearScreen(); res = eval.ExecuteText("42"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("Current interactive window is disconnected - please reset the process.\r\n", window.Error); } }
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 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.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); execute = replEval.ExecuteText("System.Console.WriteLine(42)"); execute.Wait(); Assert.AreEqual(replWindow.Output, "42\r\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 TestObjectLiteral() { 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 TestRequireInProject() { 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) { } } }
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) { throw ex.InnerException; } throw; } if (!completed) { Assert.Fail(string.Format("command didn't complete in {0} seconds", timeout / 1000.0)); } }
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 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.IsTrue(execute.Result.IsSuccessful); } replWindow.ClearScreen(); var finalExecute = replEval.ExecuteText("42"); finalExecute.Wait(); Assert.IsTrue(finalExecute.Result.IsSuccessful); Assert.AreEqual(replWindow.Output, "42\r\n"); }
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.IsTrue(execute.Result.IsSuccessful); replWindow.ClearScreen(); }
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 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.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); using (var analyzer = new VsProjectAnalyzer(PythonToolsTestUtilities.CreateMockServiceProvider(), fact, new[] { fact })) { replWindow.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer); MemberResult[] 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 TestBadSave() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("function f() { }"); Assert.IsTrue(res.Wait(10000)); res = eval.ExecuteText("function g() { }"); Assert.IsTrue(res.Wait(10000)); new SaveReplCommand().Execute(window, "C:\\Some\\Directory\\That\\Does\\Not\\Exist\\foo.js").Wait(10000); Assert.IsTrue(window.Error.Contains("Failed to save: ")); } }
public void TestVarI() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("i"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("ReferenceError: i is not defined", window.Error); Assert.AreEqual("", window.Output); res = eval.ExecuteText("var i = 987654;"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined", window.Output); res = eval.ExecuteText("i"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("undefined987654", window.Output); } }
public void TestNpmReplRedirector() { using (var eval = ProjectlessEvaluator()) { var mockWindow = new MockReplWindow(eval) { ShowAnsiCodes = true }; mockWindow.ClearScreen(); var redirector = new NpmReplCommand.NpmReplRedirector(mockWindow); redirector.WriteLine("npm The sky is at a stable equilibrium"); var expectedInfoLine = NpmReplCommand.NpmReplRedirector.NormalAnsiColor + "npm The sky is at a stable equilibrium" + Environment.NewLine; Assert.AreEqual(expectedInfoLine, mockWindow.Output); Assert.IsFalse(redirector.HasErrors); mockWindow.ClearScreen(); redirector.WriteLine("npm WARN The sky is at an unstable equilibrium!"); var expectedWarnLine = NpmReplCommand.NpmReplRedirector.WarnAnsiColor + "npm WARN" + NpmReplCommand.NpmReplRedirector.NormalAnsiColor + " The sky is at an unstable equilibrium!" + Environment.NewLine; Assert.AreEqual(expectedWarnLine, mockWindow.Output); Assert.IsFalse(redirector.HasErrors); mockWindow.ClearScreen(); redirector.WriteLine("npm ERR! The sky is falling!"); var expectedErrorLine = NpmReplCommand.NpmReplRedirector.ErrorAnsiColor + "npm ERR!" + NpmReplCommand.NpmReplRedirector.NormalAnsiColor + " The sky is falling!" + Environment.NewLine; Assert.AreEqual(expectedErrorLine, mockWindow.Output); Assert.IsTrue(redirector.HasErrors); mockWindow.ClearScreen(); var decodedInfoLine = "├── [email protected]"; string encodedText = Console.OutputEncoding.GetString(Encoding.UTF8.GetBytes(decodedInfoLine)); redirector.WriteLine(encodedText); var expectedDecodedInfoLine = NpmReplCommand.NpmReplRedirector.NormalAnsiColor + decodedInfoLine + Environment.NewLine; Assert.AreEqual(expectedDecodedInfoLine, mockWindow.Output); Assert.IsTrue(redirector.HasErrors, "Errors should remain until end"); } }
public void TestConsoleDir() { 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 ConsoleError() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("console.error('hi')"); Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("hi\r\n", window.Error); } }
public async Task TestNpmReplCommandProcessExitSucceeds() { var npmPath = Nodejs.GetPathToNodeExecutable("npm.cmd"); using (var eval = ProjectlessEvaluator()) { var mockWindow = new MockReplWindow(eval) { ShowAnsiCodes = true }; mockWindow.ClearScreen(); var redirector = new NpmReplCommand.NpmReplRedirector(mockWindow); for (int j = 0; j < 200; j++) { await NpmReplCommand.ExecuteNpmCommandAsync( redirector, npmPath, null, new[] {"config", "get", "registry"}, null); } } }
public void LargeOutput() { using (var eval = ProjectlessEvaluator()) { var window = new MockReplWindow(eval); window.ClearScreen(); var res = eval.ExecuteText("var x = 'abc'; for(i = 0; i<12; i++) { x += x; }; x"); string expected = "abc"; for (int i = 0; i < 12; i++) { expected += expected; } Assert.IsTrue(res.Wait(10000)); Assert.AreEqual("'" + expected + "'", window.Output); } }
public void TestException() { 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 TestExceptionUndefined() { 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 void TestConsoleLog() { 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); } }
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(); } }