public void Update_RecalculatesSnapshot()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText));
            var textBuffer         = new Mock <ITextBuffer>();
            var textBufferSnapshot = Mock.Of <ITextSnapshot>();

            textBuffer.Setup(buffer => buffer.CreateEdit(EditOptions.None, null, It.IsAny <IInviolableEditTag>()))
            .Returns(edit.Object);
            textBuffer.Setup(buffer => buffer.CurrentSnapshot)
            .Returns(() => textBufferSnapshot);
            var editedSnapshot = Mock.Of <ITextSnapshot>();

            edit.Setup(e => e.Apply())
            .Callback(() =>
            {
                textBufferSnapshot = editedSnapshot;
            });
            var document = new HtmlVirtualDocument(Uri, textBuffer.Object);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            Assert.Same(editedSnapshot, document.CurrentSnapshot.Snapshot);
        }
示例#2
0
        public void Update_NoChanges_InvokesPostChangedEventTwice_NoEffectiveChanges()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var called     = 0;

            textBuffer.PostChanged += (s, a) =>
            {
                textBuffer.TryGetHostDocumentSyncVersion(out var version);
                Assert.Equal(1, version);

                called += 1;
            };

            var document = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(Array.Empty <TextChange>(), hostDocumentVersion: 1);

            // Assert
            Assert.Equal(2, called);
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Hello World", text);
        }
示例#3
0
        public void Update_AlwaysSetsHostDocumentSyncVersion_AndUpdatesSnapshot()
        {
            // Arrange
            var textBuffer       = new TestTextBuffer(StringTextSnapshot.Empty);
            var document         = new HtmlVirtualDocument(Uri, textBuffer);
            var originalSnapshot = document.CurrentSnapshot;

            // Act
            document.Update(Array.Empty <TextChange>(), hostDocumentVersion: 1337);

            // Assert
            Assert.NotSame(originalSnapshot, document.CurrentSnapshot);
            Assert.Equal(1337, document.HostDocumentSyncVersion);
        }
示例#4
0
        public void Update_Delete()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var delete     = new TextChange(new TextSpan(6, 5), string.Empty);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { delete }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Hello ", text);
        }
示例#5
0
        public void Update_Replace()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("original"));
            var replace    = new TextChange(new TextSpan(0, textBuffer.CurrentSnapshot.Length), "replaced text");
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal(replace.NewText, text);
        }
示例#6
0
        public void Update_Insert()
        {
            // Arrange
            var insert     = new TextChange(new TextSpan(0, 0), "inserted text");
            var textBuffer = new TestTextBuffer(StringTextSnapshot.Empty);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { insert }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal(insert.NewText, text);
        }
示例#7
0
        public void Update_MultipleEdits()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var replace    = new VisualStudioTextChange(6, 5, "Replaced");
            var delete     = new VisualStudioTextChange(0, 6, string.Empty);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace, delete }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Replaced", text);
        }
        public void Update_Delete()
        {
            // Arrange
            var delete = new TextChange(new TextSpan(123, 4), string.Empty);
            var edit   = new Mock <ITextEdit>();

            edit.Setup(e => e.Delete(delete.Span.Start, delete.Span.Length)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { delete }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
        public void Update_Replace()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
        public void Update_Insert()
        {
            // Arrange
            var insert = new TextChange(new TextSpan(123, 0), "inserted text");
            var edit   = new Mock <ITextEdit>();

            edit.Setup(e => e.Insert(insert.Span.Start, insert.NewText)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { insert }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
        public void Update_MultipleEdits()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var delete  = new TextChange(new TextSpan(123, 4), string.Empty);
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Delete(delete.Span.Start, delete.Span.Length)).Verifiable();
            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText)).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);

            edit.Setup(e => e.Apply())
            .Returns(textBuffer.CurrentSnapshot).Verifiable();
            var document = new HtmlVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace, delete }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }