示例#1
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();
 }
示例#2
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();
     }
 }
示例#3
0
 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");
     }
 }
示例#4
0
 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);
     }
 }
示例#5
0
        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);
                }
            }
        }
示例#6
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:"));
            }
        }
示例#7
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>"));
            }
        }
示例#8
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);
            }
        }
示例#9
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);
            }
        }
示例#10
0
        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");
            }
        }
示例#11
0
        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");
        }
示例#12
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);
     }
 }
示例#13
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) {
                }
            }
        }
示例#14
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));
            }
        }
示例#15
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");
            }
        }
示例#16
0
        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");
        }
示例#17
0
 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();
 }
示例#18
0
        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();
        }
示例#19
0
        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);
                }
            }
        }
示例#20
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: "));
            }
        }
示例#21
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);
            }
        }
示例#22
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");
            }
        }
示例#23
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);
            }
        }
示例#24
0
 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);
     }
 }
示例#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);
                }
            }
        }
示例#26
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);
            }
        }
示例#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);
            }
        }
示例#28
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);
            }
        }
示例#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);
     }
 }
示例#30
0
 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();
     }
 }