Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/EditorFeatures/Test/EditorServicesTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<Compile Include="Diagnostics\GenerateType\TestProjectManagementService.cs" />
<Compile Include="Diagnostics\SuppressMessageAttributeTests.cs" />
<Compile Include="CodeFixes\ExtensionOrderingTests.cs" />
<Compile Include="Extensions\SourceTextContainerExtensionsTests.cs" />
<Compile Include="Preview\TestOnly_CompilerDiagnosticAnalyzerProviderService.cs" />
<Compile Include="DocCommentFormatting\DocCommentFormattingTests.cs" />
<Compile Include="DocumentationComments\AbstractDocumentationCommentTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.CodeAnalysis.Text;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
{
public class SourceTextContainerExtensionsTests
{
[Fact]
public void GetBufferTextFromNonTextContainerThrows()
{
var containerMock = new Mock<SourceTextContainer>();
Assert.Throws<ArgumentException>(containerMock.Object.GetTextBuffer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just testing the behavior of the mocking framework?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention was to test that the GetTextBuffer extension method threw an argument exception when provided with a SourceTextContainer that was not a TextBufferContainer or derived type. Would it have been better to have written it as
Assert.Throws<ArgumentException>(() => Microsoft.CodeAnalysis.Text.Extensions.GetTextBuffer(containerMock.Object));

Or something similar?

I did also look at asserting on the ArgumentException message to make sure that the update resource text was returned however as this also returns the parameter name as part of the message I wasn't confident that this wouldn't make the test too brittle because someone could easily break the test just by changing the parameter name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, both @rchande and I were tricked by the extension method, so switching to the more obvious invocation would probably prevent others from being confused as well.

}

[Fact]
public void GetBufferTextFromTextContainerReturnsTextBuffer()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this actually testing? Is it trying to test that the creation of the text buffer container does not mutate the text buffer, or just that the GetTextBuffer method works at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had set out to test that the GetTextBuffer method works and returns the buffer that was supplied to the TextBufferContainer. I suppose that although at present the text buffer container does not mutate the buffer it may be a valid scenario for a future revision or even for a derived type. In which case, I will alter the test to assert only that GetTextBuffer does not throw.

{
var textSnapshotMock = new Mock<VisualStudio.Text.ITextSnapshot>();
var bufferMock = new Mock<VisualStudio.Text.ITextBuffer>();
bufferMock.SetupGet(x => x.CurrentSnapshot).Returns(textSnapshotMock.Object);
bufferMock.SetupGet(x => x.Properties).Returns(new VisualStudio.Utilities.PropertyCollection());

var textContainer = Microsoft.CodeAnalysis.Text.Extensions.TextBufferContainer.From(bufferMock.Object);


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a StyleCop violation to have two blank lines in a row. StyleCop must not be running on this file given that the build passed, right? Either way, please remove the blank line.

var textBuffer = textContainer.GetTextBuffer();

Assert.Equal(bufferMock.Object, textBuffer);
}
}
}
2 changes: 1 addition & 1 deletion src/EditorFeatures/Text/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static ITextBuffer GetTextBuffer(this SourceTextContainer textContainer)
var textBuffer = TryGetTextBuffer(textContainer);
if (textBuffer == null)
{
throw new ArgumentException(TextEditorResources.TextcontainerIsNotAnSource, "textContainer");
throw new ArgumentException(TextEditorResources.TextContainerNotFromTextBuffer, "textContainer");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch this to use nameof while you're already in there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do


return textBuffer;
Expand Down
17 changes: 4 additions & 13 deletions src/EditorFeatures/Text/TextEditorResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/EditorFeatures/Text/TextEditorResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="TextcontainerIsNotAnSource" xml:space="preserve">
<value>textContainer is not an SourceTextContainer that was created from an ITextBuffer.</value>
</data>
<data name="TextIsNotAnSourcetextThat" xml:space="preserve">
<value>text is not an SourceText that was created from an ITextSnapshot.</value>
<data name="TextContainerNotFromTextBuffer" xml:space="preserve">
<value>textContainer is not a SourceTextContainer that was created from an ITextBuffer.</value>
</data>
</root>