1+ using System ;
12using System . Threading . Tasks ;
3+ using Microsoft . CodeAnalysis ;
24using Microsoft . CodeAnalysis . CSharp ;
35using Xunit ;
6+ using Xunit . Analyzers ;
47using Verify = CSharpVerifier < Xunit . Analyzers . TheoryDataRowArgumentsShouldBeSerializable > ;
8+ using Verify_v3_Pre301 = CSharpVerifier < TheoryDataRowArgumentsShouldBeSerializableTests . Analyzer_v3_Pre301 > ;
59
610public sealed class TheoryDataRowArgumentsShouldBeSerializableTests
711{
@@ -138,7 +142,7 @@ public async Task IXunitSerializerValue_DoesNotTrigger(string type)
138142 using Xunit.Sdk;
139143
140144 [assembly: RegisterXunitSerializer(typeof(CustomSerializer), typeof(ICustomSerialized))]
141-
145+
142146 public class MyClass {{
143147 public IEnumerable<TheoryDataRowBase> MyMethod() {{
144148 var value = new {0}();
@@ -152,21 +156,21 @@ public IEnumerable<TheoryDataRowBase> MyMethod() {{
152156 }}
153157
154158 public interface ICustomSerialized {{ }}
155-
159+
156160 public class CustomSerialized : ICustomSerialized {{ }}
157-
161+
158162 public class CustomSerializedDerived : CustomSerialized {{ }}
159-
163+
160164 public class CustomSerializer : IXunitSerializer {{
161165 public object Deserialize(Type type, string serializedValue) =>
162166 throw new NotImplementedException();
163-
167+
164168 public bool IsSerializable(Type type, object? value, out string? failureReason)
165169 {{
166170 failureReason = null;
167171 return true;
168172 }}
169-
173+
170174 public string Serialize(object value) =>
171175 throw new NotImplementedException();
172176 }}
@@ -301,6 +305,41 @@ public class PossiblySerializableUnsealedClass {{ }}
301305 await Verify . VerifyAnalyzerV3 ( LanguageVersion . CSharp8 , source , expected ) ;
302306 }
303307
308+ [ Theory ]
309+ [ InlineData ( "object" ) ]
310+ [ InlineData ( "Dictionary<int, string>" ) ]
311+ [ InlineData ( "PossiblySerializableUnsealedClass" ) ]
312+ public async Task MaybeNonSerializableValue_Constructable_Triggers1047 ( string type )
313+ {
314+ var source = string . Format ( /* lang=c#-test */ """
315+ #nullable enable
316+
317+ using System.Collections.Generic;
318+ using Xunit;
319+
320+ public class MyClass {{
321+ public IEnumerable<TheoryDataRowBase> MyMethod() {{
322+ var value = new {0}();
323+
324+ yield return new TheoryDataRow({{|#0:value|}});
325+ yield return new TheoryDataRow({{|#1:new {0}()|}});
326+ yield return new TheoryDataRow<{0}>({{|#2:value|}});
327+ yield return new TheoryDataRow<{0}>({{|#3:new {0}()|}});
328+ }}
329+ }}
330+
331+ public class PossiblySerializableUnsealedClass {{ }}
332+ """ , type ) ;
333+ var expected = new [ ] {
334+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 0 ) . WithArguments ( "value" , type ) ,
335+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 1 ) . WithArguments ( $ "new { type } ()", type ) ,
336+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 2 ) . WithArguments ( "value" , type ) ,
337+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 3 ) . WithArguments ( $ "new { type } ()", type ) ,
338+ } ;
339+
340+ await Verify . VerifyAnalyzerV3 ( LanguageVersion . CSharp8 , source , expected ) ;
341+ }
342+
304343 [ Fact ]
305344 public async Task IFormattableAndIParseable_DoesNotTrigger ( )
306345 {
@@ -343,7 +382,7 @@ public IEnumerable<TheoryDataRowBase> MyMethod() {
343382 var defaultValue = default(Formattable);
344383 var nullValue = default(Formattable?);
345384 var arrayValue = new Formattable[0];
346-
385+
347386 yield return new TheoryDataRow({|#0:defaultValue|}, {|#1:nullValue|}, {|#2:arrayValue|});
348387 yield return new TheoryDataRow<Formattable, Formattable, Formattable[]>({|#3:default(Formattable)|}, {|#4:default(Formattable?)|}, {|#5:new Formattable[0]|});
349388 }
@@ -354,7 +393,7 @@ public IEnumerable<TheoryDataRowBase> MyMethod() {
354393 var defaultValue = default(Parsable);
355394 var nullValue = default(Parsable?);
356395 var arrayValue = new Parsable[0];
357-
396+
358397 yield return new TheoryDataRow({|#10:defaultValue|}, {|#11:nullValue|}, {|#12:arrayValue|});
359398 yield return new TheoryDataRow<Parsable, Parsable, Parsable[]>({|#13:default(Parsable)|}, {|#14:default(Parsable?)|}, {|#15:new Parsable[0]|});
360399 }
@@ -365,7 +404,7 @@ public IEnumerable<TheoryDataRowBase> MyMethod() {
365404 var defaultValue = default(FormattableAndParsable);
366405 var nullValue = default(FormattableAndParsable?);
367406 var arrayValue = new FormattableAndParsable[0];
368-
407+
369408 yield return new TheoryDataRow(defaultValue, nullValue, arrayValue);
370409 yield return new TheoryDataRow<FormattableAndParsable, FormattableAndParsable, FormattableAndParsable[]>(default(FormattableAndParsable), default(FormattableAndParsable?), new FormattableAndParsable[0]);
371410 }
@@ -397,39 +436,57 @@ public IEnumerable<TheoryDataRowBase> MyMethod() {
397436#endif
398437 }
399438
400-
401- [ Theory ]
402- [ InlineData ( "object" ) ]
403- [ InlineData ( "Dictionary<int, string>" ) ]
404- [ InlineData ( "PossiblySerializableUnsealedClass" ) ]
405- public async Task MaybeNonSerializableValue_Constructable_Triggers1047 ( string type )
439+ [ Fact ]
440+ public async Task Tuples_OnlySupportedByV3_3_0_1 ( )
406441 {
407- var source = string . Format ( /* lang=c#-test */ """
442+ var source = /* lang=c#-test */ """
408443 #nullable enable
409444
445+ using System;
410446 using System.Collections.Generic;
411447 using Xunit;
412448
413- public class MyClass {{
414- public IEnumerable<TheoryDataRowBase> MyMethod () { {
415- var value = new {0}( );
449+ public class MyClass {
450+ public IEnumerable<TheoryDataRowBase> MyTupleMethod () {
451+ var value = Tuple.Create("Hello world", 42 );
416452
417- yield return new TheoryDataRow({{|#0:value|}});
418- yield return new TheoryDataRow({{|#1:new {0}()|}});
419- yield return new TheoryDataRow<{0}>({{|#2:value|}});
420- yield return new TheoryDataRow<{0}>({{|#3:new {0}()|}});
421- }}
422- }}
453+ yield return new TheoryDataRow({|#0:value|});
454+ yield return new TheoryDataRow({|#1:Tuple.Create("Hello world", 42)|});
455+ yield return new TheoryDataRow<Tuple<string, int>>({|#2:value|});
456+ yield return new TheoryDataRow<Tuple<string, int>>({|#3:Tuple.Create("Hello world", 42)|});
457+ }
423458
424- public class PossiblySerializableUnsealedClass {{ }}
425- """ , type ) ;
426- var expected = new [ ] {
427- Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 0 ) . WithArguments ( "value" , type ) ,
428- Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 1 ) . WithArguments ( $ "new { type } ()", type ) ,
429- Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 2 ) . WithArguments ( "value" , type ) ,
430- Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 3 ) . WithArguments ( $ "new { type } ()", type ) ,
459+ public IEnumerable<TheoryDataRowBase> MyValueTupleMethod() {
460+ var value = ValueTuple.Create("Hello world", 42);
461+
462+ yield return new TheoryDataRow({|#10:value|});
463+ yield return new TheoryDataRow({|#11:ValueTuple.Create("Hello world", 42)|});
464+ yield return new TheoryDataRow<ValueTuple<string, int>>({|#12:value|});
465+ yield return new TheoryDataRow<ValueTuple<string, int>>({|#13:ValueTuple.Create("Hello world", 42)|});
466+ }
467+ }
468+
469+ public class PossiblySerializableUnsealedClass { }
470+ """ ;
471+ var expectedUnsupported = new [ ] {
472+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 0 ) . WithArguments ( "value" , "Tuple<string, int>" ) ,
473+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 1 ) . WithArguments ( "Tuple.Create(\" Hello world\" , 42)" , "Tuple<string, int>" ) ,
474+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 2 ) . WithArguments ( "value" , "Tuple<string, int>" ) ,
475+ Verify . Diagnostic ( "xUnit1047" ) . WithLocation ( 3 ) . WithArguments ( "Tuple.Create(\" Hello world\" , 42)" , "Tuple<string, int>" ) ,
476+
477+ Verify . Diagnostic ( "xUnit1046" ) . WithLocation ( 10 ) . WithArguments ( "value" , "(string, int)" ) ,
478+ Verify . Diagnostic ( "xUnit1046" ) . WithLocation ( 11 ) . WithArguments ( "ValueTuple.Create(\" Hello world\" , 42)" , "(string, int)" ) ,
479+ Verify . Diagnostic ( "xUnit1046" ) . WithLocation ( 12 ) . WithArguments ( "value" , "(string, int)" ) ,
480+ Verify . Diagnostic ( "xUnit1046" ) . WithLocation ( 13 ) . WithArguments ( "ValueTuple.Create(\" Hello world\" , 42)" , "(string, int)" ) ,
431481 } ;
432482
433- await Verify . VerifyAnalyzerV3 ( LanguageVersion . CSharp8 , source , expected ) ;
483+ await Verify_v3_Pre301 . VerifyAnalyzerV3 ( LanguageVersion . CSharp8 , source , expectedUnsupported ) ;
484+ await Verify . VerifyAnalyzerV3 ( LanguageVersion . CSharp8 , source ) ;
485+ }
486+
487+ internal class Analyzer_v3_Pre301 : TheoryDataRowArgumentsShouldBeSerializable
488+ {
489+ protected override XunitContext CreateXunitContext ( Compilation compilation ) =>
490+ XunitContext . ForV3 ( compilation , new Version ( 3 , 0 , 0 ) ) ;
434491 }
435492}
0 commit comments