From b117d7fb9fdd36501a7f7c095333999a81e512cb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 17 May 2016 00:14:10 +0200 Subject: [PATCH] Add tests for error types with custom functions and types --- libimagerror/src/error_gen.rs | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index e32feedb..c715eb13 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -115,7 +115,11 @@ macro_rules! generate_error_types { $kindname: ident, $($kind:ident => $string:expr),* ) => { - generate_custom_error_types!($name, $kindname, members = {}, functions = {}, $($kind),*); + #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)] + pub struct SomeNotExistingTypeWithATypeNameNoOneWillEverChoose {} + generate_custom_error_types!($name, $kindname, + SomeNotExistingTypeWithATypeNameNoOneWillEverChoose, + $($kind => $string),*); } } @@ -129,6 +133,36 @@ mod test { TestErrorKindB => "testerrorkind B"); ); + #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)] + pub struct CustomData { + pub test: i32, + pub othr: i64, + } + + generate_error_imports!(); + + generate_custom_error_types!(CustomTestError, CustomTestErrorKind, + CustomData, + CustomErrorKindA => "customerrorkind a", + CustomErrorKindB => "customerrorkind B"); + + impl CustomTestError { + pub fn test(&self) -> i32 { + match self.custom_data { + Some(t) => t.test, + None => 0, + } + } + + pub fn bar(&self) -> i64 { + match self.custom_data { + Some(t) => t.othr, + None => 0, + } + } + } + + #[test] fn test_a() { use self::error::{TestError, TestErrorKind};