-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_modifier_branch_default.rs
104 lines (83 loc) · 2.79 KB
/
test_modifier_branch_default.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use jbytes_derive::{ByteDecode, ByteEncode};
use jbytes::prelude::*;
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
struct BranchExample {
pub cmd: u16,
#[jbytes(branch="cmd")]
pub body: BranchDefaultExampleBody,
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
enum BranchDefaultExampleBody {
#[jbytes(branch_value=1)]
V0,
#[jbytes(branch_default)] // match xxx { _ => xxxx }
Unknown,
}
#[test]
fn test_modifier_branch_default() {
// cmd = 1
let data = [
0x00, 0x01, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 1, body: BranchDefaultExampleBody::V0 };
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
let data = [
0x00, 0x02, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 0x0002, body: BranchDefaultExampleBody::Unknown};
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
let data = [
0x00, 0x1f, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample { cmd: 0x1f, body: BranchDefaultExampleBody::Unknown};
assert_eq!(BranchExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
struct BranchExample2 {
pub cmd: u16,
#[jbytes(branch="cmd")]
pub body: BranchDefaultExampleBody2,
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
enum BranchDefaultExampleBody2 {
#[jbytes(branch_value=1)]
V0,
V1,
}
#[test]
fn test_modifier_branch_default2() {
// cmd = 1
let data = [
0x00, 0x01, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample2 { cmd: 1, body: BranchDefaultExampleBody2::V0 };
assert_eq!(BranchExample2::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// cmd = 2
let data = [
0x00, 0x02, // cmd
];
let bytes = Bytes::new(data);
let value = BranchExample2 { cmd: 2, body: BranchDefaultExampleBody2::V1 };
assert_eq!(BranchExample2::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), data);
// error
let data = [
0x00, 0x03, // cmd
];
let bytes = Bytes::new(data);
assert_eq!(BranchExample2::decode(&bytes).is_err(), true);
assert_eq!(bytes.remaining_len(), 0);
}