expect_macros.rs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // TODO(danakj): Reuse code for comparison macros with an expect_op!() macro?
  5. /// Internal helper to log an expectation failure. Other expect_* macros invoke
  6. /// it with their standard expectation message, plus optionally a user-provided
  7. /// custom string.
  8. ///
  9. /// Both the the expectation message and the user-provided message are format
  10. /// strings with arguments. To disambiguate between them, the expectation
  11. /// message is wrapped in extra parentheses.
  12. #[macro_export]
  13. macro_rules! internal_add_expectation_failure {
  14. // Rule that both the below are forwarded to.
  15. (@imp $fmt:literal, $($arg:tt)+) => {
  16. $crate::__private::add_failure_at(
  17. file!(),
  18. line!(),
  19. &format!($fmt, $($arg)+),
  20. )
  21. };
  22. // Add a failure with the standard message.
  23. (($expectation:literal, $($e:tt)+)) => {
  24. $crate::internal_add_expectation_failure!(@imp $expectation, $($e)+)
  25. };
  26. // Add a failure with the standard message plus an additional message.
  27. (($expectation:literal, $($e:tt)+), $($arg:tt)+) => {
  28. $crate::internal_add_expectation_failure!(@imp
  29. "{}\n\n{}",
  30. format_args!($expectation, $($e)+),
  31. format_args!($($arg)+),
  32. )
  33. };
  34. }
  35. /// Evaluates the given expression. If false, a failure is reported to Gtest.
  36. ///
  37. /// # Examples
  38. /// ```
  39. /// expect_true(check_the_status_is_true());
  40. /// ```
  41. #[macro_export]
  42. macro_rules! expect_true {
  43. ($e:expr $(, $($arg:tt)*)?) => {
  44. match &$e {
  45. val => {
  46. if !(*val) {
  47. $crate::internal_add_expectation_failure!(
  48. (
  49. "Expected: {} is true\nActual: {} is {:?}",
  50. stringify!($e),
  51. stringify!($e),
  52. *val
  53. )
  54. $(, $($arg)*)?
  55. )
  56. }
  57. }
  58. }
  59. };
  60. }
  61. /// Evaluates the given expression. If true, a failure is reported to Gtest.
  62. ///
  63. /// # Examples
  64. /// ```
  65. /// expect_false(check_the_status_is_false());
  66. /// ```
  67. #[macro_export]
  68. macro_rules! expect_false {
  69. ($e:expr $(, $($arg:tt)*)?) => {
  70. match &$e {
  71. val => {
  72. if *val {
  73. $crate::internal_add_expectation_failure!(
  74. (
  75. "Expected: {} is false\nActual: {} is {:?}",
  76. stringify!($e),
  77. stringify!($e),
  78. *val
  79. )
  80. $(, $($arg)*)?
  81. )
  82. }
  83. }
  84. }
  85. };
  86. }
  87. /// Evaluates and compares the two given expressions. If not equal, a failure is reported to Gtest.
  88. /// The expressions must evaluate to the same type, which must be PartialEq.
  89. ///
  90. /// # Examples
  91. /// ```
  92. /// expect_eq(1 + 1, 2);
  93. /// ```
  94. #[macro_export]
  95. macro_rules! expect_eq {
  96. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  97. match (&$e1, &$e2) {
  98. (val1, val2) => {
  99. if !(*val1 == *val2) {
  100. $crate::internal_add_expectation_failure!(
  101. (
  102. "Expected: {} == {}\nActual: {:?} vs {:?}",
  103. stringify!($e1),
  104. stringify!($e2),
  105. *val1,
  106. *val2
  107. )
  108. $(, $($arg)*)?
  109. )
  110. }
  111. }
  112. }
  113. };
  114. }
  115. /// Evaluates and compares the two given expressions. If equal, a failure is reported to Gtest.
  116. /// The expressions must evaluate to the same type, which must be PartialEq.
  117. ///
  118. /// # Examples
  119. /// ```
  120. /// expect_ne(1 + 1, 3);
  121. /// ```
  122. #[macro_export]
  123. macro_rules! expect_ne {
  124. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  125. match (&$e1, &$e2) {
  126. (val1, val2) => {
  127. if !(*val1 != *val2) {
  128. $crate::internal_add_expectation_failure!(
  129. (
  130. "Expected: {} != {}\nActual: {:?} vs {:?}",
  131. stringify!($e1),
  132. stringify!($e2),
  133. *val1,
  134. *val2
  135. )
  136. $(, $($arg)*)?
  137. )
  138. }
  139. }
  140. }
  141. };
  142. }
  143. /// Evaluates and compares the two given expressions. If the first is not greater than the second, a
  144. /// failure is reported to Gtest. The expressions must evaluate to the same type, which must be
  145. /// PartialOrd. If a PartialOrd comparison fails, the result is false.
  146. ///
  147. /// # Examples
  148. /// ```
  149. /// expect_gt(1 + 1, 1);
  150. /// ```
  151. #[macro_export]
  152. macro_rules! expect_gt {
  153. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  154. match (&$e1, &$e2) {
  155. (val1, val2) => {
  156. if !(*val1 > *val2) {
  157. $crate::internal_add_expectation_failure!(
  158. (
  159. "Expected: {} > {}\nActual: {:?} vs {:?}",
  160. stringify!($e1),
  161. stringify!($e2),
  162. *val1,
  163. *val2
  164. )
  165. $(, $($arg)*)?
  166. )
  167. }
  168. }
  169. }
  170. };
  171. }
  172. /// Evaluates and compares the two given expressions. If the first is not less than the second, a
  173. /// failure is reported to Gtest. The expressions must evaluate to the same type, which must be
  174. /// PartialOrd. If a PartialOrd comparison fails, the result is false.
  175. ///
  176. /// # Examples
  177. /// ```
  178. /// expect_lt(1 + 1, 1 + 2);
  179. /// ```
  180. #[macro_export]
  181. macro_rules! expect_lt {
  182. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  183. match (&$e1, &$e2) {
  184. (val1, val2) => {
  185. if !(*val1 < *val2) {
  186. $crate::internal_add_expectation_failure!(
  187. (
  188. "Expected: {} < {}\nActual: {:?} vs {:?}",
  189. stringify!($e1),
  190. stringify!($e2),
  191. *val1,
  192. *val2
  193. )
  194. $(, $($arg)*)?
  195. )
  196. }
  197. }
  198. }
  199. };
  200. }
  201. /// Evaluates and compares the two given expressions. If the first is not greater than or equal to
  202. /// the second, a failure is reported to Gtest. The expressions must evaluate to the same type,
  203. /// which must be PartialOrd. If a PartialOrd comparison fails, the result is false.
  204. ///
  205. /// # Examples
  206. /// ```
  207. /// expect_ge(1 + 1, 2);
  208. /// ```
  209. #[macro_export]
  210. macro_rules! expect_ge {
  211. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  212. match (&$e1, &$e2) {
  213. (val1, val2) => {
  214. if !(*val1 >= *val2) {
  215. $crate::internal_add_expectation_failure!(
  216. (
  217. "Expected: {} >= {}\nActual: {:?} vs {:?}",
  218. stringify!($e1),
  219. stringify!($e2),
  220. *val1,
  221. *val2
  222. ) $(, $($arg)*)?
  223. )
  224. }
  225. }
  226. }
  227. };
  228. }
  229. /// Evaluates and compares the two given expressions. If the first is not less than or equal to the
  230. /// second, a failure is reported to Gtest. The expressions must evaluate to the same type, /which
  231. /// must be PartialOrd. If a PartialOrd comparison fails, the result is false.
  232. ///
  233. /// # Examples
  234. /// ```
  235. /// expect_le(2, 1 + 1);
  236. /// ```
  237. #[macro_export]
  238. macro_rules! expect_le {
  239. ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => {
  240. match (&$e1, &$e2) {
  241. (val1, val2) => {
  242. if !(*val1 <= *val2) {
  243. $crate::internal_add_expectation_failure!(
  244. (
  245. "Expected: {} <= {}\nActual: {:?} vs {:?}",
  246. stringify!($e1),
  247. stringify!($e2),
  248. *val1,
  249. *val2
  250. ) $(, $($arg)*)?
  251. )
  252. }
  253. }
  254. }
  255. };
  256. }
  257. // TODO(danakj): There's a bunch more useful macros to write, even ignoring gmock:
  258. // - EXPECT_NONFATAL_FAILURE
  259. // - SCOPED_TRACE
  260. // - EXPECT_DEATH
  261. // - FAIL (fatal failure, would this work?)
  262. // - ADD_FAILURE
  263. // - SUCCEED
  264. // - EXPECT_PANIC (catch_unwind() with an error if no panic, but this depends on us using a stdlib
  265. // with -Cpanic=unwind in tests, currently we use -Cpanic=abort.)
  266. // - EXPECT_NO_PANIC (Like above, depende on -Cpanic=unwind, or else all panics just abort the
  267. // process.)
  268. // - EXPECT_FLOAT_EQ (Comparison for equality within a small range.)
  269. // - EXPECT_NEAR (Comparison for equality within a user-specified range.)