Skip to content

Commit

Permalink
Separate tests into tests/error.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Aug 30, 2019
1 parent 47c3ad1 commit 0e6ea5d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
45 changes: 0 additions & 45 deletions src/liballoc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,48 +888,3 @@ impl dyn Error + Send + Sync {
})
}
}

#[cfg(test)]
mod tests {
use super::Error;
use core::fmt;

#[derive(Debug, PartialEq)]
struct A;
#[derive(Debug, PartialEq)]
struct B;

impl fmt::Display for A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "A")
}
}
impl fmt::Display for B {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "B")
}
}

impl Error for A {
fn description(&self) -> &str { "A-desc" }
}
impl Error for B {
fn description(&self) -> &str { "A-desc" }
}

#[test]
fn downcasting() {
let mut a = A;
let a = &mut a as &mut (dyn Error + 'static);
assert_eq!(a.downcast_ref::<A>(), Some(&A));
assert_eq!(a.downcast_ref::<B>(), None);
assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
assert_eq!(a.downcast_mut::<B>(), None);

let a: Box<dyn Error> = Box::new(A);
match a.downcast::<B>() {
Ok(..) => panic!("expected error"),
Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
}
}
}
41 changes: 41 additions & 0 deletions src/liballoc/tests/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::error::Error;
use std::fmt;

#[derive(Debug, PartialEq)]
struct A;
#[derive(Debug, PartialEq)]
struct B;

impl fmt::Display for A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "A")
}
}
impl fmt::Display for B {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "B")
}
}

impl Error for A {
fn description(&self) -> &str { "A-desc" }
}
impl Error for B {
fn description(&self) -> &str { "A-desc" }
}

#[test]
fn downcasting() {
let mut a = A;
let a = &mut a as &mut (dyn Error + 'static);
assert_eq!(a.downcast_ref::<A>(), Some(&A));
assert_eq!(a.downcast_ref::<B>(), None);
assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
assert_eq!(a.downcast_mut::<B>(), None);

let a: Box<dyn Error> = Box::new(A);
match a.downcast::<B>() {
Ok(..) => panic!("expected error"),
Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
}
}

0 comments on commit 0e6ea5d

Please sign in to comment.