Debian Codegraph @

third_party/rust/serde_repr/tests/test.rs

raw
1 #![allow(
2 clippy::derive_partial_eq_without_eq,
3 // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
4 clippy::nonstandard_macro_braces,
5 clippy::wildcard_imports,
6 )]
7
8 use serde_repr::{Deserialize_repr, Serialize_repr};
9
10 mod small_prime {
11 use super::*;
12
13 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
14 #[repr(u8)]
15 enum SmallPrime {
16 Two = 2,
17 Three = 3,
18 Five = 5,
19 Seven = 7,
20 }
21
22 #[test]
23 fn test_serialize() {
24 let j = serde_json::to_string(&SmallPrime::Seven).unwrap();
25 assert_eq!(j, "7");
26 }
27
28 #[test]
29 fn test_deserialize() {
30 let p: SmallPrime = serde_json::from_str("2").unwrap();
31 assert_eq!(p, SmallPrime::Two);
32 }
33 }
34
35 mod other {
36 use super::*;
37
38 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
39 #[repr(u8)]
40 enum TestOther {
41 A,
42 B,
43 #[serde(other, rename = "useless")]
44 Other,
45 }
46
47 #[test]
48 fn test_deserialize() {
49 let p: TestOther = serde_json::from_str("0").unwrap();
50 assert_eq!(p, TestOther::A);
51 let p: TestOther = serde_json::from_str("1").unwrap();
52 assert_eq!(p, TestOther::B);
53 let p: TestOther = serde_json::from_str("5").unwrap();
54 assert_eq!(p, TestOther::Other);
55 }
56 }
57
58 mod implicit_discriminant {
59 use super::*;
60
61 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
62 #[repr(u8)]
63 enum ImplicitDiscriminant {
64 Zero,
65 One,
66 Two,
67 Three,
68 }
69
70 #[test]
71 fn test_serialize() {
72 let j = serde_json::to_string(&ImplicitDiscriminant::Three).unwrap();
73 assert_eq!(j, "3");
74 }
75
76 #[test]
77 fn test_deserialize() {
78 let p: ImplicitDiscriminant = serde_json::from_str("2").unwrap();
79 assert_eq!(p, ImplicitDiscriminant::Two);
80 }
81 }