Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Xunit;
  4.  
  5. namespace Something
  6. {
  7. public class NullableTest
  8. {
  9. public struct FooS
  10. {
  11. }
  12. public class Foo
  13. {
  14. public Foo NonNullable() => new Foo();
  15. public Foo? Nullable() => null;
  16. public FooS Struct() => new FooS();
  17. public FooS? NullableStruct() => null;
  18. }
  19.  
  20. [Fact]
  21. public void NonNullableReturnTypeMustHaveNullableFlagsOf1()
  22. {
  23. var sut = typeof(Foo).GetMethod(nameof(Foo.NonNullable));
  24. Assert.Equal(typeof(Foo), sut.ReturnType);
  25.  
  26. var attr = sut.ReturnTypeCustomAttributes
  27. .GetCustomAttributes(true)
  28. .Single(a => a.GetType().Name.Equals("NullableAttribute"));
  29. var flag = attr.GetType().GetField("NullableFlags").GetValue(attr);
  30. Assert.Equal(new byte[] { 1 }, flag);
  31. }
  32.  
  33. [Fact]
  34. public void NullableReturnTypeMustHaveNullableFlagsOf2()
  35. {
  36. var sut = typeof(Foo).GetMethod(nameof(Foo.Nullable));
  37. Assert.Equal(typeof(Foo?), sut.ReturnType);
  38.  
  39. var attr = sut.ReturnTypeCustomAttributes
  40. .GetCustomAttributes(true)
  41. .Single(a => a.GetType().Name.Equals("NullableAttribute"));
  42. var flag = attr.GetType().GetField("NullableFlags").GetValue(attr);
  43. Assert.Equal(new byte[] { 2 }, flag);
  44. }
  45.  
  46. [Fact]
  47. public void StructReturnTypeHaveNoNullableAttribute()
  48. {
  49. var sut = typeof(Foo).GetMethod(nameof(Foo.Struct));
  50. Assert.Equal(typeof(FooS), sut.ReturnType);
  51.  
  52. var hasAttr = sut.ReturnTypeCustomAttributes
  53. .GetCustomAttributes(true)
  54. .Any(a => a.GetType().Name.Equals("NullableAttribute"));
  55. Assert.False(hasAttr);
  56. }
  57.  
  58. [Fact]
  59. public void NullableStructReturnTypeIsNullableGenericDefinition()
  60. {
  61. var sut = typeof(Foo).GetMethod(nameof(Foo.NullableStruct));
  62. Assert.Equal(typeof(FooS?), sut.ReturnType);
  63. Assert.Equal(typeof(Nullable<>), sut.ReturnType.GetGenericTypeDefinition());
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement