Implement decode tests for serverbound

This commit is contained in:
Aram 🍐 2021-08-03 19:39:09 -04:00
parent e937899f8a
commit 779d279681

View file

@ -191,6 +191,18 @@ macro_rules! impl_sentences {
}; };
} }
#[allow(unused_macros)]
macro_rules! test_decode {
([$($word:expr$(,)?)+] => $expected:expr) => {
assert_eq!(
Sentences::decode(vec![
$($word.into(),)*
]),
$expected
);
};
}
/// Messages decoded by the server. /// Messages decoded by the server.
pub mod server_bound { pub mod server_bound {
impl_sentences! { impl_sentences! {
@ -362,8 +374,8 @@ pub mod server_bound {
{ {
0: List, 0: List,
1: Client, 1: Client,
3: Arg, 2: Arg,
4: EOL, 3: EOL,
}, },
{ {
/// The name of the UPS device. /// The name of the UPS device.
@ -511,16 +523,149 @@ pub mod server_bound {
use super::*; use super::*;
#[test] #[test]
fn test_decode() { fn test_decode() {
assert_eq!( test_decode!(
Sentences::decode(vec!["VERSION".into()]), ["GET", "NUMLOGINS", "nutdev"] =>
Some(Sentences::QueryVersion {}) Some(Sentences::QueryNumLogins {
ups_name: "nutdev".into(),
})
); );
assert_eq!( test_decode!(
Sentences::decode(vec!["LIST".into(), "VAR".into(), "nutdev".into()]), ["GET", "UPSDESC", "nutdev"] =>
Some(Sentences::QueryUpsDesc {
ups_name: "nutdev".into(),
})
);
test_decode!(
["GET", "VAR", "nutdev", "test.var"] =>
Some(Sentences::QueryVar {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
})
);
test_decode!(
["GET", "TYPE", "nutdev", "test.var"] =>
Some(Sentences::QueryType {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
})
);
test_decode!(
["GET", "DESC", "nutdev", "test.var"] =>
Some(Sentences::QueryDesc {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
})
);
test_decode!(
["GET", "CMDDESC", "nutdev", "test.cmd"] =>
Some(Sentences::QueryCmdDesc {
ups_name: "nutdev".into(),
cmd_name: "test.cmd".into(),
})
);
test_decode!(
["LIST", "VAR", "nutdev"] =>
Some(Sentences::QueryListVar { Some(Sentences::QueryListVar {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) })
); );
test_decode!(
["LIST", "RW", "nutdev"] =>
Some(Sentences::QueryListRw {
ups_name: "nutdev".into(),
})
);
test_decode!(
["LIST", "CMD", "nutdev"] =>
Some(Sentences::QueryListCmd {
ups_name: "nutdev".into(),
})
);
test_decode!(
["LIST", "ENUM", "nutdev", "test.var"] =>
Some(Sentences::QueryListEnum {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
})
);
test_decode!(
["LIST", "RANGE", "nutdev", "test.var"] =>
Some(Sentences::QueryListRange {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
})
);
test_decode!(
["LIST", "CLIENT", "nutdev"] =>
Some(Sentences::QueryListClient {
ups_name: "nutdev".into(),
})
);
test_decode!(
["SET", "VAR", "nutdev", "test.var", "something"] =>
Some(Sentences::ExecSetVar {
ups_name: "nutdev".into(),
var_name: "test.var".into(),
value: "something".into(),
})
);
test_decode!(
["INSTCMD", "nutdev", "test.cmd"] =>
Some(Sentences::ExecInstCmd {
ups_name: "nutdev".into(),
cmd_name: "test.cmd".into(),
})
);
test_decode!(
["LOGOUT"] =>
Some(Sentences::ExecLogout {})
);
test_decode!(
["LOGIN", "nutdev"] =>
Some(Sentences::ExecLogin {
ups_name: "nutdev".into(),
})
);
test_decode!(
["MASTER", "nutdev"] =>
Some(Sentences::ExecMaster {
ups_name: "nutdev".into(),
})
);
test_decode!(
["FSD", "nutdev"] =>
Some(Sentences::ExecForcedShutDown {
ups_name: "nutdev".into(),
})
);
test_decode!(
["PASSWORD", "topsecret"] =>
Some(Sentences::SetPassword {
password: "topsecret".into(),
})
);
test_decode!(
["USERNAME", "john"] =>
Some(Sentences::SetUsername {
username: "john".into(),
})
);
test_decode!(
["STARTTLS"] =>
Some(Sentences::ExecStartTLS {})
);
test_decode!(
["HELP"] =>
Some(Sentences::QueryHelp {})
);
test_decode!(
["VERSION"] =>
Some(Sentences::QueryVersion {})
);
test_decode!(
["NETVER"] =>
Some(Sentences::QueryNetworkVersion {})
);
} }
} }
} }