Skip to content
Myr

Move repr_correctness tests to integration test file

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 22c2608 · Browse files · 4 files+113 added−115 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+113 added−115 removed

  • tests:+113 added−63 removed
  • other:+0 added−52 removed

4 files+113 added−115 removed

modified:src/error.rs-52 removed
525 unmodified lines
526 self.next = next.source();527 Some(next)528 }529}530531#[cfg(test)]532mod repr_correctness {533 use super::*;534 use std::marker::Unpin;535 use std::mem;536 use std::sync::atomic::AtomicBool;537 use std::sync::atomic::Ordering::SeqCst;538 use std::sync::Arc;539540 #[test]541 fn size_of_error() {542 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());543 }544545 #[test]546 fn error_autotraits() {547 fn assert<E: Unpin + Send + Sync + 'static>() {}548 assert::<Error>();549 }550551 #[test]552 fn drop_works() {553 #[derive(Debug)]554 struct DetectDrop {555 has_dropped: Arc<AtomicBool>,556 }557558 impl StdError for DetectDrop {}559560 impl Display for DetectDrop {561 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {562 write!(f, "does something")563 }564 }565566 impl Drop for DetectDrop {567 fn drop(&mut self) {568 let already_dropped = self.has_dropped.swap(true, SeqCst);569 assert!(!already_dropped);570 }571 }572573 let has_dropped = Arc::new(AtomicBool::new(false));574575 drop(Error::new(DetectDrop {576 has_dropped: has_dropped.clone(),577 }));578579 assert!(has_dropped.load(SeqCst));580 }581}
deleted:tests/macros.rs-63 removed
1use anyhow::{bail, ensure, Result};2use std::io;34fn bail_literal() -> Result<()> {5 bail!("oh no!");6}78fn bail_fmt() -> Result<()> {9 bail!("{} {}!", "oh", "no");10}1112fn bail_error() -> Result<()> {13 bail!(io::Error::new(io::ErrorKind::Other, "oh no!"));14}1516#[test]17fn test_messages() {18 assert_eq!("oh no!", bail_literal().unwrap_err().to_string());19 assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());20 assert_eq!("oh no!", bail_error().unwrap_err().to_string());21}2223#[test]24fn test_downcast() {25 assert_eq!(26 "oh no!",27 bail_literal().unwrap_err().downcast::<&str>().unwrap(),28 );29 assert_eq!(30 "oh no!",31 bail_fmt().unwrap_err().downcast::<String>().unwrap(),32 );33 assert_eq!(34 "oh no!",35 bail_error()36 .unwrap_err()37 .downcast::<io::Error>()38 .unwrap()39 .to_string(),40 );41}4243#[test]44fn test_ensure() {45 let f = || {46 ensure!(1 + 1 == 2, "This is correct");47 Ok(())48 };49 assert!(f().is_ok());5051 let v = 1;52 let f = || {53 ensure!(v + v == 2, "This is correct, v: {}", v);54 Ok(())55 };56 assert!(f().is_ok());5758 let f = || {59 ensure!(v + v == 1, "This is not correct, v: {}", v);60 Ok(())61 };62 assert!(f().is_err());63}
added:tests/test_macros.rs+63 added
1use anyhow::{bail, ensure, Result};2use std::io;34fn bail_literal() -> Result<()> {5 bail!("oh no!");6}78fn bail_fmt() -> Result<()> {9 bail!("{} {}!", "oh", "no");10}1112fn bail_error() -> Result<()> {13 bail!(io::Error::new(io::ErrorKind::Other, "oh no!"));14}1516#[test]17fn test_messages() {18 assert_eq!("oh no!", bail_literal().unwrap_err().to_string());19 assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());20 assert_eq!("oh no!", bail_error().unwrap_err().to_string());21}2223#[test]24fn test_downcast() {25 assert_eq!(26 "oh no!",27 bail_literal().unwrap_err().downcast::<&str>().unwrap(),28 );29 assert_eq!(30 "oh no!",31 bail_fmt().unwrap_err().downcast::<String>().unwrap(),32 );33 assert_eq!(34 "oh no!",35 bail_error()36 .unwrap_err()37 .downcast::<io::Error>()38 .unwrap()39 .to_string(),40 );41}4243#[test]44fn test_ensure() {45 let f = || {46 ensure!(1 + 1 == 2, "This is correct");47 Ok(())48 };49 assert!(f().is_ok());5051 let v = 1;52 let f = || {53 ensure!(v + v == 2, "This is correct, v: {}", v);54 Ok(())55 };56 assert!(f().is_ok());5758 let f = || {59 ensure!(v + v == 1, "This is not correct, v: {}", v);60 Ok(())61 };62 assert!(f().is_err());63}
added:tests/test_repr.rs+50 added
1use anyhow::Error;2use std::error::Error as StdError;3use std::fmt::{self, Display};4use std::marker::Unpin;5use std::mem;6use std::sync::atomic::AtomicBool;7use std::sync::atomic::Ordering::SeqCst;8use std::sync::Arc;910#[test]11fn size_of_error() {12 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());13}1415#[test]16fn error_autotraits() {17 fn assert<E: Unpin + Send + Sync + 'static>() {}18 assert::<Error>();19}2021#[test]22fn drop_works() {23 #[derive(Debug)]24 struct DetectDrop {25 has_dropped: Arc<AtomicBool>,26 }2728 impl StdError for DetectDrop {}2930 impl Display for DetectDrop {31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {32 write!(f, "does something")33 }34 }3536 impl Drop for DetectDrop {37 fn drop(&mut self) {38 let already_dropped = self.has_dropped.swap(true, SeqCst);39 assert!(!already_dropped);40 }41 }4243 let has_dropped = Arc::new(AtomicBool::new(false));4445 drop(Error::new(DetectDrop {46 has_dropped: has_dropped.clone(),47 }));4849 assert!(has_dropped.load(SeqCst));50}