Example #1
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 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);
     }
 }
Example #4
0
 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();
 }
Example #5
0
 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();
     }
 }
Example #6
0
        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);
        }
Example #7
0
        public void IronPythonSignatures() {
            using (var replEval = Evaluator) {
                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);
            }
        }
Example #8
0
        public void ExecuteTest() {
            using (var evaluator = MakeEvaluator()) {
                var window = new MockReplWindow(evaluator);
                evaluator._Initialize(window);

                TestOutput(window, evaluator, "print 'hello'", true, "hello");
                TestOutput(window, evaluator, "42", true, "42");
                TestOutput(window, evaluator, "for i in xrange(2):  print i\n", true, "0", "1");
                TestOutput(window, evaluator, "raise Exception()\n", false, "Traceback (most recent call last):", "  File \"<stdin>\", line 1, in <module>", "Exception");

                TestOutput(window, evaluator, "try:\r\n    print 'hello'\r\nexcept:\r\n    print 'goodbye'\r\n    \r\n    ", true, "hello");
                TestOutput(window, evaluator, "try:\r\n    print 'hello'\r\nfinally:\r\n    print 'goodbye'\r\n    \r\n    ", true, "hello", "goodbye");

                TestOutput(window, evaluator, "import sys", true);
                TestOutput(window, evaluator, "sys.exit(0)", false);
            }
        }
        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);
                }
            }
        }
Example #10
0
        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"
                );
            }
        }
Example #11
0
        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);
            }
        }
Example #12
0
        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: "));
            }
        }
Example #13
0
        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:"));
            }
        }
Example #14
0
        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>"));
            }
        }
Example #15
0
        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);
            }
        }
Example #16
0
 private static void TestOutput(MockReplWindow window, PythonInteractiveEvaluator evaluator, string code, bool success, params string[] expectedOutput) {
     TestOutput(window, evaluator, code, success, null, true, 3000, expectedOutput);
 }
Example #17
0
        public async Task NoInterpreterPath() {
            // http://pytools.codeplex.com/workitem/662

            var replEval = new PythonInteractiveEvaluator(PythonToolsTestUtilities.CreateMockServiceProvider()) {
                DisplayName = "Test Interpreter"
            };
            var replWindow = new MockReplWindow(replEval);
            await replEval._Initialize(replWindow);
            await 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 + ">"
            );
        }
Example #18
0
        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) {
                }
            }
        }
Example #19
0
        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);
            }
        }
Example #20
0
        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);
            }
        }
Example #21
0
        public void BadInterpreterPath() {
            // http://pytools.codeplex.com/workitem/662

            var replEval = new PythonInteractiveEvaluator(PythonToolsTestUtilities.CreateMockServiceProvider()) {
                DisplayName = "Test Interpreter",
                Configuration = new LaunchConfiguration(new InterpreterConfiguration("InvalidInterpreter", "Test Interpreter", path: "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
                ));
            }
        }
Example #22
0
 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);
     }
 }
Example #23
0
        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");
            }
        }
Example #24
0
        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));
            }
        }
Example #25
0
        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);
                }
            }
        }
Example #26
0
        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);
            }
        }
Example #27
0
        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);
            }
        }
Example #28
0
        public async Task TestGetAllMembers() {
            using (var evaluator = MakeEvaluator()) {
                var window = new MockReplWindow(evaluator);
                await evaluator._Initialize(window);

                // Run the ExecuteText on another thread so that we don't continue
                // onto the REPL evaluation thread, which leads to GetMemberNames being
                // blocked as it's hogging the event loop.
                AutoResetEvent are = new AutoResetEvent(false);
                ThreadPool.QueueUserWorkItem(async (x) => {
                        await evaluator.ExecuteText("globals()['my_new_value'] = 123");
                        are.Set();
                    }
                );
                are.WaitOne();
                var names = evaluator.GetMemberNames("");
                Assert.IsNotNull(names);
                AssertUtil.ContainsAtLeast(names.Select(m => m.Name), "my_new_value");
            }
        }
Example #29
0
 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);
     }
 }
Example #30
0
        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);
            }
        }