Skip to content
Myr

test_macros.rs

This file

Path

tests/test_macros.rs

97 lines · 2.2 KiB · Rust

Last changed by

Merge pull request #436 from dtolnay/upDavid Tolnaybefore adoption67df012

History of this file

Take it

1#![allow(2    clippy::assertions_on_result_states,3    clippy::eq_op,4    clippy::incompatible_msrv, // https://github.com/rust-lang/rust-clippy/issues/122575    clippy::items_after_statements,6    clippy::match_single_binding,7    clippy::needless_pass_by_value,8    clippy::shadow_unrelated,9    clippy::uninlined_format_args,10    clippy::wildcard_imports11)]1213mod common;1415use self::common::*;16use anyhow::{anyhow, ensure, Result};17use std::cell::Cell;18use std::future;1920#[test]21fn test_messages() {22    assert_eq!("oh no!", bail_literal().unwrap_err().to_string());23    assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());24    assert_eq!("oh no!", bail_error().unwrap_err().to_string());25}2627#[test]28fn test_ensure() {29    let f = || {30        ensure!(1 + 1 == 2, "This is correct");31        Ok(())32    };33    assert!(f().is_ok());3435    let v = 1;36    let f = || {37        ensure!(v + v == 2, "This is correct, v: {}", v);38        Ok(())39    };40    assert!(f().is_ok());4142    let f = || {43        ensure!(v + v == 1, "This is not correct, v: {}", v);44        Ok(())45    };46    assert!(f().is_err());4748    let f = || {49        ensure!(v + v == 1);50        Ok(())51    };52    assert_eq!(53        f().unwrap_err().to_string(),54        "Condition failed: `v + v == 1` (2 vs 1)",55    );56}5758#[test]59fn test_ensure_nonbool() -> Result<()> {60    struct Struct {61        condition: bool,62    }6364    let s = Struct { condition: true };65    match &s {66        Struct { condition } => ensure!(condition), // &bool67    }6869    Ok(())70}7172#[test]73fn test_temporaries() {74    fn require_send_sync(_: impl Send + Sync) {}7576    require_send_sync(async {77        // If anyhow hasn't dropped any temporary format_args it creates by the78        // time it's done evaluating, those will stick around until the79        // semicolon, which is on the other side of the await point, making the80        // enclosing future non-Send.81        future::ready(anyhow!("...")).await;82    });8384    fn message(cell: Cell<&str>) -> &str {85        cell.get()86    }8788    require_send_sync(async {89        future::ready(anyhow!(message(Cell::new("...")))).await;90    });91}9293#[test]94fn test_brace_escape() {95    let err = anyhow!("unterminated ${{..}} expression");96    assert_eq!("unterminated ${..} expression", err.to_string());97}