forked from SimonCropp/NullabilityInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Samples.cs
69 lines (54 loc) · 1.82 KB
/
Samples.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
using Nullability;
using Xunit;
public class Samples
{
#region Usage
[Fact]
public void Test()
{
var type = typeof(Target);
var arrayField = type.GetField("ArrayField");
var genericField = type.GetField("GenericField");
var context = new NullabilityInfoContextEx();
var arrayInfo = context.Create(arrayField);
Assert.Equal(NullabilityStateEx.NotNull, arrayInfo.ReadState);
Assert.Equal(NullabilityStateEx.Nullable, arrayInfo.ElementType.ReadState);
var genericInfo = context.Create(genericField);
Assert.Equal(NullabilityStateEx.NotNull, genericInfo.ReadState);
Assert.Equal(NullabilityStateEx.NotNull, genericInfo.GenericTypeArguments[0].ReadState);
Assert.Equal(NullabilityStateEx.Nullable, genericInfo.GenericTypeArguments[1].ReadState);
}
#endregion
#region ExtensionEx
[Fact]
public void ExtensionTests()
{
var type = typeof(Target);
var field = type.GetField("StringField");
Assert.True(field.IsNullable());
Assert.Equal(NullabilityStateEx.Nullable, field.GetNullability());
Assert.Equal(NullabilityStateEx.Nullable, field.GetNullabilityInfo().ReadState);
}
#endregion
class PropertyTarget
{
string? write;
public string? ReadWrite { get; set; }
public string? Read { get; }
public string? Write
{
set => write = value;
}
}
[Fact]
public void Property()
{
var type = typeof(PropertyTarget);
var readWrite = type.GetProperty("ReadWrite");
var write = type.GetProperty("Write");
var read = type.GetProperty("Read");
Assert.True(readWrite.IsNullable());
Assert.True(write.IsNullable());
Assert.True(read.IsNullable());
}
}