Example #1
0
        public void LoadAndUnloadModule()
        {
            var factories = new[] { InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 3)) };
            var services  = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var analyzer = new VsProjectAnalyzer(services, factories[0])) {
                var m1Path = TestData.GetPath("TestData\\SimpleImport\\module1.py");
                var m2Path = TestData.GetPath("TestData\\SimpleImport\\module2.py");

                var taskEntry1 = analyzer.AnalyzeFileAsync(m1Path);
                var taskEntry2 = analyzer.AnalyzeFileAsync(m2Path);
                taskEntry1.Wait(CancellationTokens.After5s);
                taskEntry2.Wait(CancellationTokens.After5s);
                var entry1 = taskEntry1.Result;
                var entry2 = taskEntry2.Result;

                var cancel = CancellationTokens.After60s;
                analyzer.WaitForCompleteAnalysis(_ => !cancel.IsCancellationRequested);
                cancel.ThrowIfCancellationRequested();

                var loc = new Microsoft.PythonTools.Parsing.SourceLocation(0, 1, 1);
                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc),
                    "int"
                    );

                analyzer.UnloadFileAsync(entry1).Wait();
                analyzer.WaitForCompleteAnalysis(_ => true);

                // Even though module1 has been unloaded, we still know that
                // module2 imports it.
                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc)
                    );

                analyzer.AnalyzeFileAsync(m1Path).Wait();
                analyzer.WaitForCompleteAnalysis(_ => true);

                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc),
                    "int"
                    );
            }
        }
Example #2
0
        private static void CodeFormattingTest(string input, object selection, string expected, object expectedSelection, CodeFormattingOptions options, bool selectResult = true)
        {
            var fact     = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7));
            var services = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var analyzer = new VsProjectAnalyzer(services, fact)) {
                var buffer = new MockTextBuffer(input, PythonCoreConstants.ContentType, "C:\\fob.py");
                buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                var view  = new MockTextView(buffer);
                var bi    = services.GetBufferInfo(buffer);
                var entry = analyzer.AnalyzeFileAsync(bi.Filename).WaitAndUnwrapExceptions();
                Assert.AreEqual(entry, bi.TrySetAnalysisEntry(entry, null), "Failed to set analysis entry");
                entry.GetOrCreateBufferParser(services).AddBuffer(buffer);

                var selectionSpan = new SnapshotSpan(
                    buffer.CurrentSnapshot,
                    ExtractMethodTests.GetSelectionSpan(input, selection)
                    );
                view.Selection.Select(selectionSpan, false);

                analyzer.FormatCodeAsync(
                    selectionSpan,
                    view,
                    options,
                    selectResult
                    ).Wait();

                Assert.AreEqual(expected, view.TextBuffer.CurrentSnapshot.GetText());
                if (expectedSelection != null)
                {
                    Assert.AreEqual(
                        ExtractMethodTests.GetSelectionSpan(expected, expectedSelection),
                        view.Selection.StreamSelectionSpan.SnapshotSpan.Span
                        );
                }
            }
        }
Example #3
0
        private void ExtractMethodTest(string input, Func <Span> extract, TestResult expected, string scopeName = null, string targetName = "g", Version version = null, params string[] parameters)
        {
            var fact     = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version ?? new Version(2, 7));
            var services = PythonToolsTestUtilities.CreateMockServiceProvider(suppressTaskProvider: true).GetEditorServices();

            using (var analyzer = new VsProjectAnalyzer(services, fact)) {
                var buffer = new MockTextBuffer(input, "Python", "C:\\fob.py");
                var view   = new MockTextView(buffer);
                buffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer);

                var bi    = services.GetBufferInfo(buffer);
                var entry = analyzer.AnalyzeFileAsync(bi.Filename).WaitAndUnwrapExceptions();
                Assert.AreEqual(entry, bi.TrySetAnalysisEntry(entry, null));
                entry.GetOrCreateBufferParser(services).AddBuffer(buffer);

                var extractInput = new ExtractMethodTestInput(true, scopeName, targetName, parameters ?? new string[0]);

                view.Selection.Select(
                    new SnapshotSpan(view.TextBuffer.CurrentSnapshot, extract()),
                    false
                    );

                new MethodExtractor(services.Site, view).ExtractMethod(extractInput).Wait();

                if (expected.IsError)
                {
                    Assert.AreEqual(expected.Text, extractInput.FailureReason);
                    Assert.AreEqual(input, view.TextBuffer.CurrentSnapshot.GetText());
                }
                else
                {
                    Assert.AreEqual(null, extractInput.FailureReason);
                    Assert.AreEqual(expected.Text, view.TextBuffer.CurrentSnapshot.GetText());
                }
            }
        }