--- a/rust/hedgewars-engine-messages/src/parser.rs Tue Jul 30 22:59:06 2019 +0300
+++ b/rust/hedgewars-engine-messages/src/parser.rs Fri Aug 02 13:35:23 2019 +0200
@@ -1,10 +1,9 @@
-use nom::{Err::Error, *};
-use std::str;
-
-use super::messages::{
+use crate::messages::{
ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
UnorderedEngineMessage::*, *,
};
+use nom::{Err::Error, *};
+use std::str;
macro_rules! eof_slice (
($i:expr,) => (
@@ -129,62 +128,74 @@
let consumed = buf.len() - tail.len();
Some((consumed, msg))
- },
+ }
Err(Err::Incomplete(_)) => None,
Err(Err::Error(_)) | Err(Err::Failure(_)) => unreachable!(),
}
}
-#[test]
-fn parse_length() {
- assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
- assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
- assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
- assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
- assert_eq!(
- length_specifier(b"\xff\xff"),
- Ok((&b""[..], EngineMessage::MAX_LEN))
- );
-}
+#[cfg(test)]
+mod tests {
+ use crate::messages::UnsyncedEngineMessage::*;
+ use crate::parser::*;
-#[test]
-fn parse_synced_messages() {
- assert_eq!(
- message(b"\x03L\x01\x02"),
- Ok((&b""[..], Synced(Left(Press), 258)))
- );
+ #[test]
+ fn parse_length() {
+ assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
+ assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
+ assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
+ assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
+ assert_eq!(
+ length_specifier(b"\xff\xff"),
+ Ok((&b""[..], EngineMessage::MAX_LEN))
+ );
+ }
- assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535))));
+ #[test]
+ fn parse_synced_messages() {
+ assert_eq!(
+ message(b"\x03L\x01\x02"),
+ Ok((&b""[..], Synced(Left(Press), 258)))
+ );
- assert_eq!(message(&vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]), Ok((&b""[..], Synced(Put(-31337, 65538), 0))));
-}
+ assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535))));
+
+ assert_eq!(
+ message(&vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]),
+ Ok((&b""[..], Synced(Put(-31337, 65538), 0)))
+ );
+ }
-#[test]
-fn parse_unsynced_messages() {
- assert_eq!(
- message(b"\x06shello"),
- Ok((&b""[..], Unordered(ChatMessage(String::from("hello")))))
- );
-}
+ #[test]
+ fn parse_unsynced_messages() {
+ assert_eq!(
+ message(b"\x06shello"),
+ Ok((&b""[..], Unsynced(ChatMessage(String::from("hello")))))
+ );
+ }
-#[test]
-fn parse_incorrect_messages() {
- assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
- assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
+ #[test]
+ fn parse_incorrect_messages() {
+ assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
+ assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
+
+ // garbage after correct message
+ assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown)));
+ }
- // garbage after correct message
- assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown)));
-}
+ #[test]
+ fn parse_config_messages() {
+ assert_eq!(message(b"\x01C"), Ok((&b""[..], Config(ConfigRequest))));
+ }
-#[test]
-fn parse_config_messages() {
- assert_eq!(message(b"\x01C"), Ok((&b""[..], Config(ConfigRequest))));
+ #[test]
+ fn parse_test_general() {
+ assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
+
+ assert_eq!(extract_message(b"\x02#"), None);
+ assert_eq!(
+ extract_message(b"\x01#"),
+ Some((2, Synced(TimeWrap, 65535)))
+ );
+ }
}
-
-#[test]
-fn parse_test_general() {
- assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
-
- assert_eq!(extract_message(b"\x02#"), None);
- assert_eq!(extract_message(b"\x01#"), Some((2, Synced(TimeWrap, 65535))));
-}