Skip to content

Commit 58192b4

Browse files
committed
Fixing xUnit1044/1045/1046/1047 for Index/Range for v3 projects
1 parent a08f8a7 commit 58192b4

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

src/xunit.analyzers.tests/Analyzers/X1000/TheoryDataRowArgumentsShouldBeSerializableTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public void TestMethod() {
4949
[InlineData("new Dictionary<string, List<string>>()", "Dictionary<string, List<string>>")]
5050
#if NET6_0_OR_GREATER
5151
[InlineData("DateOnly.MinValue", "DateOnly")]
52+
[InlineData("Index.Start", "Index")]
53+
[InlineData("Range.All", "Range")]
5254
[InlineData("TimeOnly.MinValue", "TimeOnly")]
5355
#endif
5456
[InlineData("Guid.Empty", "Guid")]

src/xunit.analyzers.tests/Analyzers/X1000/TheoryDataTypeArgumentsShouldBeSerializableTests.cs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,33 +207,6 @@ public enum SerializableEnumeration {{ Zero }}
207207
await Verify.VerifyAnalyzer(source);
208208
}
209209

210-
[Theory]
211-
[MemberData(nameof(TheoryDataMembers), "Guid")]
212-
[MemberData(nameof(TheoryDataMembers), "Guid?")]
213-
[MemberData(nameof(TheoryDataMembers), "Uri")]
214-
public async Task GivenTheory_WithTypeOnlySupportedInV3_TriggersInV2_DoesNotTriggerInV3(
215-
string member,
216-
string attribute,
217-
string type)
218-
{
219-
var source = string.Format(/* lang=c#-test */ """
220-
using System;
221-
using Xunit;
222-
223-
public class TestClass {{
224-
{0}
225-
226-
[Theory]
227-
[{{|#0:{1}|}}]
228-
public void TestMethod({2} parameter) {{ }}
229-
}}
230-
""", member, attribute, type);
231-
var expectedV2 = Verify.Diagnostic(type == "Uri" ? "xUnit1045" : "xUnit1044").WithLocation(0).WithArguments(type);
232-
233-
await Verify.VerifyAnalyzerV2(source, expectedV2);
234-
await Verify.VerifyAnalyzerV3(source);
235-
}
236-
237210
[Theory]
238211
[MemberData(nameof(TheoryDataMembers), "IXunitSerializable")]
239212
[MemberData(nameof(TheoryDataMembers), "IXunitSerializable[]")]
@@ -434,6 +407,36 @@ public async Task GivenTheory_WithNonSerializableTheoryDataClass_WithDiscoveryEn
434407

435408
public sealed class X1044_AvoidUsingTheoryDataTypeArgumentsThatAreNotSerializable : TheoryDataTypeArgumentsShouldBeSerializableTests
436409
{
410+
[Theory]
411+
[MemberData(nameof(TheoryDataMembers), "Guid")]
412+
[MemberData(nameof(TheoryDataMembers), "Guid?")]
413+
#if NET6_0_OR_GREATER
414+
[MemberData(nameof(TheoryDataMembers), "Index")]
415+
[MemberData(nameof(TheoryDataMembers), "Range")]
416+
#endif
417+
public async Task GivenTheory_WithTypeOnlySupportedInV3_TriggersInV2_DoesNotTriggerInV3(
418+
string member,
419+
string attribute,
420+
string type)
421+
{
422+
var source = string.Format(/* lang=c#-test */ """
423+
using System;
424+
using Xunit;
425+
426+
public class TestClass {{
427+
{0}
428+
429+
[Theory]
430+
[{{|#0:{1}|}}]
431+
public void TestMethod({2} parameter) {{ }}
432+
}}
433+
""", member, attribute, type);
434+
var expectedV2 = Verify.Diagnostic("xUnit1044").WithLocation(0).WithArguments(type);
435+
436+
await Verify.VerifyAnalyzerV2(source, expectedV2);
437+
await Verify.VerifyAnalyzerV3(source);
438+
}
439+
437440
[Theory]
438441
[MemberData(nameof(TheoryDataMembers), "Delegate")]
439442
[MemberData(nameof(TheoryDataMembers), "Delegate[]")]
@@ -492,6 +495,32 @@ public async Task GivenTheory_WithNonSerializableTheoryDataClass_Triggers(
492495

493496
public sealed class X1045_AvoidUsingTheoryDataTypeArgumentsThatMightNotBeSerializable : TheoryDataTypeArgumentsShouldBeSerializableTests
494497
{
498+
[Theory]
499+
[MemberData(nameof(TheoryDataMembers), "Uri")]
500+
[MemberData(nameof(TheoryDataMembers), "Uri?")]
501+
public async Task GivenTheory_WithTypeOnlySupportedInV3_TriggersInV2_DoesNotTriggerInV3(
502+
string member,
503+
string attribute,
504+
string type)
505+
{
506+
var source = string.Format(/* lang=c#-test */ """
507+
using System;
508+
using Xunit;
509+
510+
public class TestClass {{
511+
{0}
512+
513+
[Theory]
514+
[{{|#0:{1}|}}]
515+
public void TestMethod({2} parameter) {{ }}
516+
}}
517+
""", member, attribute, type);
518+
var expectedV2 = Verify.Diagnostic("xUnit1045").WithLocation(0).WithArguments(type);
519+
520+
await Verify.VerifyAnalyzerV2(LanguageVersion.CSharp9, source, expectedV2);
521+
await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp9, source);
522+
}
523+
495524
[Theory]
496525
[MemberData(nameof(TheoryDataMembers), "object")]
497526
[MemberData(nameof(TheoryDataMembers), "object[]")]

src/xunit.analyzers/Utility/SerializabilityAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public Serializability AnalayzeSerializability(
4848

4949
if (xunitContext.HasV3References)
5050
{
51-
if (type.Equals(typeSymbols.Guid, SymbolEqualityComparer.Default))
52-
return Serializability.AlwaysSerializable;
53-
54-
if (type.Equals(typeSymbols.Uri, SymbolEqualityComparer.Default))
51+
if (type.Equals(typeSymbols.Guid, SymbolEqualityComparer.Default)
52+
|| type.Equals(typeSymbols.Index, SymbolEqualityComparer.Default)
53+
|| type.Equals(typeSymbols.Range, SymbolEqualityComparer.Default)
54+
|| type.Equals(typeSymbols.Uri, SymbolEqualityComparer.Default))
5555
return Serializability.AlwaysSerializable;
5656
}
5757

src/xunit.analyzers/Utility/SerializableTypeSymbols.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public sealed class SerializableTypeSymbols
1212
readonly Lazy<INamedTypeSymbol?> dateOnly;
1313
readonly Lazy<INamedTypeSymbol?> dateTimeOffset;
1414
readonly Lazy<INamedTypeSymbol?> guid;
15+
readonly Lazy<INamedTypeSymbol?> index;
1516
readonly Lazy<INamedTypeSymbol?> iXunitSerializable;
17+
readonly Lazy<INamedTypeSymbol?> range;
1618
readonly Lazy<INamedTypeSymbol?> theoryDataBaseType;
1719
readonly Dictionary<int, INamedTypeSymbol> theoryDataTypes;
1820
readonly Lazy<INamedTypeSymbol?> timeOnly;
@@ -37,7 +39,9 @@ public sealed class SerializableTypeSymbols
3739
dateOnly = new(() => TypeSymbolFactory.DateOnly(compilation));
3840
dateTimeOffset = new(() => TypeSymbolFactory.DateTimeOffset(compilation));
3941
guid = new(() => TypeSymbolFactory.Guid(compilation));
42+
index = new(() => TypeSymbolFactory.Index(compilation));
4043
iXunitSerializable = new(() => xunitContext.Common.IXunitSerializableType);
44+
range = new(() => TypeSymbolFactory.Range(compilation));
4145
// For v2 and early versions of v3, the base type is "TheoryData" (non-generic). For later versions
4246
// of v3, it's "TheoryDataBase<TTheoryDataRow, TRawDataRow>". In either case, getting "TheoryData<T>"
4347
// and going up one layer gets us the type we want to be able to search for.
@@ -77,8 +81,10 @@ public sealed class SerializableTypeSymbols
7781
public INamedTypeSymbol? DateOnly => dateOnly.Value;
7882
public INamedTypeSymbol? DateTimeOffset => dateTimeOffset.Value;
7983
public INamedTypeSymbol? Guid => guid.Value;
84+
public INamedTypeSymbol? Index => index.Value;
8085
public INamedTypeSymbol? IXunitSerializable => iXunitSerializable.Value;
8186
public INamedTypeSymbol MemberDataAttribute { get; }
87+
public INamedTypeSymbol? Range => range.Value;
8288
public INamedTypeSymbol TheoryAttribute { get; }
8389
public INamedTypeSymbol? TheoryDataBaseType => theoryDataBaseType.Value;
8490
public INamedTypeSymbol? TimeOnly => timeOnly.Value;

src/xunit.analyzers/Utility/TypeSymbolFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ public static INamedTypeSymbol IEnumerableOfT(Compilation compilation) =>
175175
public static INamedTypeSymbol? IMethodInfo_V2(Compilation compilation) =>
176176
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName(Constants.Types.Xunit.IMethodInfo_V2);
177177

178+
public static INamedTypeSymbol? Index(Compilation compilation) =>
179+
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("System.Index");
180+
178181
public static INamedTypeSymbol? InlineDataAttribute(Compilation compilation) =>
179182
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName(Constants.Types.Xunit.InlineDataAttribute);
180183

@@ -316,6 +319,9 @@ public static IArrayTypeSymbol ObjectArray(Compilation compilation) =>
316319
public static INamedTypeSymbol? OptionalAttribute(Compilation compilation) =>
317320
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("System.Runtime.InteropServices.OptionalAttribute");
318321

322+
public static INamedTypeSymbol? Range(Compilation compilation) =>
323+
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("System.Range");
324+
319325
public static INamedTypeSymbol? Record(Compilation compilation) =>
320326
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName(Constants.Types.Xunit.Record);
321327

0 commit comments

Comments
 (0)