Skip to content

Commit 1f0fdc2

Browse files
committed
Add tests for DefinitelySame
1 parent 369fa34 commit 1f0fdc2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/errors.rs

+52
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl DefinitelySame for Error {
213213
impl DefinitelySame for error_chain::State {
214214
fn definitely_same(&self, other: &Self) -> bool {
215215
// We ignore backtraces
216+
// We have to remove the Send bounds, which current Rust makes a bit annoying
216217
self.next_error.definitely_same(&other.next_error)
217218
}
218219
}
@@ -413,3 +414,54 @@ where
413414
self.with_instance(|i| std::fmt::Debug::fmt(&i, f))
414415
}
415416
}
417+
418+
#[cfg(test)]
419+
mod tests {
420+
use super::*;
421+
use error_chain::{ChainedError, State};
422+
use std::error::Error as StdError;
423+
424+
#[test]
425+
fn test_def_same_option() {
426+
let a = Some(Box::from(NewError::msg("A")));
427+
let b = Some(Box::from(NewError::msg("A")));
428+
429+
assert!(a.definitely_same(&b));
430+
assert!(b.definitely_same(&a));
431+
432+
let b = Some(Box::from(NewError::msg("B")));
433+
assert!(!a.definitely_same(&b));
434+
435+
let b = None;
436+
let c = None;
437+
assert!(!a.definitely_same(&b));
438+
assert!(b.definitely_same(&c));
439+
}
440+
441+
#[test]
442+
fn test_def_same_err() {
443+
let a = Error::new(ErrorKind::Msg(String::from("A")), State::default());
444+
let b = Error::new(ErrorKind::Msg(String::from("A")), State::default());
445+
446+
// Different backtraces but should be same
447+
assert!(a.definitely_same(&b));
448+
449+
let b = Error::new(ErrorKind::BadLength(0, 0), State::default());
450+
assert!(!a.definitely_same(&b));
451+
452+
let a = Error::new(ErrorKind::NewStyle(NewError::msg("A")), State::default());
453+
assert!(!a.definitely_same(&b));
454+
455+
let b = Error::new(ErrorKind::NewStyle(NewError::msg("A")), State::default());
456+
assert!(a.definitely_same(&b));
457+
}
458+
459+
#[test]
460+
fn test_def_same_box_err() {
461+
let a: Box<dyn StdError + Send> = Box::from(NewError::msg("A"));
462+
let b: Box<dyn StdError + Send> = Box::from(NewError::msg("B"));
463+
464+
assert!(a.definitely_same(&a));
465+
assert!(!a.definitely_same(&b));
466+
}
467+
}

0 commit comments

Comments
 (0)