-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_modifier_if_expr.rs
59 lines (45 loc) · 1.69 KB
/
test_modifier_if_expr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use jbytes_derive::{ByteDecode, ByteEncode};
use jbytes::prelude::*;
#[derive(Debug, PartialEq, Eq, ByteDecode, ByteEncode)]
pub struct OptionExample {
pub flags: bool,
#[jbytes(if_expr="flags")]
pub value: Option<u16>,
}
#[test]
fn test_modifier_if_expr() {
let bytes = Bytes::new(b"\x00");
let value = OptionExample::decode(&bytes).unwrap();
assert_eq!(value, OptionExample { flags: false, value: None });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), b"\x00");
let bytes = Bytes::new(b"\x01\x00\x31");
let value = OptionExample::decode(&bytes).unwrap();
assert_eq!(value, OptionExample { flags: true, value: Some(0x31) });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), b"\x01\x00\x31");
}
#[derive(Debug, PartialEq, Eq, ByteDecode, ByteEncode)]
pub enum OptionEnumExample {
#[jbytes(branch_value=1)]
Read {
flags: bool,
#[jbytes(if_expr="flags")]
value: Option<u16>,
},
#[jbytes(branch_default)]
Unknown,
}
#[test]
fn test_modifier_if_expr_enum_example() {
let bytes = Bytes::new(b"\x01\x00");
let value = OptionEnumExample::decode(&bytes).unwrap();
assert_eq!(value, OptionEnumExample::Read { flags: false, value: None });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), b"\x01\x00");
let bytes = Bytes::new(b"\x01\x01\x00\x31");
let value = OptionEnumExample::decode(&bytes).unwrap();
assert_eq!(value, OptionEnumExample::Read { flags: true, value: Some(0x31) });
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), b"\x01\x01\x00\x31");
}