-
Notifications
You must be signed in to change notification settings - Fork 29
/
NullableTests.cs
650 lines (589 loc) · 21.5 KB
/
NullableTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
using PublicApiGeneratorTests.Examples;
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;
namespace PublicApiGeneratorTests
{
// Tests for https://github.com/ApiApprover/ApiApprover/issues/54
// See also https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-reference-types.md
[Trait("NRT", "Nullable Reference Types")]
public class NullableTests : ApiGeneratorTestsBase
{
[Fact]
public void Should_Annotate_ReturnType()
{
AssertPublicApi<ReturnType>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ReturnType
{
public ReturnType() { }
public string? ReturnProperty { get; set; }
}
}");
}
[Fact]
public void Should_Annotate_VoidReturn()
{
AssertPublicApi(typeof(VoidReturn),
@"namespace PublicApiGeneratorTests.Examples
{
public static class VoidReturn
{
public static void ShouldBeEquivalentTo(this object? actual, object? expected) { }
}
}");
}
[Fact]
public void Should_Annotate_Derived_ReturnType()
{
AssertPublicApi<ReturnArgs>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ReturnArgs : System.EventArgs
{
public ReturnArgs() { }
public string? Target { get; set; }
}
}");
}
[Fact]
public void Should_Annotate_Ctor_Args()
{
AssertPublicApi<NullableCtor>(
@"namespace PublicApiGeneratorTests.Examples
{
public class NullableCtor
{
public NullableCtor(string? nullableLabel, string nope) { }
}
}");
}
[Fact]
public void Should_Not_Annotate_Obsolete_Attribute()
{
AssertPublicApi<ClassWithObsolete>(
@"namespace PublicApiGeneratorTests.Examples
{
[System.ObsoleteAttribute(""Foo"")]
public class ClassWithObsolete
{
[System.ObsoleteAttribute(""Bar"")]
public ClassWithObsolete(string? nullableLabel) { }
[System.ObsoleteAttribute(""Bar"")]
public ClassWithObsolete(string? nullableLabel, string? nullableLabel2) { }
}
}");
}
[Fact]
public void Should_Annotate_Generic_Event()
{
AssertPublicApi<GenericEvent>(
@"namespace PublicApiGeneratorTests.Examples
{
public class GenericEvent
{
public GenericEvent() { }
public event System.EventHandler<PublicApiGeneratorTests.Examples.ReturnArgs?> ReturnEvent;
}
}");
}
[Fact]
public void Should_Annotate_Delegate_Declaration()
{
AssertPublicApi<DelegateDeclaration>(
@"namespace PublicApiGeneratorTests.Examples
{
public class DelegateDeclaration
{
public DelegateDeclaration() { }
public delegate string? OnNullableReturn(object sender, PublicApiGeneratorTests.Examples.ReturnArgs? args);
public delegate string OnReturn(object sender, PublicApiGeneratorTests.Examples.ReturnArgs? args);
}
}");
}
[Fact]
public void Should_Annotate_Nullable_Array()
{
AssertPublicApi<NullableArray>(
@"namespace PublicApiGeneratorTests.Examples
{
public class NullableArray
{
public NullableArray() { }
public PublicApiGeneratorTests.Examples.ReturnType[]? NullableMethod1() { }
public PublicApiGeneratorTests.Examples.ReturnType[]?[]? NullableMethod2() { }
}
}");
}
[Fact]
public void Should_Annotate_Nullable_Enumerable()
{
AssertPublicApi<NullableEnumerable>(
@"namespace PublicApiGeneratorTests.Examples
{
public class NullableEnumerable
{
public NullableEnumerable() { }
public System.Collections.Generic.IEnumerable<PublicApiGeneratorTests.Examples.ReturnType?>? Enumerable() { }
}
}");
}
[Fact]
public void Should_Annotate_Generic_Method()
{
AssertPublicApi<GenericMethod>(
@"namespace PublicApiGeneratorTests.Examples
{
public class GenericMethod
{
public GenericMethod() { }
public PublicApiGeneratorTests.Examples.ReturnType? NullableGenericMethod<T1, T2, T3>(T1? t1, T2 t2, T3? t3)
where T1 : class
where T2 : class
where T3 : class { }
}
}");
}
[Fact]
public void Should_Annotate_Skeet_Examples()
{
AssertPublicApi<SkeetExamplesClass>(
@"namespace PublicApiGeneratorTests.Examples
{
public class SkeetExamplesClass
{
public System.Collections.Generic.Dictionary<System.Collections.Generic.List<string?>, string[]?> SkeetExample;
public System.Collections.Generic.Dictionary<System.Collections.Generic.List<string?>, string?[]> SkeetExample2;
public System.Collections.Generic.Dictionary<System.Collections.Generic.List<string?>, string?[]?> SkeetExample3;
public SkeetExamplesClass() { }
}
}");
}
[Fact]
public void Should_Annotate_By_Ref()
{
AssertPublicApi<ByRefClass>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ByRefClass
{
public ByRefClass() { }
public bool ByRefNullableReferenceParam(PublicApiGeneratorTests.Examples.ReturnType rt1, ref PublicApiGeneratorTests.Examples.ReturnType? rt2, PublicApiGeneratorTests.Examples.ReturnType rt3, PublicApiGeneratorTests.Examples.ReturnType? rt4, out PublicApiGeneratorTests.Examples.ReturnType? rt5, PublicApiGeneratorTests.Examples.ReturnType rt6) { }
}
}");
}
[Fact]
public void Should_Annotate_Different_API()
{
AssertPublicApi<NullableApi>(
@"namespace PublicApiGeneratorTests.Examples
{
public class NullableApi
{
public PublicApiGeneratorTests.Examples.ReturnType NonNullField;
public PublicApiGeneratorTests.Examples.ReturnType? NullableField;
public NullableApi() { }
public System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<int, int?>?>>? ComplicatedDictionary { get; set; }
public PublicApiGeneratorTests.Examples.ReturnType NonNullProperty { get; set; }
public PublicApiGeneratorTests.Examples.ReturnType? NullableProperty { get; set; }
public string? Convert(string source) { }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public PublicApiGeneratorTests.Examples.ReturnType? NullableParamAndReturnMethod(string? nullableParam, string nonNullParam, int? nullableValueType) { }
public PublicApiGeneratorTests.Examples.ReturnType NullableParamMethod(string? nullableParam, string nonNullParam, int? nullableValueType) { }
public PublicApiGeneratorTests.Examples.Data<string> NullableStruct1(PublicApiGeneratorTests.Examples.Data<string> param) { }
public PublicApiGeneratorTests.Examples.Data<string>? NullableStruct2(PublicApiGeneratorTests.Examples.Data<string>? param) { }
public PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>> NullableStruct3(PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>> param) { }
public PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>?> NullableStruct4(PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>?> param) { }
public PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>?>? NullableStruct5(PublicApiGeneratorTests.Examples.Data<System.Collections.Generic.KeyValuePair<string, string?>?>? param) { }
}
}");
}
[Fact]
public void Should_Annotate_System_Nullable()
{
AssertPublicApi<SystemNullable>(
@"namespace PublicApiGeneratorTests.Examples
{
public class SystemNullable
{
public readonly int? Age;
public SystemNullable() { }
public System.DateTime? Birth { get; set; }
public float? Calc(double? first, decimal? second) { }
public System.Collections.Generic.List<System.Guid?> GetSecrets(System.Collections.Generic.Dictionary<int?, System.Collections.Generic.Dictionary<bool?, byte?>> data) { }
}
}");
}
[Fact]
public void Should_Annotate_Generics()
{
AssertPublicApi<Generics>(
@"namespace PublicApiGeneratorTests.Examples
{
public class Generics
{
public Generics() { }
public System.Collections.Generic.List<string?> GetSecretData0() { }
public System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int?>> GetSecretData1() { }
public System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int?>?> GetSecretData2() { }
public System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string?, System.Collections.Generic.List<int>?>>> GetSecretData3(System.Collections.Generic.Dictionary<int?, System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.List<int?>>>>? value) { }
public System.Collections.Generic.Dictionary<int?, string>? GetSecretData4(System.Collections.Generic.Dictionary<int?, string>? value) { }
}
}");
}
[Fact]
public void Should_Annotate_Structs()
{
AssertPublicApi<Structs>(
@"namespace PublicApiGeneratorTests.Examples
{
public class Structs
{
public System.Collections.Generic.KeyValuePair<string?, int?> field;
public Structs() { }
}
}");
}
[Fact]
public void Should_Annotate_Tuples()
{
AssertPublicApi<Tuples>(
@"namespace PublicApiGeneratorTests.Examples
{
public class Tuples
{
public Tuples() { }
public System.Tuple<string, string?, int, int?> Tuple1(System.Tuple<string, string?, int, int?>? tuple) { }
public System.ValueTuple<string, string?, int, int?> Tuple2(System.ValueTuple<string, string?, int, int?>? tuple) { }
}
}");
}
[Fact]
public void Should_Annotate_Constraints()
{
AssertPublicApi<Constraints>(
@"namespace PublicApiGeneratorTests.Examples
{
public class Constraints
{
public Constraints() { }
public void Print1<T>(T val)
where T : class { }
public void Print2<T>(T val)
where T : class? { }
public static void Print3<T>()
where T : System.IO.Stream { }
public static void Print4<T>()
where T : System.IDisposable { }
}
}");
}
[Fact]
public void Should_Annotate_Nullable_Constraints()
{
AssertPublicApi(typeof(Constraints2<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public class Constraints2<X, Y>
where X : System.IComparable<X>
where Y : class?
{
public Constraints2() { }
public T Convert<T>(T data)
where T : System.IComparable<string?> { }
public static void Print1<T>()
where T : System.IO.Stream? { }
public static void Print2<T>()
where T : System.IDisposable? { }
}
}");
}
[Fact]
public void Should_Annotate_BaseType()
{
AssertPublicApi<NullableComparable>(
@"namespace PublicApiGeneratorTests.Examples
{
public class NullableComparable : System.Collections.Generic.List<string?>, System.IComparable<string?>
{
public NullableComparable() { }
public int CompareTo(string? other) { }
}
}");
}
[Fact]
public void Should_Annotate_OpenGeneric()
{
AssertPublicApi(typeof(StringNullableList<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public class StringNullableList<T, U> : System.Collections.Generic.List<T?>, System.IComparable<U>
where T : struct
where U : class
{
public StringNullableList() { }
public int CompareTo(U other) { }
}
}");
}
[Fact]
public void Should_Annotate_NotNull_Constraint()
{
AssertPublicApi(typeof(IDoStuff1<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff1<TIn, TOut>
where TIn : notnull
where TOut : notnull
{
TOut DoStuff(TIn input);
}
}");
}
[Fact]
public void Should_Annotate_NotNull_And_Null_Constraint()
{
AssertPublicApi(typeof(IDoStuff2<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff2<TIn, TOut>
where TIn : class?
where TOut : notnull
{
TOut DoStuff(TIn input);
}
}");
}
[Fact]
public void Should_Annotate_Without_Explicit_Constraints()
{
AssertPublicApi(typeof(IDoStuff3<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff3<TIn, TOut>
{
TOut DoStuff(TIn input);
}
}");
}
[Fact]
public void Should_Annotate_NotNull_And_Null_Class_Constraint()
{
AssertPublicApi(typeof(IDoStuff4<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff4<TIn, TOut>
where TIn : class?
where TOut : class
{
TOut DoStuff(TIn input);
}
}");
}
[Fact]
public void Should_Annotate_Nullable_Class_And_Struct_Constraint()
{
AssertPublicApi(typeof(IDoStuff5<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff5<TIn, TOut>
where TIn : class?
where TOut : struct
{
TOut DoStuff(TIn input);
}
}");
}
[Fact]
public void Should_Annotate_Unmanaged_Constraint()
{
AssertPublicApi(typeof(IDoStuff6<,>),
@"namespace PublicApiGeneratorTests.Examples
{
public interface IDoStuff6<TIn, TOut>
where TIn : notnull
where TOut : unmanaged
{
TOut DoStuff(TIn input);
}
}");
}
}
#nullable enable
// ReSharper disable ClassNeverInstantiated.Global
namespace Examples
{
public class ReturnType
{
public string? ReturnProperty { get; set; }
}
public static class VoidReturn
{
public static void ShouldBeEquivalentTo(this object? actual, object? expected) { }
}
public class ReturnArgs : EventArgs
{
public string? Target { get; set; }
}
public class NullableCtor
{
public NullableCtor(string? nullableLabel, string nope) { }
}
[Obsolete("Foo")]
public class ClassWithObsolete
{
[Obsolete("Bar")]
public ClassWithObsolete(string? nullableLabel) { }
[Obsolete("Bar")]
public ClassWithObsolete(string? nullableLabel, string? nullableLabel2) { }
}
public class GenericEvent
{
public event EventHandler<ReturnArgs?> ReturnEvent { add { } remove { } }
}
public class DelegateDeclaration
{
protected delegate string OnReturn(object sender, ReturnArgs? args);
protected delegate string? OnNullableReturn(object sender, ReturnArgs? args);
}
public class Structs
{
public KeyValuePair<string?, int?> field;
}
public struct Data<T>
{
public T Value { get; }
}
public class Generics
{
public List<string?> GetSecretData0() => null;
public Dictionary<int, List<int?>> GetSecretData1() => null;
public Dictionary<int, List<int?>?> GetSecretData2() => null;
public Dictionary<int, List<KeyValuePair<string?, List<int>?>>> GetSecretData3(Dictionary<int?, List<KeyValuePair<string, List<int?>>>>? value) { return null; }
public Dictionary<int?, string>? GetSecretData4(Dictionary<int?, string>? value) { return null; }
}
public class NullableArray
{
public ReturnType[]? NullableMethod1() { return null; }
public ReturnType[]?[]? NullableMethod2() { return null; }
}
public class NullableEnumerable
{
public IEnumerable<ReturnType?>? Enumerable() { return null; }
}
public class GenericMethod
{
public ReturnType? NullableGenericMethod<T1, T2, T3>(T1? t1, T2 t2, T3? t3) where T1 : class where T2 : class where T3 : class { return null; }
}
public class SkeetExamplesClass
{
public Dictionary<List<string?>, string[]?> SkeetExample = new Dictionary<List<string?>, string[]?>();
public Dictionary<List<string?>, string?[]> SkeetExample2 = new Dictionary<List<string?>, string?[]>();
public Dictionary<List<string?>, string?[]?> SkeetExample3 = new Dictionary<List<string?>, string?[]?>();
}
public class ByRefClass
{
public bool ByRefNullableReferenceParam(ReturnType rt1, ref ReturnType? rt2, ReturnType rt3, ReturnType? rt4, out ReturnType? rt5, ReturnType rt6) { rt5 = null; return false; }
}
public class NullableApi
{
public ReturnType NonNullField = new ReturnType();
public ReturnType? NullableField;
public ReturnType NonNullProperty { get; protected set; } = new ReturnType();
public ReturnType? NullableProperty { get; set; }
public ReturnType NullableParamMethod(string? nullableParam, string nonNullParam, int? nullableValueType) { return new ReturnType(); }
public ReturnType? NullableParamAndReturnMethod(string? nullableParam, string nonNullParam, int? nullableValueType) { return default; }
public Dictionary<string, Dictionary<string, Dictionary<int, int?>?>>? ComplicatedDictionary { get; set; }
public override bool Equals(object? obj) => base.Equals(obj);
public override int GetHashCode() => base.GetHashCode();
public string? Convert(string source) => source;
public Data<string> NullableStruct1(Data<string> param) => default;
public Data<string>? NullableStruct2(Data<string>? param) => default;
public Data<KeyValuePair<string, string?>> NullableStruct3(Data<KeyValuePair<string, string?>> param) => default;
public Data<KeyValuePair<string, string?>?> NullableStruct4(Data<KeyValuePair<string, string?>?> param) => default;
public Data<KeyValuePair<string, string?>?>? NullableStruct5(Data<KeyValuePair<string, string?>?>? param) => default;
}
public class SystemNullable
{
public readonly int? Age;
public DateTime? Birth { get; set; }
public float? Calc(double? first, decimal? second) { return null; }
public List<Guid?> GetSecrets(Dictionary<int?, Dictionary<bool?, byte?>> data) => null;
}
public class Tuples
{
public Tuple<string, string?, int, int?> Tuple1(Tuple<string, string?, int, int?>? tuple) => default;
public ValueTuple<string, string?, int, int?> Tuple2(ValueTuple<string, string?, int, int?>? tuple) => default;
}
public class Constraints
{
public void Print1<T>(T val) where T : class
{
val.ToString();
}
public void Print2<T>(T val) where T : class?
{
if (val != null)
val.ToString();
}
public static void Print3<T>() where T : Stream { }
public static void Print4<T>() where T : IDisposable { }
}
public class Constraints2<X, Y> where X: IComparable<X> where Y : class?
{
public T Convert<T>(T data) where T : IComparable<string?> => default;
public static void Print1<T>() where T : Stream? { }
public static void Print2<T>() where T : IDisposable? { }
}
public class NullableComparable : List<string?>, IComparable<string?>
{
public int CompareTo(string? other)
{
throw new NotImplementedException();
}
}
public class StringNullableList<T,U> : List<T?>, IComparable<U> where T : struct where U : class
{
public int CompareTo(U other) => 0;
}
public interface IDoStuff1<TIn, TOut>
where TIn : notnull
where TOut : notnull
{
TOut DoStuff(TIn input);
}
public interface IDoStuff2<TIn, TOut>
where TIn : class?
where TOut : notnull
{
TOut DoStuff(TIn input);
}
public interface IDoStuff3<TIn, TOut>
{
TOut DoStuff(TIn input);
}
public interface IDoStuff4<TIn, TOut>
where TIn : class?
where TOut : class
{
TOut DoStuff(TIn input);
}
public interface IDoStuff5<TIn, TOut>
where TIn : class?
where TOut : struct
{
TOut DoStuff(TIn input);
}
public interface IDoStuff6<TIn, TOut>
where TIn : notnull
where TOut : unmanaged
{
TOut DoStuff(TIn input);
}
}
// ReSharper restore ClassNeverInstantiated.Global
// ReSharper restore UnusedMember.Global
}