From fccbef27eece0734b954ccd36bba8e16c66f75e0 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Sun, 24 Oct 2021 01:21:14 -0400 Subject: [PATCH] Fixed UserPreferencesV0::as_prefstring_bytes Also added more tests around prefstrings Signed-off-by: Ben Aaron Goldberg --- src/user_preferences/mod.rs | 13 +++++++++++++ src/user_preferences/v0.rs | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/user_preferences/mod.rs b/src/user_preferences/mod.rs index 3b16c4b..a3685fa 100644 --- a/src/user_preferences/mod.rs +++ b/src/user_preferences/mod.rs @@ -25,6 +25,7 @@ use data_encoding::BASE32_NOPAD; /// Because parsing a prefstring must be done in relation to an [`InstanceSettings`], the /// `UserPreferences` struct shares a lifetime with the settings it was created with. #[non_exhaustive] +#[derive(Debug, PartialEq, Eq)] pub enum UserPreferences { V0(v0::UserPreferencesV0) } @@ -140,3 +141,15 @@ impl Preference for UserPreferences { UserPreferences::V0(UserPreferencesV0::from_preferences(prefs)) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_prefstring_bytes_round_trip() { + let prefs = UserPreferences::default(); + let bytes = prefs.as_prefstring_bytes(); + assert_eq!(prefs, UserPreferences::from_prefstring_bytes(&bytes).unwrap()); + } +} diff --git a/src/user_preferences/v0.rs b/src/user_preferences/v0.rs index 368918a..dadf8de 100644 --- a/src/user_preferences/v0.rs +++ b/src/user_preferences/v0.rs @@ -125,7 +125,7 @@ impl Preference for UserPreferencesV0 { defaults_byte |= 0b10000000; } defaults_byte |= self.default_weight & 0b01111111; - vec![defaults_byte] + vec![0, defaults_byte] .into_iter() .chain(self.commands.iter().map(|cmd| cmd.into())) .collect() @@ -606,4 +606,19 @@ mod tests { let prefs = UserPreferencesV0::from_preferences(&pref_vals); assert_eq!(prefs, expected_prefs); } + + #[test] + fn test_as_prefstring_bytes() { + let prefs = UserPreferencesV0::default(); + let expected_bytes = vec![0 , 0b10000001]; + assert_eq!(expected_bytes, prefs.as_prefstring_bytes()); + } + + #[test] + fn test_from_prefstring_bytes() { + let bytes = vec![0 , 0b10000001]; + let prefs = UserPreferencesV0::from_prefstring_bytes(&bytes).unwrap(); + let expected_prefs = UserPreferencesV0::default(); + assert_eq!(expected_prefs, prefs); + } }