#![allow(clippy::unwrap_used, clippy::expect_used)]
use matter_clusters::gen::descriptor::{decode_device_type_list, DeviceTypeStruct};
use matter_clusters::gen::occupancy_sensing::decode_occupancy;
use matter_codec::{Tag, TlvWriter};
fn device_type_with_unknown_nested() -> Vec<u8> {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_structure(Tag::Anonymous).unwrap();
w.put_uint(Tag::Context(0), 0x1234_5678).unwrap();
w.start_structure(Tag::Context(99)).unwrap();
w.put_uint(Tag::Context(0), 1).unwrap();
w.end_container().unwrap();
w.put_uint(Tag::Context(1), 7).unwrap();
w.end_container().unwrap();
buf
}
#[test]
fn struct_decode_skips_unknown_nested_container() {
let buf = device_type_with_unknown_nested();
let d = DeviceTypeStruct::decode(&buf).expect("must decode despite unknown field");
assert_eq!(d.device_type, 0x1234_5678);
assert_eq!(d.revision, 7); }
#[test]
fn list_decode_skips_unknown_container_element() {
let mut buf = Vec::new();
let mut w = TlvWriter::new(&mut buf);
w.start_array(Tag::Anonymous).unwrap();
w.start_structure(Tag::Anonymous).unwrap();
w.put_uint(Tag::Context(0), 10).unwrap();
w.put_uint(Tag::Context(1), 1).unwrap();
w.end_container().unwrap();
w.start_array(Tag::Anonymous).unwrap();
w.put_uint(Tag::Anonymous, 5).unwrap();
w.end_container().unwrap();
w.start_structure(Tag::Anonymous).unwrap();
w.put_uint(Tag::Context(0), 20).unwrap();
w.put_uint(Tag::Context(1), 2).unwrap();
w.end_container().unwrap();
w.end_container().unwrap();
let list = decode_device_type_list(&buf).expect("must decode despite unknown element");
assert_eq!(list.len(), 2);
assert_eq!(list[0].device_type, 10);
assert_eq!(list[1].device_type, 20);
}
#[test]
fn bitmap_decode_retains_unknown_bits() {
let raw: u8 = 0b1000_0001;
let mut buf = Vec::new();
{
let mut w = TlvWriter::new(&mut buf);
w.put_uint(Tag::Anonymous, u64::from(raw)).unwrap();
}
let bm = decode_occupancy(&buf).expect("decodes");
assert_eq!(
bm.bits(),
raw,
"the unknown bit7 must survive the round-trip"
);
}