fix option decoding and add basic sanity test (#161)

Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
This commit is contained in:
Greg Hill
2020-09-15 08:24:05 +01:00
committed by GitHub
parent f2cf79847a
commit 2829d7d7c1
2 changed files with 34 additions and 3 deletions
+33 -2
View File
@@ -156,8 +156,11 @@ impl<T: System> EventsDecoder<T> {
}
EventArg::Option(arg) => {
match input.read_byte()? {
0 => (),
1 => self.decode_raw_bytes(&[*arg.clone()], input, output)?,
0 => output.push_byte(0),
1 => {
output.push_byte(1);
self.decode_raw_bytes(&[*arg.clone()], input, output)?
}
_ => {
return Err(Error::Other(
"unexpected first byte decoding Option".into(),
@@ -247,3 +250,31 @@ pub enum Raw {
Event(RawEvent),
Error(RuntimeError),
}
#[cfg(test)]
mod tests {
use super::*;
type TestRuntime = crate::NodeTemplateRuntime;
#[test]
fn test_decode_option() {
let decoder = EventsDecoder::<TestRuntime>::new(Metadata::default());
let value = Some(0u8);
let input = value.encode();
let mut output = Vec::<u8>::new();
decoder
.decode_raw_bytes(
&[EventArg::Option(Box::new(EventArg::Primitive(
"u8".to_string(),
)))],
&mut &input[..],
&mut output,
)
.unwrap();
assert_eq!(output, vec![1, 0]);
}
}
+1 -1
View File
@@ -73,7 +73,7 @@ pub enum MetadataError {
}
/// Runtime metadata.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Metadata {
modules: HashMap<String, ModuleMetadata>,
modules_with_calls: HashMap<String, ModuleWithCalls>,