aboutsummaryrefslogtreecommitdiff
path: root/tests/test_generics.rs
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2021-10-12 15:03:25 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-10-12 15:03:25 +0000
commitb48cebb01a46ce58d831998a638d21f9f05bb4e7 (patch)
treef25b373e9bbc8db561e78b852158174cab38b01d /tests/test_generics.rs
parent06a73ab94b452367d8a34ffb5b8fbfaf5109006f (diff)
parentd1617d538f0b3f4b926f4de85ad0cade430b0dd9 (diff)
downloadthiserror-b48cebb01a46ce58d831998a638d21f9f05bb4e7.tar.gz
Merge "Upgrade rust/crates/thiserror to 1.0.29" am: d1617d538f
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/thiserror/+/1833621 Change-Id: I4c500b7904eca7a73672e0dcf9206185a0ef7290
Diffstat (limited to 'tests/test_generics.rs')
-rw-r--r--tests/test_generics.rs161
1 files changed, 161 insertions, 0 deletions
diff --git a/tests/test_generics.rs b/tests/test_generics.rs
new file mode 100644
index 0000000..f5e1de2
--- /dev/null
+++ b/tests/test_generics.rs
@@ -0,0 +1,161 @@
+#![deny(clippy::all, clippy::pedantic)]
+
+use std::fmt::{self, Debug, Display};
+use thiserror::Error;
+
+pub struct NoFormat;
+
+#[derive(Debug)]
+pub struct DebugOnly;
+
+pub struct DisplayOnly;
+
+impl Display for DisplayOnly {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("display only")
+ }
+}
+
+#[derive(Debug)]
+pub struct DebugAndDisplay;
+
+impl Display for DebugAndDisplay {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("debug and display")
+ }
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumDebugField<E>
+// where
+// E: Debug;
+//
+// impl<E> Error for EnumDebugField<E>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumDebugGeneric<E> {
+ #[error("{0:?}")]
+ FatalError(E),
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumFromGeneric<E>;
+//
+// impl<E> Error for EnumFromGeneric<E>
+// where
+// EnumDebugGeneric<E>: Error + 'static,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumFromGeneric<E> {
+ #[error("enum from generic")]
+ Source(#[from] EnumDebugGeneric<E>),
+}
+
+// Should expand to:
+//
+// impl<HasDisplay, HasDebug, HasNeither> Display
+// for EnumCompound<HasDisplay, HasDebug, HasNeither>
+// where
+// HasDisplay: Display,
+// HasDebug: Debug;
+//
+// impl<HasDisplay, HasDebug, HasNeither> Error
+// for EnumCompound<HasDisplay, HasDebug, HasNeither>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error)]
+pub enum EnumCompound<HasDisplay, HasDebug, HasNeither> {
+ #[error("{0} {1:?}")]
+ DisplayDebug(HasDisplay, HasDebug),
+ #[error("{0}")]
+ Display(HasDisplay, HasNeither),
+ #[error("{1:?}")]
+ Debug(HasNeither, HasDebug),
+}
+
+impl<HasDisplay, HasDebug, HasNeither> Debug for EnumCompound<HasDisplay, HasDebug, HasNeither> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("EnumCompound")
+ }
+}
+
+#[test]
+fn test_display_enum_compound() {
+ let mut instance: EnumCompound<DisplayOnly, DebugOnly, NoFormat>;
+
+ instance = EnumCompound::DisplayDebug(DisplayOnly, DebugOnly);
+ assert_eq!(format!("{}", instance), "display only DebugOnly");
+
+ instance = EnumCompound::Display(DisplayOnly, NoFormat);
+ assert_eq!(format!("{}", instance), "display only");
+
+ instance = EnumCompound::Debug(NoFormat, DebugOnly);
+ assert_eq!(format!("{}", instance), "DebugOnly");
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumTransparentGeneric<E>
+// where
+// E: Display;
+//
+// impl<E> Error for EnumTransparentGeneric<E>
+// where
+// E: Error,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumTransparentGeneric<E> {
+ #[error(transparent)]
+ Other(E),
+}
+
+// Should expand to:
+//
+// impl<E> Display for StructDebugGeneric<E>
+// where
+// E: Debug;
+//
+// impl<E> Error for StructDebugGeneric<E>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+#[error("{underlying:?}")]
+pub struct StructDebugGeneric<E> {
+ pub underlying: E,
+}
+
+// Should expand to:
+//
+// impl<E> Error for StructFromGeneric<E>
+// where
+// StructDebugGeneric<E>: Error + 'static,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub struct StructFromGeneric<E> {
+ #[from]
+ pub source: StructDebugGeneric<E>,
+}
+
+// Should expand to:
+//
+// impl<E> Display for StructTransparentGeneric<E>
+// where
+// E: Display;
+//
+// impl<E> Error for StructTransparentGeneric<E>
+// where
+// E: Error,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+#[error(transparent)]
+pub struct StructTransparentGeneric<E>(E);