diff options
author | Michal 'vorner' Vaner <vorner@vorner.cz> | 2019-08-05 18:38:12 +0200 |
---|---|---|
committer | Michal 'vorner' Vaner <vorner@vorner.cz> | 2019-08-22 20:35:28 +0200 |
commit | 0b6556eacaf8f16312487594a0888bfe79723748 (patch) | |
tree | 96afc714a1713154f4c7aa17cc147088497a2811 /tests/get.rs | |
parent | ad29f8f83566366671e6affd3887079d7165f5f9 (diff) |
Allow arbitrary types of map keys
Of particular interest are maps that have numbers or enums as keys.
Closes #74.
Diffstat (limited to 'tests/get.rs')
-rw-r--r-- | tests/get.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/tests/get.rs b/tests/get.rs index 7e3be8a..acc823d 100644 --- a/tests/get.rs +++ b/tests/get.rs @@ -9,7 +9,7 @@ extern crate serde_derive; use config::*; use float_cmp::ApproxEqUlps; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; #[derive(Debug, Deserialize)] struct Place { @@ -225,3 +225,42 @@ fn test_enum() { assert_eq!(s.diodes["blue"], Diode::Blinking(300, 700)); assert_eq!(s.diodes["white"], Diode::Pattern{name: "christmas".into(), inifinite: true,}); } + +#[test] +fn test_enum_key() { + #[derive(Debug, Deserialize, PartialEq, Eq, Hash)] + enum Quark { + Up, + Down, + Strange, + Charm, + Bottom, + Top, + } + + #[derive(Debug, Deserialize)] + struct Settings { + proton: HashMap<Quark, usize>, + // Just to make sure that set keys work too. + quarks: HashSet<Quark>, + } + + let c = make(); + let s: Settings = c.try_into().unwrap(); + + assert_eq!(s.proton[&Quark::Up], 2); + assert_eq!(s.quarks.len(), 6); +} + +#[test] +fn test_int_key() { + #[derive(Debug, Deserialize, PartialEq)] + struct Settings { + divisors: HashMap<u32, u32>, + } + + let c = make(); + let s: Settings = c.try_into().unwrap(); + assert_eq!(s.divisors[&4], 3); + assert_eq!(s.divisors.len(), 4); +} |