Skip to content

Commit

Permalink
Fix issue with xUnit20207 where value is already casted to ISet<T> or…
Browse files Browse the repository at this point in the history
… IReadOnlySet<T>
  • Loading branch information
bradwilson committed Dec 30, 2023
1 parent a39562e commit 336423e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ public void TestMethod() {{
await Verify.VerifyAnalyzer(LanguageVersion.CSharp7, new[] { code, customSetAndComparer });
}

[Fact]
public async void CastedSet_DoesNotTrigger()
{
var code = @"
using Xunit;
using System.Collections.Generic;
public class TestClass {
[Fact]
public void TestMethod() {
var expected = new HashSet<string> { ""bar"", ""foo"" };
var actual = new HashSet<string> { ""foo"", ""bar"" };
Assert.Equal(expected, actual);
Assert.Equal(expected, (ISet<string>)actual);
Assert.Equal((ISet<string>)expected, actual);
Assert.Equal((ISet<string>)expected, (ISet<string>)actual);
}
}";

await Verify.VerifyAnalyzer(code);
}

public static MatrixTheoryData<string, (string type, string initializer)> MethodAndTypeAndInitializer =>
new(
new[] { "Equal", "NotEqual" },
Expand Down
2 changes: 2 additions & 0 deletions src/xunit.analyzers/X2000/SetEqualityAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected override void AnalyzeInvocation(
var interface0Type =
collection0Type
.AllInterfaces
.Concat(new[] { collection0Type })
.Where(i => i.IsGenericType)
.FirstOrDefault(i => setInterfaces.Contains(i.ConstructUnboundGenericType()));

Expand All @@ -64,6 +65,7 @@ protected override void AnalyzeInvocation(
var interface1Type =
collection1Type
.AllInterfaces
.Concat(new[] { collection1Type })
.Where(i => i.IsGenericType)
.FirstOrDefault(i => setInterfaces.Contains(i.ConstructUnboundGenericType()));

Expand Down

0 comments on commit 336423e

Please sign in to comment.