Exemplo n.º 1
0
 public MockTextSnapshot(MockTextBuffer buffer, string text, MockTextSnapshot prevVersion, params ITextChange[] changes)
 {
     _text    = text;
     _buffer  = buffer;
     _version = new MockTextVersion(prevVersion.Version.VersionNumber + 1, this);
     ((MockTextVersion)prevVersion.Version).SetNext(_version, changes);
 }
Exemplo n.º 2
0
 public MockTextSnapshot(MockTextBuffer buffer, string text)
 {
     _text = text;
     _buffer = buffer;
     _version = new MockTextVersion(0, this);
     _lines = MakeLines(_text).ToList();
 }
        public void SnapshotSpanSourceCodeReaderTest() {
            var text = "hello world\r\nHello again!";
            var buffer = new MockTextBuffer(text);
            var snapshot = new MockTextSnapshot(buffer, text);

            var reader = new SnapshotSpanSourceCodeReader(new SnapshotSpan(snapshot, new Span(0, text.Length)));

            Assert.AreEqual(reader.Snapshot, snapshot);
            Assert.AreEqual(reader.Position, 0);
            var line = reader.ReadLine();
            Assert.AreEqual(line, "hello world");
            line = reader.ReadLine();
            Assert.AreEqual(line, "Hello again!");

            reader = new SnapshotSpanSourceCodeReader(new SnapshotSpan(snapshot, new Span(0, text.Length)));
            int ch = reader.Peek();
            Assert.AreEqual(ch, (int)'h');

            char[] readBuf = new char[text.Length];
            reader.Read(readBuf, 0, readBuf.Length);

            Assert.AreEqual(new string(readBuf), text);

            reader = new SnapshotSpanSourceCodeReader(new SnapshotSpan(snapshot, new Span(0, text.Length)));
            Assert.AreEqual(reader.ReadToEnd(), text);

            reader.Reset();

            Assert.AreEqual(reader.ReadToEnd(), text);

            reader.Close();
            Assert.AreEqual(reader.Snapshot, null);
        }
Exemplo n.º 4
0
 public MockTextSnapshot(MockTextBuffer buffer, string text, MockTextSnapshot prevVersion, params ITextChange[] changes)
 {
     _text = text;
     _buffer = buffer;
     _version = new MockTextVersion(prevVersion.Version.VersionNumber + 1, this);
     ((MockTextVersion)prevVersion.Version).SetNext(_version, changes);
     _lines = MakeLines(_text).ToList();
 }
Exemplo n.º 5
0
        private void DataTipTest(string input, string selectionRegex, string expectedDataTip) {
            var buffer = new MockTextBuffer(input, @"C:\fob.js", "Node.js");
            var view = new MockTextView(buffer);

            var classifierProvider = new NodejsClassifierProvider(new MockContentTypeRegistryService(NodejsConstants.Nodejs));
            classifierProvider._classificationRegistry = new MockClassificationTypeRegistryService();
            classifierProvider.GetClassifier(buffer);

            var analyzer = new VsProjectAnalyzer();
            buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);
            analyzer.AddBuffer(buffer);
            analyzer.WaitForCompleteAnalysis();

            var m = Regex.Match(input, selectionRegex);
            Assert.IsTrue(m.Success);

            var startPos = m.Index;
            var startLine = buffer.CurrentSnapshot.GetLineFromPosition(startPos);
            var endPos = m.Index + m.Length;
            var endLine = buffer.CurrentSnapshot.GetLineFromPosition(endPos);
            var selectionSpan = new TextSpan {
                iStartLine = startLine.LineNumber,
                iStartIndex = startPos - startLine.Start.Position,
                iEndLine = endLine.LineNumber,
                iEndIndex = endPos - endLine.Start.Position
            };

            var dataTipSpan = DataTipTextViewFilter.GetDataTipSpan(view, selectionSpan);
            if (expectedDataTip == null) {
                Assert.IsNull(dataTipSpan);
                return;
            }

            Assert.IsNotNull(dataTipSpan);
            var actualSpan = dataTipSpan.Value;

            startPos = input.IndexOf(expectedDataTip);
            Assert.AreNotEqual(-1, startPos);
            startLine = buffer.CurrentSnapshot.GetLineFromPosition(startPos);
            endPos = startPos + expectedDataTip.Length;
            endLine = buffer.CurrentSnapshot.GetLineFromPosition(endPos);
            var expectedSpan = new TextSpan {
                iStartLine = startLine.LineNumber,
                iStartIndex = startPos - startLine.Start.Position,
                iEndLine = endLine.LineNumber,
                iEndIndex = endPos - endLine.Start.Position
            };

            // TextSpan doesn't override ToString, so test output is unusable in case of failure when comparing
            // two spans directly - use an anonymous type instead to produce pretty output.
            Assert.AreEqual(
                new { expectedSpan.iStartLine, expectedSpan.iStartIndex, expectedSpan.iEndLine, expectedSpan.iEndIndex },
                new { actualSpan.iStartLine, actualSpan.iStartIndex, actualSpan.iEndLine, actualSpan.iEndIndex });
        }
        public void TypeScriptPortedTests() {
            dynamic testCases = _serializer.DeserializeObject("[" + File.ReadAllText("ruleFormattingTests.json") + "]");
            
            var options = new FormattingOptions();
            int testCaseId = 0;
            foreach (var testCase in testCases) {
                string filename = "fob" + testCaseId++ + ".js";
                Console.WriteLine(testCase["input"]);
                var buffer = new MockTextBuffer(testCase["input"], "test.js", "Node.js");
                if (!ApplyEdits(filename, testCase, buffer)) {
                    continue;
                }

                Assert.AreEqual(
                    ((string)testCase["expected"]).Replace("\r\n", "\n").Replace("\n", "\r\n"),
                    buffer.CurrentSnapshot.GetText().Replace("\r\n", "\n").Replace("\n", "\r\n")
                );
            }
        }
Exemplo n.º 7
0
        private void Run()
        {
            PerformanceLogger.DumpEvents += PerformanceLogger_DumpEvents;
            try {
                foreach (var file in _testFiles) {
                    var buffer = new MockTextBuffer(File.ReadAllText(file));
                    var snapshot = buffer.CurrentSnapshot;

                    var behaviour = new LineBehavior {
                        VisibleEmpty = true,
                        VisibleEmptyAtEnd = true,
                        VisibleAligned = true,
                        VisibleAtTextEnd = false,
                        ExtendInwardsOnly = true,
                        VisibleUnaligned = true
                    };

                    foreach (var chunkSize in new[] { 5, 10, 30, 50, 100, 150, 200 }) {
                        var da = new DocumentAnalyzer(snapshot, behaviour, 4, 4, chunkSize);
                        var sw = Stopwatch.StartNew();
                        for (int repeats = 1000; repeats > 0; --repeats) {
                            da.Reset().GetAwaiter().GetResult();
                        }

                        for (int line = 0; line < da.Snapshot.LineCount; line += 30) {
                            var lines = da.GetLines(line, line + 35).ToList();
                        }

                        sw.Stop();

                        Console.WriteLine("ChunkSize = {0}", chunkSize);
                        Console.WriteLine("Duration = {0}", sw.ElapsedTicks / 1000);
                        PrintEventSummary(1000);
                        Console.WriteLine();
                    }
                }
            } finally {
                PerformanceLogger.DumpEvents -= PerformanceLogger_DumpEvents;
            }
        }
Exemplo n.º 8
0
 public MockTextSnapshot(MockTextBuffer buffer, string text)
 {
     _text    = text;
     _buffer  = buffer;
     _version = new MockTextVersion(0, this);
 }
Exemplo n.º 9
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 serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            using (var analyzer = new VsProjectAnalyzer(serviceProvider, fact)) {
                var buffer = new MockTextBuffer(input, "Python", "C:\\fob.py");
                var view = new MockTextView(buffer);
                buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                analyzer.MonitorTextBufferAsync(buffer).Wait();
                var extractInput = new ExtractMethodTestInput(true, scopeName, targetName, parameters ?? new string[0]);

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

                new MethodExtractor(serviceProvider, 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());
                }
            }
        }
 public MockTextSnapshot(MockTextBuffer buffer, string text) {
     _text = text;
     _buffer = buffer;
     _version = new MockTextVersion(0, this);
 }
Exemplo n.º 11
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 serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            using (var analyzer = new VsProjectAnalyzer(serviceProvider, fact, new[] { fact })) {
                var buffer = new MockTextBuffer(input, PythonCoreConstants.ContentType, "C:\\fob.py");
                buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                var view = new MockTextView(buffer);
                var selectionSpan = new SnapshotSpan(
                    buffer.CurrentSnapshot,
                    ExtractMethodTests.GetSelectionSpan(input, selection)
                );
                view.Selection.Select(selectionSpan, false);

                new CodeFormatter(serviceProvider, view, options).FormatCode(
                    selectionSpan,
                    selectResult
                );

                Assert.AreEqual(expected, view.TextBuffer.CurrentSnapshot.GetText());
                if (expectedSelection != null) {
                    Assert.AreEqual(
                        ExtractMethodTests.GetSelectionSpan(expected, expectedSelection),
                        view.Selection.StreamSelectionSpan.SnapshotSpan.Span
                    );
                }
            }
        }
        private static bool ApplyEdits(string filename, dynamic testCase, MockTextBuffer buffer) {
            foreach (var operation in testCase["operations"]) {
                Edit[] edits = null;
                switch ((string)operation["operation"]) {
                    case "Document":
                        edits = Formatter.GetEditsForDocument(
                            buffer.CurrentSnapshot.GetText(),
                            Options
                        );
                        break;
                    case "CloseBrace":
                        edits = Formatter.GetEditsAfterKeystroke(
                            buffer.CurrentSnapshot.GetText(),
                            (int)operation["point"]["position"],
                           '}',
                            Options
                        );
                        break;
                    case "Enter":
                        var line = buffer.CurrentSnapshot.GetLineFromPosition((int)operation["point"]["position"]);
                        if(line.LineNumber > 0) {
                            edits = Formatter.GetEditsAfterEnter(
                                buffer.CurrentSnapshot.GetText(),
                                buffer.CurrentSnapshot.GetLineFromLineNumber(line.LineNumber - 1).Start,
                                line.End,
                                Options
                            );
                        }
                        break;
                    case "Semicolon":
                        edits = Formatter.GetEditsAfterKeystroke(
                            buffer.CurrentSnapshot.GetText(),
                            (int)operation["point"]["position"],
                           ';',
                            Options
                        );
                        break;
                    case "Paste":
                        edits = Formatter.GetEditsForRange(
                            buffer.CurrentSnapshot.GetText(),
                            (int)operation["span"]["start"],
                            (int)operation["span"]["start"] + (int)operation["span"]["length"],
                            Options
                        );
                        break;
                    case "Selection":
                        edits = Formatter.GetEditsForRange(
                            buffer.CurrentSnapshot.GetText(),
                            (int)operation["span"]["start"],
                            (int)operation["span"]["start"] + (int)operation["span"]["length"],
                            Options
                        );
                        break;
                    default:
                        Console.WriteLine("Skipping unsupported test case " + operation["operation"]);
                        return false;
                }

                EditFilter.ApplyEdits(buffer, edits);
            }
            return true;
        }
 private static void FormatSelectionTest(string input, string expected, int start, int end, FormattingOptions options = null) {
     string pathToFile = "fob.py";
     var buffer = new MockTextBuffer(input, pathToFile, "Node.js");
     var edits = Formatter.GetEditsForRange(
         buffer.CurrentSnapshot.GetText(),
         start,
         end,
         options ?? new FormattingOptions()
     );
     EditFilter.ApplyEdits(buffer, edits);
     Assert.AreEqual(expected, buffer.CurrentSnapshot.GetText());
 }
 public MockVsTextLines(IServiceProvider serviceProvider, MockTextBuffer buffer) {
     _serviceProvider = serviceProvider;
     _buffer = buffer;
 }