2
0

test_receiver.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. use syn::{parse_quote, FnArg, Receiver, TraitItemMethod};
  2. #[test]
  3. fn test_by_value() {
  4. let TraitItemMethod { sig, .. } = parse_quote! {
  5. fn by_value(self: Self);
  6. };
  7. match sig.receiver() {
  8. Some(FnArg::Typed(_)) => (),
  9. value => panic!("expected FnArg::Typed, got {:?}", value),
  10. }
  11. }
  12. #[test]
  13. fn test_by_mut_value() {
  14. let TraitItemMethod { sig, .. } = parse_quote! {
  15. fn by_mut(mut self: Self);
  16. };
  17. match sig.receiver() {
  18. Some(FnArg::Typed(_)) => (),
  19. value => panic!("expected FnArg::Typed, got {:?}", value),
  20. }
  21. }
  22. #[test]
  23. fn test_by_ref() {
  24. let TraitItemMethod { sig, .. } = parse_quote! {
  25. fn by_ref(self: &Self);
  26. };
  27. match sig.receiver() {
  28. Some(FnArg::Typed(_)) => (),
  29. value => panic!("expected FnArg::Typed, got {:?}", value),
  30. }
  31. }
  32. #[test]
  33. fn test_by_box() {
  34. let TraitItemMethod { sig, .. } = parse_quote! {
  35. fn by_box(self: Box<Self>);
  36. };
  37. match sig.receiver() {
  38. Some(FnArg::Typed(_)) => (),
  39. value => panic!("expected FnArg::Typed, got {:?}", value),
  40. }
  41. }
  42. #[test]
  43. fn test_by_pin() {
  44. let TraitItemMethod { sig, .. } = parse_quote! {
  45. fn by_pin(self: Pin<Self>);
  46. };
  47. match sig.receiver() {
  48. Some(FnArg::Typed(_)) => (),
  49. value => panic!("expected FnArg::Typed, got {:?}", value),
  50. }
  51. }
  52. #[test]
  53. fn test_explicit_type() {
  54. let TraitItemMethod { sig, .. } = parse_quote! {
  55. fn explicit_type(self: Pin<MyType>);
  56. };
  57. match sig.receiver() {
  58. Some(FnArg::Typed(_)) => (),
  59. value => panic!("expected FnArg::Typed, got {:?}", value),
  60. }
  61. }
  62. #[test]
  63. fn test_value_shorthand() {
  64. let TraitItemMethod { sig, .. } = parse_quote! {
  65. fn value_shorthand(self);
  66. };
  67. match sig.receiver() {
  68. Some(FnArg::Receiver(Receiver {
  69. reference: None,
  70. mutability: None,
  71. ..
  72. })) => (),
  73. value => panic!("expected FnArg::Receiver without ref/mut, got {:?}", value),
  74. }
  75. }
  76. #[test]
  77. fn test_mut_value_shorthand() {
  78. let TraitItemMethod { sig, .. } = parse_quote! {
  79. fn mut_value_shorthand(mut self);
  80. };
  81. match sig.receiver() {
  82. Some(FnArg::Receiver(Receiver {
  83. reference: None,
  84. mutability: Some(_),
  85. ..
  86. })) => (),
  87. value => panic!("expected FnArg::Receiver with mut, got {:?}", value),
  88. }
  89. }
  90. #[test]
  91. fn test_ref_shorthand() {
  92. let TraitItemMethod { sig, .. } = parse_quote! {
  93. fn ref_shorthand(&self);
  94. };
  95. match sig.receiver() {
  96. Some(FnArg::Receiver(Receiver {
  97. reference: Some(_),
  98. mutability: None,
  99. ..
  100. })) => (),
  101. value => panic!("expected FnArg::Receiver with ref, got {:?}", value),
  102. }
  103. }
  104. #[test]
  105. fn test_ref_mut_shorthand() {
  106. let TraitItemMethod { sig, .. } = parse_quote! {
  107. fn ref_mut_shorthand(&mut self);
  108. };
  109. match sig.receiver() {
  110. Some(FnArg::Receiver(Receiver {
  111. reference: Some(_),
  112. mutability: Some(_),
  113. ..
  114. })) => (),
  115. value => panic!("expected FnArg::Receiver with ref+mut, got {:?}", value),
  116. }
  117. }