Skip to content

Commit

Permalink
Add tests to check inner structs
Browse files Browse the repository at this point in the history
  • Loading branch information
justahero committed Jan 28, 2021
1 parent e883feb commit dc6e35f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,41 @@ mod tests {
);
}

#[test]
fn display_inner_str_in_struct() {
let mut entries = BTreeMap::new();

entries.insert(
0,
TableEntry::new_without_symbol(Tag::Info, "{}".to_owned()),
);
entries.insert(
1,
TableEntry::new_without_symbol(Tag::Derived, "S {{ x: {=str} }}".to_owned()),
);

let table = Table {
entries,
timestamp: Some(TableEntry::new_without_symbol(
Tag::Timestamp,
"{=u8:µs}".to_owned(),
)),
};

let bytes = [
0, // index
2, // timestamp
1, // index into the struct
5, // length of the string
b'H', b'e', b'l', b'l', b'o', // string "Hello"
];
let frame = super::decode(&bytes, &table).unwrap().0;
assert_eq!(
frame.display(false).to_string(),
"0.000002 INFO S { x: \"Hello\" }",
);
}

#[test]
fn bools_simple() {
let bytes = [
Expand Down
22 changes: 22 additions & 0 deletions firmware/qemu/src/bin/hints_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ fn main() -> ! {
defmt::warn!("{}", S { x: "hello", y: 1024 });
}

{
// nested struct
struct S1 { x: u16, y: u32 }
impl defmt::Format for S1 {
fn format(&self, f: defmt::Formatter) {
write!(f, "S1 {{ x: {=u16:b}, y: {} }}", self.x, self.y);
}
}

struct S2 { s: S1, z: u8 }
impl defmt::Format for S2 {
fn format(&self, f: defmt::Formatter) {
write!(f, "S2 {{ s: {:?}, z: {} }}", self.s, self.z);
}
}

defmt::info!("{}", S2 { s: S1 { x: 4, y: 12 }, z: 20 });
defmt::info!("{:?}", S2 { s: S1 { x: 4, y: 12 }, z: 20 });
defmt::info!("{:x}", S2 { s: S1 { x: 4, y: 12 }, z: 20 });
defmt::info!("{:b}", S2 { s: S1 { x: 4, y: 12 }, z: 20 });
}

loop {
debug::exit(debug::EXIT_SUCCESS)
}
Expand Down

0 comments on commit dc6e35f

Please sign in to comment.