Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using Xunit;
- namespace Something
- {
- public class NullableTest
- {
- public struct FooS
- {
- }
- public class Foo
- {
- public Foo NonNullable() => new Foo();
- public Foo? Nullable() => null;
- public FooS Struct() => new FooS();
- public FooS? NullableStruct() => null;
- }
- [Fact]
- public void NonNullableReturnTypeMustHaveNullableFlagsOf1()
- {
- var sut = typeof(Foo).GetMethod(nameof(Foo.NonNullable));
- Assert.Equal(typeof(Foo), sut.ReturnType);
- var attr = sut.ReturnTypeCustomAttributes
- .GetCustomAttributes(true)
- .Single(a => a.GetType().Name.Equals("NullableAttribute"));
- var flag = attr.GetType().GetField("NullableFlags").GetValue(attr);
- Assert.Equal(new byte[] { 1 }, flag);
- }
- [Fact]
- public void NullableReturnTypeMustHaveNullableFlagsOf2()
- {
- var sut = typeof(Foo).GetMethod(nameof(Foo.Nullable));
- Assert.Equal(typeof(Foo?), sut.ReturnType);
- var attr = sut.ReturnTypeCustomAttributes
- .GetCustomAttributes(true)
- .Single(a => a.GetType().Name.Equals("NullableAttribute"));
- var flag = attr.GetType().GetField("NullableFlags").GetValue(attr);
- Assert.Equal(new byte[] { 2 }, flag);
- }
- [Fact]
- public void StructReturnTypeHaveNoNullableAttribute()
- {
- var sut = typeof(Foo).GetMethod(nameof(Foo.Struct));
- Assert.Equal(typeof(FooS), sut.ReturnType);
- var hasAttr = sut.ReturnTypeCustomAttributes
- .GetCustomAttributes(true)
- .Any(a => a.GetType().Name.Equals("NullableAttribute"));
- Assert.False(hasAttr);
- }
- [Fact]
- public void NullableStructReturnTypeIsNullableGenericDefinition()
- {
- var sut = typeof(Foo).GetMethod(nameof(Foo.NullableStruct));
- Assert.Equal(typeof(FooS?), sut.ReturnType);
- Assert.Equal(typeof(Nullable<>), sut.ReturnType.GetGenericTypeDefinition());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement