Skip to content

Commit f99eb20

Browse files
lmmxfasterthanlime
authored andcommitted
test(facet-msgpack): drafted test cases (that don't pass) from JSON crate, snapshot setup
1 parent 355e29a commit f99eb20

19 files changed

+607
-2
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

facet-msgpack/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ facet-serialize = { version = "0.20.0", path = "../facet-serialize" }
2525
eyre = "0.6.12"
2626
facet = { path = "../facet" }
2727
facet-testhelpers = { path = "../facet-testhelpers" }
28+
insta = "1.43.1"
2829
rmp-serde = "1.1"
2930
serde = { version = "1.0", features = ["derive"] }
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use eyre::Result;
2+
use facet::Facet;
3+
use facet_msgpack::from_slice;
4+
5+
#[derive(Facet, Debug)]
6+
struct FooBar {
7+
foo: u64,
8+
bar: String,
9+
}
10+
11+
#[test]
12+
fn bad_hex_1() -> Result<()> {
13+
facet_testhelpers::setup();
14+
15+
let data = [
16+
0x82, // Fixmap with 2 elements
17+
0xa3, // Fixstr with length 3
18+
0x66, 0x6f, 0x6f, // "foo"
19+
0xce, // uint32 (correct prefix according to MessagePack spec)
20+
0x00, 0x00, 0x00, 0x2a, // 42
21+
];
22+
let err = from_slice::<FooBar>(&data).unwrap_err();
23+
insta::assert_snapshot!(err);
24+
Ok(())
25+
}
26+
27+
#[test]
28+
fn bad_hex_2() -> Result<()> {
29+
facet_testhelpers::setup();
30+
31+
let data = [
32+
0x82, // Fixmap with 2 elements
33+
0xa3, // Fixstr with length 3
34+
0x66, 0x6f, 0x6f, // "foo"
35+
0xce, // uint32 (correct prefix according to MessagePack spec)
36+
0x00, 0x00, 0x00, 0x2a, // 42
37+
0xa3, // Fixstr with length 3
38+
0x62, 0x61, 0x72, // "bar"
39+
0xce, // uint32 (correct prefix according to MessagePack spec)
40+
0x00, 0x00, 0x00, 0x2a, // 42
41+
];
42+
let err = from_slice::<FooBar>(&data).unwrap_err();
43+
insta::assert_snapshot!(err);
44+
Ok(())
45+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod err;
2+
mod read;
3+
mod write;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod structs;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use eyre::Result;
2+
use facet::Facet;
3+
use facet_msgpack::from_slice;
4+
5+
#[test]
6+
fn msgpack_read_bool() -> Result<()> {
7+
facet_testhelpers::setup();
8+
9+
#[derive(Facet, Debug, PartialEq)]
10+
struct BoolStruct {
11+
yes: bool,
12+
no: bool,
13+
}
14+
15+
let data = [
16+
0x82, // Map with 2 elements
17+
0xa3, // Fixstr with length 3
18+
0x79, 0x65, 0x73, // "yes"
19+
0xc3, // true
20+
0xa2, // Fixstr with length 2
21+
0x6e, 0x6f, // "no"
22+
0xc2, // false
23+
];
24+
25+
let s: BoolStruct = from_slice(&data)?;
26+
assert_eq!(
27+
s,
28+
BoolStruct {
29+
yes: true,
30+
no: false
31+
}
32+
);
33+
34+
Ok(())
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use eyre::Result;
2+
use facet::Facet;
3+
use facet_msgpack::from_slice;
4+
5+
#[test]
6+
fn msgpack_deserialize_field_level_default_no_function() -> Result<()> {
7+
facet_testhelpers::setup();
8+
9+
#[derive(Facet, Debug, PartialEq)]
10+
struct FieldDefault {
11+
foo: i32,
12+
#[facet(default)]
13+
bar: String,
14+
}
15+
16+
// Only set foo, leave bar missing - should use Default for String
17+
// {"foo": 789}
18+
let data = [
19+
0x81, // Map with 1 element
20+
0xa3, 0x66, 0x6f, 0x6f, // "foo"
21+
0xcd, 0x03, 0x15, // 789 (uint16)
22+
];
23+
24+
let s: FieldDefault = from_slice(&data)?;
25+
assert_eq!(s.foo, 789, "Expected foo to be 789, got {}", s.foo);
26+
assert_eq!(
27+
s.bar, "",
28+
"Expected bar to be empty string, got {:?}",
29+
s.bar
30+
);
31+
Ok(())
32+
}
33+
34+
#[test]
35+
fn msgpack_deserialize_field_level_default_function() -> Result<()> {
36+
facet_testhelpers::setup();
37+
38+
fn default_number() -> i32 {
39+
12345
40+
}
41+
42+
#[derive(Facet, Debug, PartialEq)]
43+
struct FieldDefaultFn {
44+
#[facet(default = default_number())]
45+
foo: i32,
46+
bar: String,
47+
}
48+
49+
// Only set bar, leave foo missing - should use default_number()
50+
// {"bar": "hello"}
51+
let data = [
52+
0x81, // Map with 1 element
53+
0xa3, 0x62, 0x61, 0x72, // "bar"
54+
0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
55+
];
56+
57+
let s: FieldDefaultFn = from_slice(&data)?;
58+
assert_eq!(s.foo, 12345, "Expected foo to be 12345, got {}", s.foo);
59+
assert_eq!(s.bar, "hello", "Expected bar to be 'hello', got {}", s.bar);
60+
Ok(())
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use eyre::Result;
2+
use facet::Facet;
3+
use facet_msgpack::from_slice;
4+
5+
#[test]
6+
fn msgpack_deserialize_unit_enum_variant() -> Result<()> {
7+
facet_testhelpers::setup();
8+
9+
#[derive(Facet, Debug, PartialEq)]
10+
#[repr(u8)]
11+
enum FontStyle {
12+
Italic,
13+
Oblique,
14+
}
15+
16+
// "Italic"
17+
let data_italic = [
18+
0xa6, 0x49, 0x74, 0x61, 0x6c, 0x69, 0x63, // "Italic"
19+
];
20+
21+
// "Oblique"
22+
let data_oblique = [
23+
0xa7, 0x4f, 0x62, 0x6c, 0x69, 0x71, 0x75, 0x65, // "Oblique"
24+
];
25+
26+
let s_italic: FontStyle = from_slice(&data_italic)?;
27+
assert_eq!(s_italic, FontStyle::Italic);
28+
29+
let s_oblique: FontStyle = from_slice(&data_oblique)?;
30+
assert_eq!(s_oblique, FontStyle::Oblique);
31+
32+
Ok(())
33+
}
34+
35+
#[test]
36+
fn msgpack_deserialize_tuple_variant() -> Result<()> {
37+
facet_testhelpers::setup();
38+
39+
#[derive(Facet, Debug, PartialEq)]
40+
#[repr(u8)]
41+
enum Point {
42+
X(u64),
43+
Y(String, bool),
44+
}
45+
46+
// { "X": 123 }
47+
let data_x = [
48+
0x81, // Map with 1 element
49+
0xa1, 0x58, // "X"
50+
0x7b, // 123 (positive fixint)
51+
];
52+
53+
// { "Y": ["hello", true] }
54+
let data_y = [
55+
0x81, // Map with 1 element
56+
0xa1, 0x59, // "Y"
57+
0x92, // Array with 2 elements
58+
0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
59+
0xc3, // true
60+
];
61+
62+
let p_x: Point = from_slice(&data_x)?;
63+
assert_eq!(p_x, Point::X(123));
64+
65+
let p_y: Point = from_slice(&data_y)?;
66+
assert_eq!(p_y, Point::Y("hello".to_string(), true));
67+
68+
Ok(())
69+
}
70+
71+
#[test]
72+
fn msgpack_deserialize_struct_variant() -> Result<()> {
73+
facet_testhelpers::setup();
74+
75+
#[derive(Facet, Debug, PartialEq)]
76+
#[repr(u8)]
77+
#[allow(dead_code)]
78+
enum Point {
79+
Thing,
80+
Well { made: String, i: bool, guess: i32 },
81+
Other(i32),
82+
}
83+
84+
// { "Well": { "made": "in germany", "i": false, "guess": 3 } }
85+
let data = [
86+
0x81, // Map with 1 element
87+
0xa4, 0x57, 0x65, 0x6c, 0x6c, // "Well"
88+
0x83, // Map with 3 elements
89+
0xa4, 0x6d, 0x61, 0x64, 0x65, // "made"
90+
0xaa, 0x69, 0x6e, 0x20, 0x67, 0x65, 0x72, 0x6d, 0x61, 0x6e, 0x79, // "in germany"
91+
0xa1, 0x69, // "i"
92+
0xc2, // false
93+
0xa5, 0x67, 0x75, 0x65, 0x73, 0x73, // "guess"
94+
0x03, // 3 (positive fixint)
95+
];
96+
97+
let point: Point = from_slice(&data)?;
98+
assert_eq!(
99+
point,
100+
Point::Well {
101+
made: "in germany".to_string(),
102+
i: false,
103+
guess: 3
104+
}
105+
);
106+
107+
Ok(())
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use eyre::Result;
2+
use facet_msgpack::from_slice;
3+
use std::collections::HashMap;
4+
5+
#[test]
6+
fn msgpack_deserialize_hashmap() -> Result<()> {
7+
facet_testhelpers::setup();
8+
9+
// { "key1": "value1", "key2": "value2", "key3": "value3" }
10+
let data = [
11+
0x83, // Map with 3 elements
12+
// key1: value1
13+
0xa4, 0x6b, 0x65, 0x79, 0x31, // "key1"
14+
0xa6, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x31, // "value1"
15+
// key2: value2
16+
0xa4, 0x6b, 0x65, 0x79, 0x32, // "key2"
17+
0xa6, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32, // "value2"
18+
// key3: value3
19+
0xa4, 0x6b, 0x65, 0x79, 0x33, // "key3"
20+
0xa6, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x33, // "value3"
21+
];
22+
23+
let m: HashMap<String, String> = from_slice(&data)?;
24+
assert_eq!(m.get("key1").unwrap(), "value1");
25+
assert_eq!(m.get("key2").unwrap(), "value2");
26+
assert_eq!(m.get("key3").unwrap(), "value3");
27+
28+
Ok(())
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use eyre::Result;
2+
use facet::Facet;
3+
use facet_msgpack::from_slice;
4+
5+
#[test]
6+
fn msgpack_deserialize_more_types() -> Result<()> {
7+
facet_testhelpers::setup();
8+
9+
#[derive(Facet)]
10+
struct TestStructWithMoreTypes {
11+
u8_val: u8,
12+
u16_val: u16,
13+
i8_val: i8,
14+
i16_val: i16,
15+
u32_val: u32,
16+
i32_val: i32,
17+
u64_val: u64,
18+
i64_val: i64,
19+
f32_val: f32,
20+
f64_val: f64,
21+
}
22+
23+
// MessagePack encoded data representing the struct with all the numeric values
24+
let data = [
25+
0x8a, // Map with 10 elements
26+
// u8_val: 255
27+
0xa6, 0x75, 0x38, 0x5f, 0x76, 0x61, 0x6c, // "u8_val"
28+
0xcc, 0xff, // unsigned 8-bit int (255)
29+
// u16_val: 65535
30+
0xa8, 0x75, 0x31, 0x36, 0x5f, 0x76, 0x61, 0x6c, // "u16_val"
31+
0xcd, 0xff, 0xff, // unsigned 16-bit int (65535)
32+
// i8_val: -128
33+
0xa7, 0x69, 0x38, 0x5f, 0x76, 0x61, 0x6c, // "i8_val"
34+
0xd0, 0x80, // signed 8-bit int (-128)
35+
// i16_val: -32768
36+
0xa8, 0x69, 0x31, 0x36, 0x5f, 0x76, 0x61, 0x6c, // "i16_val"
37+
0xd1, 0x80, 0x00, // signed 16-bit int (-32768)
38+
// u32_val: 4294967295
39+
0xa8, 0x75, 0x33, 0x32, 0x5f, 0x76, 0x61, 0x6c, // "u32_val"
40+
0xce, 0xff, 0xff, 0xff, 0xff, // unsigned 32-bit int (4294967295)
41+
// i32_val: -2147483648
42+
0xa8, 0x69, 0x33, 0x32, 0x5f, 0x76, 0x61, 0x6c, // "i32_val"
43+
0xd2, 0x80, 0x00, 0x00, 0x00, // signed 32-bit int (-2147483648)
44+
// u64_val: 18446744073709551615
45+
0xa8, 0x75, 0x36, 0x34, 0x5f, 0x76, 0x61, 0x6c, // "u64_val"
46+
0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47+
0xff, // unsigned 64-bit int (18446744073709551615)
48+
// i64_val: -9223372036854775808
49+
0xa8, 0x69, 0x36, 0x34, 0x5f, 0x76, 0x61, 0x6c, // "i64_val"
50+
0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, // signed 64-bit int (-9223372036854775808)
52+
// f32_val: 3.14...
53+
0xa8, 0x66, 0x33, 0x32, 0x5f, 0x76, 0x61, 0x6c, // "f32_val"
54+
0xca, 0x40, 0x49, 0x0f, 0xdb, // float 32 (approx. PI)
55+
// f64_val: 3.14...
56+
0xa8, 0x66, 0x36, 0x34, 0x5f, 0x76, 0x61, 0x6c, // "f64_val"
57+
0xcb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18, // float 64 (more precise PI)
58+
];
59+
60+
let test_struct: TestStructWithMoreTypes = from_slice(&data)?;
61+
62+
assert_eq!(test_struct.u8_val, 255);
63+
assert_eq!(test_struct.u16_val, 65535);
64+
assert_eq!(test_struct.i8_val, -128);
65+
assert_eq!(test_struct.i16_val, -32768);
66+
assert_eq!(test_struct.u32_val, 4294967295);
67+
assert_eq!(test_struct.i32_val, -2147483648);
68+
assert_eq!(test_struct.u64_val, 18446744073709551615);
69+
assert_eq!(test_struct.i64_val, -9223372036854775808);
70+
assert!((test_struct.f32_val - std::f32::consts::PI).abs() < f32::EPSILON);
71+
assert!((test_struct.f64_val - std::f64::consts::PI).abs() < f64::EPSILON);
72+
73+
Ok(())
74+
}

0 commit comments

Comments
 (0)