Skip to content

Commit b3b36c6

Browse files
authored
Add tests for serde impl's Visitor::expected (#213)
This is what is added to error messages when we're expecting an `Array` but some other type in a self-describing format is encountered instead. To test this we need a self-describing format, so this uses `serde_json`, and replaces `postcard` with `serde_json` for the round trip test since we can't use `postcard` to test both.
1 parent 25d2b78 commit b3b36c6

File tree

4 files changed

+42
-57
lines changed

4 files changed

+42
-57
lines changed

Cargo.lock

Lines changed: 29 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ zeroize = { version = "1.8", optional = true, default-features = false }
2929
zerocopy = { version = "0.8", optional = true, features = ["derive"] }
3030

3131
[dev-dependencies]
32-
postcard = { version = "1", default-features = false, features = ["alloc"] }
32+
serde_json = "1"
3333

3434
[features]
3535
alloc = []

src/serde.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ mod tests {
7575
use crate::{Array, sizes::U3};
7676
type A = Array<u8, U3>;
7777

78+
#[test]
79+
#[cfg(feature = "alloc")]
80+
fn expecting() {
81+
use alloc::string::ToString;
82+
let err = serde_json::from_str::<A>("true").unwrap_err();
83+
assert!(err.to_string().contains("expected an array of length 3"));
84+
}
85+
7886
#[test]
7987
fn round_trip() {
8088
let example: A = Array([1, 2, 3]);
81-
let bytes = postcard::to_allocvec(&example).unwrap();
82-
let deserialized: A = postcard::from_bytes(&bytes).unwrap();
89+
let s = serde_json::to_string(&example).unwrap();
90+
let deserialized: A = serde_json::from_str(&s).unwrap();
8391
assert_eq!(example, deserialized);
8492
}
8593
}

src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ mod tests {
258258

259259
let (slice_aligned, rem_aligned) = [1u8, 2].as_hybrid_chunks::<U2>();
260260
assert_eq!(slice_aligned, &[Array([1u8, 2])]);
261-
assert_eq!(rem_aligned, &[]);
261+
assert_eq!(rem_aligned, b"");
262262

263263
let (slice_unaligned, rem_unaligned) = [1u8, 2, 3].as_hybrid_chunks::<U2>();
264264
assert_eq!(slice_unaligned, &[Array([1u8, 2])]);
@@ -279,7 +279,7 @@ mod tests {
279279
let mut arr2 = [1u8, 2];
280280
let (slice_aligned, rem_aligned) = arr2.as_hybrid_chunks_mut::<U2>();
281281
assert_eq!(slice_aligned, &mut [Array([1u8, 2])]);
282-
assert_eq!(rem_aligned, &mut []);
282+
assert_eq!(rem_aligned, b"");
283283

284284
let mut arr3 = [1u8, 2, 3];
285285
let (slice_unaligned, rem_unaligned) = arr3.as_hybrid_chunks_mut::<U2>();

0 commit comments

Comments
 (0)