Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,45 @@ public void ReplaceSeparator(bool collectionExpression)
Assert.Equal(2, newList.SeparatorCount);
Assert.Equal(1, newList.GetSeparator(1).GetLeadingTrivia().Count);
}

[Fact]
Copy link
Member

Choose a reason for hiding this comment

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

@copilot remove these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Removed the test methods in commit 7c89d.

public void ReplaceSeparator_ThrowsArgumentExceptionWithProperMessage_WhenSeparatorNotFound()
{
var list = SyntaxFactory.SeparatedList<SyntaxNode>(
new[] {
SyntaxFactory.IdentifierName("A"),
SyntaxFactory.IdentifierName("B"),
SyntaxFactory.IdentifierName("C"),
});

var badToken = SyntaxFactory.Token(SyntaxKind.SemicolonToken);
var newSeparator = SyntaxFactory.Token(SyntaxKind.CommaToken);

var exception = Assert.Throws<ArgumentException>(() =>
list.ReplaceSeparator(badToken, newSeparator));

Assert.Equal("separatorToken", exception.ParamName);
Assert.Contains("item specified is not the element of a list", exception.Message);
}

[Fact]
public void ReplaceSeparator_ThrowsArgumentExceptionWithProperMessage_WhenNewSeparatorHasWrongKind()
{
var list = SyntaxFactory.SeparatedList<SyntaxNode>(
new[] {
SyntaxFactory.IdentifierName("A"),
SyntaxFactory.IdentifierName("B"),
SyntaxFactory.IdentifierName("C"),
});

var firstSep = list.GetSeparator(0);
var wrongSep = SyntaxFactory.Token(SyntaxKind.SemicolonToken);

var exception = Assert.Throws<ArgumentException>(() =>
list.ReplaceSeparator(firstSep, wrongSep));

Assert.Equal("newSeparator", exception.ParamName);
Assert.Contains("must have the same RawKind and Language", exception.Message);
}
}
}
4 changes: 2 additions & 2 deletions src/Compilers/Core/Portable/Syntax/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ public SeparatedSyntaxList<TNode> ReplaceSeparator(SyntaxToken separatorToken, S
var index = nodesWithSeps.IndexOf(separatorToken);
if (index < 0)
{
throw new ArgumentException("separatorToken");
throw new ArgumentException(CodeAnalysisResources.MissingListItem, nameof(separatorToken));
}

if (newSeparator.RawKind != nodesWithSeps[index].RawKind ||
newSeparator.Language != nodesWithSeps[index].Language)
{
throw new ArgumentException("newSeparator");
throw new ArgumentException("New separator must have the same RawKind and Language as the separator being replaced.", nameof(newSeparator));
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Break this into two separate checks, and add two new resource string exception messages. One for needing to have the same RawKind and one for needing to have the same Language.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Split into two separate checks with dedicated resource strings SeparatorTokenMustHaveSameRawKind and SeparatorTokenMustHaveSameLanguage in commit 0c9ca.

}

return new SeparatedSyntaxList<TNode>(nodesWithSeps.Replace(separatorToken, newSeparator));
Expand Down