Add writing support for IntProperty
This commit is contained in:
parent
ff87348e42
commit
ee2de9c9ae
1 changed files with 41 additions and 7 deletions
|
@ -3,29 +3,46 @@ use binrw::binrw;
|
|||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
pub struct IntProperty {
|
||||
/// In bytes
|
||||
pub integer_size: u32,
|
||||
#[br(pad_before = 5)]
|
||||
#[bw(calc = 4)]
|
||||
pub size_in_bytes: u32,
|
||||
#[brw(pad_before = 5)]
|
||||
pub value: u32,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use binrw::BinRead;
|
||||
use binrw::{BinRead, BinWrite};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn zero() {
|
||||
fn read_zero() {
|
||||
let data = [
|
||||
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let mut cursor = Cursor::new(data);
|
||||
let decoded = IntProperty::read_le(&mut cursor).unwrap();
|
||||
assert_eq!(decoded.integer_size, 4);
|
||||
assert_eq!(decoded.value, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_zero() {
|
||||
let expected_data: [u8; 13] = [
|
||||
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let property = IntProperty {
|
||||
value: 0
|
||||
};
|
||||
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
{
|
||||
let mut cursor = Cursor::new(&mut buffer);
|
||||
property.write_le(&mut cursor).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(expected_data, &buffer[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer() {
|
||||
let data = [
|
||||
|
@ -33,7 +50,24 @@ mod tests {
|
|||
];
|
||||
let mut cursor = Cursor::new(data);
|
||||
let decoded = IntProperty::read_le(&mut cursor).unwrap();
|
||||
assert_eq!(decoded.integer_size, 4);
|
||||
assert_eq!(decoded.value, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_integer() {
|
||||
let expected_data: [u8; 13] = [
|
||||
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let property = IntProperty {
|
||||
value: 4
|
||||
};
|
||||
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
{
|
||||
let mut cursor = Cursor::new(&mut buffer);
|
||||
property.write_le(&mut cursor).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(expected_data, &buffer[..]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue