-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from RicoGit/elfo-test-improvements
feat(test): `extract_message` and `extract_request`
- Loading branch information
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#![warn(rust_2018_idioms, unreachable_pub)] | ||
|
||
pub use proxy::{proxy, Proxy}; | ||
pub use utils::{extract_message, extract_request}; | ||
|
||
mod proxy; | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use elfo_core::{msg, Envelope, Message, Request, ResponseToken}; | ||
|
||
/// Extracts message with type [`M`] from [`Envelope`], panics otherwise. | ||
#[track_caller] | ||
pub fn extract_message<M: Message>(envelope: Envelope) -> M { | ||
msg!(match envelope { | ||
msg @ M => msg, | ||
msg => panic!( | ||
r#"expected {}, got {:#?}"#, | ||
elfo_core::dumping::extract_name_by_type::<M>(), | ||
msg.message() | ||
), | ||
}) | ||
} | ||
|
||
/// Extracts request message with type [`R`] from [`Envelope`], panics | ||
/// otherwise. | ||
#[track_caller] | ||
pub fn extract_request<R: Request>(envelope: Envelope) -> (R, ResponseToken<R>) { | ||
msg!(match envelope { | ||
(req @ R, token) => (req, token), | ||
msg => panic!( | ||
r#"expected {}, got {:#?}"#, | ||
elfo_core::dumping::extract_name_by_type::<R>(), | ||
msg.message() | ||
), | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use elfo_core::{_priv::MessageKind, message, scope::Scope, ActorMeta, Addr}; | ||
|
||
use super::*; | ||
|
||
#[message] | ||
#[derive(PartialEq)] | ||
struct TestMessage; | ||
|
||
#[message(ret = ())] | ||
#[derive(PartialEq)] | ||
struct TestRequest; | ||
|
||
#[test] | ||
fn extract_message_test() { | ||
create_scope().sync_within(|| { | ||
let envelop = | ||
Envelope::new(TestMessage, MessageKind::Regular { sender: Addr::NULL }).upcast(); | ||
let resp = extract_message::<TestMessage>(envelop); | ||
assert_eq!(resp, TestMessage); | ||
}); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "expected TestMessage, got TestRequest")] | ||
fn extract_message_panic_test() { | ||
create_scope().sync_within(|| { | ||
let envelop = | ||
Envelope::new(TestRequest, MessageKind::Regular { sender: Addr::NULL }).upcast(); | ||
extract_message::<TestMessage>(envelop); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn extract_request_test() { | ||
create_scope().sync_within(|| { | ||
let envelop = | ||
Envelope::new(TestRequest, MessageKind::Regular { sender: Addr::NULL }).upcast(); | ||
let (resp, _token) = extract_request::<TestRequest>(envelop); | ||
assert_eq!(resp, TestRequest); | ||
}); | ||
} | ||
|
||
fn create_scope() -> Scope { | ||
Scope::test( | ||
Addr::NULL, | ||
ActorMeta { | ||
group: "group".into(), | ||
key: "key".into(), | ||
} | ||
.into(), | ||
) | ||
} | ||
} |