| 1 | //! Macros for testing conditions without panicking. The `expect_*` family of macros cause an early |
| 2 | //! return of an [`anyhow::Error`] if the expected condition is not met. |
| 3 | |
| 4 | use anyhow::anyhow; |
| 5 | |
| 6 | #[doc(hidden)] |
| 7 | pub fn msg(file: &str, line: u32, expr: &str, msg: &str) -> anyhow::Error { |
| 8 | anyhow!("\n\n\x1B[1m[{file}:{line}]\x1B[0m \x1B[4m{expr}\x1B[0m: \x1B[31m{msg}\x1B[0m\n") |
| 9 | } |
| 10 | |
| 11 | /// Expects that the given condition evaluates to `true`; otherwise returns early. |
| 12 | /// |
| 13 | /// # Examples |
| 14 | /// |
| 15 | /// ``` |
| 16 | /// # use anyhow::Result; |
| 17 | /// # use expecting::expect; |
| 18 | /// fn passing_test() -> Result<()> { |
| 19 | /// expect!(1 + 1 == 2); |
| 20 | /// Ok(()) |
| 21 | /// } |
| 22 | /// # assert!(passing_test().is_ok()); |
| 23 | /// |
| 24 | /// fn failing_test() -> Result<()> { |
| 25 | /// expect!(1 + 1 == 69); // returns early |
| 26 | /// Ok(()) // won't be reached |
| 27 | /// } |
| 28 | /// # assert!(failing_test().is_err()); |
| 29 | /// ``` |
| 30 | /// |
| 31 | /// `failing_test()` will return early after calling `expect!`. The `Err` will be wrapped in a |
| 32 | /// descriptive error message such as: |
| 33 | /// |
| 34 | /// > **[my/file.rs:12]** *"expect!(condition)"* Expected true but was false.<br/> |
| 35 | /// > [ ... wrapped Err details ...] |
| 36 | /// |
| 37 | #[macro_export] |
| 38 | macro_rules! expect { |
| 39 | ( $a:expr ) => {{ |
| 40 | if $a { |
| 41 | Ok(()) |
| 42 | } else { |
| 43 | let expr_str = format!("expect({})", stringify!($a)); |
| 44 | let msg = format!("Expected true but was false."); |
| 45 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 46 | } |
| 47 | }?}; |
| 48 | } |
| 49 | |
| 50 | /// Expects that the given [`Result`] is `Ok` and returns its unwrapped contents; otherwise returns |
| 51 | /// early. |
| 52 | /// |
| 53 | /// # Examples |
| 54 | /// |
| 55 | /// ``` |
| 56 | /// # use anyhow::{anyhow, Result}; |
| 57 | /// # use expecting::{expect_eq, expect_ok}; |
| 58 | /// fn passing_test() -> Result<()> { |
| 59 | /// let result: Result<i32> = Ok(69); |
| 60 | /// let contents = expect_ok!(result); |
| 61 | /// expect_eq!(contents, 69); |
| 62 | /// Ok(()) |
| 63 | /// } |
| 64 | /// # assert!(passing_test().is_ok()); |
| 65 | /// |
| 66 | /// fn failing_test() -> Result<()> { |
| 67 | /// let result: Result<i32> = Err(anyhow!("ruh roh!")); |
| 68 | /// let contents = expect_ok!(result); // returns early |
| 69 | /// Ok(()) // won't be reached |
| 70 | /// } |
| 71 | /// # assert!(failing_test().is_err()); |
| 72 | /// ``` |
| 73 | /// |
| 74 | /// `failing_test()` will return early after calling `expect_ok!`. The `Err` will be wrapped in a |
| 75 | /// descriptive error message such as: |
| 76 | /// |
| 77 | /// > **[my/file.rs:12]** *"expect_ok!(result)"* Expected Ok, got Err: "ruh roh!"<br/> |
| 78 | /// > [ ... wrapped Err details ...] |
| 79 | /// |
| 80 | #[macro_export] |
| 81 | macro_rules! expect_ok { |
| 82 | ( $result:expr ) => { |
| 83 | match $result { |
| 84 | Ok(t) => Ok(t), |
| 85 | Err(ref e) => { |
| 86 | let expr_str = format!("expect_ok({})", stringify!($result)); |
| 87 | let msg = format!("Expected Ok, got Err:\n{e:?})"); |
| 88 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 89 | } |
| 90 | }? |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /// Expects that the given [`Result`] is `Err` and returns the unwrapped error; otherwise returns |
| 95 | /// early. |
| 96 | /// |
| 97 | /// # Examples |
| 98 | /// |
| 99 | /// ``` |
| 100 | /// # use anyhow::{anyhow, Result}; |
| 101 | /// # use expecting::{expect, expect_err}; |
| 102 | /// fn passing_test() -> Result<()> { |
| 103 | /// let result: Result<i32> = Err(anyhow!("ruh roh!")); |
| 104 | /// let err = expect_err!(result); |
| 105 | /// expect!(err.to_string().contains("ruh roh!")); |
| 106 | /// Ok(()) |
| 107 | /// } |
| 108 | /// # assert!(passing_test().is_ok()); |
| 109 | /// |
| 110 | /// fn failing_test() -> Result<()> { |
| 111 | /// let err = expect_err!(Ok(())); // returns early |
| 112 | /// Ok(()) // won't be reached |
| 113 | /// } |
| 114 | /// # assert!(failing_test().is_err()); |
| 115 | /// ``` |
| 116 | /// |
| 117 | /// `failing_test()` will return early after calling `expect_err!`. The `Err` will be wrapped in a |
| 118 | /// descriptive error message such as: |
| 119 | /// |
| 120 | /// > **[my/file.rs:12]** *"expect_err!(result)"* Expected Err, got Ok: [ contents ]<br/> |
| 121 | /// > [ ... wrapped Err details ...] |
| 122 | /// |
| 123 | #[macro_export] |
| 124 | macro_rules! expect_err { |
| 125 | ( $result:expr ) => { |
| 126 | match $result { |
| 127 | Err(e) => Ok(e), |
| 128 | Ok(t) => { |
| 129 | let expr_str = format!("expect_err({})", stringify!($result)); |
| 130 | let msg = format!("Expected Err, got Ok:\n{t:?}"); |
| 131 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 132 | } |
| 133 | }? |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | /// Expects that the given [`Option`] is `Some` and returns its unwrapped contents; otherwise |
| 138 | /// returns early. |
| 139 | /// |
| 140 | /// # Examples |
| 141 | /// |
| 142 | /// ``` |
| 143 | /// # use anyhow::Result; |
| 144 | /// # use expecting::{expect_eq, expect_some}; |
| 145 | /// fn passing_test() -> Result<()> { |
| 146 | /// let option = Some(69); |
| 147 | /// let contents = expect_some!(option); |
| 148 | /// expect_eq!(contents, 69); |
| 149 | /// Ok(()) |
| 150 | /// } |
| 151 | /// # assert!(passing_test().is_ok()); |
| 152 | /// |
| 153 | /// fn failing_test() -> Result<()> { |
| 154 | /// let option = None::<()>; |
| 155 | /// let contents = expect_some!(option); // returns early with Err |
| 156 | /// Ok(()) // won't be reached |
| 157 | /// } |
| 158 | /// # assert!(failing_test().is_err()); |
| 159 | /// ``` |
| 160 | /// |
| 161 | /// `failing_test()` will return early after calling `expect_some!`. The `Err` will be wrapped in a |
| 162 | /// descriptive error message such as: |
| 163 | /// |
| 164 | /// > **[my/file.rs:12]** *"expect_some!(option)"* Expected Some, got None"<br/> |
| 165 | /// > [ ... wrapped Err details ...] |
| 166 | /// |
| 167 | #[macro_export] |
| 168 | macro_rules! expect_some { |
| 169 | ( $option:expr ) => { |
| 170 | match $option { |
| 171 | Some(t) => Ok(t), |
| 172 | None => { |
| 173 | let expr_str = format!("expect_some({})", stringify!($option)); |
| 174 | let msg = format!("Expected Some, got None"); |
| 175 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 176 | } |
| 177 | }? |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | /// Expects that the given [`Option`] is `None`; otherwise returns early. |
| 182 | /// |
| 183 | /// # Examples |
| 184 | /// |
| 185 | /// ``` |
| 186 | /// # use anyhow::Result; |
| 187 | /// # use expecting::expect_none; |
| 188 | /// fn passing_test() -> Result<()> { |
| 189 | /// expect_none!(None::<()>); |
| 190 | /// Ok(()) |
| 191 | /// } |
| 192 | /// # assert!(passing_test().is_ok()); |
| 193 | /// |
| 194 | /// fn failing_test() -> Result<()>{ |
| 195 | /// expect_none!(Some(())); // returns early |
| 196 | /// Ok(()) // won't be reached |
| 197 | /// } |
| 198 | /// # assert!(failing_test().is_err()); |
| 199 | /// ``` |
| 200 | /// |
| 201 | /// `failing_test()` will return early after calling `expect_none!`. The `Err` will be wrapped in a |
| 202 | /// descriptive error message such as: |
| 203 | /// |
| 204 | /// > **[my/file.rs:12]** *"expect_none!(option)"* Expected None, got Some: [ contents ]"<br/> |
| 205 | /// > [ ... wrapped Err details ...] |
| 206 | /// |
| 207 | #[macro_export] |
| 208 | macro_rules! expect_none { |
| 209 | ( $option:expr ) => { |
| 210 | match $option { |
| 211 | None => Ok(()), |
| 212 | Some(t) => { |
| 213 | let expr_str = format!("expect_none({})", stringify!($option)); |
| 214 | let msg = format!("Expected None, got Some:\n{t:?})"); |
| 215 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 216 | } |
| 217 | }? |
| 218 | }; |
| 219 | } |
| 220 | |
| 221 | /// Expects that a == b; otherwise returns early. |
| 222 | /// |
| 223 | /// # Examples |
| 224 | /// |
| 225 | /// ``` |
| 226 | /// # use anyhow::Result; |
| 227 | /// # use expecting::expect_eq; |
| 228 | /// |
| 229 | /// fn passing_test() -> Result<()> { |
| 230 | /// expect_eq!(1, 1); |
| 231 | /// Ok(()) |
| 232 | /// } |
| 233 | /// # assert!(passing_test().is_ok()); |
| 234 | /// |
| 235 | /// fn failing_test() -> Result<()> { |
| 236 | /// expect_eq!(1, 2); // returns early |
| 237 | /// Ok(()) // won't be reached |
| 238 | /// } |
| 239 | /// # assert!(failing_test().is_err()); |
| 240 | /// ``` |
| 241 | #[macro_export] |
| 242 | macro_rules! expect_eq { |
| 243 | ( $a:expr, $b:expr ) => {{ |
| 244 | let a = $a; |
| 245 | let b = $b; |
| 246 | if a == b { |
| 247 | Ok(()) |
| 248 | } else { |
| 249 | let expr_str = format!("expect_eq({}, {})", stringify!($a), stringify!($b)); |
| 250 | let msg = format!("{:?} != {:?}, expected them to be equal.", a, b); |
| 251 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 252 | } |
| 253 | }?}; |
| 254 | } |
| 255 | |
| 256 | /// Expects that a != b; otherwise returns early. |
| 257 | /// |
| 258 | /// # Examples |
| 259 | /// |
| 260 | /// ``` |
| 261 | /// # use anyhow::Result; |
| 262 | /// # use expecting::expect_ne; |
| 263 | /// |
| 264 | /// fn passing_test() -> Result<()> { |
| 265 | /// expect_ne!(1, 2); |
| 266 | /// Ok(()) |
| 267 | /// } |
| 268 | /// # assert!(passing_test().is_ok()); |
| 269 | /// |
| 270 | /// fn failing_test() -> Result<()> { |
| 271 | /// expect_ne!(1, 1); // returns early |
| 272 | /// Ok(()) // won't be reached |
| 273 | /// } |
| 274 | /// # assert!(failing_test().is_err()); |
| 275 | /// ``` |
| 276 | #[macro_export] |
| 277 | macro_rules! expect_ne { |
| 278 | ( $a:expr, $b:expr ) => {{ |
| 279 | let a = $a; |
| 280 | let b = $b; |
| 281 | if a != b { |
| 282 | Ok(()) |
| 283 | } else { |
| 284 | let expr_str = format!("expect_ne({}, {})", stringify!($a), stringify!($b)); |
| 285 | let msg = format!("{:?} == {:?}, expected them *not* to be equal.", a, b); |
| 286 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 287 | } |
| 288 | }?}; |
| 289 | } |
| 290 | |
| 291 | /// Expects that the given container (e.g., [`Vec`]) is empty; otherwise returns early. |
| 292 | /// |
| 293 | /// # Examples |
| 294 | /// |
| 295 | /// ``` |
| 296 | /// # use anyhow::Result; |
| 297 | /// # use expecting::expect_empty; |
| 298 | /// |
| 299 | /// fn passing_test() -> Result<()> { |
| 300 | /// let empty: Vec<i32> = vec![]; |
| 301 | /// expect_empty!(empty); |
| 302 | /// Ok(()) |
| 303 | /// } |
| 304 | /// # assert!(passing_test().is_ok()); |
| 305 | /// |
| 306 | /// fn failing_test() -> Result<()> { |
| 307 | /// expect_empty!(vec![0, 1, 1, 2, 3, 5, 8, 13, 21]); // returns early |
| 308 | /// Ok(()) // won't be reached |
| 309 | /// } |
| 310 | /// # assert!(failing_test().is_err()); |
| 311 | /// ``` |
| 312 | #[macro_export] |
| 313 | macro_rules! expect_empty { |
| 314 | ( $a:expr ) => {{ |
| 315 | let a = $a; |
| 316 | if a.is_empty() { |
| 317 | Ok(()) |
| 318 | } else { |
| 319 | let expr_str = format!("expect_empty({})", stringify!($a)); |
| 320 | let msg = format!("Expected empty, but contained elements:\n{:?}", a); |
| 321 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 322 | } |
| 323 | }?}; |
| 324 | } |
| 325 | |
| 326 | /// Expects that the given container (e.g., [`Vec`]) contains at least one element or given string |
| 327 | /// has non-zero length; otherwise returns early. |
| 328 | /// |
| 329 | /// # Examples |
| 330 | /// |
| 331 | /// ``` |
| 332 | /// # use anyhow::Result; |
| 333 | /// # use expecting::expect_not_empty; |
| 334 | /// |
| 335 | /// fn passing_test() -> Result<()> { |
| 336 | /// expect_not_empty!(vec![1]); |
| 337 | /// Ok(()) |
| 338 | /// } |
| 339 | /// # assert!(passing_test().is_ok()); |
| 340 | /// |
| 341 | /// fn failing_test() -> Result<()> { |
| 342 | /// let empty: Vec<i32> = vec![]; |
| 343 | /// expect_not_empty!(empty); // returns early |
| 344 | /// Ok(()) // won't be reached |
| 345 | /// } |
| 346 | /// # assert!(failing_test().is_err()); |
| 347 | /// ``` |
| 348 | #[macro_export] |
| 349 | macro_rules! expect_not_empty { |
| 350 | ( $a:expr ) => {{ |
| 351 | let a = $a; |
| 352 | if !a.is_empty() { |
| 353 | Ok(()) |
| 354 | } else { |
| 355 | let expr_str = format!("expect_not_empty({})", stringify!($a)); |
| 356 | let msg = "Expected non-empty, but was empty."; |
| 357 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 358 | } |
| 359 | }?}; |
| 360 | } |
| 361 | |
| 362 | /// Expects that the given container (e.g., [`Vec`]) contains the element or given string contains |
| 363 | /// the substring; otherwise returns early. |
| 364 | /// |
| 365 | /// # Examples |
| 366 | /// |
| 367 | /// ``` |
| 368 | /// # use anyhow::Result; |
| 369 | /// # use expecting::expect_contains; |
| 370 | /// |
| 371 | /// fn passing_test() -> Result<()> { |
| 372 | /// let v: Vec<i32> = vec![42, 1337]; |
| 373 | /// expect_contains!(v, 42); |
| 374 | /// Ok(()) |
| 375 | /// } |
| 376 | /// # assert!(passing_test().is_ok()); |
| 377 | /// |
| 378 | /// fn passing_test_for_string() -> Result<()> { |
| 379 | /// let superstring = "angelheaded hipsters"; |
| 380 | /// expect_contains!(superstring, "hip"); |
| 381 | /// Ok(()) |
| 382 | /// } |
| 383 | /// # assert!(passing_test_for_string().is_ok()); |
| 384 | /// |
| 385 | /// fn failing_test() -> Result<()> { |
| 386 | /// let v: Vec<i32> = vec![42, 1337]; |
| 387 | /// expect_contains!(v, 13); // returns early |
| 388 | /// Ok(()) // won't be reached |
| 389 | /// } |
| 390 | /// # assert!(failing_test().is_err()); |
| 391 | /// ``` |
| 392 | #[macro_export] |
| 393 | macro_rules! expect_contains { |
| 394 | ( $container:expr, $element_or_substr:expr ) => {{ |
| 395 | let container = $container; |
| 396 | let element_or_substr = $element_or_substr; |
| 397 | if container.contains(&element_or_substr) { |
| 398 | Ok(()) |
| 399 | } else { |
| 400 | let expr_str = format!( |
| 401 | "expect_contains({}, {})", |
| 402 | stringify!($container), |
| 403 | stringify!($element_or_substr) |
| 404 | ); |
| 405 | let msg = format!( |
| 406 | "Expected to contain {:?}, but did not:\n{:?}", |
| 407 | element_or_substr, container, |
| 408 | ); |
| 409 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 410 | } |
| 411 | }?}; |
| 412 | } |
| 413 | |
| 414 | /// Expects that `a` is `Some` and its contents are equal to `b`. |
| 415 | /// |
| 416 | /// # Examples |
| 417 | /// |
| 418 | /// ``` |
| 419 | /// # use anyhow::Result; |
| 420 | /// # use expecting::expect_some_eq; |
| 421 | /// |
| 422 | /// fn passing_test() -> Result<()> { |
| 423 | /// expect_some_eq!(Some(1), 1); |
| 424 | /// Ok(()) |
| 425 | /// } |
| 426 | /// # assert!(passing_test().is_ok()); |
| 427 | /// |
| 428 | /// fn failing_test1() -> Result<()> { |
| 429 | /// // wrong contents, 1 != 2 |
| 430 | /// expect_some_eq!(Some(1), 2); // returns early |
| 431 | /// Ok(()) // won't be reached |
| 432 | /// } |
| 433 | /// # assert!(failing_test1().is_err()); |
| 434 | /// |
| 435 | /// fn failing_test2() -> Result<()> { |
| 436 | /// // first arg is None |
| 437 | /// expect_some_eq!(None::<i32>, 2); // returns early |
| 438 | /// Ok(()) // won't be reached |
| 439 | /// } |
| 440 | /// # assert!(failing_test2().is_err()); |
| 441 | /// ``` |
| 442 | #[macro_export] |
| 443 | macro_rules! expect_some_eq { |
| 444 | ( $some_a:expr, $b:expr ) => {{ |
| 445 | let b = $b; |
| 446 | let msg: String = match $some_a { |
| 447 | Some(a) => { |
| 448 | if a == b { |
| 449 | "".to_string() |
| 450 | } else { |
| 451 | format!("{:?} != {:?}, expected them to be equal.", a, b) |
| 452 | } |
| 453 | } |
| 454 | None => format!("Expected {} to be Some, was None.", stringify!($some_a)), |
| 455 | }; |
| 456 | |
| 457 | if msg.is_empty() { |
| 458 | Ok(()) |
| 459 | } else { |
| 460 | let expr_str = format!( |
| 461 | "expect_some_eq({}, {})", |
| 462 | stringify!($some_a), |
| 463 | stringify!($b) |
| 464 | ); |
| 465 | Err($crate::msg(file!(), line!(), &expr_str, &msg)) |
| 466 | } |
| 467 | }?}; |
| 468 | } |
| 469 | |
| 470 | #[cfg(test)] |
| 471 | #[allow(non_snake_case)] |
| 472 | mod tests { |
| 473 | use anyhow::{anyhow, Result}; |
| 474 | |
| 475 | #[test] |
| 476 | fn expect__success__no_early_return() { |
| 477 | let test = || -> Result<()> { |
| 478 | expect!(1 + 1 == 2); |
| 479 | Err(anyhow!("no early return")) |
| 480 | }; |
| 481 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn expect__failure__causes_early_return() { |
| 486 | let test = || -> Result<()> { |
| 487 | expect!(1 + 1 == 69); // should return early |
| 488 | Ok(()) |
| 489 | }; |
| 490 | assert!(test().is_err()); |
| 491 | } |
| 492 | |
| 493 | #[test] |
| 494 | fn expect_ok__success__returns_ok_value() { |
| 495 | let test = || -> Result<i64> { |
| 496 | let result: Result<i64> = Ok(69); |
| 497 | let contents = expect_ok!(result); |
| 498 | Ok(contents) |
| 499 | }; |
| 500 | let result = test(); |
| 501 | assert!(result.is_ok()); |
| 502 | assert_eq!(result.unwrap(), 69); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn expect_ok__failure__causes_early_return() { |
| 507 | let test = || -> Result<()> { |
| 508 | let result: Result<i64> = Err(anyhow!("ruh roh!")); |
| 509 | let _ = expect_ok!(result); // returns early with Err |
| 510 | Ok(()) // won't be reached |
| 511 | }; |
| 512 | assert!(test().is_err()); |
| 513 | } |
| 514 | |
| 515 | #[test] |
| 516 | fn expect_err__success__returns_err_value() { |
| 517 | let test = || -> Result<anyhow::Error> { |
| 518 | let result: Result<i64> = Err(anyhow!("ruh roh!")); |
| 519 | let err = expect_err!(result); |
| 520 | Ok(err) |
| 521 | }; |
| 522 | let result = test(); |
| 523 | assert!(result.is_ok()); |
| 524 | assert!(result.unwrap().to_string().contains("ruh roh!")); |
| 525 | } |
| 526 | |
| 527 | #[test] |
| 528 | fn expect_err__failure__causes_early_return() { |
| 529 | let test = || -> Result<()> { |
| 530 | let result: Result<i64> = Ok(69); |
| 531 | let _ = expect_err!(result); // returns early with Err |
| 532 | Ok(()) // won't be reached |
| 533 | }; |
| 534 | assert!(test().is_err()); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn expect_some__success__returns_some_value() { |
| 539 | let test = || -> Result<i32> { |
| 540 | let some = Some(1337); |
| 541 | let contents = expect_some!(some); |
| 542 | Ok(contents) |
| 543 | }; |
| 544 | let result = test(); |
| 545 | assert!(result.is_ok()); |
| 546 | assert_eq!(result.unwrap(), 1337); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn expect_some__failure__causes_early_return() { |
| 551 | let test = || -> Result<()> { |
| 552 | let _ = expect_some!(None::<i32>); // returns early with Err |
| 553 | Ok(()) // won't be reached |
| 554 | }; |
| 555 | assert!(test().is_err()); |
| 556 | } |
| 557 | |
| 558 | #[test] |
| 559 | fn expect_none__success__no_early_return() { |
| 560 | let test = || -> Result<()> { |
| 561 | expect_none!(None::<i64>); |
| 562 | Err(anyhow!("no early return")) |
| 563 | }; |
| 564 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 565 | } |
| 566 | |
| 567 | #[test] |
| 568 | fn expect_none__failure__causes_early_return() { |
| 569 | let test = || -> Result<()> { |
| 570 | expect_none!(Some(1337)); // returns early with Err |
| 571 | Ok(()) // won't be reached |
| 572 | }; |
| 573 | assert!(test().is_err()); |
| 574 | } |
| 575 | |
| 576 | #[test] |
| 577 | fn expect_eq__success__no_early_return() { |
| 578 | let test = || -> Result<()> { |
| 579 | expect_eq!(1, 1); |
| 580 | Err(anyhow!("no early return")) |
| 581 | }; |
| 582 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 583 | } |
| 584 | |
| 585 | #[test] |
| 586 | fn expect_eq__failure__causes_early_return() { |
| 587 | let test = || -> Result<()> { |
| 588 | expect_eq!(1, 2); // returns early with Err |
| 589 | Ok(()) // won't be reached |
| 590 | }; |
| 591 | assert!(test().is_err()); |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn expect_ne__success__no_early_return() { |
| 596 | let test = || -> Result<()> { |
| 597 | expect_ne!(1, 2); |
| 598 | Err(anyhow!("no early return")) |
| 599 | }; |
| 600 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 601 | } |
| 602 | |
| 603 | #[test] |
| 604 | fn expect_ne__failure__causes_early_return() { |
| 605 | let test = || -> Result<()> { |
| 606 | expect_ne!(1, 1); // returns early with Err |
| 607 | Ok(()) // won't be reached |
| 608 | }; |
| 609 | assert!(test().is_err()); |
| 610 | } |
| 611 | |
| 612 | #[test] |
| 613 | fn expect_empty__success__no_early_return() { |
| 614 | let test = || -> Result<()> { |
| 615 | let empty: Vec<i64> = vec![]; |
| 616 | expect_empty!(empty); |
| 617 | Err(anyhow!("no early return")) |
| 618 | }; |
| 619 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 620 | } |
| 621 | |
| 622 | #[test] |
| 623 | fn expect_empty__failure__causes_early_return() { |
| 624 | let test = || -> Result<()> { |
| 625 | let not_empty: Vec<i64> = vec![1]; |
| 626 | expect_empty!(not_empty); // should return early |
| 627 | Ok(()) |
| 628 | }; |
| 629 | assert!(test().is_err()); |
| 630 | } |
| 631 | |
| 632 | #[test] |
| 633 | fn expect_not_empty__success__no_early_return() { |
| 634 | let test = || -> Result<()> { |
| 635 | let not_empty: Vec<i64> = vec![1]; |
| 636 | expect_not_empty!(not_empty); |
| 637 | Err(anyhow!("no early return")) |
| 638 | }; |
| 639 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 640 | } |
| 641 | |
| 642 | #[test] |
| 643 | fn expect_not_empty__failure__causes_early_return() { |
| 644 | let test = || -> Result<()> { |
| 645 | let empty: Vec<i64> = vec![]; |
| 646 | expect_not_empty!(empty); // should return early |
| 647 | Ok(()) |
| 648 | }; |
| 649 | assert!(test().is_err()); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn expect_not_empty__string__success__no_early_return() { |
| 654 | let test = || -> Result<()> { |
| 655 | expect_not_empty!("asdf"); |
| 656 | Err(anyhow!("no early return")) |
| 657 | }; |
| 658 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn expect_not_empty__string__failure__causes_early_return() { |
| 663 | let test = || -> Result<()> { |
| 664 | expect_not_empty!(""); // should return early |
| 665 | Ok(()) |
| 666 | }; |
| 667 | assert!(test().is_err()); |
| 668 | } |
| 669 | |
| 670 | #[test] |
| 671 | fn expect_contains__vec_of_int_success__no_early_return() { |
| 672 | let test = || -> Result<()> { |
| 673 | let v: Vec<i64> = vec![42, 1337]; |
| 674 | expect_contains!(v, 42); |
| 675 | Err(anyhow!("no early return")) |
| 676 | }; |
| 677 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 678 | } |
| 679 | |
| 680 | #[test] |
| 681 | fn expect_contains__vec_of_int_failure__causes_early_return() { |
| 682 | let test = || -> Result<()> { |
| 683 | let v: Vec<i64> = vec![42, 1337]; |
| 684 | expect_contains!(v, 69); |
| 685 | Ok(()) |
| 686 | }; |
| 687 | assert!(test().is_err()); |
| 688 | } |
| 689 | |
| 690 | #[test] |
| 691 | fn expect_contains__string_success__no_early_return() { |
| 692 | let test = || -> Result<()> { |
| 693 | let superstring = "angelheaded hipsters burning for the ancient heavenly connection"; |
| 694 | expect_contains!(superstring, "hipsters"); |
| 695 | Err(anyhow!("no early return")) |
| 696 | }; |
| 697 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 698 | } |
| 699 | |
| 700 | #[test] |
| 701 | fn expect_contains__string_failure__causes_early_return() { |
| 702 | let test = || -> Result<()> { |
| 703 | let superstring = "angelheaded hipsters burning for the ancient heavenly connection"; |
| 704 | expect_contains!(superstring, "yuppies"); |
| 705 | Ok(()) |
| 706 | }; |
| 707 | assert!(test().is_err()); |
| 708 | } |
| 709 | |
| 710 | #[test] |
| 711 | fn expect_contains__string_failure__returns_correct_err_message() { |
| 712 | let test = || -> Result<()> { |
| 713 | let superstring = "angelheaded hipsters burning"; |
| 714 | expect_contains!(superstring, "yuppies"); |
| 715 | Ok(()) |
| 716 | }; |
| 717 | let result = test(); |
| 718 | assert!(result.is_err()); |
| 719 | assert!(result.unwrap_err().to_string().contains( |
| 720 | "Expected to contain \"yuppies\", but did not:\n\"angelheaded hipsters burning\"" |
| 721 | )); |
| 722 | } |
| 723 | |
| 724 | #[test] |
| 725 | fn expect_some_eq__int_success__no_early_return() { |
| 726 | let test = || -> Result<()> { |
| 727 | expect_some_eq!(Some(1), 1); |
| 728 | Err(anyhow!("no early return")) |
| 729 | }; |
| 730 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 731 | } |
| 732 | |
| 733 | #[test] |
| 734 | fn expect_some_eq__int_wrong_contents__causes_early_return() { |
| 735 | let test = || -> Result<()> { |
| 736 | expect_some_eq!(Some(1), 2); // returns early with Err |
| 737 | Ok(()) // won't be reached |
| 738 | }; |
| 739 | assert!(test().is_err()); |
| 740 | } |
| 741 | |
| 742 | #[test] |
| 743 | fn expect_some_eq__int_none__causes_early_return() { |
| 744 | let test = || -> Result<()> { |
| 745 | expect_some_eq!(None::<i32>, 2); // returns early with Err |
| 746 | Ok(()) // won't be reached |
| 747 | }; |
| 748 | assert!(test().is_err()); |
| 749 | } |
| 750 | |
| 751 | #[test] |
| 752 | fn expect_some_eq__string_success__no_early_return() { |
| 753 | let test = || -> Result<()> { |
| 754 | let contents = "george"; |
| 755 | expect_some_eq!(Some(contents.to_string()), contents); |
| 756 | Err(anyhow!("no early return")) |
| 757 | }; |
| 758 | assert!(test().is_err_and(|e| e.to_string().contains("no early return"))); |
| 759 | } |
| 760 | } |