diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
commit | 2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 (patch) | |
tree | 033cc839730fda84ff08db877037977be94e5e3a /vendor/serde_ignored/tests/test.rs | |
parent | Initial commit. (diff) | |
download | cargo-2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4.tar.xz cargo-2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4.zip |
Adding upstream version 0.70.1+ds1.upstream/0.70.1+ds1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/serde_ignored/tests/test.rs')
-rw-r--r-- | vendor/serde_ignored/tests/test.rs | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/vendor/serde_ignored/tests/test.rs b/vendor/serde_ignored/tests/test.rs new file mode 100644 index 0000000..ce7a2bf --- /dev/null +++ b/vendor/serde_ignored/tests/test.rs @@ -0,0 +1,141 @@ +#![allow( + clippy::derive_partial_eq_without_eq, + clippy::from_iter_instead_of_collect, + clippy::zero_sized_map_values +)] + +use serde::Deserialize; +use serde_derive::Deserialize; +use std::collections::{BTreeMap as Map, BTreeSet as Set}; +use std::iter::FromIterator; + +fn assert_ignored<'de, T>(json: &'de str, expected: &[&str]) -> T +where + T: Deserialize<'de>, +{ + let de = &mut serde_json::Deserializer::from_str(json); + + let mut unused = Set::new(); + + let value: T = serde_ignored::deserialize(de, |path| { + unused.insert(path.to_string()); + }) + .unwrap(); + + let expected = Set::from_iter(expected.iter().copied().map(str::to_owned)); + assert_eq!(unused, expected); + + value +} + +#[derive(Debug, Deserialize)] +struct V { + #[allow(dead_code)] + used: (), +} + +#[test] +fn test_readme() { + #[derive(Debug, PartialEq, Deserialize)] + struct Package { + name: String, + dependencies: Map<String, Dependency>, + } + + #[derive(Debug, PartialEq, Deserialize)] + struct Dependency { + version: String, + } + + let json = r#"{ + "name": "demo", + "dependencies": { + "serde": { + "version": "1.0", + "typo1": "" + } + }, + "typo2": { + "inner": "" + }, + "typo3": {} + }"#; + + let ignored = &["dependencies.serde.typo1", "typo2", "typo3"]; + let p: Package = assert_ignored(json, ignored); + + let expected = Package { + name: "demo".to_owned(), + dependencies: { + let mut map = Map::new(); + map.insert( + "serde".to_owned(), + Dependency { + version: "1.0".to_owned(), + }, + ); + map + }, + }; + assert_eq!(p, expected); +} + +#[test] +fn test_int_key() { + #[derive(Debug, Deserialize)] + struct Test { + #[allow(dead_code)] + a: Map<usize, V>, + } + + let json = r#"{ + "a": { + "2": { + "used": null, + "unused": null + } + } + }"#; + + let ignored = &["a.2.unused"]; + assert_ignored::<Test>(json, ignored); +} + +#[test] +fn test_newtype_key() { + type Test = Map<Key, V>; + + #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)] + struct Key(&'static str); + + let json = r#"{ + "k": { + "used": null, + "unused": null + } + }"#; + + let ignored = &["k.unused"]; + assert_ignored::<Test>(json, ignored); +} + +#[test] +fn test_unit_variant_key() { + type Test = Map<Key, V>; + + #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)] + enum Key { + First, + Second, + } + + let json = r#"{ + "First": { + "used": null, + "unused": null + } + }"#; + + let ignored = &["First.unused"]; + assert_ignored::<Test>(json, ignored); +} |