Debian Codegraph @

third_party/rust/serde_with/tests/hex.rs

raw
1 //! Test Cases
2
3 mod utils;
4
5 use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
6 use expect_test::expect;
7 use serde::{Deserialize, Serialize};
8 use serde_with::{
9 formats::{Lowercase, Uppercase},
10 hex::Hex,
11 serde_as,
12 };
13
14 #[test]
15 fn hex_vec() {
16 #[serde_as]
17 #[derive(Debug, Serialize, Deserialize, PartialEq)]
18 pub struct B(#[serde_as(as = "Vec<Hex>")] Vec<Vec<u8>>);
19
20 is_equal(
21 B(vec![vec![0, 1, 2, 13], vec![14, 5, 6, 7]]),
22 expect![[r#"
23 [
24 "0001020d",
25 "0e050607"
26 ]"#]],
27 );
28
29 // Check mixed case deserialization
30 check_deserialization(
31 B(vec![vec![0xaa, 0xbc, 0xff], vec![0xe0, 0x7d]]),
32 r#"["aaBCff","E07d"]"#,
33 );
34
35 check_error_deserialization::<B>(
36 r#"["0"]"#,
37 expect![[r#"Odd number of digits at line 1 column 5"#]],
38 );
39 check_error_deserialization::<B>(
40 r#"["zz"]"#,
41 expect![[r#"Invalid character 'z' at position 0 at line 1 column 6"#]],
42 );
43 }
44
45 #[test]
46 fn hex_vec_lowercase() {
47 #[serde_as]
48 #[derive(Debug, Serialize, Deserialize, PartialEq)]
49 pub struct B(#[serde_as(as = "Vec<Hex<Lowercase>>")] Vec<Vec<u8>>);
50
51 is_equal(
52 B(vec![vec![0, 1, 2, 13], vec![14, 5, 6, 7]]),
53 expect![[r#"
54 [
55 "0001020d",
56 "0e050607"
57 ]"#]],
58 );
59
60 // Check mixed case deserialization
61 check_deserialization(
62 B(vec![vec![0xaa, 0xbc, 0xff], vec![0xe0, 0x7d]]),
63 r#"["aaBCff","E07d"]"#,
64 );
65 }
66
67 #[test]
68 fn hex_vec_uppercase() {
69 #[serde_as]
70 #[derive(Debug, Serialize, Deserialize, PartialEq)]
71 pub struct B(#[serde_as(as = "Vec<Hex<Uppercase>>")] Vec<Vec<u8>>);
72
73 is_equal(
74 B(vec![vec![0, 1, 2, 13], vec![14, 5, 6, 7]]),
75 expect![[r#"
76 [
77 "0001020D",
78 "0E050607"
79 ]"#]],
80 );
81
82 // Check mixed case deserialization
83 check_deserialization(
84 B(vec![vec![0xaa, 0xbc, 0xff], vec![0xe0, 0x7d]]),
85 r#"["aaBCff","E07d"]"#,
86 );
87 }