Fixed UserPreferencesV0::as_prefstring_bytes

Also added more tests around prefstrings

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-10-24 01:21:14 -04:00
parent 29addcaf9a
commit fccbef27ee
2 changed files with 29 additions and 1 deletions

View file

@ -25,6 +25,7 @@ use data_encoding::BASE32_NOPAD;
/// Because parsing a prefstring must be done in relation to an [`InstanceSettings`], the /// 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. /// `UserPreferences` struct shares a lifetime with the settings it was created with.
#[non_exhaustive] #[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum UserPreferences { pub enum UserPreferences {
V0(v0::UserPreferencesV0) V0(v0::UserPreferencesV0)
} }
@ -140,3 +141,15 @@ impl Preference for UserPreferences {
UserPreferences::V0(UserPreferencesV0::from_preferences(prefs)) 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());
}
}

View file

@ -125,7 +125,7 @@ impl Preference for UserPreferencesV0 {
defaults_byte |= 0b10000000; defaults_byte |= 0b10000000;
} }
defaults_byte |= self.default_weight & 0b01111111; defaults_byte |= self.default_weight & 0b01111111;
vec![defaults_byte] vec![0, defaults_byte]
.into_iter() .into_iter()
.chain(self.commands.iter().map(|cmd| cmd.into())) .chain(self.commands.iter().map(|cmd| cmd.into()))
.collect() .collect()
@ -606,4 +606,19 @@ mod tests {
let prefs = UserPreferencesV0::from_preferences(&pref_vals); let prefs = UserPreferencesV0::from_preferences(&pref_vals);
assert_eq!(prefs, expected_prefs); 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);
}
} }